Skip to contentSkip to conversation

iio: trigger: fix use-after-free in viio_trigger_alloc()

PATCH Superseded Salah Triki 4 participants 8 messages Jan 31 – Feb 4 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: trigger: fix use-after-free in viio_trigger_alloc()

Once device_initialize() is called, the reference count of the device is set to 1. The memory associated with the device must then be managed by the kobject reference counting.

In viio_trigger_alloc(), if irq_alloc_descs() or kvasprintf() fails, the code currently calls kfree(). Using kfree() in this case bypasses the device's release callback and can lead to a use-after-free or memory corruption.

Fix this by calling put_device() instead of kfree(). This ensures that the memory is freed properly via iio_trig_release() when the reference count drops to zero.

Fixes 2c99f1a09da3d ("iio: trigger: clean up viio_trigger_alloc()")
---
 drivers/iio/industrialio-trigger.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-trigger.c +1 -1
index 54416a384232..981e19757870 100644
@@ -597,7 +597,7 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
 free_descs:
 	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
 free_trig:
-	kfree(trig);
+	put_device(&trig->dev);
 	return NULL;
 }
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw

On Sat, 31 Jan 2026 10:23:33 +0100
Salah Triki <[email protected]> wrote:

Hi Salah,

This is a definitely case of the fix not being anywhere as simple as it might look at first glance.

Quoted 8 lines

Once device_initialize() is called, the reference count of the device is set to 1. The memory associated with the device must then be managed by the kobject reference counting.

In viio_trigger_alloc(), if irq_alloc_descs() or kvasprintf() fails, the code currently calls kfree(). Using kfree() in this case bypasses the device's release callback and can lead to a use-after-free or memory corruption.

In some cases yes it can cause problems, but please show me an actual path to this in the description. It should indeed be tidied up.

Quoted 4 lines

Fix this by calling put_device() instead of kfree(). This ensures that the memory is freed properly via iio_trig_release() when the reference count drops to zero.

This change is not sufficient and causes some cleanup to happen twice thus introducing some bugs that weren't there before.
So take another look.

Quoted 3 lines
Fixes 2c99f1a09da3d ("iio: trigger: clean up viio_trigger_alloc()")

No blank line here. Scripts that commonly run on the kernel tree rely on the the tags block having no blank lines in it to avoid false positives.

Quoted 18 lines
---
 drivers/iio/industrialio-trigger.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-trigger.c +1 -1
index 54416a384232..981e19757870 100644
@@ -597,7 +597,7 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
 free_descs:
 	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
 free_trig:
-	kfree(trig);
+	put_device(&trig->dev);
 	return NULL;
 }
 
#2 lore raw

Hi all,

Quoted 12 lines · Jonathan Cameron

On Sat, 31 Jan 2026 10:23:33 +0100
Salah Triki <[email protected]> wrote:

Hi Salah,

This is a definitely case of the fix not being anywhere as simple as it might look at first glance.

Quoted 4 lines

Once device_initialize() is called, the reference count of the device is set to 1. The memory associated with the device must then be managed by the kobject reference counting.

Quoted 13 lines
Quoted 4 lines

In viio_trigger_alloc(), if irq_alloc_descs() or kvasprintf() fails, the code currently calls kfree(). Using kfree() in this case bypasses the device's release callback and can lead to a use-after-free or memory corruption.

In some cases yes it can cause problems, but please show me an actual path to this in the description. It should indeed be tidied up.

Quoted 4 lines

Fix this by calling put_device() instead of kfree(). This ensures that the memory is freed properly via iio_trig_release() when the reference count drops to zero.

Not the first time this pops up and I actually thought it was already fixed. But it seems we never got v5:

https://lore.kernel.org/linux-iio/[email protected]/

Andy already fixed it for the main iio_dev allocation:

https://lore.kernel.org/linux-iio/[email protected]/

- Nuno Sá

Quoted 1 line
maintainer
#3 lore raw
Quoted 25 lines · On Mon, 02 Feb 2026 10:12:10 +0000 Nuno Sá <[email protected]> wrote:

Hi all,

Quoted 22 lines · Jonathan Cameron

On Sat, 31 Jan 2026 10:23:33 +0100
Salah Triki <[email protected]> wrote:

Hi Salah,

