From: Zhong Jinghua Date: Mon, 5 Jun 2023 12:21:59 +0000 (+0800) Subject: nbd: Add the maximum limit of allocated index in nbd_dev_add X-Git-Tag: v4.14.322~77 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7b5b97917efbdd84efa0d90373999e642b3ae6f;p=thirdparty%2Fkernel%2Fstable.git nbd: Add the maximum limit of allocated index in nbd_dev_add [ Upstream commit f12bc113ce904777fd6ca003b473b427782b3dde ] 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: Zhong Jinghua Reviewed-by: Christoph Hellwig Link: https://lore.kernel.org/r/20230605122159.2134384-1-zhongjinghua@huaweicloud.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index eb2ca7f6ab3ab..33ad48719c124 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -1630,7 +1630,8 @@ static int nbd_dev_add(int index) if (err == -ENOSPC) err = -EEXIST; } 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) index = err; }