Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3794603ybz; Tue, 28 Apr 2020 00:08:38 -0700 (PDT) X-Google-Smtp-Source: APiQypLInD74FKQJnKmjcCBKnN8MBO8Y9P4CFRMaYuxLe6eiUfz1oP9PM1S8exwAyyBPDHuyRGnJ X-Received: by 2002:a50:8dc2:: with SMTP id s2mr6053276edh.318.1588057717903; Tue, 28 Apr 2020 00:08:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588057717; cv=none; d=google.com; s=arc-20160816; b=0wQXGoTiBzM9keCmXfthKU+fGN1oFpOCUg59XZoA8fEG7v4B5poGdNxiPzTU0XK38E wp1HF4s2iwmXkfBr49UmiSsOY1aTWYlwWh+DH/gMh7SllplzS1S0AaUQjknrTLucHYtT +56r1YT6uo3xEMZOMnkXrpghk4RWy0iALyikPyUdW5uWlCI27tUhqcvkJLT/bK4Lh0fo Cpb70YMSnyaG66WQt9O02jWLd2nbtuuLcnbsBFl4RuD33V1fAbSJZ8yi/MNUT/eOwpVs 0Ce+GALwufEv1z6EOvnOXcPlmFow6R63W3e1oN6fj8iAjLxp4+S46GxGGsnwpHOdnp6c 0j7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=5xMxzq1+6fPKftxXFu1ru4KGONd/NB7Kh51Ynpspd1U=; b=rIgtKmXoEEDVAJQMP3ZuHmUaHS+NhA7wj59V3s5BuIHuYnOkhgu4grn6/xDIW9ji42 IeZSwRC4viiP4OhkCnxoRJ+s2GQK0fXhuMU+Sh1Krpv1Tu4PatCijcupalyZ0kLeMkpJ Y0NHBCiSKBQZcPHKv10Olt2yFHRxoJMsKlwTRopTa2W5dmi8Zc7e2gTwauDypqqA+44l 3KZDqIeRdIZS/hjoSNPmYTn/gxs1Rlnpn/q62l0QsLkg4Z433IyauFNJGupWe8BlAAln hLs1IMx5xyHnnFy3lpikYMBZqt5ShvLaQrhjmUu4vcBqnIkPA6X3/8y25+EPWNK68Qqn p+xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@javigon-com.20150623.gappssmtp.com header.s=20150623 header.b=hO81LZGW; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v13si1088501edi.386.2020.04.28.00.08.14; Tue, 28 Apr 2020 00:08:37 -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=@javigon-com.20150623.gappssmtp.com header.s=20150623 header.b=hO81LZGW; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726490AbgD1HGz (ORCPT + 99 others); Tue, 28 Apr 2020 03:06:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725917AbgD1HGy (ORCPT ); Tue, 28 Apr 2020 03:06:54 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11280C03C1A9 for ; Tue, 28 Apr 2020 00:06:54 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id k12so1450322wmj.3 for ; Tue, 28 Apr 2020 00:06:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=javigon-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=5xMxzq1+6fPKftxXFu1ru4KGONd/NB7Kh51Ynpspd1U=; b=hO81LZGWWo8vQjLiiioyCkIRbDV6DOkETpCDAwqQdX+A5qxIQgwr1HYewNk54bxA7U VUidLvM9h71FNcWZnNJYZ3yOumKakvofrrqMoUqQuNJ7S80BnFrt1MtCsxiIHLBGb4cO /TddRb91i9lTxuviX7DN+kD+9PvS58iLrlpFqIYwBT7Q93N0FCiNKQFEmUhyq2+dHicN ZNjbd84dQDNMQQUw9JGt9oqx+VP7v1BvscRU593V9qpLgb9ryM4WUBikbEQEXkJ4GqT4 qC6/5rkcMwZBBZ0ve117vQ1OEL6fiBT3fkqtDQQFh32D6t2ccFohocb83Vh/DdeY6Wzv eqlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=5xMxzq1+6fPKftxXFu1ru4KGONd/NB7Kh51Ynpspd1U=; b=b+xqvFjVpH8PEfI5K7AMaugVZLbbtealMAzvzX4XwdOGk2M6FW6w7YnoJgzNoj+hm3 8sMU8RspavjwFfqrsXKqg6MI9sa06Es8Ajvh/9zkHs/MbWFTll/t2ziUJH+QSEC9CzUP VWwFlSJo3dNa1CbR2lfK2tUq8TO0Q9iYv47br/QeNHaa1Q+LBzmcecPR2gQcZWk8hzAr jPy+yL3KIMaR3t+Rdqr8jTESffWIN7WW+o8FX8VNLrLRUdiW/zAvF6lFKayh1oBGJekI +KMMlVdny2rYLqyUFT26iI02Nm5WJLSk49AXFnLLGap5xjFE82XmN36ABAA+MvMAiSKA Dj6g== X-Gm-Message-State: AGi0PubrrUj95vjqipXcnV4M4mxXwWpl+B+prlqtLHS2fPTm9u1u6EAI iws9M8hVu928doAJO3uqig+/Rw== X-Received: by 2002:a7b:cf2b:: with SMTP id m11mr2697226wmg.147.1588057612762; Tue, 28 Apr 2020 00:06:52 -0700 (PDT) Received: from localhost (ip-5-186-121-52.cgn.fibianet.dk. [5.186.121.52]) by smtp.gmail.com with ESMTPSA id l9sm23897081wrq.83.2020.04.28.00.06.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2020 00:06:52 -0700 (PDT) Date: Tue, 28 Apr 2020 09:06:51 +0200 From: Javier =?utf-8?B?R29uesOhbGV6?= To: Niklas Cassel Cc: Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , Igor Konopko , Matias =?utf-8?B?QmrDuHJsaW5n?= , Jens Axboe , "linux-nvme@lists.infradead.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] nvme: prevent double free in nvme_alloc_ns() error handling Message-ID: <20200428070651.qbsyivvaakflipr4@mpHalley.localdomain> References: <20200427123443.520469-1-niklas.cassel@wdc.com> <20200427180311.nssquibbak5ib4oo@mpHalley.localdomain> <20200427182245.GA547726@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200427182245.GA547726@localhost.localdomain> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 27.04.2020 18:22, Niklas Cassel wrote: >On Mon, Apr 27, 2020 at 08:03:11PM +0200, Javier González wrote: >> On 27.04.2020 14:34, Niklas Cassel wrote: >> > When jumping to the out_put_disk label, we will call put_disk(), which will >> > trigger a call to disk_release(), which calls blk_put_queue(). >> > >> > Later in the cleanup code, we do blk_cleanup_queue(), which will also call >> > blk_put_queue(). >> > >> > Putting the queue twice is incorrect, and will generate a KASAN splat. >> > >> > Set the disk->queue pointer to NULL, before calling put_disk(), so that the >> > first call to blk_put_queue() will not free the queue. >> > >> > The second call to blk_put_queue() uses another pointer to the same queue, >> > so this call will still free the queue. >> > >> > Fixes: 85136c010285 ("lightnvm: simplify geometry enumeration") >> > Signed-off-by: Niklas Cassel >> > --- >> > drivers/nvme/host/core.c | 2 ++ >> > 1 file changed, 2 insertions(+) >> > >> > diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c >> > index 91c1bd659947..f2adea96b04c 100644 >> > --- a/drivers/nvme/host/core.c >> > +++ b/drivers/nvme/host/core.c >> > @@ -3642,6 +3642,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) >> > >> > return; >> > out_put_disk: >> > + /* prevent double queue cleanup */ >> > + ns->disk->queue = NULL; >> > put_disk(ns->disk); >> > out_unlink_ns: >> > mutex_lock(&ctrl->subsys->lock); >> > -- >> > 2.25.3 >> > >> What about delaying the assignment of ns->disk? >> >> diff --git i/drivers/nvme/host/core.c w/drivers/nvme/host/core.c >> index a4d8c90ee7cc..6da4a9ced945 100644 >> --- i/drivers/nvme/host/core.c >> +++ w/drivers/nvme/host/core.c >> @@ -3541,7 +3541,6 @@ static int nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) >> disk->queue = ns->queue; >> disk->flags = flags; >> memcpy(disk->disk_name, disk_name, DISK_NAME_LEN); >> - ns->disk = disk; >> >> __nvme_revalidate_disk(disk, id); >> >> @@ -3553,6 +3552,8 @@ static int nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) >> } >> } >> >> + ns->disk = disk; >> + > >Hello Javier! > > >The only case where we jump to the out_put_disk label, is if the >nvme_nvm_register() call failed. > >In that case, we want to undo the alloc_disk_node() operation, i.e., >decrease the refcount. > >If we don't set "ns->disk = disk;" before the call to nvme_nvm_register(), >then, if register fails, and we jump to the put_disk(ns->disk) label, >ns->disk will be NULL, so the recount will not be decreased, so I assume >that this memory would then be a memory leak. > > >I think that the problem is that the block functions are a bit messy. >Most drivers seem to do blk_cleanup_queue() first and then do put_disk(), >but some drivers do it in the opposite way, so I think that we might have >some more use-after-free bugs in some of these drivers that do it in the >opposite way. > Hi Niklas, Yes, the out_put_disk label was introduced at the same time as the LightNVM entry point. We can do a better job at separating the cleanup functions, but as far as I can see ns->disk is not used in the LightNVM initialization, so delaying the initialization should be ok. Part of this should be also changing the out_put_disk to put_disk(disk). Note that initializing other namespace types here do not require ns->disk either, so delaying initialization should be ok. We have been running with this patch locally for some time. This said, this is an alternative as your fix works. Javier