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

Zhihao Cheng chengzhihao1 at huawei.com
Fri Nov 11 19:19:27 PST 2022


在 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);
> +	}
>   }
>   
>   /**
> 




More information about the linux-mtd mailing list