Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp5018339rwb; Mon, 21 Nov 2022 15:27:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf5JlMA/VwHgcnqGKfV4LzT3+8MpLspNvpUpEcyCaW8ib2c5AV+lTkDgYsqiZSrgmOsNpf2N X-Received: by 2002:a05:6402:504:b0:461:5542:6884 with SMTP id m4-20020a056402050400b0046155426884mr8950390edv.390.1669073220229; Mon, 21 Nov 2022 15:27:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669073220; cv=none; d=google.com; s=arc-20160816; b=TgDuUYl0LLI/o1XYfNxVZddqt0nMcODNv7Ase/REpvQI0LmUCPrnlCYNuAJ07dYS83 12ofO33K3wph6hsCxRCcAz2HafbTO5Hyr0JPLx1iAyRMw7Mg13kV8J67ap059flHoBG5 qEt4rBa4dtmHKffIKp6JnqnOM7XaFUA7fAOcPCx4S27SG7MbJAACM6gVIilKRlrIXRvX mgjeKUsk8I0UhEPCo1C3gIO9b3B4rLuD8RggkgwMCytvfpLvGI/OqEBSytX4c+Sn5yI6 Vu5Rjlv7VAiXQASsb8Ph1S8w5S02/yS/cb0uiOKVHMFFuA2oviA4psnqQNFOnM5A4Zvh 8MgA== 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=jvBiVTAiVFL8oXjqYu3rGFg3jvEjEvNrylBaX+mBUaI=; b=vVqD22vQR+M+4rs5Yytkjdu16OWJ4VJq04HrcXFpHjwoSgZ6+mEOwQYHfZkJkre89v Oeia4JY8xzBlEJZi58OYHqQJYMWBn81TYeT3SuBkJYv9Ki12bHmhoqfss3o6STP843LG LRJDprCxNFNwJ50n0R8J+kHVXeITkxOX8Bdx9f1Tx0e11VxRa0o0m4bFbWefHPq27cLk 84neOxKS8pxaUbhAxtDzGJHLQOcc+WA45KSlXoVSE/gH7sMhJUbe/oz6yPkb0ilPOZ0B 9z/ysYgQXCbB15WG2S1CF3Xc7U5CzYyqs5Qp9e1nyD39xLi5l07atH8ZbwWsZTu+BfXT zpRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=YR6L75S+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i19-20020a05640242d300b00453a0393deasi11213937edc.368.2022.11.21.15.26.37; Mon, 21 Nov 2022 15:27:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=YR6L75S+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230255AbiKUXKw (ORCPT + 91 others); Mon, 21 Nov 2022 18:10:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230076AbiKUXKt (ORCPT ); Mon, 21 Nov 2022 18:10:49 -0500 Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B253CFEAB; Mon, 21 Nov 2022 15:10:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1669072248; x=1700608248; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jvBiVTAiVFL8oXjqYu3rGFg3jvEjEvNrylBaX+mBUaI=; b=YR6L75S+z6BDDd7eKMFf67q+c6emY0abLreC9oWl4v3TkT1cn5kYQ5QA p0MKXelocPF40vMimrnQM+uigoHdaObC3Dxyi5XTZD7YTONu0U53n3zgz VYEY1XNCXHIHdpd5ba5iwCN7ANnntAwHyGe/r963Jjpe48pV3yE4AYxAA Q=; X-IronPort-AV: E=Sophos;i="5.96,182,1665446400"; d="scan'208";a="153283300" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-af372327.us-west-2.amazon.com) ([10.25.36.210]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2022 23:10:47 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-pdx-2a-m6i4x-af372327.us-west-2.amazon.com (Postfix) with ESMTPS id 30E8B60F9B; Mon, 21 Nov 2022 23:10:46 +0000 (UTC) Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.42; Mon, 21 Nov 2022 23:10:45 +0000 Received: from 88665a182662.ant.amazon.com.com (10.43.162.178) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1118.20; Mon, 21 Nov 2022 23:10:41 +0000 From: Kuniyuki Iwashima To: CC: , , , , , , , , , , Subject: Re: BUG: unable to handle kernel NULL pointer dereference in sk_diag_fill Date: Mon, 21 Nov 2022 15:10:28 -0800 Message-ID: <20221121231028.50834-1-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.43.162.178] X-ClientProxiedBy: EX13D30UWC004.ant.amazon.com (10.43.162.4) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Wei, Thanks for reporting the issue. From: Wei Chen Date: Mon, 21 Nov 2022 21:34:50 +0800 > Dear Linux Developer, > > Recently when using our tool to fuzz kernel, the following crash was triggered. > > HEAD commit: 147307c69ba > git tree: upstream > compiler: clang 12.0.0 > console output: > https://drive.google.com/file/d/1WZBgd9dodq3qWgqIXNNBHNEpayHthWr5/view?usp=share_link > kernel config: https://drive.google.com/file/d/1NAf4S43d9VOKD52xbrqw-PUP1Mbj8z-S/view?usp=share_link > > Unfortunately, I didn't have a reproducer for this crash. My manual > check identifies that sk->sk_socket in sk_user_ns is null. This gave me a hint. I guess syzbot is dumping an AF_UNIX sk while close()ing it concurrently. Then, there seems to be a minor race like unix_release unix_diag_handler_dump <-- called after refcnt reaches 0 unix_release_sock unix_diag_get_exact unix_lookup_by_ino sock_hold unix_remove_socket <-- after this, sk cannot be seen in the hash table unix_remove_bsd_socket sock_orphan sk->sk_socket = NULL <-- clear the sk->sk_socket sk_diag_fill sk_diag_dump_uid sk_user_ns sk->sk_socket->file->f_cred->user_ns <-- NULL deref It seems we need to call refcnt_inc_not_zero() instead of sock_hold() in unix_lookup_by_ino(), so the fix would be like this. After checking other paths (at least netlink_dump_start() path) and doing some tests, I'll submit a patch. ---8<--- diff --git a/net/unix/diag.c b/net/unix/diag.c index 105f522a89fe..581500296515 100644 --- a/net/unix/diag.c +++ b/net/unix/diag.c @@ -241,8 +241,8 @@ static struct sock *unix_lookup_by_ino(struct net *net, unsigned int ino) for (i = 0; i < UNIX_HASH_SIZE; i++) { spin_lock(&net->unx.table.locks[i]); sk_for_each(sk, &net->unx.table.buckets[i]) { - if (ino == sock_i_ino(sk)) { - sock_hold(sk); + if (ino == sock_i_ino(sk) && + refcount_inc_not_zero(&sk->sk_refcnt)) { spin_unlock(&net->unx.table.locks[i]); return sk; } ---8<--- Thank you. > > IMPORTANT: if you fix the bug, please add the following tag to the commit: > Reported-by: Wei Chen > > BUG: kernel NULL pointer dereference, address: 0000000000000270 > #PF: supervisor read access in kernel mode > #PF: error_code(0x0000) - not-present page > PGD 12bbce067 P4D 12bbce067 PUD 12bc40067 PMD 0 > Oops: 0000 [#1] PREEMPT SMP > CPU: 0 PID: 27942 Comm: syz-executor.0 Not tainted 6.1.0-rc5-next-20221118 #2 > Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS > rel-1.13.0-48-gd9c812dda519-prebuilt.qemu.org 04/01/2014 > RIP: 0010:sk_user_ns include/net/sock.h:920 [inline] > RIP: 0010:sk_diag_dump_uid net/unix/diag.c:119 [inline] > RIP: 0010:sk_diag_fill+0x77d/0x890 net/unix/diag.c:170 > Code: 89 ef e8 66 d4 2d fd c7 44 24 40 00 00 00 00 49 8d 7c 24 18 e8 > 54 d7 2d fd 49 8b 5c 24 18 48 8d bb 70 02 00 00 e8 43 d7 2d fd <48> 8b > 9b 70 02 00 00 48 8d 7b 10 e8 33 d7 2d fd 48 8b 5b 10 48 8d > RSP: 0018:ffffc90000d67968 EFLAGS: 00010246 > RAX: ffff88812badaa48 RBX: 0000000000000000 RCX: ffffffff840d481d > RDX: 0000000000000465 RSI: 0000000000000000 RDI: 0000000000000270 > RBP: ffffc90000d679a8 R08: 0000000000000277 R09: 0000000000000000 > R10: 0001ffffffffffff R11: 0001c90000d679a8 R12: ffff88812ac03800 > R13: ffff88812c87c400 R14: ffff88812ae42210 R15: ffff888103026940 > FS: 00007f08b4e6f700(0000) GS:ffff88813bc00000(0000) knlGS:0000000000000000 > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > CR2: 0000000000000270 CR3: 000000012c58b000 CR4: 00000000003506f0 > DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > Call Trace: > > unix_diag_get_exact net/unix/diag.c:285 [inline] > unix_diag_handler_dump+0x3f9/0x500 net/unix/diag.c:317 > __sock_diag_cmd net/core/sock_diag.c:235 [inline] > sock_diag_rcv_msg+0x237/0x250 net/core/sock_diag.c:266 > netlink_rcv_skb+0x13e/0x250 net/netlink/af_netlink.c:2564 > sock_diag_rcv+0x24/0x40 net/core/sock_diag.c:277 > netlink_unicast_kernel net/netlink/af_netlink.c:1330 [inline] > netlink_unicast+0x5e9/0x6b0 net/netlink/af_netlink.c:1356 > netlink_sendmsg+0x739/0x860 net/netlink/af_netlink.c:1932 > sock_sendmsg_nosec net/socket.c:714 [inline] > sock_sendmsg net/socket.c:734 [inline] > ____sys_sendmsg+0x38f/0x500 net/socket.c:2476 > ___sys_sendmsg net/socket.c:2530 [inline] > __sys_sendmsg+0x197/0x230 net/socket.c:2559 > __do_sys_sendmsg net/socket.c:2568 [inline] > __se_sys_sendmsg net/socket.c:2566 [inline] > __x64_sys_sendmsg+0x42/0x50 net/socket.c:2566 > do_syscall_x64 arch/x86/entry/common.c:50 [inline] > do_syscall_64+0x2b/0x70 arch/x86/entry/common.c:80 > entry_SYSCALL_64_after_hwframe+0x63/0xcd > RIP: 0033:0x4697f9 > Code: f7 d8 64 89 02 b8 ff ff ff ff c3 66 0f 1f 44 00 00 48 89 f8 48 > 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d > 01 f0 ff ff 73 01 c3 48 c7 c1 bc ff ff ff f7 d8 64 89 01 48 > RSP: 002b:00007f08b4e6ec48 EFLAGS: 00000246 ORIG_RAX: 000000000000002e > RAX: ffffffffffffffda RBX: 000000000077bf80 RCX: 00000000004697f9 > RDX: 0000000000000000 RSI: 00000000200001c0 RDI: 0000000000000003 > RBP: 00000000004d29e9 R08: 0000000000000000 R09: 0000000000000000 > R10: 0000000000000000 R11: 0000000000000246 R12: 000000000077bf80 > R13: 0000000000000000 R14: 000000000077bf80 R15: 00007ffdb36bc6c0 > > Modules linked in: > CR2: 0000000000000270 > ---[ end trace 0000000000000000 ]--- > RIP: 0010:sk_user_ns include/net/sock.h:920 [inline] > RIP: 0010:sk_diag_dump_uid net/unix/diag.c:119 [inline] > RIP: 0010:sk_diag_fill+0x77d/0x890 net/unix/diag.c:170 > Code: 89 ef e8 66 d4 2d fd c7 44 24 40 00 00 00 00 49 8d 7c 24 18 e8 > 54 d7 2d fd 49 8b 5c 24 18 48 8d bb 70 02 00 00 e8 43 d7 2d fd <48> 8b > 9b 70 02 00 00 48 8d 7b 10 e8 33 d7 2d fd 48 8b 5b 10 48 8d > RSP: 0018:ffffc90000d67968 EFLAGS: 00010246 > RAX: ffff88812badaa48 RBX: 0000000000000000 RCX: ffffffff840d481d > RDX: 0000000000000465 RSI: 0000000000000000 RDI: 0000000000000270 > RBP: ffffc90000d679a8 R08: 0000000000000277 R09: 0000000000000000 > R10: 0001ffffffffffff R11: 0001c90000d679a8 R12: ffff88812ac03800 > R13: ffff88812c87c400 R14: ffff88812ae42210 R15: ffff888103026940 > FS: 00007f08b4e6f700(0000) GS:ffff88813bc00000(0000) knlGS:0000000000000000 > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > CR2: 0000000000000270 CR3: 000000012c58b000 CR4: 00000000003506f0 > DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > ---------------- > Code disassembly (best guess): > 0: 89 ef mov %ebp,%edi > 2: e8 66 d4 2d fd callq 0xfd2dd46d > 7: c7 44 24 40 00 00 00 movl $0x0,0x40(%rsp) > e: 00 > f: 49 8d 7c 24 18 lea 0x18(%r12),%rdi > 14: e8 54 d7 2d fd callq 0xfd2dd76d > 19: 49 8b 5c 24 18 mov 0x18(%r12),%rbx > 1e: 48 8d bb 70 02 00 00 lea 0x270(%rbx),%rdi > 25: e8 43 d7 2d fd callq 0xfd2dd76d > * 2a: 48 8b 9b 70 02 00 00 mov 0x270(%rbx),%rbx <-- trapping instruction > 31: 48 8d 7b 10 lea 0x10(%rbx),%rdi > 35: e8 33 d7 2d fd callq 0xfd2dd76d > 3a: 48 8b 5b 10 mov 0x10(%rbx),%rbx > 3e: 48 rex.W > 3f: 8d .byte 0x8d > > > Best, > Wei