From patchwork Tue Nov 28 16:03:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 842222 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="elWOoFqO"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3ymT3t1vLqz9ryr for ; Wed, 29 Nov 2017 03:03:38 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751979AbdK1QDg (ORCPT ); Tue, 28 Nov 2017 11:03:36 -0500 Received: from mail-pl0-f67.google.com ([209.85.160.67]:38374 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751771AbdK1QDe (ORCPT ); Tue, 28 Nov 2017 11:03:34 -0500 Received: by mail-pl0-f67.google.com with SMTP id s10so169380plj.5 for ; Tue, 28 Nov 2017 08:03:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:cc:date:in-reply-to:references :mime-version:content-transfer-encoding; bh=0HVQB0kCwbg9+xF2vj7H6WQ97V1rguGK9pegJTyjFVY=; b=elWOoFqOOMxpQDi/ZL3cLgpZnjALbmm6KUUbTvV4n8R8Vaassu31D0IokQOeHEjGQg 1ZEgR1XhvJnZv0gQ8/9moifCicDKG2pc8VHNRyk7zTbWuoREU/PwRlL8nYnEx6fWAVIh Lft7OHQds/Sf8DwRGAUxA3rN8DIanrAmohHrbzP3BPK3SrBgc9SvEtg47LUFcgdMSEXg p+oTrVC8JpFrseBMEISw1QMgJgankkrmH7s0Tp/bBH7YtsDvJr0pq3jqOW2aHjdSvVHm lL3COB91EClH5oHDk68kImUUcJFqO9YkwL6e7Ki+is98ZzgweVPuPRoNy5pYbadEFUip 7rww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=0HVQB0kCwbg9+xF2vj7H6WQ97V1rguGK9pegJTyjFVY=; b=I8AO2AF42akUC9uZb2G8+IPq/P6oknT7S1GiFCXYA6VtitS/eWJDRKv1e4EFDWYaq7 SaIoMhbRk50KCvnS3gjAItY7uRmYRtwp4ktRaXdC+kDYIat2K8FSAIanw0/wlHnb2Vvp Lgkh03z6oZVysGiPwFuZ7DZaFghaRu30nnsQvDyiI62o9tAVz5xMcYYctkpUAhLFDQ6s g0QkpYDQ3I+hCj4OR5/A4aOWeENmmmbWfzRXZV7G7s7nySvfr6a0welErd4xKmonF48P EHI6VjZMgseLp9P97s2GiiE191PiUo59RmWAac2nuaf2zFJBaHoN9F+n20Roo/XyoO8y HGdQ== X-Gm-Message-State: AJaThX5HN9kzJ3SwfhkRwOiFuf3ZP3tDt59rD9ZxkVGrchZz0fsw2gRS sRWy2+J7aVNEYIlx/nAvTl4= X-Google-Smtp-Source: AGs4zMbN610G1GG2RWhLcGEugg+wjgJb3bEdfxNl5yFbcUBZj8c79PdhW4Y5UCkZw1J5BCh1iklTJw== X-Received: by 10.84.176.3 with SMTP id u3mr29825087plb.242.1511885013988; Tue, 28 Nov 2017 08:03:33 -0800 (PST) Received: from [10.1.104.90] ([207.198.105.19]) by smtp.googlemail.com with ESMTPSA id p85sm14231360pfk.147.2017.11.28.08.03.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 28 Nov 2017 08:03:32 -0800 (PST) Message-ID: <1511885010.16595.17.camel@gmail.com> Subject: [PATCH v2 net] net/packet: fix a race in packet_bind() and packet_notifier() From: Eric Dumazet To: David Miller Cc: netdev@vger.kernel.org, fruggeri@aristanetworks.com, willemb@google.com Date: Tue, 28 Nov 2017 08:03:30 -0800 In-Reply-To: <1511882095.16595.14.camel@gmail.com> References: <1511841652.16595.11.camel@gmail.com> <20171128.094855.1432729042351719024.davem@davemloft.net> <1511882095.16595.14.camel@gmail.com> X-Mailer: Evolution 3.22.6-1+deb9u1 Mime-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Eric Dumazet 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 Reported-by: syzbot Cc: Francesco Ruggeri --- net/packet/af_packet.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index 737092ca9b4eed464b6c0907d85b679ae4da6046..cd454ca583d93841dcad4e1d83a660f15731c5fb 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -3097,6 +3097,10 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex, 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; @@ -3105,6 +3109,7 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex, dev->ifindex); } + BUG_ON(po->running); po->num = proto; po->prot_hook.type = proto;