Skip to contentSkip to conversation

iio: buffer: Ensure bounce buffer used for unaligned case is zeroed.

PATCH Superseded Jonathan Cameron 4 participants 7 messages May 31 – Jun 8 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

PATCH 1/1 iio: buffer: Ensure bounce buffer used for unaligned case is zeroed.

iio_push_to_buffers_with_ts_unaligned() leaks uninitialized heap memory to userspace if the data passed in is not a multiple of 8 bytes and the timestamp is enabled. Use memset() to zero it after resizing.

Fixes 95ec3fdf2b79 ("iio: core: Introduce iio_push_to_buffers_with_ts_unaligned()")

---
Cc: Jinseob Kim <[email protected]>

This is untested as I don't have any devices that use this and long ago dropped whatever hacks I used to test it originally.

Jinseob, could you please test this with your OSF driver - at least make sure it doesn't stop the data coming out!

---
 drivers/iio/industrialio-buffer.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iio/industrialio-buffer.c +1 -0
index 9d66510a1d49..d930e70e12ff 100644
@@ -2466,6 +2466,7 @@ int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
 				   indio_dev->scan_bytes, GFP_KERNEL);
 		if (!bb)
 			return -ENOMEM;
+		memset(iio_dev_opaque->bounce_buffer, 0, indio_dev->scan_bytes);
 		iio_dev_opaque->bounce_buffer = bb;
 		iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;
 	}
Signature · 1 line
2.54.0
#1 lore raw
Quoted 6 lines · Jonathan Cameron

iio_push_to_buffers_with_ts_unaligned() leaks uninitialized heap memory to userspace if the data passed in is not a multiple of 8 bytes and the timestamp is enabled. Use memset() to zero it after resizing.

Fixes 95ec3fdf2b79 ("iio: core: Introduce iio_push_to_buffers_with_ts_unaligned()")

Hi Jonathan,

Looks like "Reported-by:" is duplicated.

Thanks, Sanjay

Quoted 26 lines

---
Cc: Jinseob Kim <[email protected]>

This is untested as I don't have any devices that use this and long ago dropped whatever hacks I used to test it originally.

Jinseob, could you please test this with your OSF driver - at least make sure it doesn't stop the data coming out!
---
drivers/iio/industrialio-buffer.c | 1 +

1 file changed, 1 insertion(+)
drivers/iio/industrialio-buffer.c +1 -0
index 9d66510a1d49..d930e70e12ff 100644
@@ -2466,6 +2466,7 @@ int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
 				   indio_dev->scan_bytes, GFP_KERNEL);
 		if (!bb)
 			return -ENOMEM;
+		memset(iio_dev_opaque->bounce_buffer, 0, indio_dev->scan_bytes);
 		iio_dev_opaque->bounce_buffer = bb;
 		iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;
 	}
author maintainer
#2 lore raw
Quoted 11 lines · On Sun, 31 May 2026 23:58:25 +0530 Sanjay Chitroda…
Quoted 6 lines · Jonathan Cameron

iio_push_to_buffers_with_ts_unaligned() leaks uninitialized heap memory to userspace if the data passed in is not a multiple of 8 bytes and the timestamp is enabled. Use memset() to zero it after resizing.

Fixes 95ec3fdf2b79 ("iio: core: Introduce iio_push_to_buffers_with_ts_unaligned()")

Hi Jonathan,

Looks like "Reported-by:" is duplicated.

I'll tweak that whilst applying.

I would like a few more eyes on this though just to confirm I didn't mess it up!

Thanks,

Jonathan

Quoted 29 lines

Thanks, Sanjay

Quoted 26 lines

---
Cc: Jinseob Kim <[email protected]>

This is untested as I don't have any devices that use this and long ago dropped whatever hacks I used to test it originally.

Jinseob, could you please test this with your OSF driver - at least make sure it doesn't stop the data coming out!
---
drivers/iio/industrialio-buffer.c | 1 +

