Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v2] iio: trigger: loop: fix use-after-free on configfs removal
@ 2026-12-03 19:32 Yuansheng Yang
  2026-12-04 16:50 ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Yuansheng Yang @ 2026-12-03 19:32 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
	linux-kernel, stable

Removing a loop trigger via configfs frees struct iio_loop_info in
iio_loop_release() while the trigger is still referenced by an IIO
device, and leaves any running loop kthread behind. Later disabling or
re-enabling the buffer calls iio_loop_trigger_set_state(), dereferencing
the freed iio_loop_info via iio_trigger_get_drvdata():

  BUG: KASAN: slab-use-after-free in iio_loop_trigger_set_state
  Call Trace:
   iio_loop_trigger_set_state
   iio_trigger_detach_poll_func
   iio_disable_buffers
   __iio_update_buffers
   enable_store

Serialize iio_loop_trigger_set_state() and iio_trig_loop_remove() with
a mutex. In iio_trig_loop_remove(), stop any running kthread and clear
the trigger's drvdata to NULL before calling iio_trigger_free(). In
iio_loop_trigger_set_state(), return 0 on disable or -ENODEV on enable
when drvdata is NULL, and avoid storing an ERR_PTR() in loop_trig->task
if kthread_run() fails.

Gemini was used to assist with code path verification, QEMU KASAN
reproduction, and drafting the initial patch; the final code and this
changelog were reviewed and edited by me.

Fixes: bc2e1126eccb ("iio:trigger: Experimental kthread tight loop trigger (thread only)")
Cc: stable@vger.kernel.org
Signed-off-by: Yuansheng Yang <0xbeac@gmail.com>
---
Changes in v2:
- Rewrite the commit message to be concise (also document the existing
  ERR_PTR() handling in iio_loop_trigger_set_state(); no code changes).
- Trim the KASAN backtrace per Documentation/process/submitting-patches.rst
  (drop hex offsets, addresses, PIDs, and redundant alloc/free traces).

v1: https://lore.kernel.org/r/20261202034718.2917551-1-0xbeac@gmail.com

