Skip to content

Commit c688982

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 <josef@toxicpanda.com> Signed-off-by: Xiubo Li <xiubli@redhat.com> [fix to use goto out instead of return in genl_connect] Signed-off-by: Mike Christie <mchristi@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 63bb8b7 commit c688982

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
@@ -133,6 +133,8 @@ static struct dentry *nbd_dbg_dir;
133133

134134
#define NBD_MAGIC 0x68797548
135135

136+
#define NBD_DEF_BLKSIZE 1024
137+
136138
static unsigned int nbds_max = 16;
137139
static int max_part = 16;
138140
static int part_shift;
@@ -1241,6 +1243,14 @@ static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
12411243
nbd_config_put(nbd);
12421244
}
12431245

1246+
static bool nbd_is_valid_blksize(unsigned long blksize)
1247+
{
1248+
if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
1249+
blksize > PAGE_SIZE)
1250+
return false;
1251+
return true;
1252+
}
1253+
12441254
/* Must be called with config_lock held */
12451255
static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
12461256
unsigned int cmd, unsigned long arg)
@@ -1256,8 +1266,9 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
12561266
case NBD_SET_SOCK:
12571267
return nbd_add_socket(nbd, arg, false);
12581268
case NBD_SET_BLKSIZE:
1259-
if (!arg || !is_power_of_2(arg) || arg < 512 ||
1260-
arg > PAGE_SIZE)
1269+
if (!arg)
1270+
arg = NBD_DEF_BLKSIZE;
1271+
if (!nbd_is_valid_blksize(arg))
12611272
return -EINVAL;
12621273
nbd_size_set(nbd, arg,
12631274
div_s64(config->bytesize, arg));
@@ -1337,7 +1348,7 @@ static struct nbd_config *nbd_alloc_config(void)
13371348
atomic_set(&config->recv_threads, 0);
13381349
init_waitqueue_head(&config->recv_wq);
13391350
init_waitqueue_head(&config->conn_wait);
1340-
config->blksize = 1024;
1351+
config->blksize = NBD_DEF_BLKSIZE;
13411352
atomic_set(&config->live_connections, 0);
13421353
try_module_get(THIS_MODULE);
13431354
return config;
@@ -1773,6 +1784,12 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
17731784
if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
17741785
u64 bsize =
17751786
nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1787+
if (!bsize)
1788+
bsize = NBD_DEF_BLKSIZE;
1789+
if (!nbd_is_valid_blksize(bsize)) {
1790+
ret = -EINVAL;
1791+
goto out;
1792+
}
17761793
nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
17771794
}
17781795
if (info->attrs[NBD_ATTR_TIMEOUT]) {

0 commit comments

Comments
 (0)