Skip to contentSkip to conversation

iio: hw_consumer: Use struct_size() helper in kzalloc()

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Jan 20, 2022 – Jan 22, 2022 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: hw_consumer: Use struct_size() helper in kzalloc()

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/iio/buffer/industrialio-hw-consumer.c:63:23: warning: using sizeof on a flexible structure

---
 drivers/iio/buffer/industrialio-hw-consumer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/buffer/industrialio-hw-consumer.c +2 -2
index 87d9aabd20c7..fb58f599a80b 100644
@@ -52,7 +52,6 @@ static const struct iio_buffer_access_funcs iio_hw_buf_access = {
 static struct hw_consumer_buffer *iio_hw_consumer_get_buffer(
 	struct iio_hw_consumer *hwc, struct iio_dev *indio_dev)
 {
-	size_t mask_size = BITS_TO_LONGS(indio_dev->masklength) * sizeof(long);
 	struct hw_consumer_buffer *buf;
 
 	list_for_each_entry(buf, &hwc->buffers, head) {
@@ -60,7 +59,8 @@ static struct hw_consumer_buffer *iio_hw_consumer_get_buffer(
 			return buf;
 	}
 
-	buf = kzalloc(sizeof(*buf) + mask_size, GFP_KERNEL);
+	buf = kzalloc(struct_size(buf, scan_mask, BITS_TO_LONGS(indio_dev->masklength)),
+		      GFP_KERNEL);
 	if (!buf)
 		return NULL;
 
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Gustavo A. R. Silva

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/iio/buffer/industrialio-hw-consumer.c:63:23: warning: using sizeof on a flexible structure

Looks good to me.

Signature · 1 line
Kees Cook
maintainer
#2 lore raw
Quoted 8 lines · On Thu, 20 Jan 2022 15:33:14 -0800 Kees Cook <[email protected]> wrote:
Quoted 7 lines · Gustavo A. R. Silva

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/iio/buffer/industrialio-hw-consumer.c:63:23: warning: using sizeof on a flexible structure

Quoted 7 lines
Quoted 2 lines

Looks good to me.

Good thing to tidy up, but I'm a little curious why I'm not seeing these reports with latest sparse?
Ah. Found it via the report linked CF='-Wflexible-array-sizeof'

Probably worth mentioned that in the patch descriptions. I've added it to this one.

I'm still setting the sparse report even with this patch.

What am I missing?

Jonathan