linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] mtd: ubi: fix possible null-ptr-deref in ubi_free_volume()
@ 2022-11-12  4:14 Yang Yingliang
  2022-11-12  9:13 ` Zhihao Cheng
  0 siblings, 1 reply; 2+ messages in thread
From: Yang Yingliang @ 2022-11-12  4:14 UTC (permalink / raw)
  To: linux-mtd; +Cc: richard, miquel.raynal, vigneshr, yangyingliang, chengzhihao1

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>
---
v1 -> v2:
  Replace kfree() with vol_release() to avoid memory leak, if device is not initalized.
---
 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..5b7ad1c266cb 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 {
+		vol_release(&vol->dev);
+	}
 }
 
 /**
-- 
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 v2] mtd: ubi: fix possible null-ptr-deref in ubi_free_volume()
  2022-11-12  4:14 [PATCH v2] mtd: ubi: fix possible null-ptr-deref in ubi_free_volume() Yang Yingliang
@ 2022-11-12  9:13 ` Zhihao Cheng
  0 siblings, 0 replies; 2+ messages in thread
From: Zhihao Cheng @ 2022-11-12  9:13 UTC (permalink / raw)
  To: Yang Yingliang, linux-mtd; +Cc: richard, miquel.raynal, vigneshr

在 2022/11/12 12:14, Yang Yingliang 写道:

[...]
> diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
> index 8fcc0bdf0635..5b7ad1c266cb 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 {
> +		vol_release(&vol->dev);
> +	}
>   }
>   
>   /**
> 

How about this? Handle adding failed volume in ubi_add_volume(), free 
other added volumes by kill_volumes().
diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
index 8fcc0bdf0635..9c0b5f5988d5 100644
--- a/drivers/mtd/ubi/vmt.c
+++ b/drivers/mtd/ubi/vmt.c
@@ -580,7 +580,9 @@ int ubi_add_volume(struct ubi_device *ubi, struct 
ubi_volume *vol)
         if (err) {
                 ubi_err(ubi, "cannot add character device for volume 
%d, error %d",
                         vol_id, err);
-               return err;
+               ubi->volumes[vol->vol_id] = NULL;
+               vol_release(&vol->dev);
+               goto out;
         }

         vol->dev.release = vol_release;
@@ -590,14 +592,16 @@ int ubi_add_volume(struct ubi_device *ubi, struct 
ubi_volume *vol)
         vol->dev.groups = volume_dev_groups;
         dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
         err = device_register(&vol->dev);
-       if (err)
-               goto out_cdev;
+       if (err) {
+               cdev_del(&vol->cdev);
+               ubi->volumes[vol->vol_id] = NULL;
+               put_device(&vol->dev);
+               goto out;
+       }

         self_check_volumes(ubi);
-       return err;

-out_cdev:
-       cdev_del(&vol->cdev);
+out:
         return err;
  }


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

end of thread, other threads:[~2022-11-12  9:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-12  4:14 [PATCH v2] mtd: ubi: fix possible null-ptr-deref in ubi_free_volume() Yang Yingliang
2022-11-12  9:13 ` 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).