linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Eric Dumazet <edumazet@google.com>,
	syzbot <syzkaller@googlegroups.com>,
	Francesco Ruggeri <fruggeri@aristanetworks.com>,
	"David S. Miller" <davem@davemloft.net>
Subject: [PATCH 4.9 07/27] net/packet: fix a race in packet_bind() and packet_notifier()
Date: Fri, 15 Dec 2017 10:51:50 +0100	[thread overview]
Message-ID: <20171215092258.216735295@linuxfoundation.org> (raw)
In-Reply-To: <20171215092257.674368056@linuxfoundation.org>

4.9-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Eric Dumazet <edumazet@google.com>


[ Upstream commit 15fe076edea787807a7cdc168df832544b58eba6 ]

syzbot reported crashes [1] and provided a C repro easing bug hunting.

When/if packet_do_bind() calls __unregister_prot_hook() and releases
po->bind_lock, another thread can run packet_notifier() and process an
NETDEV_UP event.

This calls register_prot_hook() and hooks again the socket right before
first thread is able to grab again po->bind_lock.

Fixes this issue by temporarily setting po->num to 0, as suggested by
David Miller.

[1]
dev_remove_pack: ffff8801bf16fa80 not found
------------[ cut here ]------------
kernel BUG at net/core/dev.c:7945!  ( BUG_ON(!list_empty(&dev->ptype_all)); )
invalid opcode: 0000 [#1] SMP KASAN
Dumping ftrace buffer:
   (ftrace buffer empty)
Modules linked in:
device syz0 entered promiscuous mode
CPU: 0 PID: 3161 Comm: syzkaller404108 Not tainted 4.14.0+ #190
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
task: ffff8801cc57a500 task.stack: ffff8801cc588000
RIP: 0010:netdev_run_todo+0x772/0xae0 net/core/dev.c:7945
RSP: 0018:ffff8801cc58f598 EFLAGS: 00010293
RAX: ffff8801cc57a500 RBX: dffffc0000000000 RCX: ffffffff841f75b2
RDX: 0000000000000000 RSI: 1ffff100398b1ede RDI: ffff8801bf1f8810
device syz0 entered promiscuous mode
RBP: ffff8801cc58f898 R08: 0000000000000001 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000000 R12: ffff8801bf1f8cd8
R13: ffff8801cc58f870 R14: ffff8801bf1f8780 R15: ffff8801cc58f7f0
FS:  0000000001716880(0000) GS:ffff8801db400000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000020b13000 CR3: 0000000005e25000 CR4: 00000000001406f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
 rtnl_unlock+0xe/0x10 net/core/rtnetlink.c:106
 tun_detach drivers/net/tun.c:670 [inline]
 tun_chr_close+0x49/0x60 drivers/net/tun.c:2845
 __fput+0x333/0x7f0 fs/file_table.c:210
 ____fput+0x15/0x20 fs/file_table.c:244
 task_work_run+0x199/0x270 kernel/task_work.c:113
 exit_task_work include/linux/task_work.h:22 [inline]
 do_exit+0x9bb/0x1ae0 kernel/exit.c:865
 do_group_exit+0x149/0x400 kernel/exit.c:968
 SYSC_exit_group kernel/exit.c:979 [inline]
 SyS_exit_group+0x1d/0x20 kernel/exit.c:977
 entry_SYSCALL_64_fastpath+0x1f/0x96
RIP: 0033:0x44ad19

Fixes: 30f7ea1c2b5f ("packet: race condition in packet_bind")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reported-by: syzbot <syzkaller@googlegroups.com>
Cc: Francesco Ruggeri <fruggeri@aristanetworks.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 net/packet/af_packet.c |    5 +++++
 1 file changed, 5 insertions(+)

--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -3101,6 +3101,10 @@ static int packet_do_bind(struct sock *s
 	if (need_rehook) {
 		if (po->running) {
 			rcu_read_unlock();
+			/* prevents packet_notifier() from calling
+			 * register_prot_hook()
+			 */
+			po->num = 0;
 			__unregister_prot_hook(sk, true);
 			rcu_read_lock();
 			dev_curr = po->prot_hook.dev;
@@ -3109,6 +3113,7 @@ static int packet_do_bind(struct sock *s
 								 dev->ifindex);
 		}
 
+		BUG_ON(po->running);
 		po->num = proto;
 		po->prot_hook.type = proto;
 

  parent reply	other threads:[~2017-12-15  9:53 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-15  9:51 [PATCH 4.9 00/27] 4.9.70-stable review Greg Kroah-Hartman
2017-12-15  9:51 ` [PATCH 4.9 02/27] s390/qeth: fix early exit from error path Greg Kroah-Hartman
2017-12-15  9:51 ` [PATCH 4.9 03/27] tipc: fix memory leak in tipc_accept_from_sock() Greg Kroah-Hartman
2017-12-15  9:51 ` [PATCH 4.9 05/27] sit: update frag_off info Greg Kroah-Hartman
2017-12-15  9:51 ` [PATCH 4.9 06/27] packet: fix crash in fanout_demux_rollover() Greg Kroah-Hartman
2017-12-15  9:51 ` Greg Kroah-Hartman [this message]
2017-12-15  9:51 ` [PATCH 4.9 10/27] stmmac: reset last TSO segment size after device open Greg Kroah-Hartman
2017-12-15  9:51 ` [PATCH 4.9 12/27] s390/qeth: build max size GSO skbs on L2 devices Greg Kroah-Hartman
2017-12-15  9:51 ` [PATCH 4.9 13/27] s390/qeth: fix GSO throughput regression Greg Kroah-Hartman
2017-12-15  9:51 ` [PATCH 4.9 14/27] s390/qeth: fix thinko in IPv4 multicast address tracking Greg Kroah-Hartman
2017-12-15  9:51 ` [PATCH 4.9 15/27] tipc: call tipc_rcv() only if bearer is up in tipc_udp_recv() Greg Kroah-Hartman
2017-12-15  9:51 ` [PATCH 4.9 16/27] Fix handling of verdicts after NF_QUEUE Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 17/27] ipmi: Stop timers before cleaning up the module Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 18/27] s390: always save and restore all registers on context switch Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 19/27] usb: gadget: ffs: Forbid usb_ep_alloc_request from sleeping Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 20/27] fix kcm_clone() Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 21/27] KVM: arm/arm64: vgic-its: Preserve the revious read from the pending table Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 22/27] powerpc/64: Fix checksum folding in csum_tcpudp_nofold and ip_fast_csum_nofold Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 23/27] kbuild: do not call cc-option before KBUILD_CFLAGS initialization Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 24/27] ipvlan: fix ipv6 outbound device Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 25/27] audit: ensure that audit=1 actually enables audit for PID 1 Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 26/27] md: free unused memory after bitmap resize Greg Kroah-Hartman
2017-12-15  9:52 ` [PATCH 4.9 27/27] RDMA/cxgb4: Annotate r2 and stag as __be32 Greg Kroah-Hartman
2017-12-15 17:40 ` [PATCH 4.9 00/27] 4.9.70-stable review Guenter Roeck
2017-12-15 21:12 ` Shuah Khan
2017-12-16  5:55 ` Naresh Kamboju

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171215092258.216735295@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=fruggeri@aristanetworks.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=syzkaller@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).