This is a definitely case of the fix not being anywhere as simple as it might look at first glance.

Quoted 8 lines

Once device_initialize() is called, the reference count of the device is set to 1. The memory associated with the device must then be managed by the kobject reference counting.

In viio_trigger_alloc(), if irq_alloc_descs() or kvasprintf() fails, the code currently calls kfree(). Using kfree() in this case bypasses the device's release callback and can lead to a use-after-free or memory corruption.

In some cases yes it can cause problems, but please show me an actual path to this in the description. It should indeed be tidied up.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

Fix this by calling put_device() instead of kfree(). This ensures that the memory is freed properly via iio_trig_release() when the reference count drops to zero.

Not the first time this pops up and I actually thought it was already fixed. But it seems we never got v5:

https://lore.kernel.org/linux-iio/[email protected]/

I thought so too :(

Sadly my tracking doesn't really extend to checking that a final version shows up and I have the memory of a goldfish.

J

Quoted 7 lines

Andy already fixed it for the main iio_dev allocation:

https://lore.kernel.org/linux-iio/[email protected]/

- Nuno Sá

Quoted 1 line
#4 lore raw
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Mon, 02 Feb 2026 10:12:10 +0000 Nuno Sá <[email protected]> wrote:

On Sat, 2026-01-31 at 12:44 +0000, Jonathan Cameron wrote:

...

Quoted 3 lines
Quoted 3 lines

Andy already fixed it for the main iio_dev allocation:

https://lore.kernel.org/linux-iio/[email protected]/

Right, and this thread makes me check the status and the series wasn't applied. Now I need to go to that thread to see why.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Mon, 02 Feb 2026 10:12:10 +0000 Nuno Sá <[email protected]> wrote:

On Sat, 2026-01-31 at 12:44 +0000, Jonathan Cameron wrote:

...

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Andy already fixed it for the main iio_dev allocation:

https://lore.kernel.org/linux-iio/[email protected]/

Right, and this thread makes me check the status and the series wasn't applied. Now I need to go to that thread to see why.

And there it was mentioned as being applied... Confused.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Mon, 02 Feb 2026 10:12:10 +0000 Nuno Sá <[email protected]> wrote:

On Sat, 2026-01-31 at 12:44 +0000, Jonathan Cameron wrote:

...

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Andy already fixed it for the main iio_dev allocation:

https://lore.kernel.org/linux-iio/[email protected]/

Right, and this thread makes me check the status and the series wasn't applied. Now I need to go to that thread to see why.

And there it was mentioned as being applied... Confused.

Ah, I have some leftovers, need to clean my local tree. Sorry for the noise.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
In reply to Jonathan Cameron
Quoted 30 lines · Jonathan Cameron

On Sat, 31 Jan 2026 10:23:33 +0100
Salah Triki <[email protected]> wrote:

Hi Salah,

This is a definitely case of the fix not being anywhere as simple as it might look at first glance.

Quoted 8 lines

Once device_initialize() is called, the reference count of the device is set to 1. The memory associated with the device must then be managed by the kobject reference counting.

In viio_trigger_alloc(), if irq_alloc_descs() or kvasprintf() fails, the code currently calls kfree(). Using kfree() in this case bypasses the device's release callback and can lead to a use-after-free or memory corruption.

In some cases yes it can cause problems, but please show me an actual path to this in the description. It should indeed be tidied up.

Quoted 4 lines

Fix this by calling put_device() instead of kfree(). This ensures that the memory is freed properly via iio_trig_release() when the reference count drops to zero.

This change is not sufficient and causes some cleanup to happen twice thus introducing some bugs that weren't there before.
So take another look.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines
Fixes 2c99f1a09da3d ("iio: trigger: clean up viio_trigger_alloc()")

No blank line here. Scripts that commonly run on the kernel tree rely on the the tags block having no blank lines in it to avoid false positives.

Hi Jonathan,

Thanks for the review!

You're right – my patch was incomplete and can lead to double cleanup between the error path and iio_trig_release(). I'll rework the error handling so that once device_initialize() has been called, all cleanup goes through put_device(), and resource freeing is centralized in the release callback.

I’ll send a v2 fixing the double-free issue, showing the error path and correcting the Fixes tag format.

Thanks!
Salah