Skip to content

Commit

Permalink
iio: core: fix double free in iio_device_unregister_sysfs()
Browse files Browse the repository at this point in the history
I got the double free report:

[   68.308365][  T359] BUG: KASAN: double-free or invalid-free in kfree+0xce/0x390
[   68.309532][  T359]
[   68.309886][  T359] CPU: 0 PID: 359 Comm: xrun Tainted: G        W         5.15.0-rc3-00109-g4dfd49fafc4d-dirty torvalds#474 523b7f3c65c42247635e2ac04a95f61f9f36678d
[   68.312059][  T359] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
[   68.313566][  T359] Call Trace:
[   68.314063][  T359]  dump_stack_lvl+0xe2/0x152
[   68.314793][  T359]  print_address_description.constprop.7+0x21/0x150
[   68.315842][  T359]  ? kfree+0xce/0x390
[   68.316444][  T359]  kasan_report_invalid_free+0x6f/0xa0
[   68.317289][  T359]  ? kfree+0xce/0x390
[   68.317902][  T359]  __kasan_slab_free+0x125/0x140
[   68.318660][  T359]  slab_free_freelist_hook+0x10d/0x240
[   68.319497][  T359]  ? iio_device_unregister_sysfs+0x108/0x13b [industrialio]
[   68.321179][  T359]  kfree+0xce/0x390
[   68.321781][  T359]  iio_device_unregister_sysfs+0x108/0x13b [industrialio]
[   68.323438][  T359]  iio_dev_release+0x9e/0x10e [industrialio]
[   68.324902][  T359]  ? iio_device_unregister_sysfs+0x13b/0x13b [industrialio]
[   68.326550][  T359]  device_release+0xa5/0x240
[   68.327258][  T359]  kobject_put+0x1e5/0x540
[   68.327954][  T359]  put_device+0x20/0x30
[   68.328612][  T359]  devm_iio_device_release+0x21/0x30 [industrialio]
[   68.330172][  T359]  release_nodes+0xc3/0x3b0
[   68.330874][  T359]  ? __sanitizer_cov_trace_pc+0x1d/0x50
[   68.331765][  T359]  ? _raw_spin_unlock_irqrestore+0x4b/0x5d
[   68.332668][  T359]  ? trace_hardirqs_on+0x63/0x2d0
[   68.333509][  T359]  devres_release_group+0x1da/0x2c0
[   68.334325][  T359]  ? release_nodes+0x3b0/0x3b0
[   68.335069][  T359]  ? __devm_iio_device_register+0x36/0x80 [industrialio]
[   68.336721][  T359]  ? max517_probe+0x3df/0x6b0 [max517]
[   68.338122][  T359]  i2c_device_probe+0x628/0xbb0
[   68.338886][  T359]  ? i2c_device_match+0x110/0x110
[   68.339674][  T359]  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: 32f1717 ("iio: core: rework iio device group creation")
Reported-by: Hulk Robot <[email protected]>
Signed-off-by: Yang Yingliang <[email protected]>
  • Loading branch information
Yang Yingliang authored and intel-lab-lkp committed Oct 9, 2021
1 parent 4c2d124 commit 28455a0
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions drivers/iio/industrialio-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 28455a0

Please sign in to comment.