Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp171114lqs; Mon, 4 Mar 2024 20:31:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVy8ta5Hrkgg+1hq7e0eSpjEWAbDkvsCjpYJ688vOAdgzqJEnT34FsJiFU4bi4zGPiKYXwUtUtN+dzCtMr/0e+Mlbfa5wKDdrydMr2LxA== X-Google-Smtp-Source: AGHT+IGOwWe7MwPpSaKDfdbKVhiaKR/9ZldgMixp42j8w+tOfeheLg8zRUUD2fOW8huogwBv+LcK X-Received: by 2002:a05:622a:448:b0:42e:7a39:fa4d with SMTP id o8-20020a05622a044800b0042e7a39fa4dmr1037904qtx.19.1709613064200; Mon, 04 Mar 2024 20:31:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709613064; cv=pass; d=google.com; s=arc-20160816; b=ITYp4dtDJk6lyoNILwu8LaAwFTdorGG8khz83MxhtEGgpkft2OYsFgJyhcN/Hl7DYo tdun8TI7j7kqTBPbqR49Dk8+cE0Ir+q/W7sSSNYJV9WFh3P0sx40pxbRm0c6iap4qvZ9 ExbFNVEB82QFGVLMw/OyIMdSdGP6cOzLPWBeTg1hVaCRoJMrF5wZu1zYp3a1psR34uqy HFIg3zrCmg7svgWxHaTYBUMXSwPPK84LwTgTkRZ1XqCspAS1NFrmpkdFt9jH9Paedgzl Jv3KpONcMijwS2JID+FB/aWkXymHf4xsfft7plj6+mDgKgIXhQvltafZlMyuzOIw2qcw 2KHA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature; bh=GhrtPKb0pVN5WEPy/xngX4FY/4Md6HLKTlAkrBwGg2k=; fh=qJlrAdN2MLbZeHex/TEaDEcWWITzppJDfbSpllCPvNk=; b=spY/Uv6c4uNUNcJmbQYrLD/+tRE1mxH8XlD0r5508FHkCotR8ONJPEac9miCply3D3 s5BdfRugmTlDt3VVvrh1g+03L5qaI4S+Mqb75+ovNPU+A6+ljlc9U3QdDLYBQa/n6vQA PTrxejzUDYjE9EmL0WNYxxpBw6VnUjk9pwpQT2+J9aCVAEY4ci7W+VitttxzomIVECcU 2Td5OgNx4NICjFhdAZTUDnWn/R2JyNX8tByQj5E4zMIizjiZla/BLsXsSmKkLConUC45 mZuqDvTjIt0/Uz/GC7BoV65IJlko/pjVgJpkgTOHDFXjjIsL+7zPy+jqsy+GfGVDFTeK AHIg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ORWxleWr; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-91656-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91656-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id o22-20020ac87c56000000b0042efaefcff1si1714600qtv.589.2024.03.04.20.31.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 20:31:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-91656-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ORWxleWr; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-91656-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91656-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 684391C23130 for ; Tue, 5 Mar 2024 04:30:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0917E405F2; Tue, 5 Mar 2024 04:30:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ORWxleWr" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21A4929A2; Tue, 5 Mar 2024 04:30:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709613049; cv=none; b=KbmC3PSq2MmRmZrbaqU+5XieelZ1g0MAkdXozIPk4W2SO4q8bjIGEJuP7brMdUxYsRod6D3MnOFnl+f1myEDAC+5JnLQtUoaGseUYQyDAu3kM0GEDUi1wyEPXrGfzsJpMmhXtjMLwCuLL3aa36KmaT3vLnreVCy3DGSXkv4yMrc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709613049; c=relaxed/simple; bh=9rARLDaLXDlmL73GSWpwPcSLa2+EkPEWN6GvaI9/NfE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=afHlkZT+mHvw587iA5Pn6QMBPUqf6YNPz3aXhyXv2nIr5DNDPn0GUwHerxYpivqlgqdEXeqZNKldWhb7jGV0fDW7RhTauYJYOJTBsSSeZw6CMgCO2GLXIt8P8o4hs8vZt/rWKz2DvRLiBGmKRWjXAl29abKPp2Ey1M5YD5LuCj0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ORWxleWr; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 27F7CC433F1; Tue, 5 Mar 2024 04:30:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709613048; bh=9rARLDaLXDlmL73GSWpwPcSLa2+EkPEWN6GvaI9/NfE=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=ORWxleWrgQvOCYRmP6nAJufsAa8n+T9LMtQdGSebziw1HfoS6rMdLYhkO06pnCcWk GNMFNODTLi6BNQUEhYyozWBmnVKwfPew+MO/9GwQndCKJ7TYxoAEgta2MAwSRncNGC 1znNDWPh0KjE/7xKkAPOgz2Y7GuNbhd1VijvytFeaN0YzGYDJHGR2Q5wiGtfhRj9iU kgjOhs3PaVwlrk+spODCHlybrms6PKOsm3uXsOTtyHEqSjDgs9CQx8a+7VL+JkpHdG 0m4dkYdTwHVcyN7Xa3Eo/Q4ts/zhWB4BKPhx1HZZLJXDs3yEb9amyLFO0ShTWQkUcS tvXWbFDKXAbtw== Message-ID: <474e78e7-9056-4b74-9ce2-592b8ee9411c@kernel.org> Date: Tue, 5 Mar 2024 13:30:45 +0900 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] block: move capacity validation to blkpg_do_ioctl() Content-Language: en-US To: Li Lingfeng , axboe@kernel.dk, min15.li@samsung.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, hch@lst.de, yangerkun@huawei.com, yukuai1@huaweicloud.com, houtao1@huawei.com, yi.zhang@huawei.com References: <20240305032132.548958-1-lilingfeng@huaweicloud.com> From: Damien Le Moal Organization: Western Digital Research In-Reply-To: <20240305032132.548958-1-lilingfeng@huaweicloud.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 3/5/24 12:21, Li Lingfeng wrote: > From: Li Lingfeng > > Commit 6d4e80db4ebe ("block: add capacity validation in > bdev_add_partition()") add check of partition's start and end sectors to > prevent exceeding the size of the disk when adding partitions. However, > there is still no check for resizing partitions now. > Move the check to blkpg_do_ioctl() to cover resizing partitions. > > Signed-off-by: Li Lingfeng > --- > block/ioctl.c | 9 ++++++++- > block/partitions/core.c | 11 ----------- > 2 files changed, 8 insertions(+), 12 deletions(-) > > diff --git a/block/ioctl.c b/block/ioctl.c > index 438f79c564cf..de0cc0d215c6 100644 > --- a/block/ioctl.c > +++ b/block/ioctl.c > @@ -18,7 +18,7 @@ static int blkpg_do_ioctl(struct block_device *bdev, > { > struct gendisk *disk = bdev->bd_disk; > struct blkpg_partition p; > - sector_t start, length; > + sector_t start, length, capacity, end; > > if (!capable(CAP_SYS_ADMIN)) > return -EACCES; > @@ -41,6 +41,13 @@ static int blkpg_do_ioctl(struct block_device *bdev, > > start = p.start >> SECTOR_SHIFT; > length = p.length >> SECTOR_SHIFT; > + capacity = get_capacity(disk); > + > + if (check_add_overflow(start, length, &end)) > + return -EINVAL; > + > + if (start >= capacity || end > capacity) > + return -EINVAL; > > switch (op) { > case BLKPG_ADD_PARTITION: > diff --git a/block/partitions/core.c b/block/partitions/core.c > index 5f5ed5c75f04..b11e88c82c8c 100644 > --- a/block/partitions/core.c > +++ b/block/partitions/core.c > @@ -419,21 +419,10 @@ static bool partition_overlaps(struct gendisk *disk, sector_t start, > int bdev_add_partition(struct gendisk *disk, int partno, sector_t start, > sector_t length) > { > - sector_t capacity = get_capacity(disk), end; > struct block_device *part; > int ret; > > 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; > - } > - Why do you remove this ? The check will not be done when *existing* partitions are added. To do the check when *creating* a partition, make this code a helper and call that helper function here and from blkpg_do_ioctl() as well. > if (!disk_live(disk)) { > ret = -ENXIO; > goto out; -- Damien Le Moal Western Digital Research