[PATCH -next] nbd: Add the maximum limit of allocated index in nbd_dev_add

Zhong Jinghua posted 1 patch 2 years, 8 months ago
drivers/block/nbd.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH -next] nbd: Add the maximum limit of allocated index in nbd_dev_add
Posted by Zhong Jinghua 2 years, 8 months ago
From: Zhong Jinghua <zhongjinghua@huawei.com>

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 <zhongjinghua@huawei.com>
---
 drivers/block/nbd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 592cfa8b765a..cb38477f359f 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1770,7 +1770,8 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
 		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;
 	}
-- 
2.31.1
Re: [PATCH -next] nbd: Add the maximum limit of allocated index in nbd_dev_add
Posted by Jens Axboe 2 years, 8 months ago
On Mon, 05 Jun 2023 20:21:59 +0800, Zhong Jinghua wrote:
> 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.
> 
> 
> [...]

Applied, thanks!

[1/1] nbd: Add the maximum limit of allocated index in nbd_dev_add
      commit: f12bc113ce904777fd6ca003b473b427782b3dde

Best regards,
-- 
Jens Axboe
Re: [PATCH -next] nbd: Add the maximum limit of allocated index in nbd_dev_add
Posted by Christoph Hellwig 2 years, 8 months ago
Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>