Skip to contentSkip to conversation

iio: adc: mcp3911: Fix out of bounds access of array mcp3911_osr_table

PATCH In review Colin Ian King 2 participants 2 messages Oct 12, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: adc: mcp3911: Fix out of bounds access of array mcp3911_osr_table

Currently the for-loop is using the size of the array mcp3911_osr_table as the upper bounds which is 0..31 which is causing an out of bounds access to the array at indexes 8..31. Fix this using the ARRAY_SIZE macro to get the size of the array in elements rather than bytes.

Fixes 6d965885f4ea ("iio: adc: mcp3911: add support for oversampling ratio")
---
 drivers/iio/adc/mcp3911.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/mcp3911.c +1 -1
index b35fd2c9c3c0..015a9ffdb26a 100644
@@ -248,7 +248,7 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
 		break;
 
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
-		for (int i = 0; i < sizeof(mcp3911_osr_table); i++) {
+		for (int i = 0; i < ARRAY_SIZE(mcp3911_osr_table); i++) {
 			if (val == mcp3911_osr_table[i]) {
 				val = FIELD_PREP(MCP3911_CONFIG_OSR, i);
 				ret = mcp3911_update(adc, MCP3911_REG_CONFIG, MCP3911_CONFIG_OSR,
Signature · 1 line
2.37.3
maintainer
#1 lore raw

Hi Colin,

Quoted 7 lines · Colin Ian King

Currently the for-loop is using the size of the array mcp3911_osr_table as the upper bounds which is 0..31 which is causing an out of bounds access to the array at indexes 8..31. Fix this using the ARRAY_SIZE macro to get the size of the array in elements rather than bytes.

Fixes 6d965885f4ea ("iio: adc: mcp3911: add support for oversampling ratio")

Thank you for the patch!

Dan was a little bit earlier to catch this though
https://lore.kernel.org/all/20221002132053.113f595f@jic23-huawei/

Best regards,
Marcus Folkesson