Base: mainline 666539eecdf4 ("Merge tag 'assoc-array-fixes-v2'") with
commit 8fb20cdf9c86 ("iio: trigger: free hrtimer and loop triggers on
configfs release") applied.

Related hrtimer trigger UAF report:
  https://lore.kernel.org/lkml/20261201154946.904264-1-rongh7426@gmail.com/

 drivers/iio/trigger/iio-trig-loop.c | 43 +++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/trigger/iio-trig-loop.c b/drivers/iio/trigger/iio-trig-loop.c
index e6bea96cc56..661901ac95e 100644
--- a/drivers/iio/trigger/iio-trig-loop.c
+++ b/drivers/iio/trigger/iio-trig-loop.c
@@ -18,8 +18,10 @@
  * element kfifo buffer.
  */
 
+#include <linux/cleanup.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/irq_work.h>
@@ -35,6 +37,12 @@ struct iio_loop_info {
 	struct task_struct *task;
 };
 
+/*
+ * Serializes set_trigger_state() and remove(): iio_loop_info may be freed on
+ * configfs release while a consumer still holds a reference to iio_trigger.
+ */
+static DEFINE_MUTEX(iio_loop_lock);
+
 static void iio_loop_release(struct config_item *item)
 {
 	struct iio_sw_trigger *swt = to_iio_sw_trigger(item);
@@ -66,18 +74,29 @@ static int iio_loop_thread(void *data)
 
 static int iio_loop_trigger_set_state(struct iio_trigger *trig, bool state)
 {
-	struct iio_loop_info *loop_trig = iio_trigger_get_drvdata(trig);
+	struct iio_loop_info *loop_trig;
+	struct task_struct *task;
+
+	guard(mutex)(&iio_loop_lock);
+
+	loop_trig = iio_trigger_get_drvdata(trig);
+	if (!loop_trig)
+		return state ? -ENODEV : 0;
 
 	if (state) {
-		loop_trig->task = kthread_run(iio_loop_thread,
-					      trig, trig->name);
-		if (IS_ERR(loop_trig->task)) {
+		if (loop_trig->task)
+			return -EBUSY;
+
+		task = kthread_run(iio_loop_thread, trig, trig->name);
+		if (IS_ERR(task)) {
 			dev_err(&trig->dev,
 				"failed to create trigger loop thread\n");
-			return PTR_ERR(loop_trig->task);
+			return PTR_ERR(task);
 		}
-	} else {
+		loop_trig->task = task;
+	} else if (loop_trig->task) {
 		kthread_stop(loop_trig->task);
+		loop_trig->task = NULL;
 	}
 
 	return 0;
@@ -123,7 +142,19 @@ static struct iio_sw_trigger *iio_trig_loop_probe(const char *name)
 
 static int iio_trig_loop_remove(struct iio_sw_trigger *swt)
 {
+	struct iio_loop_info *trig_info;
+
 	iio_trigger_unregister(swt->trigger);
+
+	scoped_guard(mutex, &iio_loop_lock) {
+		trig_info = iio_trigger_get_drvdata(swt->trigger);
+		if (trig_info && trig_info->task) {
+			kthread_stop(trig_info->task);
+			trig_info->task = NULL;
+		}
+		iio_trigger_set_drvdata(swt->trigger, NULL);
+	}
+
 	iio_trigger_free(swt->trigger);
 
 	return 0;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iio: trigger: loop: fix use-after-free on configfs removal
  2026-12-03 19:32 [PATCH v2] iio: trigger: loop: fix use-after-free on configfs removal Yuansheng Yang
@ 2026-12-04 16:50 ` Andy Shevchenko
  2026-12-04 19:09   ` Yuansheng Yang
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2026-12-04 16:50 UTC (permalink / raw)
  To: Yuansheng Yang
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	linux-iio, linux-kernel, stable

On Fri, Dec 04, 2026 at 03:32:02AM +0800, Yuansheng Yang wrote:
> Removing a loop trigger via configfs frees struct iio_loop_info in
> iio_loop_release() while the trigger is still referenced by an IIO
> device, and leaves any running loop kthread behind. Later disabling or
> re-enabling the buffer calls iio_loop_trigger_set_state(), dereferencing
> the freed iio_loop_info via iio_trigger_get_drvdata():
> 
>   BUG: KASAN: slab-use-after-free in iio_loop_trigger_set_state
>   Call Trace:
>    iio_loop_trigger_set_state
>    iio_trigger_detach_poll_func
>    iio_disable_buffers
>    __iio_update_buffers
>    enable_store
> 
> Serialize iio_loop_trigger_set_state() and iio_trig_loop_remove() with
> a mutex. In iio_trig_loop_remove(), stop any running kthread and clear
> the trigger's drvdata to NULL before calling iio_trigger_free(). In
> iio_loop_trigger_set_state(), return 0 on disable or -ENODEV on enable
> when drvdata is NULL, and avoid storing an ERR_PTR() in loop_trig->task
> if kthread_run() fails.

> Gemini was used to assist with code path verification, QEMU KASAN
> reproduction, and drafting the initial patch; the final code and this
> changelog were reviewed and edited by me.

This should be moved to the comment block.

...

>  static int iio_loop_trigger_set_state(struct iio_trigger *trig, bool state)
>  {
> -	struct iio_loop_info *loop_trig = iio_trigger_get_drvdata(trig);
> +	struct iio_loop_info *loop_trig;
> +	struct task_struct *task;
> +
> +	guard(mutex)(&iio_loop_lock);

> +	loop_trig = iio_trigger_get_drvdata(trig);

Leave this line as it was before...

> +	if (!loop_trig)
> +		return state ? -ENODEV : 0;

...and just add two conditionals to each of the existing if (state) branches
below.

>  	if (state) {
> -		loop_trig->task = kthread_run(iio_loop_thread,
> -					      trig, trig->name);
> -		if (IS_ERR(loop_trig->task)) {
> +		if (loop_trig->task)
> +			return -EBUSY;
> +
> +		task = kthread_run(iio_loop_thread, trig, trig->name);
> +		if (IS_ERR(task)) {
>  			dev_err(&trig->dev,
>  				"failed to create trigger loop thread\n");
> -			return PTR_ERR(loop_trig->task);
> +			return PTR_ERR(task);
>  		}
> -	} else {
> +		loop_trig->task = task;
> +	} else if (loop_trig->task) {
>  		kthread_stop(loop_trig->task);
> +		loop_trig->task = NULL;
>  	}

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iio: trigger: loop: fix use-after-free on configfs removal
  2026-12-04 16:50 ` Andy Shevchenko
@ 2026-12-04 19:09   ` Yuansheng Yang
  0 siblings, 0 replies; 3+ messages in thread
From: Yuansheng Yang @ 2026-12-04 19:09 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	linux-iio, linux-kernel, stable

On Fri, Dec 04, 2026 at 06:50:20PM +0200, Andy Shevchenko wrote:
> On Fri, Dec 04, 2026 at 03:32:02AM +0800, Yuansheng Yang wrote:
...
> > Gemini was used to assist with code path verification, QEMU KASAN
> > reproduction, and drafting the initial patch; the final code and this
> > changelog were reviewed and edited by me.
>
> This should be moved to the comment block.

Sure, will move this below the '---' line in v3.

> >  static int iio_loop_trigger_set_state(struct iio_trigger *trig, bool state)
> >  {
> > -	struct iio_loop_info *loop_trig = iio_trigger_get_drvdata(trig);
> > +	struct iio_loop_info *loop_trig;
> > +	struct task_struct *task;
> > +
> > +	guard(mutex)(&iio_loop_lock);
>
> > +	loop_trig = iio_trigger_get_drvdata(trig);
>
> Leave this line as it was before...
>
> > +	if (!loop_trig)
> > +		return state ? -ENODEV : 0;
>
> ...and just add two conditionals to each of the existing if (state) branches
> below.

Folding the NULL checks into the two if (state) branches makes sense and
I'll do that in v3.

However, iio_trigger_get_drvdata(trig) needs to stay after
guard(mutex)(&iio_loop_lock). There is no outer lock shared between
enable_store() -> iio_loop_trigger_set_state() and configfs rmdir ->
iio_trig_loop_remove(). If iio_trigger_get_drvdata(trig) is called
before acquiring iio_loop_lock:

1. Thread A (set_trigger_state) reads a non-NULL loop_trig before taking
   iio_loop_lock.
2. Thread B (rmdir) takes iio_loop_lock in iio_trig_loop_remove(), stops
   any running kthread, clears drvdata to NULL, drops iio_loop_lock, and
   config_item_put() frees loop_trig via iio_loop_release().
3. Thread A acquires iio_loop_lock with its cached non-NULL loop_trig
   now dangling, bypasses the NULL check, and dereferences freed
   loop_trig->task.

So in v3 it would look like:

	guard(mutex)(&iio_loop_lock);

	loop_trig = iio_trigger_get_drvdata(trig);
	if (state) {
		if (!loop_trig)
			return -ENODEV;
		if (loop_trig->task)
			return -EBUSY;
		...
	} else if (loop_trig && loop_trig->task) {
		kthread_stop(loop_trig->task);
		loop_trig->task = NULL;
	}

Does that look good to you before I send v3?

Thanks,
Yuansheng

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 19:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 19:32 [PATCH v2] iio: trigger: loop: fix use-after-free on configfs removal Yuansheng Yang
2026-12-04 16:50 ` Andy Shevchenko
2026-12-04 19:09   ` Yuansheng Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox