patch "iio: buffer: Fix potential use-after-free in anonymous buffer release" added to char-misc-next
Subsystems (2)
This is a note to let you know that I've just added the patch titled
iio: buffer: Fix potential use-after-free in anonymous buffer release
to my char-misc git tree which can be found at
git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git
in the char-misc-next branch.
The patch will show up in the next release of the linux-next tree (usually sometime within the next 24 hours during the week.)
The patch will also be merged in the next major kernel release during the merge window.
If you have any questions about this process, please let me know.
From 6288b593e76eb10329326f2cd51e32557203b9e5 Mon Sep 17 00:00:00 2001
From: Lars-Peter Clausen <[email protected]>
Date: Wed, 15 Jul 2026 08:42:43 -0700
Subject: iio: buffer: Fix potential use-after-free in anonymous buffer release
An anonymous buffer handle holds a reference to the underlying IIO device. The reference is dropped in the buffer handle's release function. If the device has been removed, either through unbind or hot-unplug, the buffer handle might hold the last reference.
The release function takes the mutex for the buffer using a guard, which means the unlock happens after all the code in the function, including iio_device_put(). If the anonymous buffer holds the last reference this might free both the IIO device and the buffer, which contains the mutex, leading to use-after-free when the mutex is unlocked.
Fix this by using a scoped guard just around the buffer dmabuf list access, making sure the mutex is unlocked before releasing the IIO device.
Version 10 of the patch that introduced this issue used this exact scheme of first unlocking and then dropping the reference [1]. During review it was suggested to use a guard instead, and version 11 made that change [2].
--- drivers/iio/industrialio-buffer.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/iio/industrialio-buffer.c +10 -6
@@ -1619,12 +1619,16 @@ static int iio_buffer_chrdev_release(struct inode *inode, struct file *filep)wake_up (&buffer->pollq); -guard (mutex)(&buffer->dmabufs_mutex); - -/* Close all attached DMABUFs */ -list_for_each_entry_safe (priv, tmp, &buffer->dmabufs, entry) { -list_del_init (&priv->entry); -iio_buffer_dmabuf_put (priv->attach); +/* +* The mutex must be unlocked before iio_device_put(), which might drop the +* last reference and free the buffer. +*/ + scoped_guard(mutex, &buffer->dmabufs_mutex) { +/* Close all attached DMABUFs */ +list_for_each_entry_safe (priv, tmp, &buffer->dmabufs, entry) { +list_del_init (&priv->entry); +iio_buffer_dmabuf_put (priv->attach); + } }kfree (ib);
Signature · 1 line
2.55.0