mtd: use put_device() if device_register fail
Subsystems (2)
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
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
@@ -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
PATCH 2/2 mtd: ubi: use put_device() if device_register fail
In reply to Arvind Yadavif 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
@@ -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
Richard Weinberger
maintainerAm Freitag, 9. März 2018, 11:50:47 CET schrieb Arvind Yadav:
Quoted 6 lines
if
device_register()returned an error! Always useput_device()to give up the reference initialized.Arvind Yadav (2):
[PATCH 1/2] mtd: useput_device()ifdevice_registerfail [PATCH 2/2] mtd: ubi: useput_device()ifdevice_registerfail
Uhh, this is not obvious. Does device_register() really always return with a reference held in all (error) cases?
Thanks,
//richard
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 useput_device()to give up the reference initialized.
Quoted 8 lines
Quoted 3 lines
Arvind Yadav (2):
[PATCH 1/2] mtd: useput_device()ifdevice_registerfail [PATCH 2/2] mtd: ubi: useput_device()ifdevice_registerfailUhh, 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
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 useput_device()to give up the reference initialized.Arvind Yadav (2):
[PATCH 1/2] mtd: useput_device()ifdevice_registerfail [PATCH 2/2] mtd: ubi: useput_device()ifdevice_registerfailUhh, 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,
//richardif
device_register()returned an error! Always useput_device()to give up the reference initialized.(-- Please see the comment fordevice_register()).put_device()is able to handle those case where it'll not return a reference.
You are right.
For both patches:
Thanks,
//richard
Quoted 13 lines · On Fri, 9 Mar 2018 16:20:48 +0530 Arvind Yadav <[email protected]>…
if
device_register()returned an error! Always useput_device()to give up the reference initialized.Signed-off-by Arvind Yadav <[email protected]>--- drivers/mtd/mtdcore.c | 1 + 1 file changed, 1 insertion(+)drivers/mtd/mtdcore.c +0
-0
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
-0of_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
-0idr_remove (&mtd_idr, i);
fail_locked:
Signature · 3 lines
Boris Brezillon, Bootlin (formerly Free Electrons) Embedded Linux and Kernel engineering https://bootlin.com
Re: [PATCH 2/2] mtd: ubi: use put_device() if device_register fail
In reply to Arvind YadavQuoted 13 lines · On Fri, 9 Mar 2018 16:20:49 +0530 Arvind Yadav <[email protected]>…
if
device_register()returned an error! Always useput_device()to give up the reference initialized.Signed-off-by Arvind Yadav <[email protected]>--- drivers/mtd/ubi/vmt.c | 1 + 1 file changed, 1 insertion(+)drivers/mtd/ubi/vmt.c +0
-0
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
-0return err; }
Signature · 3 lines
Boris Brezillon, Bootlin (formerly Free Electrons) Embedded Linux and Kernel engineering https://bootlin.com
Richard Weinberger
maintainerRe: [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 useput_device()to give up the reference initialized.Signed-off-by Arvind Yadav <[email protected]>---
drivers/mtd/ubi/vmt.c | 1 + 1 file changed, 1 insertion(+)drivers/mtd/ubi/vmt.c +0
-0@@ -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
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 useput_device()to give up the reference initialized.Signed-off-by Arvind Yadav <[email protected]>---
drivers/mtd/ubi/vmt.c | 1 + 1 file changed, 1 insertion(+)drivers/mtd/ubi/vmt.c +0
-0@@ -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 thatput_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
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 useput_device()to give up the reference initialized.Signed-off-by Arvind Yadav <[email protected]>--- drivers/mtd/mtdcore.c | 1 + 1 file changed, 1 insertion(+)drivers/mtd/mtdcore.c +0
-0@@ -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
-0fail_added: +put_device (&mtd->dev);Not sure this is a good idea: the
put_device()call will trigger anmtd_devtype->release(), which will in turn calldevice_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
-0of_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 theof_node_put()and the belowidr_remove()call in the ->release() hook if you want to useput_device().Quoted 1 line
drivers/mtd/mtdcore.c +0
-0idr_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
-0fail_locked:
~arvind
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 theof_node_put()and the belowidr_remove()call in the ->release() hook if you want to useput_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/
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 theof_node_put()and the belowidr_remove()call in the ->release() hook if you want to useput_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.htmlwe 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