Skip to contentSkip to conversation

iio: core: fix double free in iio_device_unregister_sysfs()

PATCH Applied Yang Yingliang 2 participants 2 messages Oct 13, 2021 – Oct 17, 2021 2 messages not in the archive IIO

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iio: core: fix double free in iio_device_unregister_sysfs()

I got the double free report:

BUG: KASAN: double-free or invalid-free in kfree+0xce/0x390
 kfree+0xce/0x390
 iio_device_unregister_sysfs+0x108/0x13b [industrialio]
 iio_dev_release+0x9e/0x10e [industrialio]
 device_release+0xa5/0x240
 kobject_put+0x1e5/0x540
 put_device+0x20/0x30
 devm_iio_device_release+0x21/0x30 [industrialio]

If __iio_device_register() fails, iio_dev_opaque->groups will be freed in error path in iio_device_unregister_sysfs(), then iio_dev_release() will call iio_device_unregister_sysfs() again, it causes double free. Set iio_dev_opaque->groups to NULL when it's freed to fix this double free.

Fixes 32f171724e5c ("iio: core: rework iio device group creation")

---
v1 -> v3:
reduce some backtrace in commit message

---
 drivers/iio/industrialio-core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iio/industrialio-core.c +1 -0
index 2dbb37e09b8c..2dc837db50f7 100644
@@ -1600,6 +1600,7 @@ static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
 	kfree(iio_dev_opaque->chan_attr_group.attrs);
 	iio_dev_opaque->chan_attr_group.attrs = NULL;
 	kfree(iio_dev_opaque->groups);
+	iio_dev_opaque->groups = NULL;
 }
 
 static void iio_dev_release(struct device *device)
Signature · 1 line
2.25.1

A message that is not in the archive (sent off-list or not yet received): CAHp75VeyQYmKybQwWLmM2QxVQXomrUH0RttRguzRyoWXtc3TFA@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAHp75VdH2CkY-e6P6QvMzDXK6F9boxz4Vb5trwmMoPOCTmkjww@mail.gmail.com

maintainer Applied
#3 lore raw
Quoted 30 lines · On Wed, 13 Oct 2021 08:51:38 +0300 Andy Shevchenko <[email protected]>…
Quoted 27 lines · Andy Shevchenko
Quoted 7 lines · Yang Yingliang

I got the double free report:

BUG: KASAN: double-free or invalid-free in kfree+0xce/0x390  


 
 kfree+0xce/0x390  

This line also redundant, but no need to resend, I hope Jonathan can do it for you.

Quoted 5 lines
 iio_device_unregister_sysfs+0x108/0x13b [industrialio]
 iio_dev_release+0x9e/0x10e [  
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
 device_release+0xa5/0x240
 kobject_put+0x1e5/0x540
 put_device+0x20/0x30
 devm_iio_device_release+0x21/0x30 [industrialio]  

Actually above lines are also part of the noise

Not sure I'm quite so keen to strip everything back completely (though I do agree keeping it fairly minimal is good.

Anyhow, tidied it up a bit and applied. I also added a note that more radical surgery is needed to clean up the lifetime management but in the meantime this deals with the symptoms.

Applied to the fixes-togreg branch of iio.git and marked for stable.

Thanks,

Jonathan

Quoted 81 lines
Quoted 80 lines
Quoted 33 lines

If __iio_device_register() fails, iio_dev_opaque->groups will be freed in error path in iio_device_unregister_sysfs(), then iio_dev_release() will call iio_device_unregister_sysfs() again, it causes double free. Set iio_dev_opaque->groups to NULL when it's freed to fix this double free.

Fixes 32f171724e5c ("iio: core: rework iio device group creation")

---
v1 -> v3:
reduce some backtrace in commit message

---
 drivers/iio/industrialio-core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iio/industrialio-core.c +1 -0
index 2dbb37e09b8c..2dc837db50f7 100644
@@ -1600,6 +1600,7 @@ static void iio_device_unregister_sysfs(struct
 iio_dev *indio_dev)
        kfree(iio_dev_opaque->chan_attr_group.attrs);
        iio_dev_opaque->chan_attr_group.attrs = NULL;
        kfree(iio_dev_opaque->groups);
+       iio_dev_opaque->groups = NULL;
 }
 
Quoted 16 lines

static void iio_dev_release(struct device *device)
--
2.25.1

--
With Best Regards,
Andy Shevchenko