Skip to content

Commit

Permalink
block: move capacity validation to blkpg_do_ioctl()
Browse files Browse the repository at this point in the history
Commit 6d4e80d ("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 <lilingfeng3@huawei.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20240305032132.548958-1-lilingfeng@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Li Lingfeng authored and Jens Axboe committed Mar 6, 2024
1 parent 93f52fb commit b935518
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
9 changes: 8 additions & 1 deletion block/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down
11 changes: 0 additions & 11 deletions block/partitions/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

if (!disk_live(disk)) {
ret = -ENXIO;
goto out;
Expand Down

0 comments on commit b935518

Please sign in to comment.