ubi: block: fix null-pointer-dereference in ubiblock_create()

Message ID 20231208071317.1268465-1-linan666@huaweicloud.com
State New
Headers
Series ubi: block: fix null-pointer-dereference in ubiblock_create() |

Commit Message

Li Nan Dec. 8, 2023, 7:13 a.m. UTC
  From: Li Nan <linan122@huawei.com>

If idr_alloc() fails, dev->gd will be put after goto out_cleanup_disk in
ubiblock_create(), but dev->gd has not been assigned yet at this time, and
accessing it will trigger a null-pointer-dereference issue. Fix it by put
gd directly.

Signed-off-by: Li Nan <linan122@huawei.com>
---
 drivers/mtd/ubi/block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Zhihao Cheng Dec. 8, 2023, 7:29 a.m. UTC | #1
在 2023/12/8 15:13, linan666@huaweicloud.com 写道:
> From: Li Nan <linan122@huawei.com>
>
> If idr_alloc() fails, dev->gd will be put after goto out_cleanup_disk in
> ubiblock_create(), but dev->gd has not been assigned yet at this time, and
> accessing it will trigger a null-pointer-dereference issue. Fix it by put
> gd directly.
Function 'put_disk()' checks disk whether is NULL, so I think it's a 
'memleak' problem, not a null-ptr-deref problem.
>
> Signed-off-by: Li Nan <linan122@huawei.com>
> ---
>   drivers/mtd/ubi/block.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
> index 309a42aeaa4c..654bd7372cd8 100644
> --- a/drivers/mtd/ubi/block.c
> +++ b/drivers/mtd/ubi/block.c
> @@ -434,7 +434,7 @@ int ubiblock_create(struct ubi_volume_info *vi)
>   	list_del(&dev->list);
>   	idr_remove(&ubiblock_minor_idr, gd->first_minor);
>   out_cleanup_disk:
> -	put_disk(dev->gd);
> +	put_disk(gd);

For memleak solution:

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>

>   out_free_tags:
>   	blk_mq_free_tag_set(&dev->tag_set);
>   out_free_dev:
  
Li Nan Dec. 8, 2023, 7:40 a.m. UTC | #2
在 2023/12/8 15:29, Zhihao Cheng 写道:
> 在 2023/12/8 15:13, linan666@huaweicloud.com 写道:
>> From: Li Nan <linan122@huawei.com>
>>
>> If idr_alloc() fails, dev->gd will be put after goto out_cleanup_disk in
>> ubiblock_create(), but dev->gd has not been assigned yet at this time, 
>> and
>> accessing it will trigger a null-pointer-dereference issue. Fix it by put
>> gd directly.
> Function 'put_disk()' checks disk whether is NULL, so I think it's a 
> 'memleak' problem, not a null-ptr-deref problem.
>>

Damn, I overlooked it here. Thanks for your review, I will fix the log
in v2.

>> Signed-off-by: Li Nan <linan122@huawei.com>
>> ---
>>   drivers/mtd/ubi/block.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
>> index 309a42aeaa4c..654bd7372cd8 100644
>> --- a/drivers/mtd/ubi/block.c
>> +++ b/drivers/mtd/ubi/block.c
>> @@ -434,7 +434,7 @@ int ubiblock_create(struct ubi_volume_info *vi)
>>       list_del(&dev->list);
>>       idr_remove(&ubiblock_minor_idr, gd->first_minor);
>>   out_cleanup_disk:
>> -    put_disk(dev->gd);
>> +    put_disk(gd);
> 
> For memleak solution:
> 
> Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
> 
>>   out_free_tags:
>>       blk_mq_free_tag_set(&dev->tag_set);
>>   out_free_dev:
> 
> 
> .
  

Patch

diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
index 309a42aeaa4c..654bd7372cd8 100644
--- a/drivers/mtd/ubi/block.c
+++ b/drivers/mtd/ubi/block.c
@@ -434,7 +434,7 @@  int ubiblock_create(struct ubi_volume_info *vi)
 	list_del(&dev->list);
 	idr_remove(&ubiblock_minor_idr, gd->first_minor);
 out_cleanup_disk:
-	put_disk(dev->gd);
+	put_disk(gd);
 out_free_tags:
 	blk_mq_free_tag_set(&dev->tag_set);
 out_free_dev: