Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3863049pxv; Mon, 28 Jun 2021 15:03:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbZI5e2L8CqimYbMLieojEu9dUfKxLchiBNk8aN0OxkoWBLBr/Dys7JfBubP1KB/4bcdoR X-Received: by 2002:a05:6402:51c9:: with SMTP id r9mr4910391edd.326.1624917796012; Mon, 28 Jun 2021 15:03:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624917796; cv=none; d=google.com; s=arc-20160816; b=Wqym/cTqZm+Wgkp4aXKNmVoP51uoe8Du0/HqZeiNR1SeKvbMkJ2IkCbCN91YZV2Nje bGEhVUbmGMZ31CTXbFRm1Hq5VNVYq5EYsQZM/+J8tyMOr9QUylOmdMF710fE5p6o0f80 Dte5TcgtMnhjgzaYGfRzG+QwaSSaekD13oYA1lp/VIO/sti28Eq/skNkOVCcqqplYZcC acP8fmp0T8Zn2PB8g4ItxOLB3DW9QcH7JhaAesr/R4l8QaooUC34za5N/muKu9uZJFkq vF7pwWhHCgyYIMacLQAwch0nSgClcl0BBfTRuDVhJNx6RaMTaNAFN5IBksnCPeCO9KHs iRLw== 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=zRQ9q9Rol0ZRcShvEsnfVcW+YrRZAlsa61K6TdIasXo=; b=A3BfgJRXjCFJ1H8+OLFhvFU8SX41UmQMeT9Fwoso20kRRoTgr4kC7NJG3ApLb305e/ XV/Q8YRDi7Jv8Jc+Pr2x2/fegm2itCxQGiXfNgNS5EjRcGLan0kuI1AKfBQbD542ro3q GKQHBPFqNdfjt0AiwVdTcFctBsAb/KikrOQhfyT9UAsdvIflDOGl0yD6fawtKp6uQ/k8 kMN+E+nfQs66O9OyRy8TTbdzV33dvzpydQNYnZlkgu9GrtKCYAbB1CV3fUVK7TV0KB6r F8wCo592972WGfxBmrHaoNgrWetz8TBonPH0c02AWQW9jbRizDr4HflTSgLf9mf8CIy8 IUcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=mBL5piYD; 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 f8si14677409ejl.652.2021.06.28.15.02.51; Mon, 28 Jun 2021 15:03:15 -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=mBL5piYD; 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 S238638AbhF1PYj (ORCPT + 99 others); Mon, 28 Jun 2021 11:24:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:37006 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236405AbhF1PBu (ORCPT ); Mon, 28 Jun 2021 11:01:50 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1BB2C61D76; Mon, 28 Jun 2021 14:40:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624891259; bh=eUwNnlRk8uuKO1NE9Yv9SkRvv98DDeRgeQvdEdzlwpQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mBL5piYD5//1dR8fuhmDPnqrj8wiWEdfMGdrZY2yBg3p46DvRyBEDkwa8HopqHrck 4DMstH+xQS6BvVIXB5RZK05fX2xDTwvjJR5MNfbVeWdyUzPIhOFcJhnngu3M/sx3Bi lU8vbDJ2ZKf9CFcI+kKBxPX0++aqpvqM3CZmc5UD3lZk3V0fa9QfNLz0RiH7V7fKiJ I0JA+YSS8FMJ/ofMCfnGR4fty/cKN1RXmZmLAeqgsC7bARdA+w+qmqVd9GY7PZi9tu RHUROWF5IvJekCR/jmTFlNiyUkDyl0lSAdk4gv3TNEz0PH++BVvJc6OHN1ekTGipZj UmaackoRl1g0g== 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 63/71] net/packet: annotate accesses to po->ifindex Date: Mon, 28 Jun 2021 10:39:55 -0400 Message-Id: <20210628144003.34260-64-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 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 5f9007e7c28d..370d0a4af1f9 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -3173,11 +3173,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); } } @@ -3492,7 +3492,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(); @@ -3508,16 +3508,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; @@ -4097,7 +4099,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; @@ -4603,7 +4605,7 @@ static int packet_seq_show(struct seq_file *seq, void *v) atomic_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