Skip to contentSkip to conversation

iio: magnetometer: rm3100: add boundary check for the value read from RM3100_REG_TMRC

PATCH Superseded [email protected] 2 participants 2 messages Dec 31, 2023 – Jan 1, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: magnetometer: rm3100: add boundary check for the value read from RM3100_REG_TMRC

From: "zhili.liu" <[email protected]>

Recently, we encounter kernel crash in function rm3100_common_probe caused by out of bound access of array rm3100_samp_rates (because of underlying hardware failures). Add boundary check to prevent out of bound access.

---
The format of the previous patch was a bit problematic, we are sending it again.

Sorry for the trouble.

Thank you very much.
--

 drivers/iio/magnetometer/rm3100-core.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/iio/magnetometer/rm3100-core.c +6 -0
index 69938204456f..fc50b6d4a334 100644
@@ -586,6 +586,12 @@ int rm3100_common_probe(struct device *dev, struct regmap *regmap, int irq)
 	ret = regmap_read(regmap, RM3100_REG_TMRC, &tmp);
 	if (ret < 0)
 		return ret;
+
+	if (tmp < RM3100_SAMP_NUM || tmp - RM3100_TMRC_OFFSET >= RM3100_SAMP_NUM) {
+		dev_err(dev, "The value read from RM3100_REG_TMRC is invalid!\n");
+		return -EINVAL;
+	}
+
 	/* Initializing max wait time, which is double conversion time. */
 	data->conversion_time = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][2]
 				* 2;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 9 lines · On Sun, 31 Dec 2023 22:56:44 +0800 [email protected] wrote:

From: "zhili.liu" <[email protected]>

Recently, we encounter kernel crash in function rm3100_common_probe caused by out of bound access of array rm3100_samp_rates (because of underlying hardware failures). Add boundary check to prevent out of bound access.

Please provide a Fixes tag so we know how far back to backport this.

Seems like a reasonable bit of hardening against potential hardware issues. However it would be cleaner with a local variable used for the index. See inline.

Jonathan

Quoted 16 lines

---
The format of the previous patch was a bit problematic, we are sending it again.

Sorry for the trouble.

Thank you very much.
--

 drivers/iio/magnetometer/rm3100-core.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/iio/magnetometer/rm3100-core.c +0 -0
index 69938204456f..fc50b6d4a334 100644

drivers/iio/magnetometer/rm3100-core.c int rm3100_common_probe(struct device *dev, struct regmap *regmap, int irq) · line 590

Quoted 5 lines
drivers/iio/magnetometer/rm3100-core.c +2 -0
@@ -586,6 +586,12 @@ int rm3100_common_probe(struct device *dev, struct regmap *regmap, int irq)
 	ret = regmap_read(regmap, RM3100_REG_TMRC, &tmp);
 	if (ret < 0)
 		return ret;
+
+	if (tmp < RM3100_SAMP_NUM || tmp - RM3100_TMRC_OFFSET >= RM3100_SAMP_NUM) {

Just a local variable of
int samp_rate_index = tmp - RM3100_TMRC_OFFSET;
Check that for negative or >= RM3100_SAMP_NUM

Quoted 7 lines
drivers/iio/magnetometer/rm3100-core.c +4 -0
+		dev_err(dev, "The value read from RM3100_REG_TMRC is invalid!\n");
+		return -EINVAL;
+	}
+
 	/* Initializing max wait time, which is double conversion time. */
 	data->conversion_time = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][2]
 				* 2;