Skip to content

Commit 5142720

Browse files
Zhang Wenshenggregkh
authored andcommitted
nbd: fix possible overflow on 'first_minor' in nbd_dev_add()
[ Upstream commit 6d35d04 ] When 'index' is a big numbers, it may become negative which forced to 'int'. then 'index << part_shift' might overflow to a positive value that is not greater than '0xfffff', then sysfs might complains about duplicate creation. Because of this, move the 'index' judgment to the front will fix it and be better. Fixes: b0d9111 ("nbd: use an idr to keep track of nbd devices") Fixes: 940c264 ("nbd: fix possible overflow for 'first_minor' in nbd_dev_add()") Signed-off-by: Zhang Wensheng <zhangwensheng5@huawei.com> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Link: https://lore.kernel.org/r/20220310093224.4002895-1-zhangwensheng5@huawei.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 64742cf commit 5142720

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

drivers/block/nbd.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,17 +1744,6 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
17441744
refcount_set(&nbd->refs, 0);
17451745
INIT_LIST_HEAD(&nbd->list);
17461746
disk->major = NBD_MAJOR;
1747-
1748-
/* Too big first_minor can cause duplicate creation of
1749-
* sysfs files/links, since index << part_shift might overflow, or
1750-
* MKDEV() expect that the max bits of first_minor is 20.
1751-
*/
1752-
disk->first_minor = index << part_shift;
1753-
if (disk->first_minor < index || disk->first_minor > MINORMASK) {
1754-
err = -EINVAL;
1755-
goto out_free_work;
1756-
}
1757-
17581747
disk->minors = 1 << part_shift;
17591748
disk->fops = &nbd_fops;
17601749
disk->private_data = nbd;
@@ -1859,8 +1848,19 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
18591848
if (!netlink_capable(skb, CAP_SYS_ADMIN))
18601849
return -EPERM;
18611850

1862-
if (info->attrs[NBD_ATTR_INDEX])
1851+
if (info->attrs[NBD_ATTR_INDEX]) {
18631852
index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1853+
1854+
/*
1855+
* Too big first_minor can cause duplicate creation of
1856+
* sysfs files/links, since index << part_shift might overflow, or
1857+
* MKDEV() expect that the max bits of first_minor is 20.
1858+
*/
1859+
if (index < 0 || index > MINORMASK >> part_shift) {
1860+
printk(KERN_ERR "nbd: illegal input index %d\n", index);
1861+
return -EINVAL;
1862+
}
1863+
}
18641864
if (!info->attrs[NBD_ATTR_SOCKETS]) {
18651865
printk(KERN_ERR "nbd: must specify at least one socket\n");
18661866
return -EINVAL;

0 commit comments

Comments
 (0)