Skip to contentSkip to conversation

mtd: fix use-after-free in mtd release

PATCH Superseded Alexander Usyskin 5 participants 9 messages Jul 27, 2023 – Aug 3, 2023 Memory technology devices (MTD)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 works 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 <[email protected]>
Cc Zhang Xiaoxu <[email protected]>
Fixes 19bfa9ebebb5 ("mtd: use refcount to prevent corruption")
---
 drivers/mtd/mtdcore.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/mtd/mtdcore.c +11 -5
index 2466ea466466..46f15f676491 100644
@@ -93,6 +93,9 @@ static void mtd_release(struct device *dev)
 	struct mtd_info *mtd = dev_get_drvdata(dev);
 	dev_t index = MTD_DEVT(mtd->index);
 
+	idr_remove(&mtd_idr, mtd->index);
+	of_node_put(mtd_get_of_node(mtd));
+
 	if (mtd_is_partition(mtd))
 		release_mtd_partition(mtd);
 
@@ -103,6 +106,7 @@ static void mtd_release(struct device *dev)
 static void mtd_device_release(struct kref *kref)
 {
 	struct mtd_info *mtd = container_of(kref, struct mtd_info, refcnt);
+	bool is_partition = mtd_is_partition(mtd);
 
 	debugfs_remove_recursive(mtd->dbg.dfs_dir);
 
@@ -111,11 +115,13 @@ static void mtd_device_release(struct kref *kref)
 
 	device_unregister(&mtd->dev);
 
-	/* Clear dev so mtd can be safely re-registered later if desired */
-	memset(&mtd->dev, 0, sizeof(mtd->dev));
-
-	idr_remove(&mtd_idr, mtd->index);
-	of_node_put(mtd_get_of_node(mtd));
+	/*
+	 *  Clear dev so mtd can be safely re-registered later if desired.
+	 *  Should not be done for partition,
+	 *  as it was already destroyed in device_unregister().
+	 */
+	if (!is_partition)
+		memset(&mtd->dev, 0, sizeof(mtd->dev));
 
 	module_put(THIS_MODULE);
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line · Alexander Usyskin

I case of partition device_unregister in mtd_device_release

In

device_unregister()
mtd_device_release()

Quoted 1 line

calls mtd_release which frees mtd_info structure for partition.

mtd_release()

Quoted 1 line

All code after device_unregister in mtd_device_release thus

device_unregister()
mtd_device_release()

Quoted 1 line

works already freed memory.

uses?

Quoted 1 line

Move part of code to mtd_release and restict mtd->dev cleanup

mtd_release()

Quoted 7 lines

to non-partion object.
For partition object such cleanup have no sense as partition mtd_info is removed.

Cc Miquel Raynal <[email protected]>
Cc Zhang Xiaoxu <[email protected]>
Fixes 19bfa9ebebb5 ("mtd: use refcount to prevent corruption")
Closes ?
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw

Hi Andy,

[email protected] wrote on Thu, 27 Jul 2023 18:12:04 +0300:

Quoted 17 lines · Alexander Usyskin
Quoted 1 line

I case of partition device_unregister in mtd_device_release

In

device_unregister()
mtd_device_release()

Quoted 1 line

calls mtd_release which frees mtd_info structure for partition.

mtd_release()

Quoted 1 line

All code after device_unregister in mtd_device_release thus

device_unregister()
mtd_device_release()

Quoted 7 lines
Quoted 1 line

works already freed memory.

uses?

Quoted 1 line

Move part of code to mtd_release and restict mtd->dev cleanup

mtd_release()

Yup, thanks for all these suggestions, I agree with them.

Quoted 9 lines
Quoted 7 lines

to non-partion object.
For partition object such cleanup have no sense as partition mtd_info is removed.

Cc Miquel Raynal <[email protected]>
Cc Zhang Xiaoxu <[email protected]>
Fixes 19bfa9ebebb5 ("mtd: use refcount to prevent corruption")
Closes ?

Did I miss a recent update on the use of Fixes? I thought Closes was supposed to point at a bug report while Fixes would point to the faulty commit. Right now I feel like Fixes is the right tag, but if you have a source explaining why we should not longer do it like I am used to, I would appreciate a link.

Thanks,
Miquèl

#3 lore raw
Quoted 3 lines · Miquel Raynal

[email protected] wrote on Thu, 27 Jul 2023 18:12:04 +0300:

Quoted 1 line · Alexander Usyskin

...

Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Fixes 19bfa9ebebb5 ("mtd: use refcount to prevent corruption")
Closes ?

Did I miss a recent update on the use of Fixes?

They are orthogonal to each other. Actually Closes goes closer with Reported-by.

I believe both of them needs to be added (by I might miss something).

Quoted 3 lines

I thought Closes was
supposed to point at a bug report while Fixes would point to the faulty commit.

Correct.

Quoted 1 line

Right now I feel like Fixes is the right tag,

Nobody objects that (see above).

Quoted 2 lines

but if you have a source explaining why we should not longer do it like I am used to, I would appreciate a link.

Since you know about Closes already, I think there is nothing to add.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#4 lore raw

Hi Andy,

[email protected] wrote on Thu, 27 Jul 2023 18:58:29 +0300:

Quoted 24 lines · Miquel Raynal
Quoted 3 lines

[email protected] wrote on Thu, 27 Jul 2023 18:12:04 +0300:

Quoted 1 line · Alexander Usyskin

...

Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Fixes 19bfa9ebebb5 ("mtd: use refcount to prevent corruption")
Closes ?

Did I miss a recent update on the use of Fixes?

They are orthogonal to each other. Actually Closes goes closer with Reported-by.

I believe both of them needs to be added (by I might miss something).

Quoted 3 lines

I thought Closes was
supposed to point at a bug report while Fixes would point to the faulty commit.

Correct.

Quoted 8 lines
Quoted 1 line

Right now I feel like Fixes is the right tag,

Nobody objects that (see above).

Quoted 2 lines

but if you have a source explaining why we should not longer do it like I am used to, I would appreciate a link.

Since you know about Closes already, I think there is nothing to add.

Ah sorry I misunderstood your first e-mail. I thought you were suggesting to replace Fixes by Closes. Sorry for the misunderstanding :)

