Skip to contentSkip to conversation

iio: buffer: allocation and freeing buffers fix and optimization

PATCH Applied Andy Shevchenko 2 participants 6 messages Oct 13, 2021 – Nov 20, 2021 IIO

lore
Share & tools
lore
#0 lore raw

v2 cover iio: buffer: allocation and freeing buffers fix and optimization

Yang submitted a fix, but I think the code can be refactored a bit to be more robust against similar mistakes in the future, if any.

In v2:
- put SoB Yang's patch (it's good for backporting)
- added refactoring patch on top of Yang's fix

Andy Shevchenko (1):
iio: buffer: Use dedicated variable in
iio_buffers_alloc_sysfs_and_mask()

Yang Yingliang (1):
iio: buffer: Fix double-free in iio_buffers_alloc_sysfs_and_mask()

 drivers/iio/industrialio-buffer.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
Signature · 1 line
2.33.0
#1 lore raw

PATCH v2 1/2 iio: buffer: Fix double-free in iio_buffers_alloc_sysfs_and_mask()

From: Yang Yingliang <[email protected]>

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]
Fixes ee708e6baacd ("iio: buffer: introduce support for attaching more IIO buffers")
---
 drivers/iio/industrialio-buffer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-buffer.c +1 -1
index 4209e933ab80..bb181d11573c 100644
@@ -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.33.0
#2 lore raw

PATCH v2 2/2 iio: buffer: Use dedicated variable in iio_buffers_alloc_sysfs_and_mask()

In reply to Andy Shevchenko

Use dedicated variable for index in the loop in the iio_buffers_alloc_sysfs_and_mask(). This will make code cleaner and less error prone as proved by previous changes done in this function.

---
 drivers/iio/industrialio-buffer.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/iio/industrialio-buffer.c +7 -11
index bb181d11573c..d53f8e6d5935 100644
@@ -1596,8 +1596,7 @@ int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	const struct iio_chan_spec *channels;
 	struct iio_buffer *buffer;
-	int unwind_idx;
-	int ret, i;
+	int ret, i, idx;
 	size_t sz;
 
 	channels = indio_dev->channels;
@@ -1612,15 +1611,12 @@ int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
 	if (!iio_dev_opaque->attached_buffers_cnt)
 		return 0;
 
-	for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
-		buffer = iio_dev_opaque->attached_buffers[i];
-		ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, i);
-		if (ret) {
-			unwind_idx = i - 1;
+	for (idx = 0; idx < iio_dev_opaque->attached_buffers_cnt; idx++) {
+		buffer = iio_dev_opaque->attached_buffers[idx];
+		ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, idx);
+		if (ret)
 			goto error_unwind_sysfs_and_mask;
-		}
 	}
-	unwind_idx = iio_dev_opaque->attached_buffers_cnt - 1;
 
 	sz = sizeof(*(iio_dev_opaque->buffer_ioctl_handler));
 	iio_dev_opaque->buffer_ioctl_handler = kzalloc(sz, GFP_KERNEL);
@@ -1636,8 +1632,8 @@ int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
 	return 0;
 
 error_unwind_sysfs_and_mask:
-	for (; unwind_idx >= 0; unwind_idx--) {
-		buffer = iio_dev_opaque->attached_buffers[unwind_idx];
+	while (idx--) {
+		buffer = iio_dev_opaque->attached_buffers[idx];
 		__iio_buffer_free_sysfs_and_mask(buffer);
 	}
 	return ret;
Signature · 1 line
2.33.0
maintainer
#3 lore raw
In reply to Andy Shevchenko
Quoted 11 lines · On Wed, 13 Oct 2021 12:49:21 +0300 Andy Shevchenko…

Yang submitted a fix, but I think the code can be refactored a bit to be more robust against similar mistakes in the future, if any.

In v2:
- put SoB Yang's patch (it's good for backporting)
- added refactoring patch on top of Yang's fix

Andy Shevchenko (1):
iio: buffer: Use dedicated variable in
iio_buffers_alloc_sysfs_and_mask()

Quoted 6 lines

Yang Yingliang (1):
iio: buffer: Fix double-free in iio_buffers_alloc_sysfs_and_mask()

 drivers/iio/industrialio-buffer.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

1st patch applied to the fixes-togreg branch of iio.git. I may well end up sending these in the merge window anyway in which case I'll probably stick patch 2 on top of it before sending. If not I'll pick that up next cycle now.

Thanks,

Jonathan

#4 lore raw
Quoted 17 lines · Jonathan Cameron
Quoted 14 lines · On Wed, 13 Oct 2021 12:49:21 +0300 Andy Shevchenko…

Yang submitted a fix, but I think the code can be refactored a bit to be more robust against similar mistakes in the future, if any.

In v2:
- put SoB Yang's patch (it's good for backporting)
- added refactoring patch on top of Yang's fix

Andy Shevchenko (1):
iio: buffer: Use dedicated variable in
iio_buffers_alloc_sysfs_and_mask()

Yang Yingliang (1):
iio: buffer: Fix double-free in iio_buffers_alloc_sysfs_and_mask()

Quoted 6 lines
Quoted 3 lines
 drivers/iio/industrialio-buffer.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

1st patch applied to the fixes-togreg branch of iio.git. I may well end up sending these in the merge window anyway in which case I'll probably stick patch 2 on top of it before sending. If not I'll pick that up next cycle now.

Is it a right time now?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Applied
#5 lore raw
Quoted 18 lines · On Mon, 15 Nov 2021 13:12:24 +0200 Andy Shevchenko…
Quoted 17 lines · Jonathan Cameron
Quoted 14 lines · On Wed, 13 Oct 2021 12:49:21 +0300 Andy Shevchenko…

Yang submitted a fix, but I think the code can be refactored a bit to be more robust against similar mistakes in the future, if any.

In v2:
- put SoB Yang's patch (it's good for backporting)
- added refactoring patch on top of Yang's fix

Andy Shevchenko (1):
iio: buffer: Use dedicated variable in
iio_buffers_alloc_sysfs_and_mask()

Yang Yingliang (1):
iio: buffer: Fix double-free in iio_buffers_alloc_sysfs_and_mask()

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
 drivers/iio/industrialio-buffer.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

1st patch applied to the fixes-togreg branch of iio.git. I may well end up sending these in the merge window anyway in which case I'll probably stick patch 2 on top of it before sending. If not I'll pick that up next cycle now.

Is it a right time now?

Applied, but needed a bit of hand tweaking as patches have crossed with this.

Pushed out as testing for 0-day to see if we missed anything.

Thanks,

Jonathan