iio: check register fields used as table indices
Subsystems (4)
cover iio: check register fields used as table indices
These three patches add a check where a driver uses a register field read from the chip as an index into a lookup table. Each is the same kind of simple check as the recent rm3100 patch and returns -ERANGE for a value that the driver never writes.
A few other IIO drivers have the same pattern. I plan to send those after this series, unless another form is preferred.
Maoyi Xie (3):
iio: addac: ad74115: check the rate code in ad74115_get_adc_rate()
iio: imu: bno055: check the hw_xlate lookup in bno055_get_regmask()
iio: imu: bmi323: check the config fields used as table indices
drivers/iio/addac/ad74115.c | 3 +++ drivers/iio/imu/bmi323/bmi323_core.c | 9 +++++++++ drivers/iio/imu/bno055/bno055.c | 5 ++++- 3 files changed, 16 insertions(+), 1 deletion(-)
base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea
Signature · 1 line
2.34.1
PATCH 1/3 iio: addac: ad74115: check the rate code in ad74115_get_adc_rate()
ad74115_get_adc_rate() reads ADC_CONFIG from the chip and uses the 3-bit conversion rate code as an index into ad74115_adc_conv_rate_tbl[] without a check. The table has five entries, so the codes 5 to 7 read past its end. Without the adc_rdy interrupt, _ad74115_get_adc_code() divides by the value read there. The driver only writes the codes 0 to 4 in ad74115_set_adc_rate(), so this needs a chip that returns a value it should not.
Check the code against the table size and return -ERANGE.
---
Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under ASan and UBSan, which report nothing with this patch. Not tested on hardware, only compile-tested with W=1 on next-20261116.
I used an LLM to help write the harness and this patch.
drivers/iio/addac/ad74115.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/iio/addac/ad74115.c +3 -0
@@ -784,6 +784,9 @@ static int ad74115_get_adc_rate(struct ad74115_state *st,else i =FIELD_GET (AD74115_ADC_CONFIG_CONV2_RATE_MASK , i); +if (i >=ARRAY_SIZE (ad74115_adc_conv_rate_tbl)) +return -ERANGE ; + *val = ad74115_adc_conv_rate_tbl[i];return IIO_VAL_INT ;
Signature · 1 line
2.34.1
PATCH 2/3 iio: imu: bno055: check the hw_xlate lookup in bno055_get_regmask()
In reply to Maoyi Xiebno055_get_regmask() translates a register field through attr->hw_xlate[] into an index into attr->vals[]. When no entry matches, the raw field is used as the index. The gyroscope range field has three bits and bno055_gyr_scale_hw_xlate[] covers 0 to 4, so 5 to 7 read past bno055_gyr_scale_vals[]. The IIO core then divides by an int read past the table. In an x86-64 gcc build that int is zero. bno055_set_regmask() only writes 0 to 4, so this needs a chip or bus that returns a value it should not.
Commit 399b883ec828 ("iio: imu: bno055: fix OOB access of hw_xlate array") fixed the bound of the lookup loop. Return -ERANGE when no entry matches.
---
Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under ASan and UBSan, which report nothing with this patch. Not tested on hardware, only compile-tested with W=1 on next-20261116.
I used an LLM to help write the harness and this patch.
drivers/iio/imu/bno055/bno055.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/imu/bno055/bno055.c +4 -1
@@ -587,12 +587,15 @@ static int bno055_get_regmask(struct bno055_priv *priv, int *val, int *val2,return ret; idx = (hwval & mask) >> shift; -if (attr->hw_xlate) +if (attr->hw_xlate) {for (i =0 ; i < attr->hw_xlate_len; i++)if (attr->hw_xlate[i] == idx) { idx = i;break ; } +if (i == attr->hw_xlate_len) +return -ERANGE ; + }if (attr->type ==IIO_VAL_INT ) { *val = attr->vals[idx]; }else {/* IIO_VAL_INT_PLUS_MICRO or IIO_VAL_FRACTIONAL */
Signature · 1 line
2.34.1
PATCH 3/3 iio: imu: bmi323: check the config fields used as table indices
In reply to Maoyi Xiebmi323_get_average(), bmi323_get_odr() and bmi323_get_scale() read ACC_CONF or GYRO_CONF from the sensor. They use the AVG, ODR and SCL fields to index lookup tables without a check. An AVG value of 7, an ODR value of 0 or above 11, or an SCL value above 3 (accel) or 4 (gyro) reads outside its table. The driver only writes values that have a table entry, so this needs a chip or bus that returns a value it should not.
Check each field against the size of its table and return -ERANGE.
---
Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under ASan and UBSan, which report nothing with this patch. Not tested on hardware, only compile-tested with W=1 on next-20261116.
I used an LLM to help write the harness and this patch.
drivers/iio/imu/bmi323/bmi323_core.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/iio/imu/bmi323/bmi323_core.c +9 -0
@@ -1475,6 +1475,9 @@ static int bmi323_get_average(struct bmi323_data *data, } raw =FIELD_GET (BMI323_ACC_GYRO_CONF_AVG_MSK , value); +if (raw >=ARRAY_SIZE (bmi323_accel_gyro_avrg)) +return -ERANGE ; + *avg = bmi323_accel_gyro_avrg[raw];return IIO_VAL_INT ;
@@ -1578,6 +1581,9 @@ static int bmi323_get_odr(struct bmi323_data *data, } odr_raw =FIELD_GET (BMI323_ACC_GYRO_CONF_ODR_MSK , value); +if (odr_raw <1 || odr_raw >ARRAY_SIZE (bmi323_acc_gyro_odr)) +return -ERANGE ; + *odr = bmi323_acc_gyro_odr[odr_raw -1 ][0 ]; *uodr = bmi323_acc_gyro_odr[odr_raw -1 ][1 ];
@@ -1641,6 +1647,9 @@ static int bmi323_get_scale(struct bmi323_data *data, } scale_raw =FIELD_GET (BMI323_ACC_GYRO_CONF_SCL_MSK , value); +if (scale_raw >= bmi323_hw[sensor].scale_table_len) +return -ERANGE ; + *val2 = bmi323_hw[sensor].scale_table[scale_raw][1 ];return IIO_VAL_INT_PLUS_MICRO ;
Signature · 1 line
2.34.1