[PATCH v2] mtd: ubi: fix possible null-ptr-deref in ubi_free_volume()

Zhihao Cheng chengzhihao1 at huawei.com
Sat Nov 12 01:13:50 PST 2022


在 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;
  }




More information about the linux-mtd mailing list