mtd: fix use-after-free in mtd release
authorAlexander Usyskin <alexander.usyskin@intel.com>
Mon, 31 Jul 2023 11:58:36 +0000 (14:58 +0300)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Fri, 4 Aug 2023 07:01:31 +0000 (09:01 +0200)
commite9714c22c1a8238a85d069b1517941fc723312f7
tree17c305b0aff03f1165e18af417029205bfaa8424
parentd2236f6219fac62ee22d971e06a04148f4eb8cca
mtd: fix use-after-free in mtd release

I case of partition device_unregister() in mtd_device_release()
calls mtd_release() which frees mtd_info structure for partition.
All code after device_unregister in mtd_device_release thus
uses already freed memory.

Move part of code to mtd_release() and restict mtd->dev cleanup
to non-partion object.
For partition object such cleanup have no sense as partition
mtd_info is removed.

Cc: Miquel Raynal <miquel.raynal@bootlin.com>
Cc: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>
Fixes: 19bfa9ebebb5 ("mtd: use refcount to prevent corruption")
Reviewed-by: Tomas Winkler <tomas.winkler@intel.com>
Signed-off-by: Alexander Usyskin <alexander.usyskin@intel.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20230731115836.542747-1-alexander.usyskin@intel.com
drivers/mtd/mtdcore.c