From patchwork Thu Jan 11 15:41:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Zhandarovich X-Patchwork-Id: 187427 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1533848dyi; Thu, 11 Jan 2024 07:42:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IF+7eXMWlgh1Sm/yT+d3IQfQooEfRh1e5S31M1YXziaTMm4j9RB0nLi95KiYks8w3BYtVk9 X-Received: by 2002:a05:622a:410:b0:429:b7c6:b94f with SMTP id n16-20020a05622a041000b00429b7c6b94fmr852037qtx.120.1704987738165; Thu, 11 Jan 2024 07:42:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704987738; cv=none; d=google.com; s=arc-20160816; b=pvTLD6i0K/l0DBuA/bNinvk2WsgxfZpJAiG7GaT740ceTJLq3lILiN1i6bTtDnI5Ys Ai9VZgKTyS0lk3itQ3P3vhLTKloOvAo9K+3oquPDO3ZhlWXVAREm8Vw6wHVGMQ9bBx8/ W2047jQTvR5Q7BIhW8xhA43oHQwgm6Zi/XqM2sTY7fd/SzZpcmYD4ALFIzndKoIIr9IL Gr/6jez7uH/IAjDG2vmkXmPU2wTrrsvpylX6RttYYJA79vVTjcRmsP5S/Om6gFAOZem9 xXUsGLm0OsMDNYnh5A2Kkt/l0LFsZ1fYuMoR3LuqGywBMtCZkDJsgk9wyIs+rav+abw2 eozw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=9SBoA59GEK/GF1+5jjTxRqTzpF2jyR5ZDl+WsrH2o8E=; fh=Zka4AJuQrS0wA0DmSza950GM+fExTkWPpvk2MvHOfDE=; b=Zxyh+hsoigFlYO1KEepACS96GMYLNqYaAexTGMuA2XvakAZky3iUdOgnhuCgRf85Mq H3Sy2v2DgAW0hpM+1lwAFh7ITFEWnDMUeMscPx+HmQg2qJ5nDq6Vc0Fkc62BfkTeBz3b 5hjjTV4z0X/pFlGyO/5gHQIjNo8hTdh5naG4/tJqQzdnyclj+xDDaYwywg+UR6KuF45Q mJCV9j8rw9mDxbO4TCAntMUY59aIZ3dtty31PVK08XDai4WjzUFaSHzdJcvEWcyjmmIO 8p9T7Z/e/d2L4+Qa6dRAGxH4eriqI4DBweai2QV/oekSvd7YOWUMlfWCyUDmgpeKELmh auHg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-23807-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23807-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id bp40-20020a05622a1ba800b004261067bf5dsi1142477qtb.709.2024.01.11.07.42.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 07:42:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23807-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-23807-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23807-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id EEFCC1C2406F for ; Thu, 11 Jan 2024 15:42:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 574904F5E6; Thu, 11 Jan 2024 15:41:51 +0000 (UTC) Received: from exchange.fintech.ru (exchange.fintech.ru [195.54.195.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A2D94D13A; Thu, 11 Jan 2024 15:41:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fintech.ru Received: from Ex16-01.fintech.ru (10.0.10.18) by exchange.fintech.ru (195.54.195.159) with Microsoft SMTP Server (TLS) id 14.3.498.0; Thu, 11 Jan 2024 18:41:44 +0300 Received: from localhost (10.0.253.138) by Ex16-01.fintech.ru (10.0.10.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Thu, 11 Jan 2024 18:41:43 +0300 From: Nikita Zhandarovich To: "Jason A. Donenfeld" , "David S. Miller" , Eric Dumazet CC: Nikita Zhandarovich , Jakub Kicinski , Paolo Abeni , , , , syzbot , Subject: [PATCH net] wireguard: receive: annotate data-race around receiving_counter.counter Date: Thu, 11 Jan 2024 07:41:38 -0800 Message-ID: <20240111154138.7605-1-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: Ex16-02.fintech.ru (10.0.10.19) To Ex16-01.fintech.ru (10.0.10.18) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787809222834006073 X-GMAIL-MSGID: 1787809222834006073 Syzkaller with KCSAN identified a data-race issue [1] when accessing keypair->receiving_counter.counter. This patch uses READ_ONCE() and WRITE_ONCE() annotations to fix the problem. [1] BUG: KCSAN: data-race in wg_packet_decrypt_worker / wg_packet_rx_poll write to 0xffff888107765888 of 8 bytes by interrupt on cpu 0: counter_validate drivers/net/wireguard/receive.c:321 [inline] wg_packet_rx_poll+0x3ac/0xf00 drivers/net/wireguard/receive.c:461 __napi_poll+0x60/0x3b0 net/core/dev.c:6536 napi_poll net/core/dev.c:6605 [inline] net_rx_action+0x32b/0x750 net/core/dev.c:6738 __do_softirq+0xc4/0x279 kernel/softirq.c:553 do_softirq+0x5e/0x90 kernel/softirq.c:454 __local_bh_enable_ip+0x64/0x70 kernel/softirq.c:381 __raw_spin_unlock_bh include/linux/spinlock_api_smp.h:167 [inline] _raw_spin_unlock_bh+0x36/0x40 kernel/locking/spinlock.c:210 spin_unlock_bh include/linux/spinlock.h:396 [inline] ptr_ring_consume_bh include/linux/ptr_ring.h:367 [inline] wg_packet_decrypt_worker+0x6c5/0x700 drivers/net/wireguard/receive.c:499 process_one_work kernel/workqueue.c:2633 [inline] ... read to 0xffff888107765888 of 8 bytes by task 3196 on cpu 1: decrypt_packet drivers/net/wireguard/receive.c:252 [inline] wg_packet_decrypt_worker+0x220/0x700 drivers/net/wireguard/receive.c:501 process_one_work kernel/workqueue.c:2633 [inline] process_scheduled_works+0x5b8/0xa30 kernel/workqueue.c:2706 worker_thread+0x525/0x730 kernel/workqueue.c:2787 ... Fixes: a9e90d9931f3 ("wireguard: noise: separate receive counter from send counter") Reported-by: syzbot+d1de830e4ecdaac83d89@syzkaller.appspotmail.com Signed-off-by: Nikita Zhandarovich --- drivers/net/wireguard/receive.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/net/wireguard/receive.c b/drivers/net/wireguard/receive.c index a176653c8861..d91383afb6e2 100644 --- a/drivers/net/wireguard/receive.c +++ b/drivers/net/wireguard/receive.c @@ -251,7 +251,7 @@ static bool decrypt_packet(struct sk_buff *skb, struct noise_keypair *keypair) if (unlikely(!READ_ONCE(keypair->receiving.is_valid) || wg_birthdate_has_expired(keypair->receiving.birthdate, REJECT_AFTER_TIME) || - keypair->receiving_counter.counter >= REJECT_AFTER_MESSAGES)) { + READ_ONCE(keypair->receiving_counter.counter) >= REJECT_AFTER_MESSAGES)) { WRITE_ONCE(keypair->receiving.is_valid, false); return false; } @@ -318,7 +318,7 @@ static bool counter_validate(struct noise_replay_counter *counter, u64 their_cou for (i = 1; i <= top; ++i) counter->backtrack[(i + index_current) & ((COUNTER_BITS_TOTAL / BITS_PER_LONG) - 1)] = 0; - counter->counter = their_counter; + WRITE_ONCE(counter->counter, their_counter); } index &= (COUNTER_BITS_TOTAL / BITS_PER_LONG) - 1;