Commit f12bc113 authored by Zhong Jinghua's avatar Zhong Jinghua Committed by Jens Axboe
Browse files

nbd: Add the maximum limit of allocated index in nbd_dev_add



If the index allocated by idr_alloc greater than MINORMASK >> part_shift,
the device number will overflow, resulting in failure to create a block
device.

Fix it by imiting the size of the max allocation.

Signed-off-by: default avatarZhong Jinghua <zhongjinghua@huawei.com>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20230605122159.2134384-1-zhongjinghua@huaweicloud.com


Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent ddf63516
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -1776,7 +1776,8 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
		if (err == -ENOSPC)
		if (err == -ENOSPC)
			err = -EEXIST;
			err = -EEXIST;
	} else {
	} else {
		err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
		err = idr_alloc(&nbd_index_idr, nbd, 0,
				(MINORMASK >> part_shift) + 1, GFP_KERNEL);
		if (err >= 0)
		if (err >= 0)
			index = err;
			index = err;
	}
	}