Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3837181pxv; Mon, 28 Jun 2021 14:16:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyywlX5F2vYwu1I+HDQqH7i+PLeGU0rY6K3XXN9AH9FGwbnYki57kjBLF7oAB7st/bwPITb X-Received: by 2002:a92:d2c5:: with SMTP id w5mr6286992ilg.174.1624915012486; Mon, 28 Jun 2021 14:16:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624915012; cv=none; d=google.com; s=arc-20160816; b=i83fu9oy+T7HC5n/J2lJ/ol1/qkdxrfGhE7RGw+JcY2T5af1r5XbKE6POCJuCAYAhP EoSkOHOAKxzaqhLPkk0/XxAvbWe7E5ATKqyHeSoX3Risyn/JnZgedKK6gPCPVJSCkW+1 3CSJjX6uu5XWZaffV8oZy+TOdRmXCQ2pnTu3iJ6/I5EFQY6una6ynST/g8T5N1pN+QGJ k3fzsZFi2bhZLq2HpeU8EVyj6NHkHgLgaN/7LUv65paq+aQJ7ne4hXxQOm92F98fvPEU Z3nZXvgFHZGBJ8gFdyZtAbObNaZZIaGAf7U/IqXefpOx5tARFJL9NU9SOQoagEbn9g9F soOQ== 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=yZvJMo8VINvJzwouxDsnypEaGC1b/n9Tf/HBcvITdU4=; b=Z+dsIoYeeF/34eepBilO2Q0/iWuFxkitCAu22VT4GtORpLLSmMgCEcmhNI52YiZEYi JEe4UMIQkgF6yHVr+/v77JhEEpaDkzbEBHhtBrwee8HFt6PEKKRX8hbmmSal7Zr3UzNH zLmcrMPb1FxG9vf/QnSauRGdFDduuF5KTd4cl1VO81PnNhy8+pMuhNOW0gFmBZQsMPSs 97Zi0WsiSMnKGvgu+4zIs9RQ9ukzSXz6FyrAmIaopJ9QEPvIcOGqSRUF5mWf7lhimWX4 xOVynESzPdx4TZqM9ZAoy52i+HhFinzAiBOCcMKiLExWlaceHd3J0pDH3WtF0zZDsV0I RvUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=aN8iHo2R; 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 p15si6255276ilh.148.2021.06.28.14.16.35; Mon, 28 Jun 2021 14:16:52 -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=aN8iHo2R; 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 S234483AbhF1PPr (ORCPT + 99 others); Mon, 28 Jun 2021 11:15:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:34082 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236181AbhF1Oyz (ORCPT ); Mon, 28 Jun 2021 10:54:55 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CAD3F61D4B; Mon, 28 Jun 2021 14:37:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624891057; bh=ylrPwv25nASJ8nxq7coj1xwwmqiagVmYlIn23Pq96So=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aN8iHo2RXSu2PKcCn3F0joeUL2S6Z06r4xxob8EdVgpGSLsRGobbJAxjuy+n86a8S sDgR/Nc2zhpc8opTNoYpm8EBaTFhfYgictvbHW4bczWbJPCAJGzXdu0Gd/KWmWIyF9 BO0YGxWonhfm9GSylqrZn4AuiBOu9PibHuhhwCJDlC6t26oV8gcnRt2i8TbqWOSyti 9q/KFCFjqENvnpX4G1O7gOgImG3wXfb2clDzB1dHnNiuhgkta5NnlGDcNI8h7F1HBg wKvPhN5klVB1GBS0jt46JNOK3NmAxKiBV95jBLQOwkDLu8rYZMkDxiCLSX1Py8Z/aK UNGluDm25aPuQ== 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 78/88] net/packet: annotate accesses to po->bind Date: Mon, 28 Jun 2021 10:36:18 -0400 Message-Id: <20210628143628.33342-79-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 c7d2ef5dd4b03ed0ee1d13bc0c55f9cf62d49bd6 ] tpacket_snd(), packet_snd(), packet_getname() and packet_seq_show() can read po->num without holding a lock. This means other threads can change po->num at the same time. KCSAN complained about this known fact [1] Add READ_ONCE()/WRITE_ONCE() to address the issue. [1] BUG: KCSAN: data-race in packet_do_bind / packet_sendmsg write to 0xffff888131a0dcc0 of 2 bytes by task 24714 on cpu 0: packet_do_bind+0x3ab/0x7e0 net/packet/af_packet.c:3181 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 0xffff888131a0dcc0 of 2 bytes by task 24719 on cpu 1: packet_snd net/packet/af_packet.c:2899 [inline] packet_sendmsg+0x317/0x3570 net/packet/af_packet.c:3040 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_sendmsg+0x1ed/0x270 net/socket.c:2433 __do_sys_sendmsg net/socket.c:2442 [inline] __se_sys_sendmsg net/socket.c:2440 [inline] __x64_sys_sendmsg+0x42/0x50 net/socket.c:2440 do_syscall_64+0x4a/0x90 arch/x86/entry/common.c:47 entry_SYSCALL_64_after_hwframe+0x44/0xae value changed: 0x0000 -> 0x1200 Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 24719 Comm: syz-executor.5 Not tainted 5.13.0-rc4-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, 8 insertions(+), 8 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index b62ec43ed54f..6f55942619d4 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -2694,7 +2694,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) } if (likely(saddr == NULL)) { dev = packet_cached_dev_get(po); - proto = po->num; + proto = READ_ONCE(po->num); } else { err = -EINVAL; if (msg->msg_namelen < sizeof(struct sockaddr_ll)) @@ -2907,7 +2907,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len) if (likely(saddr == NULL)) { dev = packet_cached_dev_get(po); - proto = po->num; + proto = READ_ONCE(po->num); } else { err = -EINVAL; if (msg->msg_namelen < sizeof(struct sockaddr_ll)) @@ -3177,7 +3177,7 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex, /* prevents packet_notifier() from calling * register_prot_hook() */ - po->num = 0; + WRITE_ONCE(po->num, 0); __unregister_prot_hook(sk, true); rcu_read_lock(); dev_curr = po->prot_hook.dev; @@ -3187,7 +3187,7 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex, } BUG_ON(po->running); - po->num = proto; + WRITE_ONCE(po->num, proto); po->prot_hook.type = proto; if (unlikely(unlisted)) { @@ -3534,7 +3534,7 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr, sll->sll_family = AF_PACKET; sll->sll_ifindex = po->ifindex; - sll->sll_protocol = po->num; + 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); @@ -4429,7 +4429,7 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, was_running = po->running; num = po->num; if (was_running) { - po->num = 0; + WRITE_ONCE(po->num, 0); __unregister_prot_hook(sk, false); } spin_unlock(&po->bind_lock); @@ -4464,7 +4464,7 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, spin_lock(&po->bind_lock); if (was_running) { - po->num = num; + WRITE_ONCE(po->num, num); register_prot_hook(sk); } spin_unlock(&po->bind_lock); @@ -4635,7 +4635,7 @@ static int packet_seq_show(struct seq_file *seq, void *v) s, refcount_read(&s->sk_refcnt), s->sk_type, - ntohs(po->num), + ntohs(READ_ONCE(po->num)), po->ifindex, po->running, atomic_read(&s->sk_rmem_alloc), -- 2.30.2