From patchwork Fri Jun 16 11:05:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Min Li X-Patchwork-Id: 13282162 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52F42EB64D7 for ; Fri, 16 Jun 2023 07:10:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244184AbjFPHKa (ORCPT ); Fri, 16 Jun 2023 03:10:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244936AbjFPHJv (ORCPT ); Fri, 16 Jun 2023 03:09:51 -0400 Received: from mailout4.samsung.com (mailout4.samsung.com [203.254.224.34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DA7C3581 for ; Fri, 16 Jun 2023 00:09:42 -0700 (PDT) Received: from epcas5p1.samsung.com (unknown [182.195.41.39]) by mailout4.samsung.com (KnoxPortal) with ESMTP id 20230616070939epoutp0453ba0badd58a9cb1d15ba6759f4d6892~pEgxdMqDn0997809978epoutp04C for ; Fri, 16 Jun 2023 07:09:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout4.samsung.com 20230616070939epoutp0453ba0badd58a9cb1d15ba6759f4d6892~pEgxdMqDn0997809978epoutp04C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1686899379; bh=ESo4Qrt4/UzE/PV3aK8PFaKJ3Kztgy5rxcClb0hRKmc=; h=From:To:Cc:Subject:Date:References:From; b=jnXAOL2dM+MhjPSMkYXqlPLmvKiLfVbeuucEjd1t2hY4M8nXBp4qeHdkGQI3nvnwR MoEhS3g81fYMK2I5XfOOzFImdM1oOb7D+tmdCteKpqFwC32xNed8x1aYKAgu+4QnVc p1Np0pQgm6Aj5N/0WjMYKrs+Z3HHx/EDNaKAmeew= Received: from epsnrtp3.localdomain (unknown [182.195.42.164]) by epcas5p1.samsung.com (KnoxPortal) with ESMTP id 20230616070939epcas5p14d6eff9fa2c61884ebf49ea566b64ce7~pEgw0I_8D0795507955epcas5p1h; Fri, 16 Jun 2023 07:09:39 +0000 (GMT) Received: from epsmges5p2new.samsung.com (unknown [182.195.38.178]) by epsnrtp3.localdomain (Postfix) with ESMTP id 4Qj9Jc60MBz4x9Px; Fri, 16 Jun 2023 07:09:36 +0000 (GMT) Received: from epcas5p2.samsung.com ( [182.195.41.40]) by epsmges5p2new.samsung.com (Symantec Messaging Gateway) with SMTP id 4F.F6.44881.0BA0C846; Fri, 16 Jun 2023 16:09:36 +0900 (KST) Received: from epsmtrp2.samsung.com (unknown [182.195.40.14]) by epcas5p3.samsung.com (KnoxPortal) with ESMTPA id 20230616030739epcas5p31e705be33bf080f988702d42534ad32f~pBNek5lzt0688706887epcas5p3t; Fri, 16 Jun 2023 03:07:39 +0000 (GMT) Received: from epsmgms1p1new.samsung.com (unknown [182.195.42.41]) by epsmtrp2.samsung.com (KnoxPortal) with ESMTP id 20230616030739epsmtrp283d0995d1319be1df91759e62bebc516~pBNej8Gzx0328103281epsmtrp2h; Fri, 16 Jun 2023 03:07:39 +0000 (GMT) X-AuditID: b6c32a4a-c47ff7000001af51-79-648c0ab0f079 Received: from epsmtip2.samsung.com ( [182.195.34.31]) by epsmgms1p1new.samsung.com (Symantec Messaging Gateway) with SMTP id 56.31.27706.BF1DB846; Fri, 16 Jun 2023 12:07:39 +0900 (KST) Received: from ubuntu.. (unknown [109.105.118.54]) by epsmtip2.samsung.com (KnoxPortal) with ESMTPA id 20230616030738epsmtip2e4561c1f033fd4eb8ba466cd88c6fa6e~pBNdM9Vog3041530415epsmtip2w; Fri, 16 Jun 2023 03:07:38 +0000 (GMT) From: "min15.li" To: axboe@kernel.dk, willy@infradead.org, hch@lst.de, dlemoal@kernel.org, gregkh@linuxfoundation.org, wsa@kernel.org, vkoul@kernel.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, "min15.li" Subject: [PATCH v2] block: add capacity validation in bdev_add_partition() Date: Fri, 16 Jun 2023 11:05:57 +0000 Message-Id: <20230616110557.12106-1-min15.li@samsung.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrOJsWRmVeSWpSXmKPExsWy7bCmhu4Grp4Ug5aj+har7/azWTzYb2/R vHg9m8WN4++YLea+vsRisXL1USaLvbe0LS7vmsNmsXxVB5PF2QkfWC123jnBbPH7B1Ds7v65 jBY31l1lceDz2LxCy+Py2VKPTas62Tz2z13D7rH7ZgObR9+WVYwenzfJBbBHZdtkpCampBYp pOYl56dk5qXbKnkHxzvHm5oZGOoaWlqYKynkJeam2iq5+AToumXmAN2rpFCWmFMKFApILC5W 0rezKcovLUlVyMgvLrFVSi1IySkwKdArTswtLs1L18tLLbEyNDAwMgUqTMjOeLetkbXgCGfF z+tSDYzX2bsYOTkkBEwkPvYtYwaxhQR2M0p037TpYuQCsj8xSmx4OpsdwvnGKLGnezojTMfU Lf2sEB17GSXuTneGsJ8DFZ0Bi7MJqEsc+HgUbIOIQAejxJajnCA2s0CSxIxlv9lAbGEBL4l5 L06C1bMIqErMnnkUbD6vgKVE994PzBC75CX2HzzLDBEXlDg58wkLxBx5ieats5lBjpMQ6OWQ aPz9HOo4F4nZn34zQdjCEq+Ob4F6U0ri87u9QIs5gOxiiZc/wiDCNRK7v92GKreW2LZ+HRNI CbOApsT6XfoQYVmJqafWMUGs5ZPo/f0EqpxXYsc8GFtJ4u+Fc1AXSEgs3v+QFWKTh8T2fnNI 6MRKPPzWxjKBUX4WkmdmIXlmFsLiBYzMqxglUwuKc9NTi00LjPJSy+Fxmpyfu4kRnGK1vHYw PnzwQe8QIxMH4yFGCQ5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6YklqdmpqQWpRTBZ Jg5OqQam5Cm5T9f9zVm4+/eUCVfEVobNunN4+9S5/3/sYXkXeu38kxJml57D/yp1uWWOr93r 6Kh95LzZ151WK51OaLnu223RKe0csvlW1OSW2woNxVZyjBt/Zzz5HuvTvIYhLK22eQpDa8bb GweUD2+1jNl0q7yGYRtvkf+6fy/kM3o6fit76Gws7elo/h8zcY6N0s951/ru9q/7+/H9ruCO TLVI+9XiB14IRH0WvVi2pWvqJPHz8hpsFl9+exnvS7p39l/x2b2nrHL4TspFJTLKGpRmL52e 8PvWWbvOD1nyVyziLyfk6eybKfu5Y/pDrf/ravQ+zPvTu6XryLQ1U1ilrq05rp/7JnvC/q9s Glz/VFek2SixFGckGmoxFxUnAgChOTjy6wIAAA== X-CMS-MailID: 20230616030739epcas5p31e705be33bf080f988702d42534ad32f X-Msg-Generator: CA X-Sendblock-Type: REQ_APPROVE CMS-TYPE: 105P DLP-Filter: Pass X-CFilter-Loop: Reflected X-CMS-RootMailID: 20230616030739epcas5p31e705be33bf080f988702d42534ad32f References: Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org In the function bdev_add_partition(),there is no check that the start and end sectors exceed the size of the disk before calling add_partition. When we call the block's ioctl interface directly to add a partition, and the capacity of the disk is set to 0 by driver,the command will continue to execute. v1->v2: check for overflows of the start + length value and put the capacity check at the beginning of the function. Signed-off-by: min15.li Reviewed-by: Christoph Hellwig --- block/partitions/core.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/block/partitions/core.c b/block/partitions/core.c index 49e0496ff23c..3546b43d5124 100644 --- a/block/partitions/core.c +++ b/block/partitions/core.c @@ -438,8 +438,20 @@ int bdev_add_partition(struct gendisk *disk, int partno, sector_t start, { struct block_device *part; int ret; + sector_t end; + sector_t capacity = get_capacity(disk); mutex_lock(&disk->open_mutex); + if (check_add_overflow(start, length, &end)) { + ret = -EINVAL; + goto out; + } + + if (start >= capacity || end > capacity) { + ret = -EINVAL; + goto out; + } + if (!disk_live(disk)) { ret = -ENXIO; goto out;