[PATCH 05/18] iio: adc: ad4170-4: Fix out-of-bounds accesses to pins_fn[]

From: Janani Sunil

Date: Thu Dec 03 2026 - 10:20:39 EST


pins_fn[] has one entry per analog input pin, but it is indexed with
unchecked devicetree values. Special inputs such as AVSS and REFOUT are
encoded as 17 to 29, so the VBIAS tests in ad4170_get_ain_voltage_uv()
and ad4170_parse_external_sensor() read past the end of the array
whenever a channel uses one.

Bound the two VBIAS tests and reject out-of-range adi,vbias-pins.

Fixes: 6098df897d13 ("iio: adc: ad4170-4: Add support for weigh scale, thermocouple, and RTD sens")
Signed-off-by: Janani Sunil <janani.sunil@xxxxxxxxxx>
---
drivers/iio/adc/ad4170-4.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/adc/ad4170-4.c b/drivers/iio/adc/ad4170-4.c
index a04ab03f3938..9bad480e69d7 100644
--- a/drivers/iio/adc/ad4170-4.c
+++ b/drivers/iio/adc/ad4170-4.c
@@ -984,7 +984,8 @@ static int ad4170_get_ain_voltage_uv(struct ad4170_state *st, int ain_n,
* So, we do AVDD - AVSS to compute the DC voltage generated by the bias
* voltage generator.
*/
- if (st->pins_fn[ain_n] & AD4170_PIN_VBIAS) {
+ if (ain_n < AD4170_NUM_ANALOG_PINS &&
+ st->pins_fn[ain_n] & AD4170_PIN_VBIAS) {
int v_diff = st->vrefs_uv[AD4170_AVDD_SUP] - st->vrefs_uv[AD4170_AVSS_SUP];
*ain_voltage = v_diff / 2;
return 0;
@@ -2144,7 +2145,8 @@ static int ad4170_parse_external_sensor(struct ad4170_state *st,
ac_excited = fwnode_property_read_bool(child, "adi,excitation-ac");

if (s_type == AD4170_THERMOCOUPLE_SENSOR) {
- if (st->pins_fn[chan->channel2] & AD4170_PIN_VBIAS) {
+ if (chan->channel2 < AD4170_NUM_ANALOG_PINS &&
+ st->pins_fn[chan->channel2] & AD4170_PIN_VBIAS) {
reg_val = BIT(chan->channel2);
ret = regmap_write(st->regmap, AD4170_V_BIAS_REG, reg_val);
if (ret)
@@ -2568,8 +2570,14 @@ static int ad4170_parse_firmware(struct iio_dev *indio_dev)
return dev_err_probe(dev, ret,
"Failed to read vbias pins\n");

- for (i = 0; i < num_vbias_pins; i++)
+ for (i = 0; i < num_vbias_pins; i++) {
+ if (vbias_pins[i] >= AD4170_NUM_ANALOG_PINS)
+ return dev_err_probe(dev, -EINVAL,
+ "Invalid vbias pin %u\n",
+ vbias_pins[i]);
+
st->pins_fn[vbias_pins[i]] |= AD4170_PIN_VBIAS;
+ }
}

ret = ad4170_parse_channels(indio_dev);

--
2.43.0