diff mbox series

[v2] iio: core: fix double free in iio_device_unregister_sysfs()

Message ID 20211011092028.2310144-1-yangyingliang@huawei.com (mailing list archive)
State Superseded
Headers show
Series [v2] iio: core: fix double free in iio_device_unregister_sysfs() | expand

Commit Message

Yang Yingliang Oct. 11, 2021, 9:20 a.m. UTC
I got the double free report:

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

CPU: 0 PID: 359 Comm: xrun Tainted: G        W         5.15.0-rc3-00109-g4dfd49fafc4d-dirty #474 523b7f3c65c42247635e2ac04a95f61f9f36678d
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
 dump_stack_lvl+0xe2/0x152
 print_address_description.constprop.7+0x21/0x150
 kasan_report_invalid_free+0x6f/0xa0
 __kasan_slab_free+0x125/0x140
 slab_free_freelist_hook+0x10d/0x240
 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]
 release_nodes+0xc3/0x3b0
 devres_release_group+0x1da/0x2c0
 i2c_device_probe+0x628/0xbb0
 really_probe+0x285/0xc30

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")
Reported-by: Hulk Robot <hulkci@huawei.com>
Reviewed-by: Alexandru Ardelean <ardeleanalex@gmail.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/iio/industrialio-core.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Andy Shevchenko Oct. 12, 2021, 8:27 p.m. UTC | #1
On Mon, Oct 11, 2021 at 12:12 PM Yang Yingliang
<yangyingliang@huawei.com> wrote:
>
> I got the double free report:
>
> BUG: KASAN: double-free or invalid-free in kfree+0xce/0x390
>
> CPU: 0 PID: 359 Comm: xrun Tainted: G        W         5.15.0-rc3-00109-g4dfd49fafc4d-dirty #474 523b7f3c65c42247635e2ac04a95f61f9f36678d
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
> Call Trace:
>  dump_stack_lvl+0xe2/0x152
>  print_address_description.constprop.7+0x21/0x150
>  kasan_report_invalid_free+0x6f/0xa0
>  __kasan_slab_free+0x125/0x140
>  slab_free_freelist_hook+0x10d/0x240
>  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]
>  release_nodes+0xc3/0x3b0
>  devres_release_group+0x1da/0x2c0
>  i2c_device_probe+0x628/0xbb0
>  really_probe+0x285/0xc30

Please, reduce this noise to the ~4-5 important lines only!
diff mbox series

Patch

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 2dbb37e09b8c..2dc837db50f7 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -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)