Skip to contentSkip to conversation

mtd: use put_device() if device_register fail

PATCH Superseded Arvind Yadav 4 participants 13 messages Mar 9, 2018 – Mar 21, 2018 1 message not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover mtd: use put_device() if device_register fail

if device_register() returned an error! Always use put_device() to give up the reference initialized.

Arvind Yadav (2):
[PATCH 1/2] mtd: use put_device() if device_register fail [PATCH 2/2] mtd: ubi: use put_device() if device_register fail

 drivers/mtd/mtdcore.c | 1 +
 drivers/mtd/ubi/vmt.c | 1 +
 2 files changed, 2 insertions(+)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/2 mtd: use put_device() if device_register fail

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---
 drivers/mtd/mtdcore.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mtd/mtdcore.c +1 -0
index 28553c8..4d77ca2 100644
@@ -586,6 +586,7 @@ int add_mtd_device(struct mtd_info *mtd)
 	return 0;
 
 fail_added:
+	put_device(&mtd->dev);
 	of_node_put(mtd_get_of_node(mtd));
 	idr_remove(&mtd_idr, i);
 fail_locked:
Signature · 1 line
1.9.1
#2 lore raw
Quoted 13 lines · On Fri, 9 Mar 2018 16:20:48 +0530 Arvind Yadav <[email protected]>…

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---
 drivers/mtd/mtdcore.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mtd/mtdcore.c +0 -0
index 28553c8..4d77ca2 100644

drivers/mtd/mtdcore.c int add_mtd_device(struct mtd_info *mtd) · line 589

Quoted 4 lines
drivers/mtd/mtdcore.c +1 -0
@@ -586,6 +586,7 @@ int add_mtd_device(struct mtd_info *mtd)
 	return 0;
 
 fail_added:
+	put_device(&mtd->dev);

Not sure this is a good idea: the put_device() call will trigger an mtd_devtype->release(), which will in turn call device_destroy() on something that does not exist yet. Not sure if this is a real problem, but it does not look like the right thing to do.

drivers/mtd/mtdcore.c int add_mtd_device(struct mtd_info *mtd) · line 590

Quoted 1 line
drivers/mtd/mtdcore.c +0 -0
 	of_node_put(mtd_get_of_node(mtd));

You're referencing an object that is supposed to have been freed/released by the put_device() call. Again, it's not really a problem because in our case ->release() does not free the mtd object (as is usually done in other parts of the kernel), but it still looks wrong. It's probably better to move the of_node_put() and the below idr_remove() call in the ->release() hook if you want to use put_device().

Quoted 5 lines
drivers/mtd/mtdcore.c +0 -0
 	idr_remove(&mtd_idr, i);
 

fail_locked:

Signature · 3 lines
Boris Brezillon, Bootlin (formerly Free Electrons)
Embedded Linux and Kernel engineering
https://bootlin.com
author
#3 lore raw
Quoted 18 lines · Boris Brezillon
Quoted 15 lines · On Fri, 9 Mar 2018 16:20:48 +0530 Arvind Yadav <[email protected]>…

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---
  drivers/mtd/mtdcore.c | 1 +
  1 file changed, 1 insertion(+)
drivers/mtd/mtdcore.c +0 -0
index 28553c8..4d77ca2 100644
@@ -586,6 +586,7 @@ int add_mtd_device(struct mtd_info *mtd)
  	return 0;
 
Quoted 7 lines

drivers/mtd/mtdcore.c int add_mtd_device(struct mtd_info *mtd) · line 589

Quoted 2 lines
drivers/mtd/mtdcore.c +1 -0
  fail_added:
+	put_device(&mtd->dev);

Not sure this is a good idea: the put_device() call will trigger an mtd_devtype->release(), which will in turn call device_destroy() on something that does not exist yet. Not sure if this is a real problem, but it does not look like the right thing to do.

yes, you are correct. No need to call put_device(). which can cause other problem.

Quoted 10 lines

drivers/mtd/mtdcore.c int add_mtd_device(struct mtd_info *mtd) · line 590

Quoted 1 line
drivers/mtd/mtdcore.c +0 -0
  	of_node_put(mtd_get_of_node(mtd));

You're referencing an object that is supposed to have been freed/released by the put_device() call. Again, it's not really a problem because in our case ->release() does not free the mtd object (as is usually done in other parts of the kernel), but it still looks wrong. It's probably better to move the of_node_put() and the below idr_remove() call in the ->release() hook if you want to use put_device().

