Skip to content

Commit fad7cd3

Browse files
LiBaokun96axboe
authored andcommitted
nbd: add the check to prevent overflow in __nbd_ioctl()
If user specify a large enough value of NBD blocks option, it may trigger signed integer overflow which may lead to nbd->config->bytesize becomes a large or small value, zero in particular. UBSAN: Undefined behaviour in drivers/block/nbd.c:325:31 signed integer overflow: 1024 * 4611686155866341414 cannot be represented in type 'long long int' [...] Call trace: [...] handle_overflow+0x188/0x1dc lib/ubsan.c:192 __ubsan_handle_mul_overflow+0x34/0x44 lib/ubsan.c:213 nbd_size_set drivers/block/nbd.c:325 [inline] __nbd_ioctl drivers/block/nbd.c:1342 [inline] nbd_ioctl+0x998/0xa10 drivers/block/nbd.c:1395 __blkdev_driver_ioctl block/ioctl.c:311 [inline] [...] Although it is not a big deal, still silence the UBSAN by limit the input value. Reported-by: Hulk Robot <[email protected]> Signed-off-by: Baokun Li <[email protected]> Reviewed-by: Josef Bacik <[email protected]> Link: https://lore.kernel.org/r/[email protected] [axboe: dropped unlikely()] Signed-off-by: Jens Axboe <[email protected]>
1 parent da20b58 commit fad7cd3

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

drivers/block/nbd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
13841384
unsigned int cmd, unsigned long arg)
13851385
{
13861386
struct nbd_config *config = nbd->config;
1387+
loff_t bytesize;
13871388

13881389
switch (cmd) {
13891390
case NBD_DISCONNECT:
@@ -1398,8 +1399,9 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
13981399
case NBD_SET_SIZE:
13991400
return nbd_set_size(nbd, arg, config->blksize);
14001401
case NBD_SET_SIZE_BLOCKS:
1401-
return nbd_set_size(nbd, arg * config->blksize,
1402-
config->blksize);
1402+
if (check_mul_overflow((loff_t)arg, config->blksize, &bytesize))
1403+
return -EINVAL;
1404+
return nbd_set_size(nbd, bytesize, config->blksize);
14031405
case NBD_SET_TIMEOUT:
14041406
nbd_set_cmd_timeout(nbd, arg);
14051407
return 0;

0 commit comments

Comments
 (0)