Skip to contentSkip to conversation

iio: imu: st_lsm6dsx: Allocate ext_channels with ARRAY_SIZE()

PATCH In review Kees Cook 3 participants 3 messages Sep 17 – Sep 20

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 iio: imu: st_lsm6dsx: Allocate ext_channels with ARRAY_SIZE()

From: Kees Cook <[email protected]>

In preparation for making the devm_kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This is allocating a copy of magn_channels, which is an array of struct iio_chan_spec, but the size was taken from the whole array, which would make the allocation type a pointer to the array rather than the "struct iio_chan_spec *" being assigned. Allocate ARRAY_SIZE-many entries instead. The resulting allocation size is the same.

Build tested ARCH=x86_64 allmodconfig with GCC 16.2.0:
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.o

Assisted-by LLM coccinelle

---
Cc: Lorenzo Bianconi <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: David Lechner <[email protected]>
Cc: "Nuno Sá" <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: <[email protected]>

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +2 -2
index d6a1eeb151ca..cbc47fa5b101 100644
@@ -766,8 +766,8 @@ st_lsm6dsx_shub_alloc_iiodev(struct st_lsm6dsx_hw *hw,
 			IIO_CHAN_SOFT_TIMESTAMP(3),
 		};
 
-		ext_channels = devm_kzalloc(hw->dev, sizeof(magn_channels),
-					    GFP_KERNEL);
+		ext_channels = devm_kcalloc(hw->dev, ARRAY_SIZE(magn_channels),
+					    sizeof(*ext_channels), GFP_KERNEL);
 		if (!ext_channels)
 			return NULL;
 
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines

From: Kees Cook <[email protected]>

In preparation for making the devm_kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This is allocating a copy of magn_channels, which is an array of struct iio_chan_spec, but the size was taken from the whole array, which would make the allocation type a pointer to the array rather than the "struct iio_chan_spec *" being assigned. Allocate ARRAY_SIZE-many entries instead. The resulting allocation size is the same.

Quoted 5 lines

Build tested ARCH=x86_64 allmodconfig with GCC 16.2.0:
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.o

Assisted-by LLM coccinelle
Quoted 29 lines

---
Cc: Lorenzo Bianconi <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: David Lechner <[email protected]>
Cc: "Nuno Sá" <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: <[email protected]>

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +2 -2
index d6a1eeb151ca..cbc47fa5b101 100644
@@ -766,8 +766,8 @@ st_lsm6dsx_shub_alloc_iiodev(struct st_lsm6dsx_hw *hw,
 			IIO_CHAN_SOFT_TIMESTAMP(3),
 		};
 
-		ext_channels = devm_kzalloc(hw->dev, sizeof(magn_channels),
-					    GFP_KERNEL);
+		ext_channels = devm_kcalloc(hw->dev, ARRAY_SIZE(magn_channels),
+					    sizeof(*ext_channels), GFP_KERNEL);
 		if (!ext_channels)
 			return NULL;
 
Signature · 1 line
2.34.1
maintainer
#2 lore raw
Quoted 14 lines · On Fri, 18 Sep 2026 08:40:05 +0200 Lorenzo Bianconi <[email protected]> wrote:
Quoted 14 lines

From: Kees Cook <[email protected]>

In preparation for making the devm_kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This is allocating a copy of magn_channels, which is an array of struct iio_chan_spec, but the size was taken from the whole array, which would make the allocation type a pointer to the array rather than the "struct iio_chan_spec *" being assigned. Allocate ARRAY_SIZE-many entries instead. The resulting allocation size is the same.

Quoted 7 lines
Quoted 5 lines

Build tested ARCH=x86_64 allmodconfig with GCC 16.2.0:
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.o

Assisted-by LLM coccinelle

Applied to the testing branch of iio.git (next merge window material).

Thanks

Jonathan

Quoted 30 lines
Quoted 30 lines

---
Cc: Lorenzo Bianconi <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: David Lechner <[email protected]>
Cc: "Nuno Sá" <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: <[email protected]>

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +2 -2
index d6a1eeb151ca..cbc47fa5b101 100644
@@ -766,8 +766,8 @@ st_lsm6dsx_shub_alloc_iiodev(struct st_lsm6dsx_hw *hw,
 			IIO_CHAN_SOFT_TIMESTAMP(3),
 		};
 
-		ext_channels = devm_kzalloc(hw->dev, sizeof(magn_channels),
-					    GFP_KERNEL);
+		ext_channels = devm_kcalloc(hw->dev, ARRAY_SIZE(magn_channels),
+					    sizeof(*ext_channels), GFP_KERNEL);
 		if (!ext_channels)
 			return NULL;
 
Signature · 1 line
2.34.1