Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3863238pxv; Mon, 28 Jun 2021 15:03:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4kgYAMGIns1XKpVPELMh8y85TJAzn1Bckg1nDeNCHL0aM6aJardyPk5MuKLegV4ZMU8RH X-Received: by 2002:a05:6402:18f6:: with SMTP id x54mr36900898edy.53.1624917811608; Mon, 28 Jun 2021 15:03:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624917811; cv=none; d=google.com; s=arc-20160816; b=gPFLVJ6SwyXc6BNVWekhCHqd3P0pTkhDi8g1MPKGZM7ceHdHI5Y47pzysgXRIcpbIO xIDlPx7EpNw+d1ie6X7cUpW2ID9hWUWN8rnC/83M04IuKknAZCkNrIXNp+kSJ9OBgOnc n2hWsbl12qaZi0yxQ4zyB97jUNTbSIkMJK7S+QwPY1SUhRLtwiJS3yfbQ9bKRYsdKU0u ypO0uC29G26er967zbAkd5AtDCRQkOuMA2BUKhUg0UsvGuCGnVH9+vEmz3EZHEUzAN/f 3opAPeyzxdnq8U5P/X3QwzRQinL6hUvndJ734XfoW99oVLGJmBjhZXGHoI9LDY5b4I4r RRgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wUkqCjO1cX5vx3tGKN8fWpFHludnKs8lIIzRHlowNfA=; b=JYBABoPUOuiCrOb2Tuln2kWzoGA8D7/0oLURgcHvF2+ZBSoreLrpD4AAodoGjvRsUN GORofCnax0pcciQONbVrIGyWuq6AH2w4/WXiTue8r/Hgyoe4Di4MPQ1VKYvwOot2CH+y 3W8MyhVf8bAiMcVeSqhqi4n5YffB+wA8GyPvWtw6unlw+9AhvjS4sadK8YwDvkGPlXqI Hm6zhntzlfFmWNPgNeJSsponM9wPSWxHgZhEdEgNnZYPO/OCaZs2QLGrxUQYwLE4GXn+ fpuzsSh1aF4F9zOahTTpWv4lNZJuJaVoei3wLLae0LtAJggOZ0zIPIXqCpfkNceOyO4v 0U5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="JB01tnP/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ig1si6678068ejc.173.2021.06.28.15.03.06; Mon, 28 Jun 2021 15:03:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="JB01tnP/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238620AbhF1PYi (ORCPT + 99 others); Mon, 28 Jun 2021 11:24:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:36568 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235148AbhF1PAi (ORCPT ); Mon, 28 Jun 2021 11:00:38 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8BE7F61D73; Mon, 28 Jun 2021 14:40:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624891257; bh=LGKoQAFHYquSjF1r7xIhXKbJSQh+dIVjs06fQFY5kW0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JB01tnP/pgL+hmeOsmBIPU5aHjwDalX6mTqmP3TnVfcmoR/ybq74mepSAdZNAl/+g W3SirmLESdybFCbqmqRHcIwZPmMCnmAi2f3RETYvBiPgdRKjclvfSIU1TV06f7QqHo MROTipk141oXadUOIra+AEaqGTGZZPXggbSi1kHfFSc1I4eeTqYD25Oj2e0FgIT7jF o+ErzSZu5hCx6MREEesu/XHosbBKp1xnFXo/f7I7ageCCuF4gJzXaFmVIscRGCBHLT 6xn22GMMyg55Vs7x/hO03QmMxSeuvstCEaR7bm+aP3lyyEnOqiVs6U9cF8dC7bQOMD InEifBNVzTCPA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Eric Dumazet , syzbot , "David S . Miller" , Sasha Levin Subject: [PATCH 4.9 60/71] inet: annotate date races around sk->sk_txhash Date: Mon, 28 Jun 2021 10:39:52 -0400 Message-Id: <20210628144003.34260-61-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210628144003.34260-1-sashal@kernel.org> References: <20210628144003.34260-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v4.x/stable-review/patch-4.9.274-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-4.9.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 4.9.274-rc1 X-KernelTest-Deadline: 2021-06-30T14:39+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet [ Upstream commit b71eaed8c04f72a919a9c44e83e4ee254e69e7f3 ] UDP sendmsg() path can be lockless, it is possible for another thread to re-connect an change sk->sk_txhash under us. There is no serious impact, but we can use READ_ONCE()/WRITE_ONCE() pair to document the race. BUG: KCSAN: data-race in __ip4_datagram_connect / skb_set_owner_w write to 0xffff88813397920c of 4 bytes by task 30997 on cpu 1: sk_set_txhash include/net/sock.h:1937 [inline] __ip4_datagram_connect+0x69e/0x710 net/ipv4/datagram.c:75 __ip6_datagram_connect+0x551/0x840 net/ipv6/datagram.c:189 ip6_datagram_connect+0x2a/0x40 net/ipv6/datagram.c:272 inet_dgram_connect+0xfd/0x180 net/ipv4/af_inet.c:580 __sys_connect_file net/socket.c:1837 [inline] __sys_connect+0x245/0x280 net/socket.c:1854 __do_sys_connect net/socket.c:1864 [inline] __se_sys_connect net/socket.c:1861 [inline] __x64_sys_connect+0x3d/0x50 net/socket.c:1861 do_syscall_64+0x4a/0x90 arch/x86/entry/common.c:47 entry_SYSCALL_64_after_hwframe+0x44/0xae read to 0xffff88813397920c of 4 bytes by task 31039 on cpu 0: skb_set_hash_from_sk include/net/sock.h:2211 [inline] skb_set_owner_w+0x118/0x220 net/core/sock.c:2101 sock_alloc_send_pskb+0x452/0x4e0 net/core/sock.c:2359 sock_alloc_send_skb+0x2d/0x40 net/core/sock.c:2373 __ip6_append_data+0x1743/0x21a0 net/ipv6/ip6_output.c:1621 ip6_make_skb+0x258/0x420 net/ipv6/ip6_output.c:1983 udpv6_sendmsg+0x160a/0x16b0 net/ipv6/udp.c:1527 inet6_sendmsg+0x5f/0x80 net/ipv6/af_inet6.c:642 sock_sendmsg_nosec net/socket.c:654 [inline] sock_sendmsg net/socket.c:674 [inline] ____sys_sendmsg+0x360/0x4d0 net/socket.c:2350 ___sys_sendmsg net/socket.c:2404 [inline] __sys_sendmmsg+0x315/0x4b0 net/socket.c:2490 __do_sys_sendmmsg net/socket.c:2519 [inline] __se_sys_sendmmsg net/socket.c:2516 [inline] __x64_sys_sendmmsg+0x53/0x60 net/socket.c:2516 do_syscall_64+0x4a/0x90 arch/x86/entry/common.c:47 entry_SYSCALL_64_after_hwframe+0x44/0xae value changed: 0xbca3c43d -> 0xfdb309e0 Reported by Kernel Concurrency Sanitizer on: CPU: 0 PID: 31039 Comm: syz-executor.2 Not tainted 5.13.0-rc3-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Signed-off-by: Eric Dumazet Reported-by: syzbot Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- include/net/sock.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index d0e18917d8be..cf27f3688c39 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1681,7 +1681,8 @@ static inline u32 net_tx_rndhash(void) static inline void sk_set_txhash(struct sock *sk) { - sk->sk_txhash = net_tx_rndhash(); + /* This pairs with READ_ONCE() in skb_set_hash_from_sk() */ + WRITE_ONCE(sk->sk_txhash, net_tx_rndhash()); } static inline void sk_rethink_txhash(struct sock *sk) @@ -1936,9 +1937,12 @@ static inline void sock_poll_wait(struct file *filp, static inline void skb_set_hash_from_sk(struct sk_buff *skb, struct sock *sk) { - if (sk->sk_txhash) { + /* This pairs with WRITE_ONCE() in sk_set_txhash() */ + u32 txhash = READ_ONCE(sk->sk_txhash); + + if (txhash) { skb->l4_hash = 1; - skb->hash = sk->sk_txhash; + skb->hash = txhash; } } -- 2.30.2