iio: buffer: Fix double-free in iio_buffers_alloc_sysfs_and_mask()
PATCH 1/1 iio: buffer: Fix double-free in iio_buffers_alloc_sysfs_and_mask()
When __iio_buffer_alloc_sysfs_and_mask() failed, 'unwind_idx' should be set to 'i - 1' to prevent double-free when cleanup resources.
BUG: KASAN: double-free or invalid-free in __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio] Call Trace: kfree+0x117/0x4c0 __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio] iio_buffers_alloc_sysfs_and_mask+0x60d/0x1570 [industrialio] __iio_device_register+0x483/0x1a30 [industrialio] ina2xx_probe+0x625/0x980 [ina2xx_adc]
--- drivers/iio/industrialio-buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-buffer.c +1 -1
@@ -1616,7 +1616,7 @@ int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev) buffer = iio_dev_opaque->attached_buffers[i]; ret =__iio_buffer_alloc_sysfs_and_mask (buffer, indio_dev, i);if (ret) { - unwind_idx = i; + unwind_idx = i -1 ; goto error_unwind_sysfs_and_mask; } }
Signature · 1 line
2.25.1
Quoted 4 lines · Yang Yingliang
When
__iio_buffer_alloc_sysfs_and_mask()failed, 'unwind_idx' should be set to 'i - 1' to prevent double-free when cleanup resources.
Quoted 8 lines
BUG: KASAN: double-free or invalid-free in __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio] Call Trace: kfree+0x117/0x4c0 __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio] iio_buffers_alloc_sysfs_and_mask+0x60d/0x1570 [industrialio] __iio_device_register+0x483/0x1a30 [industrialio] ina2xx_probe+0x625/0x980 [ina2xx_adc]
Makes sense.
Thanks for the catch.
Quoted 23 lines
Reported-by Hulk Robot <[email protected]>Fixes ee708e6baacd ("iio: buffer: introduce support for attaching more IIO buffers")Signed-off-by Yang Yingliang <[email protected]>--- drivers/iio/industrialio-buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/industrialio-buffer.c +1
-2@@ -1616,7 +1616,7 @@ int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev) buffer = iio_dev_opaque->attached_buffers[i]; ret =__iio_buffer_alloc_sysfs_and_mask (buffer, indio_dev, i);if (ret) { - unwind_idx = i; + unwind_idx = i -1 ; goto error_unwind_sysfs_and_mask; } } --2.25.1
Quoted 20 lines · Alexandru Ardelean
Quoted 4 lines · Yang Yingliang
When
__iio_buffer_alloc_sysfs_and_mask()failed, 'unwind_idx' should be set to 'i - 1' to prevent double-free when cleanup resources.Quoted 8 lines
BUG: KASAN: double-free or invalid-free in __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio] Call Trace: kfree+0x117/0x4c0 __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio] iio_buffers_alloc_sysfs_and_mask+0x60d/0x1570 [industrialio] __iio_device_register+0x483/0x1a30 [industrialio] ina2xx_probe+0x625/0x980 [ina2xx_adc]Makes sense.
Thanks for the catch.Reviewed-by Alexandru Ardelean <[email protected]>
...
Quoted 5 lines
drivers/iio/industrialio-buffer.c int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev) · line 1620
Quoted 5 lines
ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, i); if (ret) {drivers/iio/industrialio-buffer.c +1
-1- unwind_idx = i; + unwind_idx = i -1 ; goto error_unwind_sysfs_and_mask;
I prefer to see
- for (; unwind_idx >= 0; unwind_idx--) {
+ while (unwind_idx--)
instead.
Quoted 2 lines
Quoted 2 lines
} }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Alexandru Ardelean
Quoted 3 lines · Yang Yingliang
When
__iio_buffer_alloc_sysfs_and_mask()failed, 'unwind_idx' should be set to 'i - 1' to prevent double-free when cleanup resources.
[]
Quoted 5 lines
I prefer to see
- for (;
unwind_idx>= 0; unwind_idx--) {
+ while (unwind_idx--)
Not the same code as unwind_idx would be decremented before entering the code block.
You'd want
do {
...
} while (unwind_idx--);Quoted 12 lines · Joe Perches
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Alexandru Ardelean
Quoted 3 lines · Yang Yingliang
When
__iio_buffer_alloc_sysfs_and_mask()failed, 'unwind_idx' should be set to 'i - 1' to prevent double-free when cleanup resources.[]
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
I prefer to see
- for (;
unwind_idx>= 0; unwind_idx--) {
+ while (unwind_idx--)Not the same code as
unwind_idxwould be decremented before entering the code block.
It's kinda cryptic what you are pointing out.
What's needed additionally is to change
- unwind_idx = iio_dev_opaque->attached_buffers_cnt - 1;
+ unwind_idx = i;
Quoted 5 lines
You'd want
do { ... } while (unwind_idx--);
Of course not. See above. The usual pattern is
while (i--)
do_clean_item(i);Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 15 lines · Andy Shevchenko
Quoted 14 lines · Joe Perches
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Alexandru Ardelean
Quoted 3 lines · Yang Yingliang
When
__iio_buffer_alloc_sysfs_and_mask()failed, 'unwind_idx' should be set to 'i - 1' to prevent double-free when cleanup resources.[]
Quoted 3 lines
I prefer to see
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
- for (;
unwind_idx>= 0; unwind_idx--) {
+ while (unwind_idx--)Not the same code as
unwind_idxwould be decremented before entering the code block.It's kinda cryptic what you are pointing out.
Not really,
Quoted 4 lines
What's needed additionally is to change
-
unwind_idx=iio_dev_opaque->attached_buffers_cnt- 1;
+unwind_idx= i;
You left out that 'additional change' above from your reply.
Quoted 4 lines
Of course not. See above. The usual pattern is
while (i--)
do_clean_item(i);
Of course, but that's not what you replied.
I was merely pointing out that your reply included a logic change converting a loop from for to while.
cheers, Joe
Quoted 7 lines · Joe Perches
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Joe Perches
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Alexandru Ardelean
On Tue, Oct 12, 2021 at 12:18 PM Yang Yingliang
<[email protected]> wrote:
...
Quoted 11 lines
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
I prefer to see
- for (;
unwind_idx>= 0; unwind_idx--) {
+ while (unwind_idx--)Not the same code as
unwind_idxwould be decremented before entering the code block.It's kinda cryptic what you are pointing out.
Not really,
It's. It lacks the very same "additional" words to explain what you meant and why.
Quoted 6 lines
Quoted 4 lines
What's needed additionally is to change
-
unwind_idx=iio_dev_opaque->attached_buffers_cnt- 1;
+unwind_idx= i;You left out that 'additional change' above from your reply.
Yes, that's true, but it took some time to decrypt your message.
Quoted 8 lines
Quoted 4 lines
Of course not. See above. The usual pattern is
while (i--) do_clean_item(i);Of course, but that's not what you replied.
I was merely pointing out that your reply included a logic change converting a loop from for to while.
I expect that developers actually think about the changes they do and double check what's proposed by reviewers. If they just copy'n'paste whatever others propose, I wouldn't take any patch from such a developer.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi,
Quoted 32 lines · Andy Shevchenko
Quoted 7 lines · Joe Perches
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Joe Perches
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Alexandru Ardelean
On Tue, Oct 12, 2021 at 12:18 PM Yang Yingliang
<[email protected]> wrote:...
Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
I prefer to see
- for (;
unwind_idx>= 0; unwind_idx--) {
+ while (unwind_idx--)Not the same code as
unwind_idxwould be decremented before entering the code block.It's kinda cryptic what you are pointing out.
Not really,
It's. It lacks the very same "additional" words to explain what you meant and why.
Quoted 5 lines
Quoted 4 lines
What's needed additionally is to change
-
unwind_idx=iio_dev_opaque->attached_buffers_cnt- 1;
+unwind_idx= i;You left out that 'additional change' above from your reply.
Yes, that's true, but it took some time to decrypt your message.
Quoted 4 lines
Quoted 4 lines
Of course not. See above. The usual pattern is
while (i--) do_clean_item(i);
Quoted 7 lines
Quoted 3 lines
Of course, but that's not what you replied.
I was merely pointing out that your reply included a logic change converting a loop from for to while.I expect that developers actually think about the changes they do and double check what's proposed by reviewers. If they just copy'n'paste whatever others propose, I wouldn't take any patch from such a developer.
I think in alloc path is using for loop, and in error/free path also using for loop is better to read the code.
Quoted 1 line
Quoted 36 lines · Yang Yingliang
Quoted 35 lines · Andy Shevchenko
Quoted 7 lines · Joe Perches
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Joe Perches
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Alexandru Ardelean
On Tue, Oct 12, 2021 at 12:18 PM Yang Yingliang
<[email protected]> wrote:...
Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
I prefer to see
- for (;
unwind_idx>= 0; unwind_idx--) {
+ while (unwind_idx--)Not the same code as
unwind_idxwould be decremented before entering the code block.It's kinda cryptic what you are pointing out.
Not really,
It's. It lacks the very same "additional" words to explain what you meant and why.
Quoted 5 lines
Quoted 4 lines
What's needed additionally is to change
-
unwind_idx=iio_dev_opaque->attached_buffers_cnt- 1;
+unwind_idx= i;You left out that 'additional change' above from your reply.
Yes, that's true, but it took some time to decrypt your message.
Quoted 7 lines
Quoted 4 lines
Of course not. See above. The usual pattern is
while (i--) do_clean_item(i);Of course, but that's not what you replied.
I was merely pointing out that your reply included a logic change converting a loop from for to while.
Quoted 6 lines
Quoted 4 lines
I expect that developers actually think about the changes they do and double check what's proposed by reviewers. If they just copy'n'paste whatever others propose, I wouldn't take any patch from such a developer.
I think in alloc path is using for loop, and in error/free path also using for loop is better to read the code.
I don't think so.
while(idx--)
is kinda idiom which is really easy to read.
I could send a v2 on your behalf.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 4 lines · Andy Shevchenko
On Wed, Oct 13, 2021 at 12:17 PM Yang Yingliang
<[email protected]> wrote:I could send a v2 on your behalf.
Looking into code further I think that your patch is good as is, see v2 I have just sent.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi,
Quoted 26 lines · Andy Shevchenko
Quoted 17 lines · Alexandru Ardelean
Quoted 11 lines · Yang Yingliang
When
__iio_buffer_alloc_sysfs_and_mask()failed, 'unwind_idx' should be set to 'i - 1' to prevent double-free when cleanup resources.BUG: KASAN: double-free or invalid-free in __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio] Call Trace: kfree+0x117/0x4c0 __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio] iio_buffers_alloc_sysfs_and_mask+0x60d/0x1570 [industrialio] __iio_device_register+0x483/0x1a30 [industrialio] ina2xx_probe+0x625/0x980 [ina2xx_adc]Makes sense.
Thanks for the catch.Reviewed-by Alexandru Ardelean <[email protected]>...
Quoted 5 lines
drivers/iio/industrialio-buffer.c int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev) · line 1620
Quoted 5 lines
ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, i); if (ret) {drivers/iio/industrialio-buffer.c +1
-1- unwind_idx = i; + unwind_idx = i -1 ; goto error_unwind_sysfs_and_mask;
Quoted 6 lines
I prefer to see
- for (;
unwind_idx>= 0; unwind_idx--) {
+ while (unwind_idx--)instead.
With using while loop, 'unwind_idx = iio_dev_opaque->attached_buffers_cnt - 1;' need be changed.
I think my change is clear and simple, do I need resend a new version with using while loop ?
Thanks,
Yang
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
} }