[PATCH 04/10] iio: adc: stm32-adc: Validate channel index before writing chan_name

From: Lee Jones

Date: Thu Nov 19 2026 - 10:33:13 EST


stm32_adc_generic_chan_init() reads the child node's reg property into a
signed int and, when the optional label property is present, writes into
adc->chan_name[val] and calls stm32_adc_populate_int_ch() before
checking whether val exceeds adc_info->max_channels. Because val is
signed, values with the top bit set also become negative and bypass the
upper-bound check altogether.

Store val as a u32, matching fwnode_property_read_u32(), and move the
max_channels bounds check immediately after reading reg.

Fixes: 95bc818404b2 ("iio: adc: stm32-adc: add support of generic channels binding")
Closes: https://sashiko.dev/#/bug/linux-efd2944a-ec30-488d-b2ac-1c7feb831095
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/iio/adc/stm32-adc.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
index ab99116aea0a..42f1fc64250a 100644
--- a/drivers/iio/adc/stm32-adc.c
+++ b/drivers/iio/adc/stm32-adc.c
@@ -2745,9 +2745,9 @@ static int stm32_adc_generic_chan_init(struct iio_dev *indio_dev,
const struct stm32_adc_info *adc_info = adc->cfg->adc_info;
struct device *dev = &indio_dev->dev;
const char *name;
- int val, scan_index = 0, ret;
+ int scan_index = 0, ret;
bool differential;
- u32 vin[2];
+ u32 val, vin[2];

device_for_each_child_node_scoped(dev, child) {
ret = fwnode_property_read_u32(child, "reg", &val);
@@ -2755,6 +2755,10 @@ static int stm32_adc_generic_chan_init(struct iio_dev *indio_dev,
return dev_err_probe(dev, ret,
"Missing channel index\n");

+ if (val >= adc_info->max_channels)
+ return dev_err_probe(dev, -EINVAL,
+ "Invalid channel %u\n", val);
+
ret = fwnode_property_read_string(child, "label", &name);
/* label is optional */
if (!ret) {
@@ -2773,10 +2777,6 @@ static int stm32_adc_generic_chan_init(struct iio_dev *indio_dev,
return dev_err_probe(dev, ret, "Invalid label\n");
}

- if (val >= adc_info->max_channels)
- return dev_err_probe(dev, -EINVAL,
- "Invalid channel %d\n", val);
-
differential = false;
ret = fwnode_property_read_u32_array(child, "diff-channels", vin, 2);
/* diff-channels is optional */
--
2.56.1.708.g77570a6a1d-goog