Re: [PATCH -next] block: Fix the partition start may overflow in add_partition()

From: zhongjinghua
Date: Thu May 25 2023 - 10:12:34 EST



在 2023/5/25 16:55, Christoph Hellwig 写道:
On Mon, May 22, 2023 at 03:06:15PM +0800, Zhong Jinghua wrote:
+ if (p.start < 0 || p.length <= 0 || p.start + p.length < 0)
+ return -EINVAL;
+
start = p.start >> SECTOR_SHIFT;
length = p.length >> SECTOR_SHIFT;
+ /* length may be equal to 0 after right shift */
+ if (!length || start + length > get_capacity(bdev->bd_disk))
+ return -EINVAL;
While we're at it, shouldn't these be switched to use
check_add_overflow?

However, using check_add_overflow requires the introduction of an additional local variable for the third parameter, which does not make much difference to the current check. Is it worth it?

e.g:

diff --git a/block/ioctl.c b/block/ioctl.c
index 3223ea862523..9a40e8f864cb 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;
-       long long start, length;
+       long long start, length, tmp_check;

        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
@@ -33,7 +33,7 @@ static int blkpg_do_ioctl(struct block_device *bdev,
        if (op == BLKPG_DEL_PARTITION)
                return bdev_del_partition(disk, p.pno);

-       if (p.start < 0 || p.length <= 0 || p.start + p.length < 0)
+       if (p.start < 0 || p.length <= 0 || check_add_overflow(p.start, p.length, &tmp_check))
                return -EINVAL;

        start = p.start >> SECTOR_SHIFT;

Or do you have a better idea?