Skip to content

Commit dcbddf5

Browse files
Christoph Hellwigaxboe
authored andcommitted
nbd: validate the block size in nbd_set_size
Move the validation of the block from the callers into nbd_set_size. Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 2dc691c commit dcbddf5

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

drivers/block/nbd.c

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,21 @@ static void nbd_size_clear(struct nbd_device *nbd)
296296
}
297297
}
298298

299-
static void nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
299+
static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
300300
loff_t blksize)
301301
{
302302
struct block_device *bdev;
303303

304+
if (!blksize)
305+
blksize = NBD_DEF_BLKSIZE;
306+
if (blksize < 512 || blksize > PAGE_SIZE || !is_power_of_2(blksize))
307+
return -EINVAL;
308+
304309
nbd->config->bytesize = bytesize;
305310
nbd->config->blksize = blksize;
306311

307312
if (!nbd->task_recv)
308-
return;
313+
return 0;
309314

310315
if (nbd->config->flags & NBD_FLAG_SEND_TRIM) {
311316
nbd->disk->queue->limits.discard_granularity = blksize;
@@ -325,6 +330,7 @@ static void nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
325330
bdput(bdev);
326331
}
327332
kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
333+
return 0;
328334
}
329335

330336
static void nbd_complete_rq(struct request *req)
@@ -1304,8 +1310,7 @@ static int nbd_start_device(struct nbd_device *nbd)
13041310
args->index = i;
13051311
queue_work(nbd->recv_workq, &args->work);
13061312
}
1307-
nbd_set_size(nbd, config->bytesize, config->blksize);
1308-
return error;
1313+
return nbd_set_size(nbd, config->bytesize, config->blksize);
13091314
}
13101315

13111316
static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
@@ -1347,14 +1352,6 @@ static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
13471352
nbd_config_put(nbd);
13481353
}
13491354

1350-
static bool nbd_is_valid_blksize(unsigned long blksize)
1351-
{
1352-
if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
1353-
blksize > PAGE_SIZE)
1354-
return false;
1355-
return true;
1356-
}
1357-
13581355
static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout)
13591356
{
13601357
nbd->tag_set.timeout = timeout * HZ;
@@ -1379,19 +1376,12 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
13791376
case NBD_SET_SOCK:
13801377
return nbd_add_socket(nbd, arg, false);
13811378
case NBD_SET_BLKSIZE:
1382-
if (!arg)
1383-
arg = NBD_DEF_BLKSIZE;
1384-
if (!nbd_is_valid_blksize(arg))
1385-
return -EINVAL;
1386-
nbd_set_size(nbd, config->bytesize, arg);
1387-
return 0;
1379+
return nbd_set_size(nbd, config->bytesize, arg);
13881380
case NBD_SET_SIZE:
1389-
nbd_set_size(nbd, arg, config->blksize);
1390-
return 0;
1381+
return nbd_set_size(nbd, arg, config->blksize);
13911382
case NBD_SET_SIZE_BLOCKS:
1392-
nbd_set_size(nbd, arg * config->blksize,
1393-
config->blksize);
1394-
return 0;
1383+
return nbd_set_size(nbd, arg * config->blksize,
1384+
config->blksize);
13951385
case NBD_SET_TIMEOUT:
13961386
nbd_set_cmd_timeout(nbd, arg);
13971387
return 0;
@@ -1809,18 +1799,11 @@ static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
18091799
if (info->attrs[NBD_ATTR_SIZE_BYTES])
18101800
bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
18111801

1812-
if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1802+
if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES])
18131803
bsize = nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1814-
if (!bsize)
1815-
bsize = NBD_DEF_BLKSIZE;
1816-
if (!nbd_is_valid_blksize(bsize)) {
1817-
printk(KERN_ERR "Invalid block size %llu\n", bsize);
1818-
return -EINVAL;
1819-
}
1820-
}
18211804

18221805
if (bytes != config->bytesize || bsize != config->blksize)
1823-
nbd_set_size(nbd, bytes, bsize);
1806+
return nbd_set_size(nbd, bytes, bsize);
18241807
return 0;
18251808
}
18261809

0 commit comments

Comments
 (0)