Skip to contentSkip to conversation

iio: check more register fields used as table indices

PATCH In review Maoyi Xie 2 participants 6 messages Nov 30

Subsystems (3)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

cover iio: check more register fields used as table indices

As mentioned in the cover letter of the first series [1], a few other IIO drivers have the same pattern. This series adds the same kind of check to three of them.

Patch 2 only changes the return type of two fxls8962af helpers to int. Patch 3 then adds the check to one of them.

[1] https://lore.kernel.org/linux-iio/[email protected]/

Maoyi Xie (4):
iio: accel: adxl380: check the HPF corner code in adxl380_get_hpf()
iio: accel: fxls8962af: return int from two read helpers
iio: accel: fxls8962af: check WAKE_ODR in fxls8962af_read_samp_freq()
iio: light: rpr0521: check the gain index in rpr0521_get_gain()

 drivers/iio/accel/adxl380.c         | 2 ++
 drivers/iio/accel/fxls8962af-core.c | 9 +++++----
 drivers/iio/light/rpr0521.c         | 3 +++
 3 files changed, 10 insertions(+), 4 deletions(-)

base-commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/4 iio: accel: adxl380: check the HPF corner code in adxl380_get_hpf()

adxl380_get_hpf() reads the FILTER register and uses its 3-bit HPF corner code as an index into st->hpf_tbl[] without a check. The table has seven rows, so code 7 reads the eight bytes that follow it in struct adxl380_state. The driver only writes codes 0 to 6 in adxl380_set_hpf(), so this needs a chip or bus 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 UBSan. Not tested on hardware.

I used an LLM to help write the harness and this patch.

 drivers/iio/accel/adxl380.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/accel/adxl380.c +2 -0
index 03b140303520..42696e31376c 100644
@@ -615,6 +615,8 @@ static int adxl380_get_hpf(struct adxl380_state *st, int *hpf_int, int *hpf_frac
 		return ret;
 
 	hpf_idx = FIELD_GET(ADXL380_FILTER_HPF_CORNER_MSK, trig_cfg);
+	if (hpf_idx >= ARRAY_SIZE(st->hpf_tbl))
+		return -ERANGE;
 
 	*hpf_int = st->hpf_tbl[hpf_idx][0];
 	*hpf_frac = st->hpf_tbl[hpf_idx][1];
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/4 iio: accel: fxls8962af: return int from two read helpers

In reply to Maoyi Xie

fxls8962af_read_full_scale() and fxls8962af_read_samp_freq() are declared to return unsigned int. They return an IIO_VAL_* type or the negative error code of regmap_read(), and fxls8962af_read_raw() returns their value as an int. Declare both to return int.

No functional change intended.

---
Not tested on hardware, only compile-tested with W=1 on next-20261116.

I used an LLM to help write this patch.

 drivers/iio/accel/fxls8962af-core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/accel/fxls8962af-core.c +3 -4
index 18d7b09bddd2..fd00b1d3e876 100644
@@ -361,8 +361,7 @@ static int fxls8962af_set_full_scale(struct fxls8962af_data *data, u32 scale)
 					FXLS8962AF_SC1_FSR_PREP(i));
 }
 
-static unsigned int fxls8962af_read_full_scale(struct fxls8962af_data *data,
-					       int *val)
+static int fxls8962af_read_full_scale(struct fxls8962af_data *data, int *val)
 {
 	int ret;
 	unsigned int reg;
@@ -397,8 +396,8 @@ static int fxls8962af_set_samp_freq(struct fxls8962af_data *data, u32 val,
 					FXLS8962AF_SC3_WAKE_ODR_PREP(i));
 }
 
-static unsigned int fxls8962af_read_samp_freq(struct fxls8962af_data *data,
-					      int *val, int *val2)
+static int fxls8962af_read_samp_freq(struct fxls8962af_data *data,
+				     int *val, int *val2)
 {
 	int ret;
 	unsigned int reg;
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/4 iio: accel: fxls8962af: check WAKE_ODR in fxls8962af_read_samp_freq()

In reply to Maoyi Xie

fxls8962af_read_samp_freq() reads SENS_CONFIG3 from the sensor and uses its 4-bit WAKE_ODR field as an index into fxls8962af_samp_freq_table[] without a check. The table has 13 rows, so a value of 13 to 15 reads past its end. The driver only writes values 0 to 12, so this needs a chip or bus that returns a value it should not.

Check the field against the size of the 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. Not tested on hardware.

I used an LLM to help write the harness and this patch.

 drivers/iio/accel/fxls8962af-core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/accel/fxls8962af-core.c +2 -0
index fd00b1d3e876..cf55e7d45b6a 100644
@@ -408,6 +408,8 @@ static int fxls8962af_read_samp_freq(struct fxls8962af_data *data,
 		return ret;
 
 	range_idx = FXLS8962AF_SC3_WAKE_ODR_GET(reg);
+	if (range_idx >= ARRAY_SIZE(fxls8962af_samp_freq_table))
+		return -ERANGE;
 
 	*val = fxls8962af_samp_freq_table[range_idx][0];
 	*val2 = fxls8962af_samp_freq_table[range_idx][1];
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 4/4 iio: light: rpr0521: check the gain index in rpr0521_get_gain()

In reply to Maoyi Xie

rpr0521_get_gain() uses the gain field of a channel's control register as an index into the channel's gain table without a check. The proximity gain field has two bits and rpr0521_pxs_gain[] has three entries, so the value 3 reads one entry past the end of the table. The driver itself sets only 0 to 2. The register is cached, so this needs a chip or bus that returns 3 on the first read after probe.

rpr0521_read_samp_freq() already has such a check. Check the gain index here as well and return -ERANGE.

---
Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under ASan. Not tested on hardware.

I used an LLM to help write the harness and this patch.

 drivers/iio/light/rpr0521.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iio/light/rpr0521.c +3 -0
index 7d82efc31f3a..98f6bcb95515 100644
@@ -573,6 +573,9 @@ static int rpr0521_get_gain(struct rpr0521_data *data, int chan,
 		return ret;
 
 	idx = (rpr0521_gain[chan].mask & reg) >> rpr0521_gain[chan].shift;
+	if (idx >= rpr0521_gain[chan].size)
+		return -ERANGE;
+
 	*val = rpr0521_gain[chan].gain[idx].scale;
 	*val2 = rpr0521_gain[chan].gain[idx].uscale;
 
Signature · 1 line
2.34.1