Skip to content

Commit 399b883

Browse files
dlechjic23
authored andcommitted
iio: imu: bno055: fix OOB access of hw_xlate array
Fix a potential out-of-bounds array access of the hw_xlate array in bno055.c. In bno055_get_regmask(), hw_xlate was iterated over the length of the vals array instead of the length of the hw_xlate array. In the case of bno055_gyr_scale, the vals array is larger than the hw_xlate array, so this could result in an out-of-bounds access. In practice, this shouldn't happen though because a match should always be found which breaks out of the for loop before it iterates beyond the end of the hw_xlate array. By adding a new hw_xlate_len field to the bno055_sysfs_attr, we can be sure we are iterating over the correct length. Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202507100510.rGt1YOOx-lkp@intel.com/ Fixes: 4aefe1c ("iio: imu: add Bosch Sensortec BNO055 core driver") Signed-off-by: David Lechner <dlechner@baylibre.com> Link: https://patch.msgid.link/20250709-iio-const-data-19-v2-1-fb3fc9191251@baylibre.com Cc: <Stable@vger.kernel.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 8749c54 commit 399b883

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

drivers/iio/imu/bno055/bno055.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ struct bno055_sysfs_attr {
118118
int len;
119119
int *fusion_vals;
120120
int *hw_xlate;
121+
int hw_xlate_len;
121122
int type;
122123
};
123124

@@ -170,20 +171,24 @@ static int bno055_gyr_scale_vals[] = {
170171
1000, 1877467, 2000, 1877467,
171172
};
172173

174+
static int bno055_gyr_scale_hw_xlate[] = {0, 1, 2, 3, 4};
173175
static struct bno055_sysfs_attr bno055_gyr_scale = {
174176
.vals = bno055_gyr_scale_vals,
175177
.len = ARRAY_SIZE(bno055_gyr_scale_vals),
176178
.fusion_vals = (int[]){1, 900},
177-
.hw_xlate = (int[]){4, 3, 2, 1, 0},
179+
.hw_xlate = bno055_gyr_scale_hw_xlate,
180+
.hw_xlate_len = ARRAY_SIZE(bno055_gyr_scale_hw_xlate),
178181
.type = IIO_VAL_FRACTIONAL,
179182
};
180183

181184
static int bno055_gyr_lpf_vals[] = {12, 23, 32, 47, 64, 116, 230, 523};
185+
static int bno055_gyr_lpf_hw_xlate[] = {5, 4, 7, 3, 6, 2, 1, 0};
182186
static struct bno055_sysfs_attr bno055_gyr_lpf = {
183187
.vals = bno055_gyr_lpf_vals,
184188
.len = ARRAY_SIZE(bno055_gyr_lpf_vals),
185189
.fusion_vals = (int[]){32},
186-
.hw_xlate = (int[]){5, 4, 7, 3, 6, 2, 1, 0},
190+
.hw_xlate = bno055_gyr_lpf_hw_xlate,
191+
.hw_xlate_len = ARRAY_SIZE(bno055_gyr_lpf_hw_xlate),
187192
.type = IIO_VAL_INT,
188193
};
189194

@@ -561,7 +566,7 @@ static int bno055_get_regmask(struct bno055_priv *priv, int *val, int *val2,
561566

562567
idx = (hwval & mask) >> shift;
563568
if (attr->hw_xlate)
564-
for (i = 0; i < attr->len; i++)
569+
for (i = 0; i < attr->hw_xlate_len; i++)
565570
if (attr->hw_xlate[i] == idx) {
566571
idx = i;
567572
break;

0 commit comments

Comments
 (0)