Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3443305ybv; Sun, 9 Feb 2020 23:35:21 -0800 (PST) X-Google-Smtp-Source: APXvYqzTSNvCf6vdecViQMXhGvwZ/8TipyKXAN6kyxPHyPG86oS+wfz/HWcKc1UNa1wyaE3qfZ94 X-Received: by 2002:a9d:8f8:: with SMTP id 111mr108803otf.107.1581320121202; Sun, 09 Feb 2020 23:35:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581320121; cv=none; d=google.com; s=arc-20160816; b=D0jqniRiiLuLNbq26/wxfrPOWNbHL25yBTpTC63ZzAscZ3dsp12EbTEk/9n/DE4ev8 GLkk07tTMgOp1JXgQTnVdNE3eJQu0X/ne0N9AqTrl2jW9bpbjLf8h1u7Yvcb7KQH2PuL 9I+A/xkglFHxmVJOscGDOylI/jQqNf+nsUH//UWJx2IuTO4blQCUUdA64/jRLfj5Mgcc KAaLu4y60g7kYTz0zqS6UNf7a3JsfqiBRDu2ZX5+AQZsSiftlXv9xKzTT5d9s2HFxfUF KXaWCRxZDFglmmp0478gu/9obw/U7N6Dk8cgOns8ZDvIFNXZJeyppl2GNxVV/HNUJItV 3RFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=8KT3bgl/Pd39SKeV/MmkAYcbUaoVbRP1wL2394HwTnw=; b=qUV633fUQkmq+15cCdqGIgulTtuVQiHtza9yL8q7kXkfot8ZQz97YgqL1uwPE+4rou OgsQnSBvg/2rhU8cyVaC1khkxuLVlpf8qXrjQr3K8k3r9np6Is5Aod6+4chMQqbvXk0V MORiPNF+3Egx06VNNhsY4ZyCK3X0YBdAhBrbIAIphdXIcbMOaeLGV/S8QkjN4z3tdDRa AO2/qzIPcLOdA27OPb34abIDjvQaEs/mNYbb3SgDPX8JvOEmlKctBtTsttLNE0J++W8p DLimoGQnwqCyxeCe3i55KX1Z9Ea6WPxJbk7frjulw0g/Hks3Brpt+lhn+FRMlt9LiD0N dSCA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v21si4138377otj.282.2020.02.09.23.35.08; Sun, 09 Feb 2020 23:35:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727051AbgBJHd4 (ORCPT + 99 others); Mon, 10 Feb 2020 02:33:56 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:10606 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726061AbgBJHd4 (ORCPT ); Mon, 10 Feb 2020 02:33:56 -0500 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 28D5B2F7CEFFAE2091B8; Mon, 10 Feb 2020 15:33:50 +0800 (CST) Received: from huawei.com (10.175.124.28) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.439.0; Mon, 10 Feb 2020 15:33:41 +0800 From: Sun Ke To: , , , CC: , , Subject: [v3] nbd: fix potential NULL pointer fault in nbd_genl_disconnect Date: Mon, 10 Feb 2020 15:32:41 +0800 Message-ID: <20200210073241.41813-1-sunke32@huawei.com> X-Mailer: git-send-email 2.17.2 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.124.28] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. To fix it, just add a check for a non null task_recv in nbd_genl_disconnect. Signed-off-by: Sun Ke --- v1 -> v2: Add an omitted mutex_unlock. v2 -> v3: Add nbd->config_lock, suggested by Josef. --- drivers/block/nbd.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index b4607dd96185..870b3fd0c101 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -2008,12 +2008,20 @@ static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info) index); return -EINVAL; } + mutex_lock(&nbd->config_lock); if (!refcount_inc_not_zero(&nbd->refs)) { + mutex_unlock(&nbd->config_lock); mutex_unlock(&nbd_index_mutex); printk(KERN_ERR "nbd: device at index %d is going down\n", index); return -EINVAL; } + if (!nbd->recv_workq) { + mutex_unlock(&nbd->config_lock); + mutex_unlock(&nbd_index_mutex); + return -EINVAL; + } + mutex_unlock(&nbd->config_lock); mutex_unlock(&nbd_index_mutex); if (!refcount_inc_not_zero(&nbd->config_refs)) { nbd_put(nbd); -- 2.17.2