iio: buffer: Ensure bounce buffer used for unaligned case is zeroed.
Jonathan Cameron
maintainerPATCH v2 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.
---
v2: Zero the new buffer not the old one (which might not exist)
Drop bonus Reported-by:
--- drivers/iio/industrialio-buffer.c | 1 + 1 file changed, 1 insertion(+)
drivers/iio/industrialio-buffer.c +1 -0
@@ -2445,6 +2445,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; iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes; }
Signature · 1 line
2.54.0
Quoted 3 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. Usememset()to zero it after resizing.
...
drivers/iio/industrialio-buffer.c int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev, · line 2448
Quoted 4 lines
drivers/iio/industrialio-buffer.c +1
-0indio_dev->scan_bytes, GFP_KERNEL);if (!bb)return -ENOMEM ; +memset (bb,0 , indio_dev->scan_bytes);
May I suggest different approach, id est use __GFP_ZERO instead of hunting correct pointers?
With Best Regards,
Andy Shevchenko
Jonathan Cameron
maintainerQuoted 7 lines · On Thu, 4 Jun 2026 12:10:42 +0300 Andy Shevchenko…
Quoted 3 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. Usememset()to zero it after resizing....
Quoted 7 lines
drivers/iio/industrialio-buffer.c int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev, · line 2448
Quoted 4 lines
drivers/iio/industrialio-buffer.c +1
-0indio_dev->scan_bytes, GFP_KERNEL);if (!bb)return -ENOMEM ; +memset (bb,0 , indio_dev->scan_bytes);May I suggest different approach, id est use
__GFP_ZEROinstead of hunting correct pointers?
That's a weird beast when combined with a krealloc so I was a bit nervous about readability (and less so whether it was correct). It should be fine in that we will either get stale data or zeros because we always use this path to allocate the buffer so if you think it is obviously fine then I don't mind.
The oddities are that if an object grows within a slab but doesn't need a new one we are relying on those bits happening to be zero based on the original allocation doing __GFP_ZERO as well (as it's the same call)
I'm nervous though as that region off the end is sometimes used for debug objects and I really don't understand that bit of slab well enough. If it actually does this, then seems like we'd end up with a lot of nasty corner cases so I assume it doesn't. However, I couldn't convince myself enough not to just force a memset of the whole thing.
Quoted 6 lines
With Best Regards,
Andy Shevchenko
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Thu, 4 Jun 2026 12:10:42 +0300 Andy Shevchenko…
On Thu, Jun 04, 2026 at 09:43:07AM +0100, Jonathan Cameron wrote:
...
Quoted 14 lines
Quoted 7 lines
drivers/iio/industrialio-buffer.c int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev, · line 2448
Quoted 4 lines
drivers/iio/industrialio-buffer.c +1
-0indio_dev->scan_bytes, GFP_KERNEL);if (!bb)return -ENOMEM ; +memset (bb,0 , indio_dev->scan_bytes);May I suggest different approach, id est use
__GFP_ZEROinstead of hunting correct pointers?That's a weird beast when combined with a krealloc so I was a bit nervous about readability (and less so whether it was correct). It should be fine in that we will either get stale data or zeros because we always use this path to allocate the buffer so if you think it is obviously fine then I don't mind.
Quoted 8 lines
The oddities are that if an object grows within a slab but doesn't need a new one we are relying on those bits happening to be zero based on the original allocation doing
__GFP_ZEROas well (as it's the same call)I'm nervous though as that region off the end is sometimes used for debug objects and I really don't understand that bit of slab well enough. If it actually does this, then seems like we'd end up with a lot of nasty corner cases so I assume it doesn't.
Such a bug will be a serious issue in mm. I don't think it exists. But, of course, chances are not completely 0.
Current users (not a comprehensive list)
arch/arm64/kvm/nested.c:92 drivers/firmware/efi/capsule-loader.c:61 drivers/gpio/gpiolib.c:5198 drivers/media/platform/amphion/vdec.c:329 drivers/net/ethernet/intel/ice/ice_lib.c:3044 drivers/net/ethernet/mellanox/mlx5/core/steering/hws/bwc.c:785 drivers/net/wireless/ti/wl18xx/main.c:1582 drivers/nvme/target/pci-epf.c:708 drivers/spi/spi.c:1411 drivers/usb/gadget/function/uvc_configfs.c:880 fs/ceph/mdsmap.c:322fs/smb/server/ksmbd_work.c:123 lib/tests/slub_kunit.c:273 sound/soc/meson/meson-card-utils.c:49
Also note the kernel-doc for krealloc_array(). It has some note WRT __GFP_ZERO and it means the flag must be supported. Also there is a note in mm/slub.c.
Quoted 2 lines
However, I couldn't convince myself enough not to just force a memset of the whole thing.
I personally would go with __GFP_ZERO.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 50 lines · Andy Shevchenko
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Thu, 4 Jun 2026 12:10:42 +0300 Andy Shevchenko…
On Thu, Jun 04, 2026 at 09:43:07AM +0100, Jonathan Cameron wrote:
...
Quoted 14 lines
Quoted 7 lines
drivers/iio/industrialio-buffer.c int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev, · line 2448
Quoted 4 lines
indio_dev->scan_bytes,GFP_KERNEL); if (!bb)
return-ENOMEM;drivers/iio/industrialio-buffer.c +1
-0+ memset(bb, 0, indio_dev->scan_bytes);May I suggest different approach, id est use
__GFP_ZEROinstead of hunting correct pointers?That's a weird beast when combined with a krealloc so I was a bit nervous about readability (and less so whether it was correct). It should be fine in that we will either get stale data or zeros because we always use this path to allocate the buffer so if you think it is obviously fine then I don't mind.
Quoted 8 lines
The oddities are that if an object grows within a slab but doesn't need a new one we are relying on those bits happening to be zero based on the original allocation doing
__GFP_ZEROas well (as it's the same call)I'm nervous though as that region off the end is sometimes used for debug objects and I really don't understand that bit of slab well enough. If it actually does this, then seems like we'd end up with a lot of nasty corner cases so I assume it doesn't.
Such a bug will be a serious issue in mm. I don't think it exists. But, of course, chances are not completely 0.
Current users (not a comprehensive list)
arch/arm64/kvm/nested.c:92drivers/firmware/efi/capsule-loader.c:61drivers/gpio/gpiolib.c:5198drivers/media/platform/amphion/vdec.c:329drivers/net/ethernet/intel/ice/ice_lib.c:3044drivers/net/ethernet/mellanox/mlx5/core/steering/hws/bwc.c:785drivers/net/wireless/ti/wl18xx/main.c:1582drivers/nvme/target/pci-epf.c:708drivers/spi/spi.c:1411drivers/usb/gadget/function/uvc_configfs.c:880fs/ceph/mdsmap.c:322fs/smb/server/ksmbd_work.c:123lib/tests/slub_kunit.c:273sound/soc/meson/meson-card-utils.c:49
Quoted 7 lines
Also note the kernel-doc for
krealloc_array(). It has some note WRT__GFP_ZEROand it means the flag must be supported. Also there is a note inmm/slub.c.Quoted 2 lines
However, I couldn't convince myself enough not to just force a memset of the whole thing.
I personally would go with
__GFP_ZERO.
If I'm not missing anything, this should also be proof that the flag should be fine:
https://elixir.bootlin.com/linux/v7.1-rc7/source/lib/tests/slub_kunit.c#L275
- Nuno Sá
Jonathan Cameron
maintainerQuoted 54 lines · On Mon, 08 Jun 2026 21:00:30 +0100 Nuno Sá <[email protected]> wrote:
Quoted 53 lines · Andy Shevchenko
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Thu, 4 Jun 2026 12:10:42 +0300 Andy Shevchenko…
On Thu, Jun 04, 2026 at 09:43:07AM +0100, Jonathan Cameron wrote:
...
Quoted 14 lines
Quoted 7 lines
drivers/iio/industrialio-buffer.c int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev, · line 2448
Quoted 4 lines
indio_dev->scan_bytes,GFP_KERNEL); if (!bb)
return-ENOMEM;drivers/iio/industrialio-buffer.c +1
-0+ memset(bb, 0, indio_dev->scan_bytes);May I suggest different approach, id est use
__GFP_ZEROinstead of hunting correct pointers?That's a weird beast when combined with a krealloc so I was a bit nervous about readability (and less so whether it was correct). It should be fine in that we will either get stale data or zeros because we always use this path to allocate the buffer so if you think it is obviously fine then I don't mind.
Quoted 8 lines
The oddities are that if an object grows within a slab but doesn't need a new one we are relying on those bits happening to be zero based on the original allocation doing
__GFP_ZEROas well (as it's the same call)I'm nervous though as that region off the end is sometimes used for debug objects and I really don't understand that bit of slab well enough. If it actually does this, then seems like we'd end up with a lot of nasty corner cases so I assume it doesn't.
Such a bug will be a serious issue in mm. I don't think it exists. But, of course, chances are not completely 0.
Current users (not a comprehensive list)
arch/arm64/kvm/nested.c:92drivers/firmware/efi/capsule-loader.c:61drivers/gpio/gpiolib.c:5198drivers/media/platform/amphion/vdec.c:329drivers/net/ethernet/intel/ice/ice_lib.c:3044drivers/net/ethernet/mellanox/mlx5/core/steering/hws/bwc.c:785drivers/net/wireless/ti/wl18xx/main.c:1582drivers/nvme/target/pci-epf.c:708drivers/spi/spi.c:1411drivers/usb/gadget/function/uvc_configfs.c:880fs/ceph/mdsmap.c:322fs/smb/server/ksmbd_work.c:123lib/tests/slub_kunit.c:273sound/soc/meson/meson-card-utils.c:49Also note the kernel-doc for
krealloc_array(). It has some note WRT__GFP_ZEROand it means the flag must be supported. Also there is a note inmm/slub.c.
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
However, I couldn't convince myself enough not to just force a memset of the whole thing.
I personally would go with
__GFP_ZERO.If I'm not missing anything, this should also be proof that the flag should be fine:
https://elixir.bootlin.com/linux/v7.1-rc7/source/lib/tests/slub_kunit.c#L275
Perfect! Thanks. I'll hopefully spin an updated version in next few days
Jonathan
Quoted 1 line
- Nuno Sá