Skip to content

Commit 498ade7

Browse files
lxbszgregkh
authored andcommitted
nbd: fix crash when the blksize is zero
[ Upstream commit 553768d ] This will allow the blksize to be set zero and then use 1024 as default. Reviewed-by: Josef Bacik <[email protected]> Signed-off-by: Xiubo Li <[email protected]> [fix to use goto out instead of return in genl_connect] Signed-off-by: Mike Christie <[email protected]> Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 3f57d4b commit 498ade7

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

drivers/block/nbd.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ static struct dentry *nbd_dbg_dir;
132132

133133
#define NBD_MAGIC 0x68797548
134134

135+
#define NBD_DEF_BLKSIZE 1024
136+
135137
static unsigned int nbds_max = 16;
136138
static int max_part = 16;
137139
static struct workqueue_struct *recv_workqueue;
@@ -1216,6 +1218,14 @@ static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
12161218
nbd_config_put(nbd);
12171219
}
12181220

1221+
static bool nbd_is_valid_blksize(unsigned long blksize)
1222+
{
1223+
if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
1224+
blksize > PAGE_SIZE)
1225+
return false;
1226+
return true;
1227+
}
1228+
12191229
/* Must be called with config_lock held */
12201230
static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
12211231
unsigned int cmd, unsigned long arg)
@@ -1231,8 +1241,9 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
12311241
case NBD_SET_SOCK:
12321242
return nbd_add_socket(nbd, arg, false);
12331243
case NBD_SET_BLKSIZE:
1234-
if (!arg || !is_power_of_2(arg) || arg < 512 ||
1235-
arg > PAGE_SIZE)
1244+
if (!arg)
1245+
arg = NBD_DEF_BLKSIZE;
1246+
if (!nbd_is_valid_blksize(arg))
12361247
return -EINVAL;
12371248
nbd_size_set(nbd, arg,
12381249
div_s64(config->bytesize, arg));
@@ -1312,7 +1323,7 @@ static struct nbd_config *nbd_alloc_config(void)
13121323
atomic_set(&config->recv_threads, 0);
13131324
init_waitqueue_head(&config->recv_wq);
13141325
init_waitqueue_head(&config->conn_wait);
1315-
config->blksize = 1024;
1326+
config->blksize = NBD_DEF_BLKSIZE;
13161327
atomic_set(&config->live_connections, 0);
13171328
try_module_get(THIS_MODULE);
13181329
return config;
@@ -1744,6 +1755,12 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
17441755
if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
17451756
u64 bsize =
17461757
nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1758+
if (!bsize)
1759+
bsize = NBD_DEF_BLKSIZE;
1760+
if (!nbd_is_valid_blksize(bsize)) {
1761+
ret = -EINVAL;
1762+
goto out;
1763+
}
17471764
nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
17481765
}
17491766
if (info->attrs[NBD_ATTR_TIMEOUT]) {

0 commit comments

Comments
 (0)