LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] nbd: fix crash when the blksize is zero
@ 2019-05-27  5:44 xiubli
  2019-05-29 13:50 ` Josef Bacik
  2019-05-29 18:55 ` Mike Christie
  0 siblings, 2 replies; 4+ messages in thread
From: xiubli @ 2019-05-27  5:44 UTC (permalink / raw)
  To: josef, axboe, nbd; +Cc: linux-block, linux-kernel, atumball, Xiubo Li

From: Xiubo Li <xiubli@redhat.com>

This will allow the blksize to be set zero and then use 1024 as
default.

Signed-off-by: Xiubo Li <xiubli@redhat.com>
---
 drivers/block/nbd.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 053958a..4c1de1c 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -135,6 +135,8 @@ struct nbd_cmd {
 
 #define NBD_MAGIC 0x68797548
 
+#define NBD_DEF_BLKSIZE 1024
+
 static unsigned int nbds_max = 16;
 static int max_part = 16;
 static struct workqueue_struct *recv_workqueue;
@@ -1237,6 +1239,14 @@ static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
 		nbd_config_put(nbd);
 }
 
+static bool nbd_is_valid_blksize(unsigned long blksize)
+{
+	if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
+		blksize > PAGE_SIZE)
+		return false;
+	return true;
+}
+
 /* Must be called with config_lock held */
 static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
 		       unsigned int cmd, unsigned long arg)
@@ -1252,8 +1262,9 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
 	case NBD_SET_SOCK:
 		return nbd_add_socket(nbd, arg, false);
 	case NBD_SET_BLKSIZE:
-		if (!arg || !is_power_of_2(arg) || arg < 512 ||
-		    arg > PAGE_SIZE)
+		if (!arg)
+			arg = NBD_DEF_BLKSIZE;
+		if (!nbd_is_valid_blksize(arg))
 			return -EINVAL;
 		nbd_size_set(nbd, arg,
 			     div_s64(config->bytesize, arg));
@@ -1333,7 +1344,7 @@ static struct nbd_config *nbd_alloc_config(void)
 	atomic_set(&config->recv_threads, 0);
 	init_waitqueue_head(&config->recv_wq);
 	init_waitqueue_head(&config->conn_wait);
-	config->blksize = 1024;
+	config->blksize = NBD_DEF_BLKSIZE;
 	atomic_set(&config->live_connections, 0);
 	try_module_get(THIS_MODULE);
 	return config;
@@ -1769,6 +1780,10 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
 	if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
 		u64 bsize =
 			nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