1 file changed, 1 insertion(+)
drivers/iio/industrialio-buffer.c +1 -0
index 9d66510a1d49..d930e70e12ff 100644
@@ -2466,6 +2466,7 @@ int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
 				   indio_dev->scan_bytes, GFP_KERNEL);
 		if (!bb)
 			return -ENOMEM;
+		memset(iio_dev_opaque->bounce_buffer, 0, indio_dev->scan_bytes);
 		iio_dev_opaque->bounce_buffer = bb;
 		iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;
 	}  
#3 lore raw
Quoted 12 lines · Jonathan Cameron
Quoted 8 lines · On Sun, 31 May 2026 23:58:25 +0530 Sanjay Chitroda…
Quoted 6 lines · Jonathan Cameron

iio_push_to_buffers_with_ts_unaligned() leaks uninitialized heap memory to userspace if the data passed in is not a multiple of 8 bytes and the timestamp is enabled. Use memset() to zero it after resizing.

Fixes 95ec3fdf2b79 ("iio: core: Introduce iio_push_to_buffers_with_ts_unaligned()")
Quoted 6 lines
Quoted 3 lines

Hi Jonathan,

Looks like "Reported-by:" is duplicated.

I'll tweak that whilst applying.

I would like a few more eyes on this though just to confirm I didn't mess it up!

Sashiko had some remarks about the wrong member being memset, however it seems relatively standard... human 1, Sashiko 0?

Does this memset operate on the wrong pointer?
When iio_push_to_buffers_with_ts_unaligned() is called for the first time, iio_dev_opaque->bounce_buffer is NULL. devm_krealloc() allocates a new buffer and returns it in bb, but this memset uses the old pointer, which would result in a NULL pointer dereference.
Additionally, if scan_bytes increases on subsequent calls, devm_krealloc() might free the old block. This would cause memset to write into freed memory using the new, larger size, resulting in a use-after-free and out-of-bounds write.

Signature · 3 lines
Kind regards

CJD
author maintainer
#4 lore raw
Quoted 23 lines · On Wed, 3 Jun 2026 16:11:15 +0200 Joshua Crofts <[email protected]>…
Quoted 12 lines · Jonathan Cameron
Quoted 8 lines · On Sun, 31 May 2026 23:58:25 +0530 Sanjay Chitroda…
Quoted 6 lines · Jonathan Cameron

iio_push_to_buffers_with_ts_unaligned() leaks uninitialized heap memory to userspace if the data passed in is not a multiple of 8 bytes and the timestamp is enabled. Use memset() to zero it after resizing.

Fixes 95ec3fdf2b79 ("iio: core: Introduce iio_push_to_buffers_with_ts_unaligned()")
Quoted 6 lines
Quoted 3 lines

Hi Jonathan,

Looks like "Reported-by:" is duplicated.

I'll tweak that whilst applying.

I would like a few more eyes on this though just to confirm I didn't mess it up!

Sashiko had some remarks about the wrong member being memset, however it seems relatively standard... human 1, Sashiko 0?

Quoted 10 lines

Does this memset operate on the wrong pointer?
When iio_push_to_buffers_with_ts_unaligned() is called for the first time, iio_dev_opaque->bounce_buffer is NULL. devm_krealloc() allocates a new buffer and returns it in bb, but this memset uses the old pointer, which would result in a NULL pointer dereference.
Additionally, if scan_bytes increases on subsequent calls, devm_krealloc() might free the old block. This would cause memset to write into freed memory using the new, larger size, resulting in a use-after-free and out-of-bounds write.

doh. It is right - the patch is garbage. 1 to sashiko 0 to idiot developer.

I messed up the line ordering. Memset should be at least one line later.

drivers/iio/industrialio-buffer.c +1 -0
@@ -2466,6 +2466,7 @@ int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
 				   indio_dev->scan_bytes, GFP_KERNEL);
 		if (!bb)
 			return -ENOMEM;
 		iio_dev_opaque->bounce_buffer = bb; // the buffering being zeroed is set here.
 		iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;
