]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
nbd: Add the maximum limit of allocated index in nbd_dev_add
authorZhong Jinghua <zhongjinghua@huawei.com>
Mon, 5 Jun 2023 12:21:59 +0000 (20:21 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 11 Aug 2023 09:33:47 +0000 (11:33 +0200)
[ 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 <zhongjinghua@huawei.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20230605122159.2134384-1-zhongjinghua@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/block/nbd.c

index eb2ca7f6ab3abae1ad0fccba0d6ab9d2cc3bc1be..33ad48719c12425d4b45dbca279508f3d5187796 100644 (file)
@@ -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;
        }