Thanks,
Miquèl

#5 lore raw
Quoted 37 lines

Hi Andy,

[email protected] wrote on Thu, 27 Jul 2023 18:58:29 +0300:

Quoted 31 lines · Miquel Raynal
Quoted 3 lines

[email protected] wrote on Thu, 27 Jul 2023 18:12:04 +0300:

Quoted 1 line · Alexander Usyskin

...

Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Fixes 19bfa9ebebb5 ("mtd: use refcount to prevent corruption")
Closes ?

Did I miss a recent update on the use of Fixes?

They are orthogonal to each other. Actually Closes goes closer with Reported-by.

I believe both of them needs to be added (by I might miss something).

Quoted 3 lines

I thought Closes was
supposed to point at a bug report while Fixes would point to the faulty commit.

Correct.

Quoted 1 line

Right now I feel like Fixes is the right tag,

Nobody objects that (see above).

Quoted 2 lines

but if you have a source explaining why we should not longer do it like I am used to, I would appreciate a link.

Quoted 7 lines
Quoted 1 line

Since you know about Closes already, I think there is nothing to add.

Ah sorry I misunderstood your first e-mail. I thought you were suggesting to replace Fixes by Closes. Sorry for the misunderstanding :)

Thanks,
Miquèl

Miquel, is this patch helps with your original problem of devices not freed?

Zhang, is this patch helps with your problem with KAsan?

Signature · 2 lines
Thanks,
Sasha
#6 lore raw
Quoted 3 lines · Alexander

Miquel, is this patch helps with your original problem of devices not freed?

Zhang, is this patch helps with your problem with KAsan?

After this patch applied, the problem can still be reproduced.

maintainer
#7 lore raw

Hi zhang,

[email protected] wrote on Mon, 31 Jul 2023 09:35:42 +0800:

Quoted 5 lines · Alexander
Quoted 3 lines

Miquel, is this patch helps with your original problem of devices not freed?

Zhang, is this patch helps with your problem with KAsan?

After this patch applied, the problem can still be reproduced.

Did you test my patch as well? Does Kasan still complain with it?

Thanks,
Miquèl

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

#8 lore raw
Quoted 11 lines · Miquel Raynal

Hi zhang,

[email protected] wrote on Mon, 31 Jul 2023 09:35:42 +0800:

Quoted 5 lines · Alexander
Quoted 3 lines

Miquel, is this patch helps with your original problem of devices not freed?

Zhang, is this patch helps with your problem with KAsan?

After this patch applied, the problem can still be reproduced.

Did you test my patch as well? Does Kasan still complain with it?

After this patch applied, the problem can still be reproduced.

Quoted 3 lines

Thanks,
Miquèl

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