Quoted 1 line
drivers/mtd/mtdcore.c +0 -0
  	idr_remove(&mtd_idr, i);

Sure, we can move put_device() below this. But need to check how we can add hook in release.

Quoted 4 lines

drivers/mtd/mtdcore.c int add_mtd_device(struct mtd_info *mtd) · line 592

Quoted 2 lines
drivers/mtd/mtdcore.c +0 -0
  fail_locked:

~arvind

#4 lore raw
Quoted 9 lines · arvindY
Quoted 9 lines
Quoted 1 line

      of_node_put(mtd_get_of_node(mtd));

You're referencing an object that is supposed to have been freed/released by the put_device() call. Again, it's not really a problem because in our case ->release() does not free the mtd object (as is usually done in other parts of the kernel), but it still looks wrong. It's probably better to move the of_node_put() and the below idr_remove() call in the ->release() hook if you want to use put_device().

Quoted 3 lines
Quoted 1 line
Quoted 1 line

      idr_remove(&mtd_idr, i);

Sure, we can move put_device() below this. But need to check how we can add hook in release.

My guess is that you would need this:
http://lists.infradead.org/pipermail/linux-mtd/2017-May/074373.html

Martin

Quoted 9 lines
Quoted 3 lines
Quoted 1 line

  fail_locked:

~arvind

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

Hi Arvind,

Quoted 14 lines · arvindY
Quoted 11 lines · Martin Habets
Quoted 10 lines · arvindY
Quoted 10 lines
Quoted 1 line

      of_node_put(mtd_get_of_node(mtd));

You're referencing an object that is supposed to have been freed/released by the put_device() call. Again, it's not really a problem because in our case ->release() does not free the mtd object (as is usually done in other parts of the kernel), but it still looks wrong. It's probably better to move the of_node_put() and the below idr_remove() call in the ->release() hook if you want to use put_device().

Quoted 1 line

      idr_remove(&mtd_idr, i);

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Sure, we can move put_device() below this. But need to check how we can add hook in release.

My guess is that you would need this:
http://lists.infradead.org/pipermail/linux-mtd/2017-May/074373.html

we should not removes(device_destroy) a MTD device in release. MTD device should be removes when deleting(unregister) a MTD device.

No, deleting an MTD device should only decrement a refcounter. At this point there can still be other processes with a /dev/mtd* device open.
When there are no more users release gets called to remove it.

Quoted 2 lines

MTD device should decrement refcount of a node and Remove MTD from IDR in dev->release().

You could be right about this, I'm not sure.

My patch allows the caller to free the mtd_info memory. This is needed since the caller allocated the memory in the first place, and because the caller has no other of knowing that the last MTD user is gone.

Martin

#7 lore raw

PATCH 2/2 mtd: ubi: use put_device() if device_register fail

In reply to Arvind Yadav

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---
 drivers/mtd/ubi/vmt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mtd/ubi/vmt.c +1 -0
index 3fd8d7f..db85b68 100644
@@ -609,6 +609,7 @@ int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
 	return err;
 
 out_cdev:
+	put_device(&vol->dev);
 	cdev_del(&vol->cdev);
 	return err;
 }
Signature · 1 line
1.9.1
#8 lore raw

Re: [PATCH 2/2] mtd: ubi: use put_device() if device_register fail

Quoted 13 lines · On Fri, 9 Mar 2018 16:20:49 +0530 Arvind Yadav <[email protected]>…

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---
 drivers/mtd/ubi/vmt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mtd/ubi/vmt.c +0 -0
index 3fd8d7f..db85b68 100644

drivers/mtd/ubi/vmt.c int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol) · line 613

Quoted 5 lines
drivers/mtd/ubi/vmt.c +1 -0
@@ -609,6 +609,7 @@ int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
 	return err;
 
 out_cdev:
+	put_device(&vol->dev);
 	cdev_del(&vol->cdev);

use-after-free bug here: put_device() has freed the vol obj, and you're dereferencing the pointer just after that.

Quoted 2 lines
drivers/mtd/ubi/vmt.c +0 -0
 	return err;
 }
Signature · 3 lines
Boris Brezillon, Bootlin (formerly Free Electrons)
Embedded Linux and Kernel engineering
https://bootlin.com
#9 lore raw

Re: [PATCH 2/2] mtd: ubi: use put_device() if device_register fail

Am Mittwoch, 14. März 2018, 19:56:52 CET schrieb Boris Brezillon:

Quoted 20 lines

On Fri, 9 Mar 2018 16:20:49 +0530

