Skip to contentSkip to conversation

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

PATCH In review Maoyi Xie 3 participants 3 messages Nov 20 – Nov 21

Subsystems (2)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

PATCH v2 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. Check the value 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 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.

v2:
- keep the declarations in reversed xmas tree order (Andy)
- use ARRAY_SIZE() for the upper bound (Andy)
- return -ERANGE instead of -EINVAL (Andy)

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

 drivers/iio/magnetometer/rm3100-core.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/rm3100-core.c +8 -2
index 46fce2978d76705..8e80cf6790c728f 100644
@@ -277,6 +277,7 @@ static const int rm3100_samp_rates[RM3100_SAMP_NUM][3] = {
 
 static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2)
 {
+	int samp_rate_index;
 	unsigned int tmp;
 	int ret;
 
@@ -286,8 +287,13 @@ 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 >= ARRAY_SIZE(rm3100_samp_rates))
+		return -ERANGE;
+
+	*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 11 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. Check the value here as well and return -ERANGE.

Quoted 4 lines

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.

This paragraph should go after '---' line below.

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

Not sure. It sounds like hardening against malicious / broken HW.

...

Code wise looks good now.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 17 lines · On Fri, 20 Nov 2026 14:11:34 +0200 Andy Shevchenko…
Quoted 11 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. Check the value here as well and return -ERANGE.

Quoted 4 lines

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.

Quoted 5 lines

This paragraph should go after '---' line below.

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

Not sure. It sounds like hardening against malicious / broken HW.

Agreed. My approximate rule on these is that I'm fine taking hardening against hardware returning values that it shouldn't as long as it is simple to understand. General hardening of a driver against malicious hardware is a whole different thing and I'm not sure appropriate for IIO.

So this one is simple so I'm fine taking it, but not considering it a fix.

Dropped the paragraph Andy highlights above and the fixes tag. Applied like that the testing branch of iio.git which means it is heading towards the next merge window.

Thanks

Jonathan

Quoted 5 lines

...

Code wise looks good now.