Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1614732pxb; Mon, 11 Oct 2021 09:31:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDHUZAn8EPHcOx+Nv1Aw2s7gwnGNBiTeT4eoU+UpEk7ddThV95RsOJDTzrHwPogcMW4IK4 X-Received: by 2002:a05:6402:1e88:: with SMTP id f8mr30855764edf.86.1633969889481; Mon, 11 Oct 2021 09:31:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633969889; cv=none; d=google.com; s=arc-20160816; b=fCJSPU/HB5QTaSJ5uqeaQ6cdBSlhZqW6KoT085Fr3ivYkQhVNyhLzZGwC6ym+jpfbm nyBjMuDXAO8zi8H0FNFMqS9l78yqTaVrY94Oo60Zf3yaofcIc7MLrT0dCpPYfRmX6kwN ot2gO752DRRDQ76Ew13ZGsoL5SClkRDpRRyhkuMivdR0v+UpfxMlWy+m4a7iZTARWF0L 3pUNbv9B9InZnuzc007ePiM9bB9mdCfWxizkqBrrc/ZJ0UdWa254A0XMwoBxRiDVpaH+ f+wjSmjcO1gQRSsAwviwM36oDK3diDABKQvHKR23n2mFCk0eoZLtD/f3z67MpTf0ejKn +qxw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=OHpJih+vx7ANT6C8QBBDLcTH/tpHjM1/aNYgAjC7z18=; b=YwHuL7oTrWw8EBr6zwxGY0K7R+Vv/0iI70crBg9UaQBH0JyiATa+v+Yl3WZyeyN7Cv utHNeEi+9/66UlZ4J7uSz9mWZBD98YloV7pf/gbUGJSsBCUnVR1o5Dv19AfPVf/IucrG hv8Z+bWX6BTL6R6Nsyn8eCn7Uo1RRHcW7LMJI1k1FSxXM4ROYFP/S+aJvQWtf2Gb4yMl j3f/D5T+XtWo9kJvOPaE/t1lbOHvvjrNzvbiTUJNfnu0NgvnNzK12X5y5fZNadnF1+rF PwJuCOmzoZsZQ2dBtTA9lmMHKedj7CBhnhWIctOsCjMg4Wf/TANyp/hDQKNsggXFENvN xlRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KIuwrZiv; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eb8si1647249edb.325.2021.10.11.09.31.06; Mon, 11 Oct 2021 09:31:29 -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=@linuxfoundation.org header.s=korg header.b=KIuwrZiv; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237673AbhJKNwS (ORCPT + 99 others); Mon, 11 Oct 2021 09:52:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:39388 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237366AbhJKNvL (ORCPT ); Mon, 11 Oct 2021 09:51:11 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id F2F8860F21; Mon, 11 Oct 2021 13:49:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633960149; bh=fvvCLpnxyuVH+IDAOepqFwkw9uu/56HD6cSdulS1j+s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KIuwrZiv8ZDRiNO31X+TCvHiaKUBpCNOyMPV95puDG9ayp20tsaRRk5JeJdwDHCxr F/wFOphWqECb6gcMntpF3PO2s5HdWxCnf0qfTsENlCYS6t/eQ2Iackqna/PIlkyioq Kd0kKz1kKscY6dp4vNRhWj1RHJuTVw7Rrr6aRg1s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , syzbot , "David S. Miller" , Sasha Levin Subject: [PATCH 5.4 36/52] netlink: annotate data races around nlk->bound Date: Mon, 11 Oct 2021 15:46:05 +0200 Message-Id: <20211011134504.970228383@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211011134503.715740503@linuxfoundation.org> References: <20211011134503.715740503@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet [ Upstream commit 7707a4d01a648e4c655101a469c956cb11273655 ] While existing code is correct, KCSAN is reporting a data-race in netlink_insert / netlink_sendmsg [1] It is correct to read nlk->bound without a lock, as netlink_autobind() will acquire all needed locks. [1] BUG: KCSAN: data-race in netlink_insert / netlink_sendmsg write to 0xffff8881031c8b30 of 1 bytes by task 18752 on cpu 0: netlink_insert+0x5cc/0x7f0 net/netlink/af_netlink.c:597 netlink_autobind+0xa9/0x150 net/netlink/af_netlink.c:842 netlink_sendmsg+0x479/0x7c0 net/netlink/af_netlink.c:1892 sock_sendmsg_nosec net/socket.c:703 [inline] sock_sendmsg net/socket.c:723 [inline] ____sys_sendmsg+0x360/0x4d0 net/socket.c:2392 ___sys_sendmsg net/socket.c:2446 [inline] __sys_sendmsg+0x1ed/0x270 net/socket.c:2475 __do_sys_sendmsg net/socket.c:2484 [inline] __se_sys_sendmsg net/socket.c:2482 [inline] __x64_sys_sendmsg+0x42/0x50 net/socket.c:2482 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x3d/0x90 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae read to 0xffff8881031c8b30 of 1 bytes by task 18751 on cpu 1: netlink_sendmsg+0x270/0x7c0 net/netlink/af_netlink.c:1891 sock_sendmsg_nosec net/socket.c:703 [inline] sock_sendmsg net/socket.c:723 [inline] __sys_sendto+0x2a8/0x370 net/socket.c:2019 __do_sys_sendto net/socket.c:2031 [inline] __se_sys_sendto net/socket.c:2027 [inline] __x64_sys_sendto+0x74/0x90 net/socket.c:2027 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x3d/0x90 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae value changed: 0x00 -> 0x01 Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 18751 Comm: syz-executor.0 Not tainted 5.14.0-rc1-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Fixes: da314c9923fe ("netlink: Replace rhash_portid with bound") Signed-off-by: Eric Dumazet Reported-by: syzbot Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/netlink/af_netlink.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index acc76a738cfd..cb35680db9b2 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -585,7 +585,10 @@ static int netlink_insert(struct sock *sk, u32 portid) /* We need to ensure that the socket is hashed and visible. */ smp_wmb(); - nlk_sk(sk)->bound = portid; + /* Paired with lockless reads from netlink_bind(), + * netlink_connect() and netlink_sendmsg(). + */ + WRITE_ONCE(nlk_sk(sk)->bound, portid); err: release_sock(sk); @@ -1003,7 +1006,8 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr, if (nlk->ngroups < BITS_PER_LONG) groups &= (1UL << nlk->ngroups) - 1; - bound = nlk->bound; + /* Paired with WRITE_ONCE() in netlink_insert() */ + bound = READ_ONCE(nlk->bound); if (bound) { /* Ensure nlk->portid is up-to-date. */ smp_rmb(); @@ -1089,8 +1093,9 @@ static int netlink_connect(struct socket *sock, struct sockaddr *addr, /* No need for barriers here as we return to user-space without * using any of the bound attributes. + * Paired with WRITE_ONCE() in netlink_insert(). */ - if (!nlk->bound) + if (!READ_ONCE(nlk->bound)) err = netlink_autobind(sock); if (err == 0) { @@ -1879,7 +1884,8 @@ static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) dst_group = nlk->dst_group; } - if (!nlk->bound) { + /* Paired with WRITE_ONCE() in netlink_insert() */ + if (!READ_ONCE(nlk->bound)) { err = netlink_autobind(sock); if (err) goto out; -- 2.33.0