+		memset(iio_dev_opaque->bounce_buffer, 0, indio_dev->scan_bytes);
 	}

Or better as

drivers/iio/industrialio-buffer.c +0 -0
@@ -2466,6 +2466,7 @@ int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
 				   indio_dev->scan_bytes, GFP_KERNEL);
 		if (!bb)
 			return -ENOMEM;
		memset(bb, 0, indio_dev->scan_bytes);

iio_dev_opaque->bounce_buffer = bb; // the buffering being zeroed is set here. iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;

}

Jonathan

#5 lore raw
Quoted 14 lines · Jonathan Cameron
Quoted 13 lines

Sashiko had some remarks about the wrong member being memset, however it seems relatively standard... human 1, Sashiko 0?

Does this memset operate on the wrong pointer?
When iio_push_to_buffers_with_ts_unaligned() is called for the first time, iio_dev_opaque->bounce_buffer is NULL. devm_krealloc() allocates a new buffer and returns it in bb, but this memset uses the old pointer, which would result in a NULL pointer dereference.
Additionally, if scan_bytes increases on subsequent calls, devm_krealloc() might free the old block. This would cause memset to write into freed memory using the new, larger size, resulting in a use-after-free and out-of-bounds write.

Quoted 3 lines

doh. It is right - the patch is garbage. 1 to sashiko 0 to idiot developer.

I messed up the line ordering. Memset should be at least one line later.

Right, I must be losing my marbles as I've completely glanced over this even after reading Sashiko's comment :( At least my remark had some sort of purpose.

Signature · 3 lines
Kind regards

CJD
Tested-by
#6 lore raw
Quoted 2 lines

Jinseob, could you please test this with your OSF driver - at least make sure it doesn't stop the data coming out!

I tested the v2 patch as posted, with:

        memset(bb, 0, indio_dev->scan_bytes);

The test was done on a Raspberry Pi running 6.12.75+rpt-rpi-v8 with an OSF GREEN prototype connected over UART. The patched industrialio.ko was loaded non-persistently from /tmp, without overwriting /lib/modules and without depmod or reboot.

With the patched industrialio module active, the OSF driver registered:

osf-accel
osf-gyro
osf-magn
osf-temp

Raw reads passed for all four devices. Buffer reads also passed:

osf-accel: 2 samples, scan_bytes = 24
osf-gyro: 2 samples, scan_bytes = 24
osf-magn: 1 sample, scan_bytes = 24
osf-temp: 2 samples, scan_bytes = 16

The padding bytes were zero in the captured samples:

3-axis devices: bytes 12..15 were zero
temperature: bytes 4..7 were zero

No OSF/IIO oops, panic, BUG, WARNING, or call trace was observed.

This test covers the current memset(bb, ...) version of the patch, not a possible __GFP_ZERO variant.

Thanks,

Jinseob

2026년 6월 4일 (목) 오후 5:43, Joshua Crofts <[email protected]>님이 작성:

Quoted 28 lines · Jonathan Cameron
Quoted 14 lines
Quoted 13 lines

Sashiko had some remarks about the wrong member being memset, however it seems relatively standard... human 1, Sashiko 0?

Does this memset operate on the wrong pointer?
When iio_push_to_buffers_with_ts_unaligned() is called for the first time, iio_dev_opaque->bounce_buffer is NULL. devm_krealloc() allocates a new buffer and returns it in bb, but this memset uses the old pointer, which would result in a NULL pointer dereference.
Additionally, if scan_bytes increases on subsequent calls, devm_krealloc() might free the old block. This would cause memset to write into freed memory using the new, larger size, resulting in a use-after-free and out-of-bounds write.

Quoted 3 lines

doh. It is right - the patch is garbage. 1 to sashiko 0 to idiot developer.

I messed up the line ordering. Memset should be at least one line later.

Right, I must be losing my marbles as I've completely glanced over this even after reading Sashiko's comment :( At least my remark had some sort of purpose.

--
Kind regards

CJD