Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3162972ybz; Mon, 27 Apr 2020 11:05:35 -0700 (PDT) X-Google-Smtp-Source: APiQypIFOYaX394VzpkBeOSVPgPu0F40pauj7pybPNEGwWcaNSAAgZKg6y7Ox8gNsK/4cu8g8xF3 X-Received: by 2002:a17:906:13d1:: with SMTP id g17mr21353737ejc.162.1588010735239; Mon, 27 Apr 2020 11:05:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588010735; cv=none; d=google.com; s=arc-20160816; b=tTFyhJbnl2Q01dU0UGP0cN1QxoRDepgkkQ0Va//zww4JM0iNdwbQiTR4hRxp4ebMsh VElj5ACl/4a5Q58i3U+w3n2BU/7bdSoD5uKaXP10zemB6ng1AOvrcdm3VEKHORuJuMwf Htn38PVI0v68z0kcrrYsmnuWBZ27NWgztoH+pVTu7POlBiPWaDf6lWqib7xCojDatjoS amrc5/6v4tqcT0sIqL5TPgmg8lBc/TBzUFy+h2fRh3YMeLMkLaQY9AejzQn4vpuYDfSu AE3ZgMgmisVKw6G6t0iPwIdVQ5Ip0zQ5e4TXwwHMbN4wvhnC6w83nkUmoB6JRPoGIc3y Shuw== 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-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=8kkiQoZyL52bM98uS+NVNJ+KUSycw9kMBRFEFYEjPyo=; b=ydNvvDDRyZMC6f9sDTL4yBR3Ie9GP+K1m1tZRiF3MHgYyof4fNuXXZAYCzFIKvOb54 734sI2auRJmyYe21rKIoFEvKXN0eH91D1wn+7QcBO2ILb4LQ9R1VkE7V2ejzUQqraddb P73VrAT0POF6JIEPjDprIyrCC2Cm3ZJr/1PMTCyW4FC3sKjLvWlv30+U1AdYn18NmYgz BjnFp8/ocv7CzJO+Ifq+0nMe478G3GqW3cSab6jGMbTRCQ2TRxRU1S+QO09zEAtanXDw LLQPn6XlSKoEPExkKq5yh9JMKe8lw9oQVgAXp2pUidDQn+b/d8ovbaBTVVECRFSeIbhs HLnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@javigon-com.20150623.gappssmtp.com header.s=20150623 header.b=Iv7lN4SY; 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 u18si202482ejx.502.2020.04.27.11.05.10; Mon, 27 Apr 2020 11:05:35 -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=Iv7lN4SY; 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 S1726228AbgD0SDQ (ORCPT + 99 others); Mon, 27 Apr 2020 14:03:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726208AbgD0SDP (ORCPT ); Mon, 27 Apr 2020 14:03:15 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D97BFC0610D5 for ; Mon, 27 Apr 2020 11:03:14 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id x25so641200wmc.0 for ; Mon, 27 Apr 2020 11:03:14 -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:in-reply-to; bh=8kkiQoZyL52bM98uS+NVNJ+KUSycw9kMBRFEFYEjPyo=; b=Iv7lN4SYgCC8OCj34c9Lww3ZLGvoQTjyKptBU5gHoNiBmYcM6Rn7qkDZO4H9ehwNRI SxI0MVDhCwo1G4h7Upp9rJgTrU8tsu6w2MjiZAaxNZQl2up9C6rIig0GGKUFmyHFZEG5 5qelknXeJyP5KKvQjk0ughV21vg44zk+NAgpD1JxF8NnQ5S7cXSjJnO/Tw49f0YQ6DVK sFgEZ1jf7yvJVI5yAqeqR0ncOP3jHQ1u3ia/jgiEJlG/R9LYLot9BYzo9V7ByfXdruaX 0h1fFb0VdmK+EirNJQMvCOG2+3+Ll5qd7aHBHslD8CYAMKvLnOUBBUNwr7HnTaBe3Zyu PhtA== 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:in-reply-to; bh=8kkiQoZyL52bM98uS+NVNJ+KUSycw9kMBRFEFYEjPyo=; b=pa/DnNsxkfYE/B1XWv4lCXTPI29byOArNgIXsYBmYv8uwVx81P2zDjwEEu4410jfP2 yBaQR9PebIEtoUfe3zN1u+Csb/80Kq6L8mDlGMIGXIhiHFFlFvHU9F3JywoIp1TbymsE KATJScXEt3qE4A2XandtHVR08Go3Qy3QZ7PsMIs9lZCcLYN3ZkENnEZ1Qb/DRKPydeOo +lppX7O1MY5jQnYcnlOU+jRgchnF/VbyPzXr0FtEhGh2tMcW2Frd160jx1/Nr6VPDbfJ eT8two9LdszLpPlzgez+xmN7CbFUkpipirOQ89NQ8r+ERirT2jvuFK6oZKUwpJXUnMxh jjRA== X-Gm-Message-State: AGi0PuYellEB4w6ufUDN0JlstBSkgJVuPxp056KKsVw+sDJHkzFpITaI wCHmfn9d/oELGlAXHSTOAy0/Aw== X-Received: by 2002:a7b:c4c7:: with SMTP id g7mr663782wmk.97.1588010593436; Mon, 27 Apr 2020 11:03:13 -0700 (PDT) Received: from localhost (ip-5-186-121-52.cgn.fibianet.dk. [5.186.121.52]) by smtp.gmail.com with ESMTPSA id n25sm11445511wmk.9.2020.04.27.11.03.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 11:03:12 -0700 (PDT) Date: Mon, 27 Apr 2020 20:03:11 +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: <20200427180311.nssquibbak5ib4oo@mpHalley.localdomain> References: <20200427123443.520469-1-niklas.cassel@wdc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline In-Reply-To: <20200427123443.520469-1-niklas.cassel@wdc.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; + down_write(&ctrl->namespaces_rwsem); list_add_tail(&ns->list, &ctrl->namespaces); up_write(&ctrl->namespaces_rwsem); Javier