* [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc()
@ 2026-01-31 9:23 Salah Triki
2026-01-31 12:44 ` Jonathan Cameron
0 siblings, 1 reply; 8+ messages in thread
From: Salah Triki @ 2026-01-31 9:23 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko
Cc: linux-iio, linux-kernel, Salah Triki
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()")
Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
drivers/iio/industrialio-trigger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 54416a384232..981e19757870 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -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.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc()
2026-01-31 9:23 [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc() Salah Triki
@ 2026-01-31 12:44 ` Jonathan Cameron
2026-02-02 10:12 ` Nuno Sá
2026-02-04 20:03 ` Salah Triki
0 siblings, 2 replies; 8+ messages in thread
From: Jonathan Cameron @ 2026-01-31 12:44 UTC (permalink / raw)
To: Salah Triki
Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
linux-kernel
On Sat, 31 Jan 2026 10:23:33 +0100
Salah Triki <salah.triki@gmail.com> wrote:
Hi Salah,
This is a definitely case of the fix not being anywhere as simple
as it might look at first glance.
> 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.
>
> 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.
>
> 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.
> Signed-off-by: Salah Triki <salah.triki@gmail.com>
> ---
> drivers/iio/industrialio-trigger.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
> index 54416a384232..981e19757870 100644
> --- a/drivers/iio/industrialio-trigger.c
> +++ b/drivers/iio/industrialio-trigger.c
> @@ -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;
> }
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc()
2026-01-31 12:44 ` Jonathan Cameron
@ 2026-02-02 10:12 ` Nuno Sá
2026-02-02 20:52 ` Jonathan Cameron
2026-02-04 20:03 ` Salah Triki
1 sibling, 1 reply; 8+ messages in thread
From: Nuno Sá @ 2026-02-02 10:12 UTC (permalink / raw)
To: Jonathan Cameron, Salah Triki
Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
linux-kernel
Hi all,
On Sat, 2026-01-31 at 12:44 +0000, Jonathan Cameron wrote:
> On Sat, 31 Jan 2026 10:23:33 +0100
> Salah Triki <salah.triki@gmail.com> wrote:
>
> Hi Salah,
>
> This is a definitely case of the fix not being anywhere as simple
> as it might look at first glance.
>
> > 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.
>
> >
> > 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/20251110035838.37029-1-make24@iscas.ac.cn/
Andy already fixed it for the main iio_dev allocation:
https://lore.kernel.org/linux-iio/20251112145735.2075527-3-andriy.shevchenko@linux.intel.com/
- Nuno Sá
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc()
2026-02-02 10:12 ` Nuno Sá
@ 2026-02-02 20:52 ` Jonathan Cameron
2026-02-03 11:18 ` Andy Shevchenko
0 siblings, 1 reply; 8+ messages in thread
From: Jonathan Cameron @ 2026-02-02 20:52 UTC (permalink / raw)
To: Nuno Sá
Cc: Salah Triki, David Lechner, Nuno Sá, Andy Shevchenko,
linux-iio, linux-kernel
On Mon, 02 Feb 2026 10:12:10 +0000
Nuno Sá <noname.nuno@gmail.com> wrote:
> Hi all,
>
> On Sat, 2026-01-31 at 12:44 +0000, Jonathan Cameron wrote:
> > On Sat, 31 Jan 2026 10:23:33 +0100
> > Salah Triki <salah.triki@gmail.com> wrote:
> >
> > Hi Salah,
> >
> > This is a definitely case of the fix not being anywhere as simple
> > as it might look at first glance.
> >
> > > 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.
>
> >
> > >
> > > 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/20251110035838.37029-1-make24@iscas.ac.cn/
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
>
> Andy already fixed it for the main iio_dev allocation:
>
> https://lore.kernel.org/linux-iio/20251112145735.2075527-3-andriy.shevchenko@linux.intel.com/
>
> - Nuno Sá
> >
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc()
2026-02-02 20:52 ` Jonathan Cameron
@ 2026-02-03 11:18 ` Andy Shevchenko
2026-02-03 11:20 ` Andy Shevchenko
0 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2026-02-03 11:18 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Nuno Sá, Salah Triki, David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel
On Mon, Feb 02, 2026 at 08:52:17PM +0000, Jonathan Cameron wrote:
> On Mon, 02 Feb 2026 10:12:10 +0000
> Nuno Sá <noname.nuno@gmail.com> wrote:
> > On Sat, 2026-01-31 at 12:44 +0000, Jonathan Cameron wrote:
...
> > Andy already fixed it for the main iio_dev allocation:
> >
> > https://lore.kernel.org/linux-iio/20251112145735.2075527-3-andriy.shevchenko@linux.intel.com/
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.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc()
2026-02-03 11:18 ` Andy Shevchenko
@ 2026-02-03 11:20 ` Andy Shevchenko
2026-02-03 11:26 ` Andy Shevchenko
0 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2026-02-03 11:20 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Nuno Sá, Salah Triki, David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel
On Tue, Feb 03, 2026 at 01:18:14PM +0200, Andy Shevchenko wrote:
> On Mon, Feb 02, 2026 at 08:52:17PM +0000, Jonathan Cameron wrote:
> > On Mon, 02 Feb 2026 10:12:10 +0000
> > Nuno Sá <noname.nuno@gmail.com> wrote:
> > > On Sat, 2026-01-31 at 12:44 +0000, Jonathan Cameron wrote:
...
> > > Andy already fixed it for the main iio_dev allocation:
> > >
> > > https://lore.kernel.org/linux-iio/20251112145735.2075527-3-andriy.shevchenko@linux.intel.com/
>
> 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.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc()
2026-02-03 11:20 ` Andy Shevchenko
@ 2026-02-03 11:26 ` Andy Shevchenko
0 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2026-02-03 11:26 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Nuno Sá, Salah Triki, David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel
On Tue, Feb 03, 2026 at 01:20:02PM +0200, Andy Shevchenko wrote:
> On Tue, Feb 03, 2026 at 01:18:14PM +0200, Andy Shevchenko wrote:
> > On Mon, Feb 02, 2026 at 08:52:17PM +0000, Jonathan Cameron wrote:
> > > On Mon, 02 Feb 2026 10:12:10 +0000
> > > Nuno Sá <noname.nuno@gmail.com> wrote:
> > > > On Sat, 2026-01-31 at 12:44 +0000, Jonathan Cameron wrote:
...
> > > > Andy already fixed it for the main iio_dev allocation:
> > > >
> > > > https://lore.kernel.org/linux-iio/20251112145735.2075527-3-andriy.shevchenko@linux.intel.com/
> >
> > 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.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc()
2026-01-31 12:44 ` Jonathan Cameron
2026-02-02 10:12 ` Nuno Sá
@ 2026-02-04 20:03 ` Salah Triki
1 sibling, 0 replies; 8+ messages in thread
From: Salah Triki @ 2026-02-04 20:03 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
linux-kernel
On Sat, Jan 31, 2026 at 12:44:16PM +0000, Jonathan Cameron wrote:
> On Sat, 31 Jan 2026 10:23:33 +0100
> Salah Triki <salah.triki@gmail.com> wrote:
>
> Hi Salah,
>
> This is a definitely case of the fix not being anywhere as simple
> as it might look at first glance.
>
> > 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.
>
> >
> > 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.
>
> >
> > 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
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-02-04 20:03 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-31 9:23 [PATCH] iio: trigger: fix use-after-free in viio_trigger_alloc() Salah Triki
2026-01-31 12:44 ` Jonathan Cameron
2026-02-02 10:12 ` Nuno Sá
2026-02-02 20:52 ` Jonathan Cameron
2026-02-03 11:18 ` Andy Shevchenko
2026-02-03 11:20 ` Andy Shevchenko
2026-02-03 11:26 ` Andy Shevchenko
2026-02-04 20:03 ` Salah Triki
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox