Skip to content

Commit b935518

Browse files
Li Lingfengaxboe
authored andcommitted
block: move capacity validation to blkpg_do_ioctl()
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>
1 parent 93f52fb commit b935518

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

block/ioctl.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static int blkpg_do_ioctl(struct block_device *bdev,
1818
{
1919
struct gendisk *disk = bdev->bd_disk;
2020
struct blkpg_partition p;
21-
sector_t start, length;
21+
sector_t start, length, capacity, end;
2222

2323
if (!capable(CAP_SYS_ADMIN))
2424
return -EACCES;
@@ -41,6 +41,13 @@ static int blkpg_do_ioctl(struct block_device *bdev,
4141

4242
start = p.start >> SECTOR_SHIFT;
4343
length = p.length >> SECTOR_SHIFT;
44+
capacity = get_capacity(disk);
45+
46+
if (check_add_overflow(start, length, &end))
47+
return -EINVAL;
48+
49+
if (start >= capacity || end > capacity)
50+
return -EINVAL;
4451

4552
switch (op) {
4653
case BLKPG_ADD_PARTITION:

block/partitions/core.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -419,21 +419,10 @@ static bool partition_overlaps(struct gendisk *disk, sector_t start,
419419
int bdev_add_partition(struct gendisk *disk, int partno, sector_t start,
420420
sector_t length)
421421
{
422-
sector_t capacity = get_capacity(disk), end;
423422
struct block_device *part;
424423
int ret;
425424

426425
mutex_lock(&disk->open_mutex);
427-
if (check_add_overflow(start, length, &end)) {
428-
ret = -EINVAL;
429-
goto out;
430-
}
431-
432-
if (start >= capacity || end > capacity) {
433-
ret = -EINVAL;
434-
goto out;
435-
}
436-
437426
if (!disk_live(disk)) {
438427
ret = -ENXIO;
439428
goto out;

0 commit comments

Comments
 (0)