+		if (!bsize)
+			bsize = NBD_DEF_BLKSIZE;
+		if (!nbd_is_valid_blksize(bsize))
+			return -EINVAL;
 		nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
 	}
 	if (info->attrs[NBD_ATTR_TIMEOUT]) {
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nbd: fix crash when the blksize is zero
  2019-05-27  5:44 [PATCH] nbd: fix crash when the blksize is zero xiubli
@ 2019-05-29 13:50 ` Josef Bacik
  2019-05-29 18:55 ` Mike Christie
  1 sibling, 0 replies; 4+ messages in thread
From: Josef Bacik @ 2019-05-29 13:50 UTC (permalink / raw)
  To: xiubli; +Cc: josef, axboe, nbd, linux-block, linux-kernel, atumball

On Mon, May 27, 2019 at 01:44:38PM +0800, xiubli@redhat.com wrote:
> From: Xiubo Li <xiubli@redhat.com>
> 
> This will allow the blksize to be set zero and then use 1024 as
> default.
> 
> Signed-off-by: Xiubo Li <xiubli@redhat.com>

Hmm sorry I missed this somehow

Reviewed-by: Josef Bacik <josef@toxicpanda.com>

Thanks,

Josef

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nbd: fix crash when the blksize is zero
  2019-05-27  5:44 [PATCH] nbd: fix crash when the blksize is zero xiubli
  2019-05-29 13:50 ` Josef Bacik
@ 2019-05-29 18:55 ` Mike Christie
  2019-05-30  1:28   ` Xiubo Li
  1 sibling, 1 reply; 4+ messages in thread
From: Mike Christie @ 2019-05-29 18:55 UTC (permalink / raw)
  To: xiubli, josef, axboe, nbd; +Cc: linux-block, linux-kernel, atumball

On 05/27/2019 12:44 AM, xiubli@redhat.com wrote:
> From: Xiubo Li <xiubli@redhat.com>
> 
> This will allow the blksize to be set zero and then use 1024 as
> default.
> 
> Signed-off-by: Xiubo Li <xiubli@redhat.com>
> ---
>  drivers/block/nbd.c | 21 ++++++++++++++++++---
>  1 file changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> index 053958a..4c1de1c 100644
> --- a/drivers/block/nbd.c
> +++ b/drivers/block/nbd.c
> @@ -135,6 +135,8 @@ struct nbd_cmd {
>  
>  #define NBD_MAGIC 0x68797548
>  
> +#define NBD_DEF_BLKSIZE 1024
> +
>  static unsigned int nbds_max = 16;
>  static int max_part = 16;
>  static struct workqueue_struct *recv_workqueue;
> @@ -1237,6 +1239,14 @@ static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
>  		nbd_config_put(nbd);
>  }
>  
> +static bool nbd_is_valid_blksize(unsigned long blksize)
> +{
> +	if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
> +		blksize > PAGE_SIZE)
> +		return false;
> +	return true;
> +}
> +
>  /* Must be called with config_lock held */
>  static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
>  		       unsigned int cmd, unsigned long arg)
> @@ -1252,8 +1262,9 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
>  	case NBD_SET_SOCK:
>  		return nbd_add_socket(nbd, arg, false);
>  	case NBD_SET_BLKSIZE:
> -		if (!arg || !is_power_of_2(arg) || arg < 512 ||
> -		    arg > PAGE_SIZE)
> +		if (!arg)
> +			arg = NBD_DEF_BLKSIZE;
> +		if (!nbd_is_valid_blksize(arg))
>  			return -EINVAL;
>  		nbd_size_set(nbd, arg,
>  			     div_s64(config->bytesize, arg));
> @@ -1333,7 +1344,7 @@ static struct nbd_config *nbd_alloc_config(void)
>  	atomic_set(&config->recv_threads, 0);
>  	init_waitqueue_head(&config->recv_wq);
>  	init_waitqueue_head(&config->conn_wait);
> -	config->blksize = 1024;
> +	config->blksize = NBD_DEF_BLKSIZE;
>  	atomic_set(&config->live_connections, 0);
>  	try_module_get(THIS_MODULE);
>  	return config;
> @@ -1769,6 +1780,10 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
>  	if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
>  		u64 bsize =
>  			nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
> +		if (!bsize)
> +			bsize = NBD_DEF_BLKSIZE;
> +		if (!nbd_is_valid_blksize(bsize))
> +			return -EINVAL;

You can't only return here. You need to also drop the mutex, do
nbd_put, and drop config_refs reference.

Maybe you want to move this check to the beginning of the function with
the NBD_ATTR_SIZE_BYTES sanity check since the error handling is easier
there.


>  		nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
>  	}
>  	if (info->attrs[NBD_ATTR_TIMEOUT]) {
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nbd: fix crash when the blksize is zero
  2019-05-29 18:55 ` Mike Christie
@ 2019-05-30  1:28   ` Xiubo Li
  0 siblings, 0 replies; 4+ messages in thread
From: Xiubo Li @ 2019-05-30  1:28 UTC (permalink / raw)
  To: Mike Christie, josef, axboe, nbd; +Cc: linux-block, linux-kernel, atumball

On 2019/5/30 2:55, Mike Christie wrote:
> On 05/27/2019 12:44 AM, xiubli@redhat.com wrote:
>> From: Xiubo Li <xiubli@redhat.com>
>>
>> This will allow the blksize to be set zero and then use 1024 as
>> default.
>>
>> Signed-off-by: Xiubo Li <xiubli@redhat.com>
>> ---
>>   drivers/block/nbd.c | 21 ++++++++++++++++++---
>>   1 file changed, 18 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
>> index 053958a..4c1de1c 100644
>> --- a/drivers/block/nbd.c
>> +++ b/drivers/block/nbd.c
>> @@ -135,6 +135,8 @@ struct nbd_cmd {
>>   
>>   #define NBD_MAGIC 0x68797548
>>   
>> +#define NBD_DEF_BLKSIZE 1024
>> +
>>   static unsigned int nbds_max = 16;
>>   static int max_part = 16;
>>   static struct workqueue_struct *recv_workqueue;
>> @@ -1237,6 +1239,14 @@ static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
>>   		nbd_config_put(nbd);
>>   }
>>   
>> +static bool nbd_is_valid_blksize(unsigned long blksize)
>> +{
>> +	if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
>> +		blksize > PAGE_SIZE)
>> +		return false;
>> +	return true;
>> +}
>> +
>>   /* Must be called with config_lock held */
>>   static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
>>   		       unsigned int cmd, unsigned long arg)
>> @@ -1252,8 +1262,9 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
>>   	case NBD_SET_SOCK:
>>   		return nbd_add_socket(nbd, arg, false);
>>   	case NBD_SET_BLKSIZE:
>> -		if (!arg || !is_power_of_2(arg) || arg < 512 ||
>> -		    arg > PAGE_SIZE)
>> +		if (!arg)
>> +			arg = NBD_DEF_BLKSIZE;
>> +		if (!nbd_is_valid_blksize(arg))
>>   			return -EINVAL;
>>   		nbd_size_set(nbd, arg,
>>   			     div_s64(config->bytesize, arg));
>> @@ -1333,7 +1344,7 @@ static struct nbd_config *nbd_alloc_config(void)
>>   	atomic_set(&config->recv_threads, 0);
>>   	init_waitqueue_head(&config->recv_wq);
>>   	init_waitqueue_head(&config->conn_wait);
>> -	config->blksize = 1024;
>> +	config->blksize = NBD_DEF_BLKSIZE;
>>   	atomic_set(&config->live_connections, 0);
>>   	try_module_get(THIS_MODULE);
>>   	return config;
>> @@ -1769,6 +1780,10 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
>>   	if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
>>   		u64 bsize =
>>   			nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
>> +		if (!bsize)
>> +			bsize = NBD_DEF_BLKSIZE;
>> +		if (!nbd_is_valid_blksize(bsize))
>> +			return -EINVAL;
> You can't only return here. You need to also drop the mutex, do
> nbd_put, and drop config_refs reference.
>
> Maybe you want to move this check to the beginning of the function with
> the NBD_ATTR_SIZE_BYTES sanity check since the error handling is easier
> there.

Yeah, right.

I saw your resend patch of this and that looks good to me.

Thanks
BRs
Xiubo


>
>>   		nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
>>   	}
>>   	if (info->attrs[NBD_ATTR_TIMEOUT]) {
>>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-05-30  1:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-27  5:44 [PATCH] nbd: fix crash when the blksize is zero xiubli
2019-05-29 13:50 ` Josef Bacik
2019-05-29 18:55 ` Mike Christie
2019-05-30  1:28   ` Xiubo Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).