Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2563616rdb; Mon, 5 Feb 2024 10:13:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IHxjC6P0UoQxHOsiWZjbUAFWYg6TfFxtjJLAU7gOq6Gg6oXv2PVPPy/IWtWjISAw77sy8vC X-Received: by 2002:a05:620a:4d4:b0:783:f683:e28b with SMTP id 20-20020a05620a04d400b00783f683e28bmr233817qks.34.1707156780431; Mon, 05 Feb 2024 10:13:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707156780; cv=pass; d=google.com; s=arc-20160816; b=wJs8AXFwUWYhfTCxGcaoIf6UXHTnREJOn6sR3o32ScBFT4/vOoNq9eknVOrb/94zzr ZalMq77Vv5oDjWPJtDBWdjWGixwhhjGCY8rtUAlWHbxOa0+s+9Tvajxd5o2E5QwPMtWQ l9j12gA9nHi92CjiqJPfMEk8X8LdL0dGl1NcC55ESMbMcKKdiRn41ncsaGYT4CS3kwlY 99kW9wGnaFkC3GXFh9Lf/9Yb8H+R5wQQDWhUqMVT1OM+JQPZDks8VPRzjM1FNd+AWkCF v506NmafZcxMKa4jm6oob9hwBeE2kehsoVEOlOq00BBs27kRpWYbHH4cFXuFDbOc6gzx HUGQ== ARC-Message-Signature: i=2; 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=9GvqXnavjFFA/jgGqYAUFF+ryCuT8ZP/V35VthCVhyM=; fh=LhKEd75qwB0iakUJvGmhzeyXw4BBaPqb0t94+9ErS/Q=; b=xi7PFHRCnbgZYReVwu0FuQwc+JGLNY032GXpYVxCJ5N0rRK2wRWdtaFkn/eD2b+qMU VW+RqGB18b3nHP/8ZPlbEShjtDm+Pxbosp/6SemM7gtqTFzRXcqz9t9VMVNdlkeL188+ 28kJiaNxoKgUDEJYXuWJ2pEiIFXrE/dTIjIkw3nIOueY/j5ew+UaTEMI1qkC3WznmJ6r EjWiFQuHZeaypE7r+IR5AhSPYyo9SfSQ/GAU+BX/Jqh101k7DT3S9oWdez+tJRC4wTHC 4Zrhf1ez8ER4LU3/xvZ+JmasjtwId4DeMhbo5gmtgZMXtRLX/ea2nHILizSh2eq0TbIU I1Jw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=fintech.ru); spf=pass (google.com: domain of linux-kernel+bounces-53187-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53187-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCVnq+p84M3bR10sqB7vkRTMbpdeKWBfftWjL5u/cAj5nXfS6tIYciJbOPXYesO6gG+gt5u71QW1JeiuFHLGw1MKYhCxs7EKn9vyA+Ep+Q== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u18-20020a05620a121200b00785393967a7si354944qkj.345.2024.02.05.10.13.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 10:13:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-53187-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=fintech.ru); spf=pass (google.com: domain of linux-kernel+bounces-53187-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53187-linux.lists.archive=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 34E1F1C22F70 for ; Mon, 5 Feb 2024 18:13:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 23CD847F7B; Mon, 5 Feb 2024 18:12:54 +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 1B11D481AC; Mon, 5 Feb 2024 18:12:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.54.195.159 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707156773; cv=none; b=NWddX+bt9XiBqfhCrkCChaX62s/A2z1UWYYI93ragA9qmqGF53cP+TbcKyaprwJGTo5n5vw2GfbN8cW00b+PHnx9NtJRcWRDUOMc3sAcOG6O/+De2G+N/5ttA0dI5ggXhzF0xlOBkQvZ/c1qFs1TFNZ85E6nAGxPMjUeVkdvsG4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707156773; c=relaxed/simple; bh=n8N7r/5U8euCWe7HHeBpW4qZBL/8nbymCpH6xYaz0cM=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=NqSQ8iiY4jloemsIv3SGFQpEG5B0mQVj7ObU8ytTMJ3FsFSQoalWVs6/U9dmJlj+T+QJz7cx5fWKStyA6nbQEZIXt0g08MDpymb0aywUP8KKdCnI2Zii4g2x/qLfwdhoqjX5Ak0ZShT+Qstec3q/HEFdpQaBVgQYGnvfbUUfmCg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru; spf=pass smtp.mailfrom=fintech.ru; arc=none smtp.client-ip=195.54.195.159 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; Mon, 5 Feb 2024 21:11:36 +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; Mon, 5 Feb 2024 21:11:35 +0300 From: Nikita Zhandarovich To: "Jason A. Donenfeld" , "David S. Miller" , Eric Dumazet CC: Nikita Zhandarovich , Jakub Kicinski , Paolo Abeni , , , , syzbot , Subject: [PATCH RESEND net v2] wireguard: receive: annotate data-race around receiving_counter.counter Date: Mon, 5 Feb 2024 10:11:11 -0800 Message-ID: <20240205181111.43414-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 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: Ex16-02.fintech.ru (10.0.10.19) To Ex16-01.fintech.ru (10.0.10.18) 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 --- v2: add missing annotation in wg_packet_rx_poll() per Eric Duzamet's helpful suggestion. drivers/net/wireguard/receive.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/net/wireguard/receive.c b/drivers/net/wireguard/receive.c index a176653c8861..db01ec03bda0 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; @@ -463,7 +463,7 @@ int wg_packet_rx_poll(struct napi_struct *napi, int budget) net_dbg_ratelimited("%s: Packet has invalid nonce %llu (max %llu)\n", peer->device->dev->name, PACKET_CB(skb)->nonce, - keypair->receiving_counter.counter); + READ_ONCE(keypair->receiving_counter.counter)); goto next; }