Re: [PATCH] iio: trigger: loop: fix use-after-free on configfs removal

From: Andy Shevchenko

Date: Thu Dec 03 2026 - 06:18:13 EST


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