Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp8945167ybl; Fri, 17 Jan 2020 03:53:15 -0800 (PST) X-Google-Smtp-Source: APXvYqyzQ5wSfxkU/9ShBxW1vVUOFMW/pAT21ME8w8P4lwPI8WMQnrHtV4NanurISA9tgPqFxhLG X-Received: by 2002:a05:6830:16d1:: with SMTP id l17mr5558339otr.21.1579261995003; Fri, 17 Jan 2020 03:53:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579261994; cv=none; d=google.com; s=arc-20160816; b=QMUdu4hzzKQdT6vXwG5JA1tB2RLJ5FjcBSLYSq9uHJ5vxjNHzOiD6ZPxN7yMCPaJZ9 M6Zr7jrHVM/uKeT9tGUHGSzseCUhUcywIfPGtkqTBYoPJqdy7Hsa+ah/MUozgFaR5UgQ DGyzMHb40Zs30GjyfJAcagexdZftiDaAQ61SGjBPbSa+lKf+K0R8RlZ+xPqZrxgZN7DT gv+sT31XyKQQ4EL5pslj1sfOCRhZGQPcjeyWkO5EEnqmYFjgILZOCPawxqsKVlwvdM2+ yiqVnax/fn5tqrU3/k+As9zf5nZAWI4aAuDiIgdn8660otUPw6cHfU9TcdfJFoaaeD5c zPVg== 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=F0NKC6W38fVzaqsIGXIP+QYHEijKvKZmGD2fo7JuNP0=; b=bBIfst705vtX0qID1KD2y+ZNrICt3/ZL9vHSy++N7LcilNQslXpe4HvbNEe+ZdKgdC v6Dehycv5IkYVyVyVRTG7FGt3TPjNITl1eh+5aDVZ7Ssl/6CSDrfS8Phn3ovkMAtKBul G5c0XC2iLH0CcYtSojLEazEVrSzNmiWH0zwsK8HTgWAOMZt52PI3l5qeNypQw6X9wlAH hgbUT1rEgV39Go+XLxrIg5ptTrWMtQY3c7E8CaSRI0GmdIicROYmTpOxVw3WlO/gh0IL 6z9DUCIZmKmI9xDmQh5lErlyC0eieZrbscCRvFPD4++K/AwWpICyJ2Mey2dkvS5Sr3Uv Kv+w== 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 x20si15194212otq.222.2020.01.17.03.53.02; Fri, 17 Jan 2020 03:53:14 -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 S1726973AbgAQLvH (ORCPT + 99 others); Fri, 17 Jan 2020 06:51:07 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:35410 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726689AbgAQLvG (ORCPT ); Fri, 17 Jan 2020 06:51:06 -0500 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id A345CB9D861EFE57526B; Fri, 17 Jan 2020 19:51:03 +0800 (CST) Received: from huawei.com (10.175.124.28) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.439.0; Fri, 17 Jan 2020 19:50:54 +0800 From: Sun Ke To: , , CC: , , Subject: [PATCH] nbd: fix potential NULL pointer fault in connect and disconnect process Date: Fri, 17 Jan 2020 19:50:05 +0800 Message-ID: <20200117115005.37006-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 Connect and disconnect a nbd device repeatedly, will cause NULL pointer fault. It will appear by the steps: 1. Connect the nbd device and disconnect it, but now nbd device is not disconnected totally. 2. Connect the same nbd device again immediately, it will fail in nbd_start_device with a EBUSY return value. 3. Wait a second to make sure the last config_refs is reduced and run nbd_config_put to disconnect the nbd device totally. 4. Start another process to open the nbd_device, config_refs will increase and at the same time disconnect it. To fix it, add a NBD_HAS_STARTED flag. Set it in nbd_start_device_ioctl and nbd_genl_connect if nbd device is started successfully. Clear it in nbd_config_put. Test it in nbd_genl_disconnect and nbd_genl_reconfigure. Signed-off-by: Sun Ke --- drivers/block/nbd.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index b4607dd96185..ddd364e208ab 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -83,6 +83,7 @@ struct link_dead_args { #define NBD_DESTROY_ON_DISCONNECT 0 #define NBD_DISCONNECT_REQUESTED 1 +#define NBD_HAS_STARTED 2 struct nbd_config { u32 flags; @@ -1215,6 +1216,7 @@ static void nbd_config_put(struct nbd_device *nbd) nbd->disk->queue->limits.discard_alignment = 0; blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX); blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue); + clear_bit(NBD_HAS_STARTED, &nbd->flags); mutex_unlock(&nbd->config_lock); nbd_put(nbd); @@ -1290,6 +1292,8 @@ static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *b ret = nbd_start_device(nbd); if (ret) return ret; + else + set_bit(NBD_HAS_STARTED, &nbd->flags); if (max_part) bdev->bd_invalidated = 1; @@ -1961,6 +1965,7 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info) mutex_unlock(&nbd->config_lock); if (!ret) { set_bit(NBD_RT_HAS_CONFIG_REF, &config->runtime_flags); + set_bit(NBD_HAS_STARTED, &nbd->flags); refcount_inc(&nbd->config_refs); nbd_connect_reply(info, nbd->index); } @@ -2008,6 +2013,14 @@ static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info) index); return -EINVAL; } + + if (!test_bit(NBD_HAS_STARTED, &nbd->flags)) { + mutex_unlock(&nbd_index_mutex); + printk(KERN_ERR "nbd: device at index %d failed to start\n", + index); + return -EBUSY; + } + if (!refcount_inc_not_zero(&nbd->refs)) { mutex_unlock(&nbd_index_mutex); printk(KERN_ERR "nbd: device at index %d is going down\n", @@ -2049,6 +2062,14 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info) index); return -EINVAL; } + + if (!test_bit(NBD_HAS_STARTED, &nbd->flags)) { + mutex_unlock(&nbd_index_mutex); + printk(KERN_ERR "nbd: device at index %d failed to start\n", + index); + return -EBUSY; + } + if (!refcount_inc_not_zero(&nbd->refs)) { mutex_unlock(&nbd_index_mutex); printk(KERN_ERR "nbd: device at index %d is going down\n", -- 2.17.2