Skip to contentSkip to conversation

iio: buffer: hw-consumer: fix use-after-free in error path

PATCH Superseded Felix Gu 4 participants 8 messages Apr 28 – Apr 29 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Felix Gu

author
#0 lore raw

PATCH 1/1 iio: buffer: hw-consumer: fix use-after-free in error path

In the err_put_buffers cleanup path of iio_hw_consumer_alloc(), the code was using list_for_each_entry() to iterate through buffers while calling iio_buffer_put() which can free the current buffer if refcount drops to 0. The list_for_each_entry() loop macro then evaluates buf->head.next to continue iteration, accessing the freed buffer.

Fix this by using list_for_each_entry_safe().

Fixes 48b66f8f936f ("iio: Add hardware consumer buffer support")
---
 drivers/iio/buffer/industrialio-hw-consumer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/buffer/industrialio-hw-consumer.c +2 -2
index 24d7df603760..7406efefc123 100644
@@ -85,7 +85,7 @@ static struct hw_consumer_buffer *iio_hw_consumer_get_buffer(
  */
 struct iio_hw_consumer *iio_hw_consumer_alloc(struct device *dev)
 {
-	struct hw_consumer_buffer *buf;
+	struct hw_consumer_buffer *buf, *n;
 	struct iio_hw_consumer *hwc;
 	struct iio_channel *chan;
 	int ret;
@@ -116,7 +116,7 @@ struct iio_hw_consumer *iio_hw_consumer_alloc(struct device *dev)
 	return hwc;
 
 err_put_buffers:
-	list_for_each_entry(buf, &hwc->buffers, head)
+	list_for_each_entry_safe(buf, n, &hwc->buffers, head)
 		iio_buffer_put(&buf->buffer);
 	iio_channel_release_all(hwc->channels);
 err_free_hwc:

---
base-commit: 7080e32d3f09d8688c4a87d81bdcc71f7f606b16
change-id: 20260428-iio-buf-4c8559020a54

Best regards,

Signature · 1 line
Felix Gu <[email protected]>
#1 lore raw
Quoted 9 lines · Felix Gu

In the err_put_buffers cleanup path of iio_hw_consumer_alloc(), the code was using list_for_each_entry() to iterate through buffers while calling iio_buffer_put() which can free the current buffer if refcount drops to 0. The list_for_each_entry() loop macro then evaluates buf->head.next to continue iteration, accessing the freed buffer.

Fix this by using list_for_each_entry_safe().

Format is wrong, missing space.

Quoted 1 line

Tag block should have no blank lines.

Quoted 2 lines
Fixes 48b66f8f936f ("iio: Add hardware consumer buffer support")

I am also wondering should we put Reported-by with the reference to AI somehow? Jonathan, others, what are your opinions?

...

drivers/iio/buffer/industrialio-hw-consumer.c static struct hw_consumer_buffer *iio_hw_consumer_get_buffer( · line 88

Quoted 2 lines
drivers/iio/buffer/industrialio-hw-consumer.c +1 -1
-	struct hw_consumer_buffer *buf;
+	struct hw_consumer_buffer *buf, *n;

Please, name it rather *tmp.

Quoted 1 line

{

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 1 line · Andy Shevchenko

On Tue, Apr 28, 2026 at 10:53:25PM +0800, Felix Gu wrote:

...

Quoted 4 lines

drivers/iio/buffer/industrialio-hw-consumer.c static struct hw_consumer_buffer *iio_hw_consumer_get_buffer( · line 88

Quoted 2 lines
drivers/iio/buffer/industrialio-hw-consumer.c +1 -1
-	struct hw_consumer_buffer *buf;
+	struct hw_consumer_buffer *buf, *n;

Please, name it rather *tmp.

Or *next. The point is that most of the one-letter variables are kinda reserved either for integers (like loop variables, local size or number holders, or some char * pointers).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw
In reply to Andy Shevchenko
Quoted 15 lines · On Tue, 28 Apr 2026 18:59:28 +0300 Andy Shevchenko…
Quoted 9 lines · Felix Gu

In the err_put_buffers cleanup path of iio_hw_consumer_alloc(), the code was using list_for_each_entry() to iterate through buffers while calling iio_buffer_put() which can free the current buffer if refcount drops to 0. The list_for_each_entry() loop macro then evaluates buf->head.next to continue iteration, accessing the freed buffer.

Fix this by using list_for_each_entry_safe().

Format is wrong, missing space.

Quoted 1 line
Quoted 7 lines

Tag block should have no blank lines.

Quoted 2 lines
Fixes 48b66f8f936f ("iio: Add hardware consumer buffer support")

I am also wondering should we put Reported-by with the reference to AI somehow? Jonathan, others, what are your opinions?

Would be nice to do so for these - things noticed whilst reviewing a patch type reports.

Roman (+CC), any suggestions on how to do this?

Quoted 10 lines

...

drivers/iio/buffer/industrialio-hw-consumer.c static struct hw_consumer_buffer *iio_hw_consumer_get_buffer( · line 88

Quoted 2 lines
drivers/iio/buffer/industrialio-hw-consumer.c +1 -1
-	struct hw_consumer_buffer *buf;
+	struct hw_consumer_buffer *buf, *n;  

Please, name it rather *tmp.

Quoted 1 line

{

#4 lore raw
Quoted 26 lines · Jonathan Cameron

On Tue, 28 Apr 2026 18:59:28 +0300

Quoted 20 lines · Andy Shevchenko
Quoted 10 lines · Felix Gu

In the err_put_buffers cleanup path of iio_hw_consumer_alloc(), the code was using list_for_each_entry() to iterate through buffers while calling iio_buffer_put() which can free the current buffer if refcount drops to 0. The list_for_each_entry() loop macro then evaluates buf->head.next to continue iteration, accessing the freed buffer.

Fix this by using list_for_each_entry_safe().

Format is wrong, missing space.

Quoted 1 line

Tag block should have no blank lines.

Quoted 2 lines
Fixes 48b66f8f936f ("iio: Add hardware consumer buffer support")
Quoted 7 lines
Quoted 2 lines

I am also wondering should we put Reported-by with the reference to AI somehow? Jonathan, others, what are your opinions?

Would be nice to do so for these - things noticed whilst reviewing a patch type reports.

Roman (+CC), any suggestions on how to do this?

I appreciate when people are recognizing Sashiko, but I don’t have any strong preference. Many engineers are using it locally before sending patches upstream, so we’ll never know it anyway. But Reported-by/Assisted-by tags are appreciated when appropriate.

#5 lore raw
Quoted 3 lines · Roman Gushchin
Quoted 3 lines · Jonathan Cameron

On Tue, 28 Apr 2026 18:59:28 +0300
Andy Shevchenko <[email protected]> wrote:

...

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

I am also wondering should we put Reported-by with the reference to AI somehow? Jonathan, others, what are your opinions?

Would be nice to do so for these - things noticed whilst reviewing a patch type reports.

Quoted 6 lines
Quoted 1 line

Roman (+CC), any suggestions on how to do this?

I appreciate when people are recognizing Sashiko, but I don’t have any strong preference. Many engineers are using it locally before sending patches upstream, so we’ll never know it anyway. But Reported-by/Assisted-by tags are appreciated when appropriate.

The problem is what to put to Reported-by as an email. For example LKP bot or syzbot have dedicated emails for that. What about Sashiko?

P.S.
An offtopic, how can I be email notified on the reviews done by Sashiko against my patches sent to Linux kernel project (mailing lists)? If you can add me as [email protected], I appreciate that!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 15 lines · Andy Shevchenko
Quoted 3 lines · Roman Gushchin
Quoted 3 lines · Jonathan Cameron

On Tue, 28 Apr 2026 18:59:28 +0300
Andy Shevchenko <[email protected]> wrote:

...

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

I am also wondering should we put Reported-by with the reference to AI somehow? Jonathan, others, what are your opinions?

Would be nice to do so for these - things noticed whilst reviewing a patch type reports.

Roman (+CC), any suggestions on how to do this?

Quoted 7 lines
Quoted 4 lines

I appreciate when people are recognizing Sashiko, but I don’t have any strong preference. Many engineers are using it locally before sending patches upstream, so we’ll never know it anyway. But Reported-by/Assisted-by tags are appreciated when appropriate.

The problem is what to put to Reported-by as an email. For example LKP bot or syzbot have dedicated emails for that. What about Sashiko?

We have [email protected] !

Quoted 5 lines

P.S.
An offtopic, how can I be email notified on the reviews done by Sashiko against my patches sent to Linux kernel project (mailing lists)? If you can add me as [email protected], I appreciate that!

I don't have a personal opt-in at the moment, it's configurable per mailing list. The current configuration is here:
https://github.com/sashiko-dev/sashiko/blob/main/sashiko.dev/email_policy.toml

I generally rely on the consensus decision by corresponding maintainers.

Thanks!

#7 lore raw
Quoted 5 lines · Roman Gushchin
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Roman Gushchin
Quoted 3 lines · Jonathan Cameron

On Tue, 28 Apr 2026 18:59:28 +0300
Andy Shevchenko <[email protected]> wrote:

...

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 7 lines
Quoted 2 lines

I am also wondering should we put Reported-by with the reference to AI somehow? Jonathan, others, what are your opinions?

Would be nice to do so for these - things noticed whilst reviewing a patch type reports.

Roman (+CC), any suggestions on how to do this?

I appreciate when people are recognizing Sashiko, but I don’t have any strong preference. Many engineers are using it locally before sending patches upstream, so we’ll never know it anyway. But Reported-by/Assisted-by tags are appreciated when appropriate.

Quoted 4 lines
Quoted 2 lines

The problem is what to put to Reported-by as an email. For example LKP bot or syzbot have dedicated emails for that. What about Sashiko?

We have [email protected] !

Good to know, thanks.

Quoted 6 lines
Quoted 4 lines

P.S.
An offtopic, how can I be email notified on the reviews done by Sashiko against my patches sent to Linux kernel project (mailing lists)? If you can add me as [email protected], I appreciate that!

I don't have a personal opt-in at the moment,

Whenever it appears (I hope to see it rather sooner) put me first in the list to be Cc'ed!

Quoted 5 lines

it's configurable per
mailing list. The current configuration is here:
https://github.com/sashiko-dev/sashiko/blob/main/sashiko.dev/email_policy.toml

I generally rely on the consensus decision by corresponding maintainers.

Noted, thanks for clarification.

Signature · 2 lines
With Best Regards,
Andy Shevchenko