2024-05-22 09:17:39

by Li Nan

[permalink] [raw]
Subject: [PATCH v3] ubi: block: fix null-pointer-dereference in ubiblock_create()

From: Li Nan <[email protected]>

Similar to commit adbf4c4954e3 ("ubi: block: fix memleak in
ubiblock_create()"), 'dev->gd' is not assigned but dereferenced if
blk_mq_alloc_tag_set() fails, and leading to a null-pointer-dereference.
Fix it by using pr_err() and variable 'dev' to print error log.

Additionally, the log in the error handle path of idr_alloc() has
been improved by using pr_err(), too. Before initializing device
name, using dev_err() will print error log with 'null' instead of
the actual device name, like this:
block (null): ...
~~~~~~
It is unclear. Using pr_err() can print more details of the device.
The improved log is:
ubiblock0_0: ...

Fixes: 77567b25ab9f ("ubi: use blk_mq_alloc_disk and blk_cleanup_disk")
Reported-by: Dan Carpenter <[email protected]>
Signed-off-by: Li Nan <[email protected]>
---
v3: spliting this patch into two in v2 is not a good idea. This version
is consistent with v1, but only optimizes the commit message.

drivers/mtd/ubi/block.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
index f82e3423acb9..bf7308e8ec2f 100644
--- a/drivers/mtd/ubi/block.c
+++ b/drivers/mtd/ubi/block.c
@@ -390,7 +390,8 @@ int ubiblock_create(struct ubi_volume_info *vi)

ret = blk_mq_alloc_tag_set(&dev->tag_set);
if (ret) {
- dev_err(disk_to_dev(dev->gd), "blk_mq_alloc_tag_set failed");
+ pr_err("ubiblock%d_%d: blk_mq_alloc_tag_set failed\n",
+ dev->ubi_num, dev->vol_id);
goto out_free_dev;
}

@@ -407,8 +408,8 @@ int ubiblock_create(struct ubi_volume_info *vi)
gd->minors = 1;
gd->first_minor = idr_alloc(&ubiblock_minor_idr, dev, 0, 0, GFP_KERNEL);
if (gd->first_minor < 0) {
- dev_err(disk_to_dev(gd),
- "block: dynamic minor allocation failed");
+ pr_err("ubiblock%d_%d: block: dynamic minor allocation failed\n",
+ dev->ubi_num, dev->vol_id);
ret = -ENODEV;
goto out_cleanup_disk;
}
--
2.39.2



2024-05-22 09:22:18

by Zhihao Cheng

[permalink] [raw]
Subject: Re: [PATCH v3] ubi: block: fix null-pointer-dereference in ubiblock_create()

?? 2024/5/23 1:10, [email protected] ะด??:
> From: Li Nan <[email protected]>
>
> Similar to commit adbf4c4954e3 ("ubi: block: fix memleak in
> ubiblock_create()"), 'dev->gd' is not assigned but dereferenced if
> blk_mq_alloc_tag_set() fails, and leading to a null-pointer-dereference.
> Fix it by using pr_err() and variable 'dev' to print error log.
>
> Additionally, the log in the error handle path of idr_alloc() has
> been improved by using pr_err(), too. Before initializing device
> name, using dev_err() will print error log with 'null' instead of
> the actual device name, like this:
> block (null): ...
> ~~~~~~
> It is unclear. Using pr_err() can print more details of the device.
> The improved log is:
> ubiblock0_0: ...
>
> Fixes: 77567b25ab9f ("ubi: use blk_mq_alloc_disk and blk_cleanup_disk")
> Reported-by: Dan Carpenter <[email protected]>
> Signed-off-by: Li Nan <[email protected]>
> ---
> v3: spliting this patch into two in v2 is not a good idea. This version
> is consistent with v1, but only optimizes the commit message.
>
> drivers/mtd/ubi/block.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)

Reviewed-by: Zhihao Cheng <[email protected]>
>
> diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
> index f82e3423acb9..bf7308e8ec2f 100644
> --- a/drivers/mtd/ubi/block.c
> +++ b/drivers/mtd/ubi/block.c
> @@ -390,7 +390,8 @@ int ubiblock_create(struct ubi_volume_info *vi)
>
> ret = blk_mq_alloc_tag_set(&dev->tag_set);
> if (ret) {
> - dev_err(disk_to_dev(dev->gd), "blk_mq_alloc_tag_set failed");
> + pr_err("ubiblock%d_%d: blk_mq_alloc_tag_set failed\n",
> + dev->ubi_num, dev->vol_id);
> goto out_free_dev;
> }
>
> @@ -407,8 +408,8 @@ int ubiblock_create(struct ubi_volume_info *vi)
> gd->minors = 1;
> gd->first_minor = idr_alloc(&ubiblock_minor_idr, dev, 0, 0, GFP_KERNEL);
> if (gd->first_minor < 0) {
> - dev_err(disk_to_dev(gd),
> - "block: dynamic minor allocation failed");
> + pr_err("ubiblock%d_%d: block: dynamic minor allocation failed\n",
> + dev->ubi_num, dev->vol_id);
> ret = -ENODEV;
> goto out_cleanup_disk;
> }
>


2024-06-05 14:39:20

by Daniel Golle

[permalink] [raw]
Subject: Re: [PATCH v3] ubi: block: fix null-pointer-dereference in ubiblock_create()

On Thu, May 23, 2024 at 01:10:35AM +0800, [email protected] wrote:
> From: Li Nan <[email protected]>
>
> Similar to commit adbf4c4954e3 ("ubi: block: fix memleak in
> ubiblock_create()"), 'dev->gd' is not assigned but dereferenced if
> blk_mq_alloc_tag_set() fails, and leading to a null-pointer-dereference.
> Fix it by using pr_err() and variable 'dev' to print error log.
>
> Additionally, the log in the error handle path of idr_alloc() has
> been improved by using pr_err(), too. Before initializing device
> name, using dev_err() will print error log with 'null' instead of
> the actual device name, like this:
> block (null): ...
> ~~~~~~
> It is unclear. Using pr_err() can print more details of the device.
> The improved log is:
> ubiblock0_0: ...
>
> Fixes: 77567b25ab9f ("ubi: use blk_mq_alloc_disk and blk_cleanup_disk")
> Reported-by: Dan Carpenter <[email protected]>
> Signed-off-by: Li Nan <[email protected]>

Reviewed-by: Daniel Golle <[email protected]>

> ---
> v3: spliting this patch into two in v2 is not a good idea. This version
> is consistent with v1, but only optimizes the commit message.
>
> drivers/mtd/ubi/block.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
> index f82e3423acb9..bf7308e8ec2f 100644
> --- a/drivers/mtd/ubi/block.c
> +++ b/drivers/mtd/ubi/block.c
> @@ -390,7 +390,8 @@ int ubiblock_create(struct ubi_volume_info *vi)
>
> ret = blk_mq_alloc_tag_set(&dev->tag_set);
> if (ret) {
> - dev_err(disk_to_dev(dev->gd), "blk_mq_alloc_tag_set failed");
> + pr_err("ubiblock%d_%d: blk_mq_alloc_tag_set failed\n",
> + dev->ubi_num, dev->vol_id);
> goto out_free_dev;
> }
>
> @@ -407,8 +408,8 @@ int ubiblock_create(struct ubi_volume_info *vi)
> gd->minors = 1;
> gd->first_minor = idr_alloc(&ubiblock_minor_idr, dev, 0, 0, GFP_KERNEL);
> if (gd->first_minor < 0) {
> - dev_err(disk_to_dev(gd),
> - "block: dynamic minor allocation failed");
> + pr_err("ubiblock%d_%d: block: dynamic minor allocation failed\n",
> + dev->ubi_num, dev->vol_id);
> ret = -ENODEV;
> goto out_cleanup_disk;
> }
> --
> 2.39.2
>
>
> ______________________________________________________
> Linux MTD discussion mailing list
> http://lists.infradead.org/mailman/listinfo/linux-mtd/