Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp107232pxj; Mon, 17 May 2021 22:20:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwTCDqsMk+EOzJTLRC0CwROKGRpZ6f5cHk6B/dTyfz+6UJk0A8sR1pcQP+KGcQ14U045wfM X-Received: by 2002:a92:360d:: with SMTP id d13mr2878387ila.144.1621315225039; Mon, 17 May 2021 22:20:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621315225; cv=none; d=google.com; s=arc-20160816; b=UvdD5xfxWpnh7SYOY+TTTArnP6Li5yC/oOxV95WUdlesvtZs3WdR2WgqAVV1hTCwpO hIUMZDXLVfswBUWxZAH/kGdAf84y6sbPFr/JMk0PXaY4QxljM0aUB9WFNI+9qN4KkNCN VRqLVOSmZEI+NtFZ9aJdEijTkuqCMauIQGo1NDp3tmDrGjM7WqSb2Pq2zP5Psr5IteeL TynC7o3Uxdnc3GnxdPo1N7+NgwQOmlbXcWnBOjwZgFVbNwfcIFdFRjgh3KWctQgmRZxd PCAoT23dcps6dSujn1ZbSArm/If6qgwMQ5n5k3rUS3/Tp1PqlHgq99YbIvTP/3NDcewM ebSQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=CsrVPV8guXaGm0r9U8y338aNX2KiBp1gVSB7OkkKUYc=; b=B/07Uf5wGwuifqLFPHM/0WtRRRrNAawt0KDZX54AUCCrgPFTYZ4Qo0UbvxWJOZCCB5 Pl4RPqRc8IDJGMxMusiAq2WfAaxCG6BTNEnMkr4Vq2Ip0mwLErM9ZCDzMr+XwyFOqOBG D+vBVoKIyzXjhGTd/uy4jf2CQkrj9Y/aXV7B3cmrvHhPEPj98M4jfwDGHJHAD888Oi2u If0k3M+tIotvbGhbn4nEtNkpxG3JHP9bOEZ2AJ6arXsVSAnFuo4g7fJprU8hawlfH64I GsKJ5M8c5AxkEGJJ+nYUv94eswDwOSCLLgAvtFF6KBZFlw/PtEdXXiabfNAUHuIQY5aX 3G5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uwnWz2nT; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o5si1233373ill.66.2021.05.17.22.20.12; Mon, 17 May 2021 22:20:25 -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=@linuxfoundation.org header.s=korg header.b=uwnWz2nT; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241630AbhEQOp1 (ORCPT + 99 others); Mon, 17 May 2021 10:45:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:37394 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240488AbhEQOiO (ORCPT ); Mon, 17 May 2021 10:38:14 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9BC666140B; Mon, 17 May 2021 14:18:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621261089; bh=6p5kt912GqOkhx27Te3YBlB+nBGbDz2/PEVVoviKVfk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uwnWz2nTrPGTAxkDoMVmYGVkRvMt0vwAQsvCdsTQvD2NXD5BOwKdyO8vf9I3CQhNl y/u+QMR/yTZN4DfjRKndCh70EmpwEO1K4x4SHyGeSYcDCjZrO0WqJni2wQycIXAv7O ldGzfQPt5tT8fN50Ujyk0pl2wYz6OTpViOxanJWg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sun Ke , Josef Bacik , Jens Axboe , Sasha Levin Subject: [PATCH 5.12 304/363] nbd: Fix NULL pointer in flush_workqueue Date: Mon, 17 May 2021 16:02:50 +0200 Message-Id: <20210517140312.876716158@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140302.508966430@linuxfoundation.org> References: <20210517140302.508966430@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sun Ke [ Upstream commit 79ebe9110fa458d58f1fceb078e2068d7ad37390 ] Open /dev/nbdX first, the config_refs will be 1 and the pointers in nbd_device are still null. Disconnect /dev/nbdX, then reference a null recv_workq. The protection by config_refs in nbd_genl_disconnect is useless. [ 656.366194] BUG: kernel NULL pointer dereference, address: 0000000000000020 [ 656.368943] #PF: supervisor write access in kernel mode [ 656.369844] #PF: error_code(0x0002) - not-present page [ 656.370717] PGD 10cc87067 P4D 10cc87067 PUD 1074b4067 PMD 0 [ 656.371693] Oops: 0002 [#1] SMP [ 656.372242] CPU: 5 PID: 7977 Comm: nbd-client Not tainted 5.11.0-rc5-00040-g76c057c84d28 #1 [ 656.373661] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS ?-20190727_073836-buildvm-ppc64le-16.ppc.fedoraproject.org-3.fc31 04/01/2014 [ 656.375904] RIP: 0010:mutex_lock+0x29/0x60 [ 656.376627] Code: 00 0f 1f 44 00 00 55 48 89 fd 48 83 05 6f d7 fe 08 01 e8 7a c3 ff ff 48 83 05 6a d7 fe 08 01 31 c0 65 48 8b 14 25 00 6d 01 00 48 0f b1 55 d [ 656.378934] RSP: 0018:ffffc900005eb9b0 EFLAGS: 00010246 [ 656.379350] RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000 [ 656.379915] RDX: ffff888104cf2600 RSI: ffffffffaae8f452 RDI: 0000000000000020 [ 656.380473] RBP: 0000000000000020 R08: 0000000000000000 R09: ffff88813bd6b318 [ 656.381039] R10: 00000000000000c7 R11: fefefefefefefeff R12: ffff888102710b40 [ 656.381599] R13: ffffc900005eb9e0 R14: ffffffffb2930680 R15: ffff88810770ef00 [ 656.382166] FS: 00007fdf117ebb40(0000) GS:ffff88813bd40000(0000) knlGS:0000000000000000 [ 656.382806] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 656.383261] CR2: 0000000000000020 CR3: 0000000100c84000 CR4: 00000000000006e0 [ 656.383819] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 656.384370] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 656.384927] Call Trace: [ 656.385111] flush_workqueue+0x92/0x6c0 [ 656.385395] nbd_disconnect_and_put+0x81/0xd0 [ 656.385716] nbd_genl_disconnect+0x125/0x2a0 [ 656.386034] genl_family_rcv_msg_doit.isra.0+0x102/0x1b0 [ 656.386422] genl_rcv_msg+0xfc/0x2b0 [ 656.386685] ? nbd_ioctl+0x490/0x490 [ 656.386954] ? genl_family_rcv_msg_doit.isra.0+0x1b0/0x1b0 [ 656.387354] netlink_rcv_skb+0x62/0x180 [ 656.387638] genl_rcv+0x34/0x60 [ 656.387874] netlink_unicast+0x26d/0x590 [ 656.388162] netlink_sendmsg+0x398/0x6c0 [ 656.388451] ? netlink_rcv_skb+0x180/0x180 [ 656.388750] ____sys_sendmsg+0x1da/0x320 [ 656.389038] ? ____sys_recvmsg+0x130/0x220 [ 656.389334] ___sys_sendmsg+0x8e/0xf0 [ 656.389605] ? ___sys_recvmsg+0xa2/0xf0 [ 656.389889] ? handle_mm_fault+0x1671/0x21d0 [ 656.390201] __sys_sendmsg+0x6d/0xe0 [ 656.390464] __x64_sys_sendmsg+0x23/0x30 [ 656.390751] do_syscall_64+0x45/0x70 [ 656.391017] entry_SYSCALL_64_after_hwframe+0x44/0xa9 To fix it, just add if (nbd->recv_workq) to nbd_disconnect_and_put(). Fixes: e9e006f5fcf2 ("nbd: fix max number of supported devs") Signed-off-by: Sun Ke Reviewed-by: Josef Bacik Link: https://lore.kernel.org/r/20210512114331.1233964-2-sunke32@huawei.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/block/nbd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 4ff71b579cfc..974da561b8e5 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -1980,7 +1980,8 @@ static void nbd_disconnect_and_put(struct nbd_device *nbd) * config ref and try to destroy the workqueue from inside the work * queue. */ - flush_workqueue(nbd->recv_workq); + if (nbd->recv_workq) + flush_workqueue(nbd->recv_workq); if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF, &nbd->config->runtime_flags)) nbd_config_put(nbd); -- 2.30.2