iio: buffer: hw-consumer: fix use-after-free in error path
PATCH v2 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().
---
Changes in v2:
- Fix Andy's comment.
- Add Reported-by tag.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- drivers/iio/buffer/industrialio-hw-consumer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/buffer/industrialio-hw-consumer.c +2 -2
@@ -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, *tmp;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, tmp, &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]>
Quoted 3 lines · Felix Gu
In the
err_put_bufferscleanup path ofiio_hw_consumer_alloc(), the code was usinglist_for_each_entry()to iterate through buffers while callingiio_buffer_put()which can free the current buffer if refcount drops to 0=
.
Quoted 9 lines
The
list_for_each_entry()loop macro then evaluatesbuf->head.nextto continue iteration, accessing the freed buffer.Fix this by using
list_for_each_entry_safe().Fixes 48b66f8f936f ("iio: Add hardware consumer buffer support")Reported-by sashiko <[email protected]>Signed-off-by Felix Gu <[email protected]>
Quoted 9 lines
---
Changes in v2:
- Fix Andy's comment.
- Add Reported-by tag.
- Link to v1: https://lore.kernel.org/r/[email protected]--- drivers/iio/buffer/industrialio-hw-consumer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Looks fine to me.
best regards,
maxwell
Quoted 3 lines
Best regards,
--=
Felix Gu <[email protected]>
Quoted 24 lines · On Thu, 30 Apr 2026 20:27:27 -0500 "Maxwell Doose" <[email protected]> wrote:
Quoted 3 lines · Felix Gu
In the
err_put_bufferscleanup path ofiio_hw_consumer_alloc(), the code was usinglist_for_each_entry()to iterate through buffers while callingiio_buffer_put()which can free the current buffer if refcount drops to 0=.
Quoted 9 lines
The
list_for_each_entry()loop macro then evaluatesbuf->head.nextto continue iteration, accessing the freed buffer.Fix this by using
list_for_each_entry_safe().Fixes 48b66f8f936f ("iio: Add hardware consumer buffer support")Reported-by sashiko <[email protected]>Signed-off-by Felix Gu <[email protected]>Quoted 9 lines
---
Changes in v2:
- Fix Andy's comment.
- Add Reported-by tag.
- Link to v1: https://lore.kernel.org/r/[email protected]--- drivers/iio/buffer/industrialio-hw-consumer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 6 lines
Looks fine to me.
Reviewed-by Maxwell Doose <[email protected]>best regards,
maxwell
Applied to the fixes-togreg branch of iio.git and marked for stable.
Thanks
J
Quoted 7 lines
Quoted 6 lines
Best regards,
--=
Felix Gu <[email protected]>
Quoted 12 lines · Felix Gu
In the
err_put_bufferscleanup path ofiio_hw_consumer_alloc(), the code was usinglist_for_each_entry()to iterate through buffers while callingiio_buffer_put()which can free the current buffer if refcount drops to 0. Thelist_for_each_entry()loop macro then evaluatesbuf->head.nextto continue iteration, accessing the freed buffer.Fix this by using
list_for_each_entry_safe().Fixes 48b66f8f936f ("iio: Add hardware consumer buffer support")Reported-by sashiko <[email protected]>Closes:
https://sashiko.dev/#/patchset/20260427-iio_buf-v1-1-2bbdac844647%40gmail.com
Quoted 8 lines
Signed-off-by Felix Gu <[email protected]>---
Changes in v2:
- Fix Andy's comment.
- Add Reported-by tag.
- Link to v1:
https://lore.kernel.org/r/[email protected]
---
LGTM
Quoted 33 lines
drivers/iio/buffer/industrialio-hw-consumer.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/buffer/industrialio-hw-consumer.c +1
-1@@ -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, *tmp; 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:drivers/iio/buffer/industrialio-hw-consumer.c +1
-1-list_for_each_entry (buf, &hwc->buffers, head) +list_for_each_entry_safe (buf, tmp, &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-4c8559020a54Best regards,
Quoted 7 lines · Felix Gu
In the
err_put_bufferscleanup path ofiio_hw_consumer_alloc(), the code was usinglist_for_each_entry()to iterate through buffers while callingiio_buffer_put()which can free the current buffer if refcount drops to 0. Thelist_for_each_entry()loop macro then evaluatesbuf->head.nextto continue iteration, accessing the freed buffer.
Quoted 5 lines
Fix this by using
list_for_each_entry_safe().Fixes 48b66f8f936f ("iio: Add hardware consumer buffer support")Reported-by sashiko <[email protected]>
FYI, Sashiko reported 2 additional regressions (neither introduced by your patches), one of them critical. Maybe another patch would be good?
https://sashiko.dev/#/patchset/20260430-iio-buf-v2-1-84c2231dea5e%40gmail.com?part=1
--
Kind regards
CJD
Quoted 10 lines · Joshua Crofts
Quoted 9 lines · Felix Gu
In the
err_put_bufferscleanup path ofiio_hw_consumer_alloc(), the code was usinglist_for_each_entry()to iterate through buffers while callingiio_buffer_put()which can free the current buffer if refcount drops to 0. Thelist_for_each_entry()loop macro then evaluatesbuf->head.nextto continue iteration, accessing the freed buffer.Fix this by using
list_for_each_entry_safe().
Quoted 9 lines
Quoted 3 lines
Fixes 48b66f8f936f ("iio: Add hardware consumer buffer support")Reported-by sashiko <[email protected]>FYI, Sashiko reported 2 additional regressions (neither introduced by your patches), one of them critical. Maybe another patch would be good?
https://sashiko.dev/#/patchset/20260430-iio-buf-v2-1-84c2231dea5e%40gmail.com?part=1
I think one of the issues raised by sashiko has an active patch:
https://lore.kernel.org/linux-iio/[email protected]/
and I think they were waiting for Nuno to review it, since they noted they were on a "wild goose chase". Thus the only issue raised by sashiko that would still need a patch would be the OOB issue, but I haven't looked at its explaination thoroughly to see if it's hallucinating.
Anyways this one should still be good to merge.
best regards,
maxwell
Quoted 4 lines
--
Kind regardsCJD
⁷
Quoted 11 lines · Maxwell Doose
I think one of the issues raised by sashiko has an active patch:
https://lore.kernel.org/linux-iio/[email protected]/
and I think they were waiting for Nuno to review it, since they noted they were on a "wild goose chase". Thus the only issue raised by sashiko that would still need a patch would be the OOB issue, but I haven't looked at its explaination thoroughly to see if it's hallucinating.
Anyways this one should still be good to merge.
Yeah this is good to merge, I was only pointing out that Sashiko reported additional errors (unrelated to this patch, it seems to keep finding more issues with the driver).
Signature · 3 lines
Kind regards CJD
Quoted 13 lines · Joshua Crofts
⁷
Quoted 9 lines · Maxwell Doose
I think one of the issues raised by sashiko has an active patch:
https://lore.kernel.org/linux-iio/[email protected]/
and I think they were waiting for Nuno to review it, since they noted they were on a "wild goose chase". Thus the only issue raised by sashiko that would still need a patch would be the OOB issue, but I haven't looked at its explaination thoroughly to see if it's hallucinating.
Quoted 5 lines
Quoted 2 lines
Anyways this one should still be good to merge.
Yeah this is good to merge, I was only pointing out that Sashiko reported additional errors (unrelated to this patch, it seems to keep finding more issues with the driver).
Also, if anybody feels strongly about the OOB issue that sashiko raised in iio_hw_consumer_alloc(), you can probably add something like:
if (chan->channel->scan_index < 0) continue;
but I feel at that point if that even happens there's something else seriously wrong. But it might be a good idea to add that check anyways, better safe than sorry. Not to mention, since this is a public function, we need to code far more defensively.
best regards,
maxwell
Quoted 7 lines · Felix Gu
In the
err_put_bufferscleanup path ofiio_hw_consumer_alloc(), the code was usinglist_for_each_entry()to iterate through buffers while callingiio_buffer_put()which can free the current buffer if refcount drops to 0. Thelist_for_each_entry()loop macro then evaluatesbuf->head.nextto continue iteration, accessing the freed buffer.Fix this by using
list_for_each_entry_safe().
Signature · 2 lines
With Best Regards, Andy Shevchenko