Quoted 17 lines · Arvind Yadav

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---

 drivers/mtd/ubi/vmt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mtd/ubi/vmt.c +0 -0
index 3fd8d7f..db85b68 100644
@@ -609,6 +609,7 @@ int ubi_add_volume(struct ubi_device *ubi, struct

ubi_volume *vol)>
return err;

Quoted 7 lines

drivers/mtd/ubi/vmt.c int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol) · line 613

Quoted 4 lines

out_cdev:

drivers/mtd/ubi/vmt.c +1 -0
+	put_device(&vol->dev);
 
 	cdev_del(&vol->cdev);

use-after-free bug here: put_device() has freed the vol obj, and you're dereferencing the pointer just after that.

eeek, thanks for looking at more context.
Arvind, while you are right that put_device() is missing, please double check that freeing the devices is also correct.

Thanks,
//richard

#10 lore raw

Re: [PATCH 2/2] mtd: ubi: use put_device() if device_register fail

Quoted 27 lines · Richard Weinberger

Am Mittwoch, 14. März 2018, 19:56:52 CET schrieb Boris Brezillon:

Quoted 26 lines

On Fri, 9 Mar 2018 16:20:49 +0530

Quoted 18 lines · Arvind Yadav

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---

  drivers/mtd/ubi/vmt.c | 1 +
  1 file changed, 1 insertion(+)
drivers/mtd/ubi/vmt.c +0 -0
index 3fd8d7f..db85b68 100644
@@ -609,6 +609,7 @@ int ubi_add_volume(struct ubi_device *ubi, struct

ubi_volume *vol)>
return err;

out_cdev:

drivers/mtd/ubi/vmt.c int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol) · line 613

Quoted 3 lines
drivers/mtd/ubi/vmt.c +1 -0
+	put_device(&vol->dev);
 
  	cdev_del(&vol->cdev);

use-after-free bug here: put_device() has freed the vol obj, and you're dereferencing the pointer just after that.

Thanks Boris, to point out this error.

Quoted 6 lines

eeek, thanks for looking at more context.
Arvind, while you are right that put_device() is missing, please double check that freeing the devices is also correct.

Thanks,
//richard

Sorry for that. I will take care of this.

~arvind

#11 lore raw
In reply to Arvind Yadav

Am Freitag, 9. März 2018, 11:50:47 CET schrieb Arvind Yadav:

Quoted 6 lines

if device_register() returned an error! Always use put_device() to give up the reference initialized.

Arvind Yadav (2):
[PATCH 1/2] mtd: use put_device() if device_register fail [PATCH 2/2] mtd: ubi: use put_device() if device_register fail

Uhh, this is not obvious. Does device_register() really always return with a reference held in all (error) cases?

Thanks,
//richard

#12 lore raw
Quoted 4 lines · Richard Weinberger

Am Freitag, 9. März 2018, 11:50:47 CET schrieb Arvind Yadav:

Quoted 3 lines

if device_register() returned an error! Always use put_device() to give up the reference initialized.

Quoted 8 lines
Quoted 3 lines

Arvind Yadav (2):
[PATCH 1/2] mtd: use put_device() if device_register fail [PATCH 2/2] mtd: ubi: use put_device() if device_register fail

Uhh, this is not obvious. Does device_register() really always return with a reference held in all (error) cases?

Thanks,
//richard

if device_register() returned an error! Always use put_device() to give up the reference initialized.(-- Please see the comment for device_register() ). put_device() is able to handle those case where it'll not return a reference.

~arvind

maintainer Acked-by
#13 lore raw

Arvind,

Am Montag, 12. März 2018, 06:51:24 CET schrieb Arvind Yadav:

Quoted 12 lines · Richard Weinberger
Quoted 11 lines

Am Freitag, 9. März 2018, 11:50:47 CET schrieb Arvind Yadav:

Quoted 6 lines

if device_register() returned an error! Always use put_device() to give up the reference initialized.

Arvind Yadav (2):
[PATCH 1/2] mtd: use put_device() if device_register fail [PATCH 2/2] mtd: ubi: use put_device() if device_register fail

Uhh, this is not obvious. Does device_register() really always return with a reference held in all (error) cases?

Quoted 7 lines
Quoted 2 lines

Thanks,
//richard

if device_register() returned an error! Always use put_device() to give up the reference initialized.(-- Please see the comment for device_register() ). put_device() is able to handle those case where it'll not return a reference.

You are right.

For both patches:

Thanks,
//richard