From patchwork Mon Mar 18 14:07:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yufen Yu X-Patchwork-Id: 10857695 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 802BC1669 for ; Mon, 18 Mar 2019 14:02:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6775229222 for ; Mon, 18 Mar 2019 14:02:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5BE1E292A6; Mon, 18 Mar 2019 14:02:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 012F829235 for ; Mon, 18 Mar 2019 14:02:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727605AbfCROCu (ORCPT ); Mon, 18 Mar 2019 10:02:50 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:37152 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726093AbfCROCt (ORCPT ); Mon, 18 Mar 2019 10:02:49 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id CB0D035276FDAB61C688; Mon, 18 Mar 2019 22:02:47 +0800 (CST) Received: from huawei.com (10.90.53.225) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.408.0; Mon, 18 Mar 2019 22:02:45 +0800 From: Yufen Yu To: CC: , , Subject: [PATCH 1/2] block: remove devt from ext_devt_idr when delete partition Date: Mon, 18 Mar 2019 22:07:02 +0800 Message-ID: <20190318140703.28963-2-yuyufen@huawei.com> X-Mailer: git-send-email 2.16.2.dirty In-Reply-To: <20190318140703.28963-1-yuyufen@huawei.com> References: <20190318140703.28963-1-yuyufen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.53.225] X-CFilter-Loop: Reflected Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In part_release(), it will remove devt from ext_devt_idr and get_gendisk cannot find it. But, if disk_release() works before part_release, open device partition may cause use-after-free of disk in get_gendisk(). We use md device as example, the race sence: Process1 Worker Process2 md_free blkdev_open del_gendisk add delete_partition_work_fn() to wq __blkdev_get get_gendisk put_disk disk_release kfree(disk) find part from ext_devt_idr get_disk_and_module(disk) cause use after free delete_partition_work_fn put_device(part) part_release remove part from ext_devt_idr Before Woker thread removes part from ext_devt_idr, Process2 can find the part and access the disk, resulting use-after-free. We fix this by removing the devt from ext_devt_idr when delete partition. Signed-off-by: Yufen Yu --- block/partition-generic.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/block/partition-generic.c b/block/partition-generic.c index 1ee3e1d1bc2a..30d1039d5e8d 100644 --- a/block/partition-generic.c +++ b/block/partition-generic.c @@ -288,6 +288,11 @@ void delete_partition(struct gendisk *disk, int partno) kobject_put(part->holder_dir); device_del(part_to_dev(part)); + /* + * We should ensuere to delete part from idr before kfree(disk), + * avoiding use-after-free of disk. + */ + blk_free_devt(part_devt(part)); hd_struct_kill(part); }