linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] iio: buffer: Fix potential use-after-free in anonymous buffer release
@ 2026-07-15 15:42 Lars-Peter Clausen
  2026-07-15 15:42 ` [PATCH 2/3] iio: buffer: Tie IIO dma fence lock lifetime to the fence Lars-Peter Clausen
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Lars-Peter Clausen @ 2026-07-15 15:42 UTC (permalink / raw)
  To: Jonathan Cameron, linux-iio
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Paul Cercueil,
	Lars-Peter Clausen

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].

[1] https://lore.kernel.org/linux-iio/20240605110845.86740-4-paul@crapouillou.net
[2] https://lore.kernel.org/linux-iio/20240618100302.72886-4-paul@crapouillou.net

Reported-by: codex:gpt-5.6
Fixes: 3e26d9f08fbe ("iio: core: Add new DMABUF interface infrastructure")
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 drivers/iio/industrialio-buffer.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 531fc4ccc15de..04b3916f89e2d 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -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);
-- 
2.47.3


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

end of thread, other threads:[~2026-07-19  0:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-15 15:42 [PATCH 1/3] iio: buffer: Fix potential use-after-free in anonymous buffer release Lars-Peter Clausen
2026-07-15 15:42 ` [PATCH 2/3] iio: buffer: Tie IIO dma fence lock lifetime to the fence Lars-Peter Clausen
2026-07-15 15:42 ` [PATCH 3/3] iio: buffer: Make IIO DMA fence release RCU-safe Lars-Peter Clausen
2026-07-15 16:17   ` Andy Shevchenko
2026-07-15 17:21     ` Lars-Peter Clausen
2026-07-19  0:56       ` Jonathan Cameron
2026-07-15 16:12 ` [PATCH 1/3] iio: buffer: Fix potential use-after-free in anonymous buffer release Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).