Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp4195581rwi; Mon, 17 Oct 2022 02:58:06 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6XmdTrxtzyaylqHXJe9heI0wl568TXEpG6+u59XtMdpdwIfBtGdbMOz8m+XYbDCIDReVES X-Received: by 2002:a17:90b:1d11:b0:20d:4c69:6886 with SMTP id on17-20020a17090b1d1100b0020d4c696886mr31849963pjb.14.1666000686523; Mon, 17 Oct 2022 02:58:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666000686; cv=none; d=google.com; s=arc-20160816; b=LiOSET7YXP7m0Jm5899+R3g2O1nxNoWKPkg68GoX5paEO/PxtcVWSYFwFdkfudGLfZ 0GuCS6MfcYiqxV5FCtdgadPkfkdiyOSum4tIJsbsDraCVuhfMqLsAk9rPwl3qt+qE49Y yIc9DqSdp4OsnEGFXNDtbj+BQ8L8aL2kmaQOYFP6AQjTwz+EO7tdpX7jL7iDb2KTMB/f 9V6nZfDdhDUcKFgv5UFVO/m4RRFE0hJuT+m/nYRvhBh8CI0v8BT8Zq8qGsgFh8jfw7al DE0puqu5kTPoPO7woFxA593MFvFfu070bkFs/o98q90PptG9cLM6t9ndfKJfOw4xql7d Exmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=UReGk7eZDaLMwm4P4gLh+XNnK3PtkFrjaCK8kHR45ps=; b=gf/v6hVRbMCC/gVLHPzHpRUyTjVjpW5JTNgPvReIRs3zpioUgdBAJzKpR+MVSxbSjg Z8w3S5iocD6Gj1hMEhLb905hfLHU22uSEcsmvKsrkfv5ViCamslb8wTR2r4v0MrAtmRE QJ1tTSUmeka59cpG+ilUQfUetS86FRBnTm5L8ajqQJz7DJ7VyLSbDwlYuL6gGBun/y2C ODjG35RCNh8v6IVknU1EyMfrIYNxIhltkrYc5PmV8/AesM2DUvYGIslqe6bcMmcH/83W 9WCjT59HQQgshb/LZLgcRNU6rwaPZ3x4XX1oy+lpNrx0sqA35q1Y1V6H/qeoiqhmhJbO iP0g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s19-20020a056a00195300b0056594f6e700si12958944pfk.94.2022.10.17.02.57.54; Mon, 17 Oct 2022 02:58:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231208AbiJQJYo (ORCPT + 99 others); Mon, 17 Oct 2022 05:24:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231199AbiJQJYd (ORCPT ); Mon, 17 Oct 2022 05:24:33 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA4FE959B; Mon, 17 Oct 2022 02:24:24 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4MrWj54Yv8zKFNQ; Mon, 17 Oct 2022 17:22:01 +0800 (CST) Received: from [10.174.176.73] (unknown [10.174.176.73]) by APP4 (Coremail) with SMTP id gCh0CgCXmMpEH01jcy+kAQ--.60665S3; Mon, 17 Oct 2022 17:24:22 +0800 (CST) Subject: Re: [PATCH RFC] block: fix use after free for bd_holder_dir/slave_dir To: Christoph Hellwig , Yu Kuai Cc: axboe@kernel.dk, gregkh@linuxfoundation.org, willy@infradead.org, kch@nvidia.com, martin.petersen@oracle.com, johannes.thumshirn@wdc.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yi.zhang@huawei.com, "yukuai (C)" References: <20221012125838.1608619-1-yukuai1@huaweicloud.com> From: Yu Kuai Message-ID: Date: Mon, 17 Oct 2022 17:24:20 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID: gCh0CgCXmMpEH01jcy+kAQ--.60665S3 X-Coremail-Antispam: 1UD129KBjvJXoWxCF4kWw1fCFWfXryDZF43ZFb_yoW5ZFW5pF yrCayrJr4IqF4DuF4Utw4UJFWjga48tF1xuFyfKryIgrnrJrZ29FZ5CF1UuFn8Ars7Kr4q vr45JrWY9ayvkFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9214x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvEwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFIxGxcIEc7CjxVA2Y2ka 0xkIwI1lc7I2V7IY0VAS07AlzVAYIcxG8wCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7x kEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E 67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCw CI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6rWUJVWr Zr1UMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYx BIdaVFxhVjvjDU0xZFpf9x0JUdHUDUUUUU= X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, christoph! ?? 2022/10/17 15:59, Christoph Hellwig ะด??: > AFAICS the problem is the that pre-registered holders don't get > unregistered for a late add_disk failure. > > Something like this should fix your error: > I agree that this patch do make sense, however, it seems to me this patch should fix the problem that kobject is leaked, not uaf... And I verified that the problem can still be reporduced with this patch. Some details about our error?? while creating dm using sda, sda is removed. Thus device_add_disk() will fail at bd_register_pending_holders() and and slave_dir is removed. Then, later in dm error path, slave_dir is accessed again. Perhaps refactor dm error path can fix this error, however, I still think the root cause is lifecycle of slave_dir/bd_holder_dir, and there are still probably other potential problems... Thanks, Kuai > diff --git a/block/genhd.c b/block/genhd.c > index 17b33c62423df..6123005154b2a 100644 > --- a/block/genhd.c > +++ b/block/genhd.c > @@ -484,7 +484,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk, > > ret = blk_register_queue(disk); > if (ret) > - goto out_put_slave_dir; > + goto out_unregister_holders; > > if (!(disk->flags & GENHD_FL_HIDDEN)) { > ret = bdi_register(disk->bdi, "%u:%u", > @@ -526,6 +526,8 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk, > bdi_unregister(disk->bdi); > out_unregister_queue: > blk_unregister_queue(disk); > +out_unregister_holders: > + bd_unregister_all_holders(disk); > out_put_slave_dir: > kobject_put(disk->slave_dir); > out_put_holder_dir: > diff --git a/block/holder.c b/block/holder.c > index 5283bc804cc14..12c09d5c21280 100644 > --- a/block/holder.c > +++ b/block/holder.c > @@ -169,3 +169,13 @@ int bd_register_pending_holders(struct gendisk *disk) > mutex_unlock(&disk->open_mutex); > return ret; > } > + > +void bd_unregister_all_holders(struct gendisk *disk) > +{ > + struct bd_holder_disk *holder; > + > + mutex_lock(&disk->open_mutex); > + list_for_each_entry(holder, &disk->slave_bdevs, list) > + __unlink_disk_holder(holder->bdev, disk); > + mutex_unlock(&disk->open_mutex); > +} > diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h > index 50e358a19d986..ccab9a2dae4bd 100644 > --- a/include/linux/blkdev.h > +++ b/include/linux/blkdev.h > @@ -840,6 +840,7 @@ void set_capacity(struct gendisk *disk, sector_t size); > int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk); > void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk); > int bd_register_pending_holders(struct gendisk *disk); > +void bd_unregister_all_holders(struct gendisk *disk); > #else > static inline int bd_link_disk_holder(struct block_device *bdev, > struct gendisk *disk) > @@ -854,6 +855,9 @@ static inline int bd_register_pending_holders(struct gendisk *disk) > { > return 0; > } > +static inline void bd_unregister_all_holders(struct gendisk *disk) > +{ > +} > #endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */ > > dev_t part_devt(struct gendisk *disk, u8 partno); > . >