Skip to contentSkip to conversation

UBI: fix use after free in ubi_remove_volume()

PATCH In review Wen Yang 2 participants 3 messages Nov 30, 2019 – Dec 5, 2019

Subsystems (2)
lore
Share & tools
lore

Wen Yang

author
#0 lore raw

PATCH 1/1 UBI: fix use after free in ubi_remove_volume()

We can't use "vol" after it has been freed.

Fixes 493cfaeaa0c9 ("mtd: utilize new cdev_device_add helper function")
Cc Richard Weinberger <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
---
 drivers/mtd/ubi/vmt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mtd/ubi/vmt.c +2 -1
index 139ee13..8ff1478 100644
@@ -375,7 +375,6 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
 	}
 
 	cdev_device_del(&vol->cdev, &vol->dev);
-	put_device(&vol->dev);
 
 	spin_lock(&ubi->volumes_lock);
 	ubi->rsvd_pebs -= reserved_pebs;
@@ -388,6 +387,8 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
 	if (!no_vtbl)
 		self_check_volumes(ubi);
 
+	put_device(&vol->dev);
+
 	return 0;
 
 out_err:
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 35 lines · Wen Yang

We can't use "vol" after it has been freed.

Fixes 493cfaeaa0c9 ("mtd: utilize new cdev_device_add helper function")
Cc Richard Weinberger <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
---
 drivers/mtd/ubi/vmt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mtd/ubi/vmt.c +2 -1
index 139ee13..8ff1478 100644
@@ -375,7 +375,6 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
 	}
 
 	cdev_device_del(&vol->cdev, &vol->dev);
-	put_device(&vol->dev);
 
 	spin_lock(&ubi->volumes_lock);
 	ubi->rsvd_pebs -= reserved_pebs;
@@ -388,6 +387,8 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
 	if (!no_vtbl)
 		self_check_volumes(ubi);
 
+	put_device(&vol->dev);
+
 	return 0;
 
 out_err:
#2 lore raw

Hi,

Quoted 2 lines · Hou Tao

Sorry for my early conclusion.

The reference of ubi_volume had already been increased in ubi_open_volume() in ubi_cdev_ioctl(), so this reference dropped in ubi_remove_volume() will not be the last one, and there will no use-after-free problem.

Regards,
Tao

Quoted 43 lines · Wen Yang
Quoted 27 lines

We can't use "vol" after it has been freed.

Fixes 493cfaeaa0c9 ("mtd: utilize new cdev_device_add helper function")
Cc Richard Weinberger <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
---
 drivers/mtd/ubi/vmt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mtd/ubi/vmt.c +0 -1
index 139ee13..8ff1478 100644
@@ -375,7 +375,6 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
 	}
 
 	cdev_device_del(&vol->cdev, &vol->dev);
-	put_device(&vol->dev);
 
 	spin_lock(&ubi->volumes_lock);
 	ubi->rsvd_pebs -= reserved_pebs;

drivers/mtd/ubi/vmt.c int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl) · line 394

Quoted 8 lines
drivers/mtd/ubi/vmt.c +2 -0
@@ -388,6 +387,8 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
 	if (!no_vtbl)
 		self_check_volumes(ubi);
 
+	put_device(&vol->dev);
+
 	return 0;
 
 out_err:

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