* [PATCH] mtd: ubi: fix possible null-ptr-deref in ubi_free_volume()
@ 2022-11-11 14:48 Yang Yingliang
2022-11-12 3:19 ` Zhihao Cheng
0 siblings, 1 reply; 2+ messages in thread
From: Yang Yingliang @ 2022-11-11 14:48 UTC (permalink / raw)
To: linux-mtd; +Cc: richard, miquel.raynal, vigneshr, yangyingliang
It willl cause null-ptr-deref in the following case:
uif_init()
ubi_add_volume()
cdev_add() -> if it fails, call kill_volumes()
device_register()
kill_volumes() -> if ubi_add_volume() fails call this function
ubi_free_volume()
cdev_del()
device_unregister() -> trying to delete a not added device,
it causes null-ptr-deref
So in ubi_free_volume(), it delete devices whether they are added
or not, it will causes null-ptr-deref.
Fix this by adding some checks.
Fixes: 801c135ce73d ("UBI: Unsorted Block Images")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
drivers/mtd/ubi/vmt.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
index 8fcc0bdf0635..c379b8739f52 100644
--- a/drivers/mtd/ubi/vmt.c
+++ b/drivers/mtd/ubi/vmt.c
@@ -614,8 +614,14 @@ void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
dbg_gen("free volume %d", vol->vol_id);
ubi->volumes[vol->vol_id] = NULL;
- cdev_del(&vol->cdev);
- device_unregister(&vol->dev);
+ if (device_is_registered(&vol->dev)) {
+ cdev_del(&vol->cdev);
+ device_unregister(&vol->dev);
+ } else if (vol->dev.kobj.state_initialized) {
+ put_device(&vol->dev);
+ } else {
+ kfree(vol);
+ }
}
/**
--
2.25.1
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] mtd: ubi: fix possible null-ptr-deref in ubi_free_volume()
2022-11-11 14:48 [PATCH] mtd: ubi: fix possible null-ptr-deref in ubi_free_volume() Yang Yingliang
@ 2022-11-12 3:19 ` Zhihao Cheng
0 siblings, 0 replies; 2+ messages in thread
From: Zhihao Cheng @ 2022-11-12 3:19 UTC (permalink / raw)
To: Yang Yingliang, linux-mtd; +Cc: richard, miquel.raynal, vigneshr
在 2022/11/11 22:48, Yang Yingliang 写道:
[...]
> diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
> index 8fcc0bdf0635..c379b8739f52 100644
> --- a/drivers/mtd/ubi/vmt.c
> +++ b/drivers/mtd/ubi/vmt.c
> @@ -614,8 +614,14 @@ void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
> dbg_gen("free volume %d", vol->vol_id);
>
> ubi->volumes[vol->vol_id] = NULL;
> - cdev_del(&vol->cdev);
> - device_unregister(&vol->dev);
> + if (device_is_registered(&vol->dev)) {
> + cdev_del(&vol->cdev);
> + device_unregister(&vol->dev);
> + } else if (vol->dev.kobj.state_initialized) {
> + put_device(&vol->dev);
> + } else {
Following process will trigger a memleak after this patch applied.
ubi_attach
ubi_read_volume_table
init_volumes
ubi_fastmap_init_checkmap
vol->checkmap = bitmap_zalloc(leb_count, GFP_KERNEL); // alloc
bitmap
ubi_eba_init
ubi_eba_replace_table
vol->eba_tbl = tbl // alloc eba_tbl
uif_init
ubi_add_volume
cdev_add // failed
kill_volumes
ubi_free_volume
kfree(vol) // miss freeing eba_tbl and bitmap
> + kfree(vol);
> + }
> }
>
> /**
>
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-11-12 3:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-11 14:48 [PATCH] mtd: ubi: fix possible null-ptr-deref in ubi_free_volume() Yang Yingliang
2022-11-12 3:19 ` Zhihao Cheng
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).