[1/2] ubi: Fix use-after-free when volume resizing failed

Message ID 20221021102157.1341807-2-lizetao1@huawei.com
State New
Headers
Series Fix use-after-free and kmemleak in ubi_resize_volume() |

Commit Message

Li Zetao Oct. 21, 2022, 10:21 a.m. UTC
  There is an use-after-free problem reported by KASAN:
  ==================================================================
  BUG: KASAN: use-after-free in ubi_eba_copy_table+0x11f/0x1c0 [ubi]
  Read of size 8 at addr ffff888101eec008 by task ubirsvol/4735

  CPU: 2 PID: 4735 Comm: ubirsvol
  Not tainted 6.1.0-rc1-00003-g84fa3304a7fc-dirty #14
  Hardware name: QEMU Standard PC (i440FX + PIIX, 1996),
  BIOS 1.14.0-1.fc33 04/01/2014
  Call Trace:
   <TASK>
   dump_stack_lvl+0x34/0x44
   print_report+0x171/0x472
   kasan_report+0xad/0x130
   ubi_eba_copy_table+0x11f/0x1c0 [ubi]
   ubi_resize_volume+0x4f9/0xbc0 [ubi]
   ubi_cdev_ioctl+0x701/0x1850 [ubi]
   __x64_sys_ioctl+0x11d/0x170
   do_syscall_64+0x35/0x80
   entry_SYSCALL_64_after_hwframe+0x46/0xb0
   </TASK>

When ubi_change_vtbl_record() returns an error in ubi_resize_volume(),
"new_eba_tbl" will be freed on error handing path, but it is holded
by "vol->eba_tbl" in ubi_eba_replace_table(). It means that the liftcycle
of "vol->eba_tbl" and "vol" are different, so when resizing volume in
next time, it causing an use-after-free fault.

Fix it by not freeing "new_eba_tbl" after it replaced in
ubi_eba_replace_table(), while will be freed in next volume resizing.

Fixes: 801c135ce73d ("UBI: Unsorted Block Images")
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
 drivers/mtd/ubi/vmt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
  

Comments

Zhihao Cheng Oct. 21, 2022, 1:24 p.m. UTC | #1
在 2022/10/21 18:21, Li Zetao 写道:
> There is an use-after-free problem reported by KASAN:
>    ==================================================================
>    BUG: KASAN: use-after-free in ubi_eba_copy_table+0x11f/0x1c0 [ubi]
>    Read of size 8 at addr ffff888101eec008 by task ubirsvol/4735
>
>    CPU: 2 PID: 4735 Comm: ubirsvol
>    Not tainted 6.1.0-rc1-00003-g84fa3304a7fc-dirty #14
>    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996),
>    BIOS 1.14.0-1.fc33 04/01/2014
>    Call Trace:
>     <TASK>
>     dump_stack_lvl+0x34/0x44
>     print_report+0x171/0x472
>     kasan_report+0xad/0x130
>     ubi_eba_copy_table+0x11f/0x1c0 [ubi]
>     ubi_resize_volume+0x4f9/0xbc0 [ubi]
>     ubi_cdev_ioctl+0x701/0x1850 [ubi]
>     __x64_sys_ioctl+0x11d/0x170
>     do_syscall_64+0x35/0x80
>     entry_SYSCALL_64_after_hwframe+0x46/0xb0
>     </TASK>
>
> When ubi_change_vtbl_record() returns an error in ubi_resize_volume(),
> "new_eba_tbl" will be freed on error handing path, but it is holded
> by "vol->eba_tbl" in ubi_eba_replace_table(). It means that the liftcycle
> of "vol->eba_tbl" and "vol" are different, so when resizing volume in
> next time, it causing an use-after-free fault.
>
> Fix it by not freeing "new_eba_tbl" after it replaced in
> ubi_eba_replace_table(), while will be freed in next volume resizing.
>
> Fixes: 801c135ce73d ("UBI: Unsorted Block Images")
> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> ---
>   drivers/mtd/ubi/vmt.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
> index 8fcc0bdf0635..74637575346e 100644
> --- a/drivers/mtd/ubi/vmt.c
> +++ b/drivers/mtd/ubi/vmt.c
> @@ -464,7 +464,7 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
>   		for (i = 0; i < -pebs; i++) {
>   			err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
>   			if (err)
> -				goto out_acc;
> +				goto out_free;
>   		}
>   		spin_lock(&ubi->volumes_lock);
>   		ubi->rsvd_pebs += pebs;
> @@ -512,6 +512,8 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
>   		ubi->avail_pebs += pebs;
>   		spin_unlock(&ubi->volumes_lock);
>   	}
> +	return err;
> +
>   out_free:
>   	kfree(new_eba_tbl);
>   	return err;

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
  

Patch

diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
index 8fcc0bdf0635..74637575346e 100644
--- a/drivers/mtd/ubi/vmt.c
+++ b/drivers/mtd/ubi/vmt.c
@@ -464,7 +464,7 @@  int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
 		for (i = 0; i < -pebs; i++) {
 			err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
 			if (err)
-				goto out_acc;
+				goto out_free;
 		}
 		spin_lock(&ubi->volumes_lock);
 		ubi->rsvd_pebs += pebs;
@@ -512,6 +512,8 @@  int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
 		ubi->avail_pebs += pebs;
 		spin_unlock(&ubi->volumes_lock);
 	}
+	return err;
+
 out_free:
 	kfree(new_eba_tbl);
 	return err;