Skip to content

Commit 792595b

Browse files
zhili.liujic23
authored andcommitted
iio: magnetometer: rm3100: add boundary check for the value read from RM3100_REG_TMRC
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. Fixes: 121354b ("iio: magnetometer: Add driver support for PNI RM3100") Suggested-by: Zhouyi Zhou <[email protected]> Signed-off-by: zhili.liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Cc: <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent b67f3e6 commit 792595b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

drivers/iio/magnetometer/rm3100-core.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ int rm3100_common_probe(struct device *dev, struct regmap *regmap, int irq)
530530
struct rm3100_data *data;
531531
unsigned int tmp;
532532
int ret;
533+
int samp_rate_index;
533534

534535
indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
535536
if (!indio_dev)
@@ -586,9 +587,14 @@ int rm3100_common_probe(struct device *dev, struct regmap *regmap, int irq)
586587
ret = regmap_read(regmap, RM3100_REG_TMRC, &tmp);
587588
if (ret < 0)
588589
return ret;
590+
591+
samp_rate_index = tmp - RM3100_TMRC_OFFSET;
592+
if (samp_rate_index < 0 || samp_rate_index >= RM3100_SAMP_NUM) {
593+
dev_err(dev, "The value read from RM3100_REG_TMRC is invalid!\n");
594+
return -EINVAL;
595+
}
589596
/* Initializing max wait time, which is double conversion time. */
590-
data->conversion_time = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][2]
591-
* 2;
597+
data->conversion_time = rm3100_samp_rates[samp_rate_index][2] * 2;
592598

593599
/* Cycle count values may not be what we want. */
594600
if ((tmp - RM3100_TMRC_OFFSET) == 0)

0 commit comments

Comments
 (0)