Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3597698pxv; Mon, 28 Jun 2021 08:15:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxKDl7vlFWv1O8pEdNXn8QceQZdn6pE4vTMIj8WkH444YWCEHT5HrdjV5noh5tyFylF58mt X-Received: by 2002:a92:c792:: with SMTP id c18mr18047826ilk.75.1624893307897; Mon, 28 Jun 2021 08:15:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624893307; cv=none; d=google.com; s=arc-20160816; b=T7t00Wlzg5pHFYbhmxZRU7XCWYY8TUjbBCHV79EOo9d3KrNehuLHtRSCIkTXhcIf8f AWsOgoRpjeo7p4uVTjFDE7AZXxqg6jy7BQL5OGomdkZNKBQVOPShvroo+BIt7NIpgvHp QhXYrYOLJop6ITB6DArxeMNFumx9mIPcabxF22UnxsuvSLi7meGzDYclJlimhdmep6Ar jMv4IEO2yfbpn+okYNB2Qqgw3rd4QXeNQAgVHMzBoCTkDBgLZYwaffAlS3STK9+TSzP5 LcsL7MO5VOscpKDgGtv5JgSy7tzPRs3D7jRIAoOqPehI7GK/xEGul52kqg+bckOvHgQ7 zSAQ== 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=c/MfHL5IFXi7LD45JDWat/RVOLTysLQvvFwhYWhjl+4=; b=u9Ec1Jq9rkgX/2FQWbSh6R+2DH6B/62qi1DMZ3oxgGKNxQMkVooMGlWCpwpvZWfAwI 1IByL0rqtAg3+LUjIVRBg1K8fZlRHWBaLiEUog2nID/+jrQeHOuMFtP+w6CYND+DnUXn 4/aTNWZ0zre+tw+wWB82KkKrYHMEe4NNLHHaDsBM6HZk+kd50FYNCbksk8fGQUMv222i YJg7z+pH5R3V9tud+N2cT10KInMwIBojTMbhgqCiEa2bBK04PfdAnrNHWJKTERKkZj4j 7OOVI0j1rJKsPEzXxmbRD/XFCd9KLy0tlUQPQMO14C4ojEwCkzgUyzGeIVNPj54fsIKl hTtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=t4GXQ1W+; 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 l8si17807798ilk.17.2021.06.28.08.14.52; Mon, 28 Jun 2021 08:15:07 -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=t4GXQ1W+; 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 S235980AbhF1PPk (ORCPT + 99 others); Mon, 28 Jun 2021 11:15:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:59474 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236188AbhF1Oyz (ORCPT ); Mon, 28 Jun 2021 10:54:55 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A9A9361D4C; Mon, 28 Jun 2021 14:37:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624891058; bh=1eWAoRn5nInbiwq7vRDa/HF9ku5pZTgeUaxplz+/z7k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t4GXQ1W+3L89TRXI3PI7L7z7LbpezKMct8HbSlWpgN6Oh1J2VLYN+41ehnpCRri2w fG3dwpfOIqAbsDaBM4SuG217u7xbYIImIKVuWLyn8x3SxzZroVZ6DvJ8R1QwflWlR4 PeFKsXcGPZ2JokuSWPgxLj2dStBn1eShfvKhj5mn3sf5iqt8YtkLA5FWVeOGDWfGZB EZhX/MmCEFGdyGl7afSeAxA25BE0a3u0ihLIIySMh4nl9zM3hmoRLrQr9k74+mF3P0 RDJsaRZedirAaFagNIW7aNaMfHYWpP9zS8wnqxHPWBBGAiDgkMvbakfPko9uDFZNGC CGqyF30SGZ+gA== 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.14 79/88] net/packet: annotate accesses to po->ifindex Date: Mon, 28 Jun 2021 10:36:19 -0400 Message-Id: <20210628143628.33342-80-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210628143628.33342-1-sashal@kernel.org> References: <20210628143628.33342-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v4.x/stable-review/patch-4.14.238-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-4.14.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 4.14.238-rc1 X-KernelTest-Deadline: 2021-06-30T14:36+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 e032f7c9c7cefffcfb79b9fc16c53011d2d9d11f ] Like prior patch, we need to annotate lockless accesses to po->ifindex For instance, packet_getname() is reading po->ifindex (twice) while another thread is able to change po->ifindex. KCSAN reported: BUG: KCSAN: data-race in packet_do_bind / packet_getname write to 0xffff888143ce3cbc of 4 bytes by task 25573 on cpu 1: packet_do_bind+0x420/0x7e0 net/packet/af_packet.c:3191 packet_bind+0xc3/0xd0 net/packet/af_packet.c:3255 __sys_bind+0x200/0x290 net/socket.c:1637 __do_sys_bind net/socket.c:1648 [inline] __se_sys_bind net/socket.c:1646 [inline] __x64_sys_bind+0x3d/0x50 net/socket.c:1646 do_syscall_64+0x4a/0x90 arch/x86/entry/common.c:47 entry_SYSCALL_64_after_hwframe+0x44/0xae read to 0xffff888143ce3cbc of 4 bytes by task 25578 on cpu 0: packet_getname+0x5b/0x1a0 net/packet/af_packet.c:3525 __sys_getsockname+0x10e/0x1a0 net/socket.c:1887 __do_sys_getsockname net/socket.c:1902 [inline] __se_sys_getsockname net/socket.c:1899 [inline] __x64_sys_getsockname+0x3e/0x50 net/socket.c:1899 do_syscall_64+0x4a/0x90 arch/x86/entry/common.c:47 entry_SYSCALL_64_after_hwframe+0x44/0xae value changed: 0x00000000 -> 0x00000001 Reported by Kernel Concurrency Sanitizer on: CPU: 0 PID: 25578 Comm: syz-executor.5 Not tainted 5.13.0-rc6-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 --- net/packet/af_packet.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index 6f55942619d4..50ca70b3c175 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -3193,11 +3193,11 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex, if (unlikely(unlisted)) { dev_put(dev); po->prot_hook.dev = NULL; - po->ifindex = -1; + WRITE_ONCE(po->ifindex, -1); packet_cached_dev_reset(po); } else { po->prot_hook.dev = dev; - po->ifindex = dev ? dev->ifindex : 0; + WRITE_ONCE(po->ifindex, dev ? dev->ifindex : 0); packet_cached_dev_assign(po, dev); } } @@ -3512,7 +3512,7 @@ static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr, uaddr->sa_family = AF_PACKET; memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data)); rcu_read_lock(); - dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex); + dev = dev_get_by_index_rcu(sock_net(sk), READ_ONCE(pkt_sk(sk)->ifindex)); if (dev) strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data)); rcu_read_unlock(); @@ -3528,16 +3528,18 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr, struct sock *sk = sock->sk; struct packet_sock *po = pkt_sk(sk); DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr); + int ifindex; if (peer) return -EOPNOTSUPP; + ifindex = READ_ONCE(po->ifindex); sll->sll_family = AF_PACKET; - sll->sll_ifindex = po->ifindex; + sll->sll_ifindex = ifindex; sll->sll_protocol = READ_ONCE(po->num); sll->sll_pkttype = 0; rcu_read_lock(); - dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex); + dev = dev_get_by_index_rcu(sock_net(sk), ifindex); if (dev) { sll->sll_hatype = dev->type; sll->sll_halen = dev->addr_len; @@ -4117,7 +4119,7 @@ static int packet_notifier(struct notifier_block *this, } if (msg == NETDEV_UNREGISTER) { packet_cached_dev_reset(po); - po->ifindex = -1; + WRITE_ONCE(po->ifindex, -1); if (po->prot_hook.dev) dev_put(po->prot_hook.dev); po->prot_hook.dev = NULL; @@ -4636,7 +4638,7 @@ static int packet_seq_show(struct seq_file *seq, void *v) refcount_read(&s->sk_refcnt), s->sk_type, ntohs(READ_ONCE(po->num)), - po->ifindex, + READ_ONCE(po->ifindex), po->running, atomic_read(&s->sk_rmem_alloc), from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)), -- 2.30.2