Skip to contentSkip to conversation

iio: magnetometer: rm3100: check TMRC in rm3100_get_samp_freq()

PATCH Superseded Maoyi Xie 2 participants 2 messages Nov 18 – Nov 19

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

PATCH 1/1 iio: magnetometer: rm3100: check TMRC in rm3100_get_samp_freq()

rm3100_get_samp_freq() reads TMRC from the sensor and uses the value minus RM3100_TMRC_OFFSET as an index into rm3100_samp_rates[] without a check. A value outside 0x92 to 0x9f reads outside the table. The driver only writes values in that range, so this needs a chip or bus that misbehaves after probe.

Commit 792595bab492 ("iio: magnetometer: rm3100: add boundary check for the value read from RM3100_REG_TMRC") added the check to rm3100_common_probe() after a crash on failing hardware. Do the same here and return -EINVAL.

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.

Fixes 121354b2eceb ("iio: magnetometer: Add driver support for PNI RM3100")

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

 drivers/iio/magnetometer/rm3100-core.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/rm3100-core.c +7 -2
index 46fce2978d76705..2fec02e5679eaf7 100644
@@ -279,6 +279,7 @@ static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2)
 {
 	unsigned int tmp;
 	int ret;
+	int samp_rate_index;
 
 	guard(mutex)(&data->lock);
 
@@ -286,8 +287,12 @@ static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2)
 	if (ret < 0)
 		return ret;
 
-	*val = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][0];
-	*val2 = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][1];
+	samp_rate_index = tmp - RM3100_TMRC_OFFSET;
+	if (samp_rate_index < 0 || samp_rate_index >= RM3100_SAMP_NUM)
+		return -EINVAL;
+
+	*val = rm3100_samp_rates[samp_rate_index][0];
+	*val2 = rm3100_samp_rates[samp_rate_index][1];
 
 	return IIO_VAL_INT_PLUS_MICRO;
 }

base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea

Signature · 1 line
2.34.1
#1 lore raw
Quoted 15 lines · Maoyi Xie

rm3100_get_samp_freq() reads TMRC from the sensor and uses the value minus RM3100_TMRC_OFFSET as an index into rm3100_samp_rates[] without a check. A value outside 0x92 to 0x9f reads outside the table. The driver only writes values in that range, so this needs a chip or bus that misbehaves after probe.

Commit 792595bab492 ("iio: magnetometer: rm3100: add boundary check for the value read from RM3100_REG_TMRC") added the check to rm3100_common_probe() after a crash on failing hardware. Do the same here and return -EINVAL.

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.

...

drivers/iio/magnetometer/rm3100-core.c static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2) · line 282

Quoted 5 lines

static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2)
{

drivers/iio/magnetometer/rm3100-core.c +1 -0
 	unsigned int tmp;
 	int ret;
+	int samp_rate_index;

Preserve reversed xmas tree order.

...

drivers/iio/magnetometer/rm3100-core.c static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2) · line 291

Quoted 4 lines
drivers/iio/magnetometer/rm3100-core.c +2 -2
-	*val = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][0];
-	*val2 = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][1];
+	samp_rate_index = tmp - RM3100_TMRC_OFFSET;
+	if (samp_rate_index < 0 || samp_rate_index >= RM3100_SAMP_NUM)

Wouldn't be better to use ARRAY_SIZE() if possible?

drivers/iio/magnetometer/rm3100-core.c static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2) · line 292

Quoted 1 line
drivers/iio/magnetometer/rm3100-core.c +1 -0
+		return -EINVAL;

Maybe -ERANGE?

Quoted 2 lines
drivers/iio/magnetometer/rm3100-core.c +2 -0
+	*val = rm3100_samp_rates[samp_rate_index][0];
+	*val2 = rm3100_samp_rates[samp_rate_index][1];
Signature · 2 lines
With Best Regards,
Andy Shevchenko