mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] iio: trigger: loop: fix use-after-free on configfs removal
@ 2026-12-02  3:47 Yuansheng Yang
  2026-12-03 11:18 ` Andy Shevchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Yuansheng Yang @ 2026-12-02  3:47 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel, stable

Removing a tight-loop trigger directory via configfs while an IIO
consumer still holds a reference to its iio_trigger frees iio_loop_info
underneath the consumer:

1. iio_trig_loop_probe() allocates struct iio_loop_info (embedding
   struct iio_sw_trigger swt and storing struct task_struct *task) and
   sets it as the trigger's drvdata.
2. Attaching the trigger to a device's current_trigger acquires a
   reference on struct iio_trigger via iio_trigger_get().
3. rmdir on the configfs trigger directory invokes
   iio_trig_loop_remove(), which calls iio_trigger_unregister() and
   iio_trigger_free() (only dropping one device reference), then
   configfs drops the last config_item reference and iio_loop_release()
   frees iio_loop_info.  Any active loop kthread is also left running.
4. Disabling or re-enabling the consumer's buffer calls
   iio_loop_trigger_set_state(), which retrieves the freed
   iio_loop_info via iio_trigger_get_drvdata() and reads or writes
   loop_trig->task.

On an x86_64 KASAN QEMU guest with commit 8fb20cdf9c86 applied,
disabling the buffer after rmdir triggers:

  BUG: KASAN: slab-use-after-free in iio_loop_trigger_set_state+0x138/0x150
  Read of size 8 at addr ffff888008b25a98 by task bash/336
  Call Trace:
   iio_loop_trigger_set_state+0x138/0x150
   iio_trigger_detach_poll_func+0x23a/0x2d0
   iio_disable_buffers+0x38e/0x490
   __iio_update_buffers+0x1dfc/0x2470
   enable_store+0x1c1/0x1f0
  Allocated by task 344:
   iio_trig_loop_probe+0x47/0x1d0
   iio_sw_trigger_create+0x156/0x260
   trigger_make_group+0x33/0x90
   configfs_mkdir+0x521/0xe90
  Freed by task 354:
   kfree+0x154/0x420
   config_item_cleanup+0x143/0x1e0
   config_item_put+0x91/0xa0
   configfs_rmdir+0x881/0xac0

Re-enabling the buffer afterwards also triggers a slab-use-after-free
write of size 8 at iio_loop_trigger_set_state+0x149/0x150.

Commit 8fb20cdf9c86 ("iio: trigger: free hrtimer and loop triggers on
configfs release") moved kfree(trig_info) from iio_trig_loop_remove()
to iio_loop_release(), fixing the plain mkdir/rmdir config_item UAF,
but left the consumer-outlives-rmdir lifetime mismatch intact.

Serialize iio_loop_trigger_set_state() and iio_trig_loop_remove() with a
static mutex.  In iio_trig_loop_remove(), stop any running kthread
after iio_trigger_unregister() and clear the trigger's drvdata to NULL
before iio_trigger_free().  In iio_loop_trigger_set_state(), return 0 on
disable or -ENODEV on enable once drvdata has been cleared.

Discovered by manual code inspection following the sibling hrtimer
trigger UAF report, with Gemini assisting in path verification, QEMU
reproduction, and preparing this patch and changelog.

Fixes: bc2e1126eccb ("iio:trigger: Experimental kthread tight loop trigger (thread only)")
Cc: stable@vger.kernel.org
Signed-off-by: Yuansheng Yang <0xbeac@gmail.com>
---
Sibling hrtimer trigger UAF report on lkml/linux-iio:
  https://lore.kernel.org/lkml/20261201154946.904264-1-rongh7426@gmail.com/

Base: next-20261201 / fixes-togreg state of drivers/iio/trigger/iio-trig-loop.c
(blob e6bea96cc565, i.e. 666539eecdf with 8fb20cdf9c86 applied).

Testing:
- drivers/iio/trigger/iio-trig-loop.o compiled with W=1 on x86_64
  (GCC 15.2.0, CONFIG_IIO_TIGHTLOOP_TRIGGER=y, CONFIG_KASAN=y) with
  0 warnings and 0 errors.
- On an x86_64 KASAN QEMU guest (iio_simple_dummy with buffer support),
  before this fix rmdir of triggers/loop/t0 while a buffer held t0
  followed by buffer disable/enable triggered KASAN slab-use-after-free
  reads and writes in iio_loop_trigger_set_state().  With this fix,
  the same sequence plus rmdir-before-enable, 10x create/bind/enable/
  disable/unbind/rmdir cycles, and active kthread teardown checks
  completed with 0 KASAN reports or warnings; post-rmdir buffer enable
  returns -ENODEV cleanly.

 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	[flat|nested] 2+ messages in thread

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

On Wed, Dec 02, 2026 at 11:47:18AM +0800, Yuansheng Yang wrote:
> Removing a tight-loop trigger directory via configfs while an IIO
> consumer still holds a reference to its iio_trigger frees iio_loop_info
> underneath the consumer:
> 
> 1. iio_trig_loop_probe() allocates struct iio_loop_info (embedding
>    struct iio_sw_trigger swt and storing struct task_struct *task) and
>    sets it as the trigger's drvdata.
> 2. Attaching the trigger to a device's current_trigger acquires a
>    reference on struct iio_trigger via iio_trigger_get().
> 3. rmdir on the configfs trigger directory invokes
>    iio_trig_loop_remove(), which calls iio_trigger_unregister() and
>    iio_trigger_free() (only dropping one device reference), then
>    configfs drops the last config_item reference and iio_loop_release()
>    frees iio_loop_info.  Any active loop kthread is also left running.
> 4. Disabling or re-enabling the consumer's buffer calls
>    iio_loop_trigger_set_state(), which retrieves the freed
>    iio_loop_info via iio_trigger_get_drvdata() and reads or writes
>    loop_trig->task.
> 
> On an x86_64 KASAN QEMU guest with commit 8fb20cdf9c86 applied,
> disabling the buffer after rmdir triggers:
> 
>   BUG: KASAN: slab-use-after-free in iio_loop_trigger_set_state+0x138/0x150
>   Read of size 8 at addr ffff888008b25a98 by task bash/336
>   Call Trace:
>    iio_loop_trigger_set_state+0x138/0x150
>    iio_trigger_detach_poll_func+0x23a/0x2d0
>    iio_disable_buffers+0x38e/0x490
>    __iio_update_buffers+0x1dfc/0x2470
>    enable_store+0x1c1/0x1f0
>   Allocated by task 344:
>    iio_trig_loop_probe+0x47/0x1d0
>    iio_sw_trigger_create+0x156/0x260
>    trigger_make_group+0x33/0x90
>    configfs_mkdir+0x521/0xe90
>   Freed by task 354:
>    kfree+0x154/0x420
>    config_item_cleanup+0x143/0x1e0
>    config_item_put+0x91/0xa0
>    configfs_rmdir+0x881/0xac0
> 
> Re-enabling the buffer afterwards also triggers a slab-use-after-free
> write of size 8 at iio_loop_trigger_set_state+0x149/0x150.
> 
> Commit 8fb20cdf9c86 ("iio: trigger: free hrtimer and loop triggers on
> configfs release") moved kfree(trig_info) from iio_trig_loop_remove()
> to iio_loop_release(), fixing the plain mkdir/rmdir config_item UAF,
> but left the consumer-outlives-rmdir lifetime mismatch intact.
> 
> Serialize iio_loop_trigger_set_state() and iio_trig_loop_remove() with a
> static mutex.  In iio_trig_loop_remove(), stop any running kthread
> after iio_trigger_unregister() and clear the trigger's drvdata to NULL
> before iio_trigger_free().  In iio_loop_trigger_set_state(), return 0 on
> disable or -ENODEV on enable once drvdata has been cleared.
> 
> Discovered by manual code inspection following the sibling hrtimer
> trigger UAF report, with Gemini assisting in path verification, QEMU
> reproduction, and preparing this patch and changelog.

Read Submitting Patches documentation about backtraces in the commit messages
and use *your own words* to make this commit message readable.

This is no-go in this form.

-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2026-12-03 11:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02  3:47 [PATCH] iio: trigger: loop: fix use-after-free on configfs removal Yuansheng Yang
2026-12-03 11:18 ` Andy Shevchenko

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

all inboxes | Powered by JetHome®