Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp9299821ybl; Fri, 17 Jan 2020 09:33:27 -0800 (PST) X-Google-Smtp-Source: APXvYqym3CTcKt+EU/WjAh208E1dTWZje13LZNsVAFnxYJy5MFjkHirE2d15T0ifizWn/RRHoJ29 X-Received: by 2002:a54:4e8d:: with SMTP id c13mr4182056oiy.27.1579282407078; Fri, 17 Jan 2020 09:33:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579282407; cv=none; d=google.com; s=arc-20160816; b=lBmu3bWC9ec+6YADDkyk3ppp/nX5gKFfwv5sqo+13s+ZfOWRAfAT1KASUE1PZMyu3n OoJ8OzKXL40Ab6S6Bt3memjvy12EO22kVKzibaKU1HTTHKVUc/VvpUJ/0nvchUD1TNM2 uCrkpxRZ+Lvv6n0MKkjB85JS5Gua3TT91y8VWcYFsV9XokMza+pkDtS/33XfHDM9+nIB dyaz48s3sarJVzGoJJr+jVpFxwAj5tW1zMGTRQYh0Ow9U1ld+Xs3HjN+qRtHC4+FORYm vdnTbCiewDY3koHomGoUubCcBnLwM+zwvF/Z2dZ12QySpNUA36VOfUfVwdy6OgCnK1JM ZKlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject:dkim-signature; bh=V/YBTzCrOCnTnUPO3jYLrCWgF4tW5vUC7tcFD9TTyzQ=; b=Ubt4Z1GzjAKughosPvyBZtSC5DQ2j6TCMn26eDlfS5Wyp5DtgC/GnSht28Udl0KMng Zi6eRh7ZasjTT9iEdwYwLn4lSdtpJ28SYiV5KcBpQP4ilxWRIx9Hz+iHqtVUVRbyGs01 u+WUr5sDbfJSRxONtmV8WvlFvm0zkioscWPC87YdLdlKhS9mf014XYrTj6/Ek96yLGRu qcs1ptChb/7sEvqLq4XK3qAyyYnpWFduTTyxYWtaiNBQ6A435WnzAs+qfOD2KIVorxKI wRP1Ftcj2NhHvfzxX8O2O1XtHea5cWvr0/BvarGlK3x9vLpA+znIVI7YQJ4yDKm2NWoS Pqwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P7dNZrFJ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q72si14508714oic.18.2020.01.17.09.33.14; Fri, 17 Jan 2020 09:33:27 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P7dNZrFJ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728773AbgAQRcO (ORCPT + 99 others); Fri, 17 Jan 2020 12:32:14 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:55150 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727115AbgAQRcN (ORCPT ); Fri, 17 Jan 2020 12:32:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579282332; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V/YBTzCrOCnTnUPO3jYLrCWgF4tW5vUC7tcFD9TTyzQ=; b=P7dNZrFJE86iM8JVKC9HoKCSaIWT7sowqShoXhnZkbhwq18bmWkKPMebj36TM1iq8obnmm 8Zri94+6VPblxnqgwzSl76gudkbqd8b90w2AN1kIzeW8gVO4pxhBgg7QD1GUwmpKg8JqRn Mf7HxV+oU9CKW71mPgUDExW3GGMU/Bw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-352-EOGSUIB5MLehJqk8hDdr4Q-1; Fri, 17 Jan 2020 12:32:09 -0500 X-MC-Unique: EOGSUIB5MLehJqk8hDdr4Q-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ECA5118CA241; Fri, 17 Jan 2020 17:32:07 +0000 (UTC) Received: from [10.10.126.209] (ovpn-126-209.rdu2.redhat.com [10.10.126.209]) by smtp.corp.redhat.com (Postfix) with ESMTP id C9FC581201; Fri, 17 Jan 2020 17:32:06 +0000 (UTC) Subject: Re: [PATCH] nbd: fix potential NULL pointer fault in connect and disconnect process To: Sun Ke , josef@toxicpanda.com, axboe@kernel.dk References: <20200117115005.37006-1-sunke32@huawei.com> Cc: linux-block@vger.kernel.org, nbd@other.debian.org, linux-kernel@vger.kernel.org, Xiubo Li From: Mike Christie Message-ID: <5E21EF96.1010204@redhat.com> Date: Fri, 17 Jan 2020 11:32:06 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <20200117115005.37006-1-sunke32@huawei.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 01/17/2020 05:50 AM, Sun Ke wrote: > 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. Just to make sure I understood this, for step 4 the process is doing: open(/dev/nbdX); ioctl(NBD_DISCONNECT, /dev/nbdX) or nbd_genl_disconnect(for /dev/nbdX) ? There is no successful NBD_DO_IT / nbd_genl_connect between the open and disconnect calls at step #4, because it would normally be done at #2 and that failed. nbd_disconnect_and_put could then reference a null recv_workq. If we are also racing with a close() then that could free the device/config from under nbd_disconnect_and_put. > > To fix it, add a NBD_HAS_STARTED flag. Set it in nbd_start_device_ioctl I'm not sure if we need the new bit. We could just add a check for a non null task_recv in nbd_genl_disconnect like how nbd_start_device and nbd_genl_disconnect do. The new bit might be more clear which is nice. If we got this route, should the new bit be a runtime_flag like other device state bits? > 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", >