Skip to content

Commit d08d82d

Browse files
Sam Dalygregkh
authored andcommitted
iio: adc: ti-ads1298: add bounds check to pga_settings index
commit 95e8a48 upstream. ads1298_pga_settings has 7 elements but ADS1298_MASK_CH_PGA can yield values 0-7. If it yields a value >= 7, this causes an out-of-bounds array access. Add a bounds check and return -EINVAL if the index is out of range. Note that the remaining value b111 is reserved so should not be seen in a correctly functioning system. Assisted-by: gkh_clanker_2000 Cc: stable <stable@kernel.org> Cc: Jonathan Cameron <jic23@kernel.org> Cc: David Lechner <dlechner@baylibre.com> Cc: "Nuno Sá" <nuno.sa@analog.com> Cc: Andy Shevchenko <andy@kernel.org> Signed-off-by: Sam Daly <sam@samdaly.ie> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Jonathan Cameron <jic23@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 0a89002 commit d08d82d

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

drivers/iio/adc/ti-ads1298.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ static const u8 ads1298_pga_settings[] = { 6, 1, 2, 3, 4, 8, 12 };
279279
static int ads1298_get_scale(struct ads1298_private *priv,
280280
int channel, int *val, int *val2)
281281
{
282+
unsigned int pga_idx;
282283
int ret;
283284
unsigned int regval;
284285
u8 gain;
@@ -302,7 +303,11 @@ static int ads1298_get_scale(struct ads1298_private *priv,
302303
if (ret)
303304
return ret;
304305

305-
gain = ads1298_pga_settings[FIELD_GET(ADS1298_MASK_CH_PGA, regval)];
306+
pga_idx = FIELD_GET(ADS1298_MASK_CH_PGA, regval);
307+
if (pga_idx >= ARRAY_SIZE(ads1298_pga_settings))
308+
return -EINVAL;
309+
310+
gain = ads1298_pga_settings[pga_idx];
306311
*val /= gain; /* Full scale is VREF / gain */
307312

308313
*val2 = ADS1298_BITS_PER_SAMPLE - 1; /* Signed, hence the -1 */

0 commit comments

Comments
 (0)