iio: pressure: bmp280: fix out-of-bounds access in sampling frequency lookup
Subsystems (2)
PATCH 1/1 iio: pressure: bmp280: fix out-of-bounds access in sampling frequency lookup
The sampling frequency tables store each frequency as an integer part and a fractional part in micro units. num_sampling_freq_avail is initialized to the number of flattened integer elements because read_avail() returns the table as a flat array.
bmp280_write_sampling_frequency(), however, indexes the same table as a two-dimensional array and uses num_sampling_freq_avail as the number of rows. This makes the lookup walk past the end of the table when an unsupported sampling frequency is written.
Convert the flattened element count back to the number of rows before iterating over the table.
--- drivers/iio/pressure/bmp280-core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/pressure/bmp280-core.c +2 -1
@@ -836,7 +836,8 @@ static int bmp280_write_sampling_frequency(struct bmp280_data *data, int val, int val2) {const int (*avail)[2 ] = data->chip_info->sampling_freq_avail; -const int n = data->chip_info->num_sampling_freq_avail; +const int n = data->chip_info->num_sampling_freq_avail / +ARRAY_SIZE (*avail);int ret, prev;int i;
Signature · 1 line
2.43.0
Quoted 10 lines · On Wed, 5 Aug 2026 15:41:27 +0800 Hui Su <[email protected]> wrote:
The sampling frequency tables store each frequency as an integer part and a fractional part in micro units.
num_sampling_freq_availis initialized to the number of flattened integer elements becauseread_avail()returns the table as a flat array.
bmp280_write_sampling_frequency(), however, indexes the same table as a two-dimensional array and usesnum_sampling_freq_availas the number of rows. This makes the lookup walk past the end of the table when an unsupported sampling frequency is written.
Quoted 7 lines
Convert the flattened element count back to the number of rows before iterating over the table.
Fixes 10b40ffba2f9 ("iio: pressure: bmp280: Add more tunable config parameters for BMP380")Signed-off-by Hui Su <[email protected]>---
LGTM
Signature · 2 lines
Kind regards, Joshua Crofts
Quoted 12 lines · Hui Su
The sampling frequency tables store each frequency as an integer part and a fractional part in micro units.
num_sampling_freq_availis initialized to the number of flattened integer elements becauseread_avail()returns the table as a flat array.
bmp280_write_sampling_frequency(), however, indexes the same table as a two-dimensional array and usesnum_sampling_freq_availas the number of rows. This makes the lookup walk past the end of the table when an unsupported sampling frequency is written.Convert the flattened element count back to the number of rows before iterating over the table.
...
drivers/iio/pressure/bmp280-core.c static int bmp280_write_sampling_frequency(struct bmp280_data *data, · line 840
Quoted 7 lines
static int bmp280_write_sampling_frequency(struct
bmp280_data*data,drivers/iio/pressure/bmp280-core.c +2
-1int val, int val2) {const int (*avail)[2 ] = data->chip_info->sampling_freq_avail; -const int n = data->chip_info->num_sampling_freq_avail; +const int n = data->chip_info->num_sampling_freq_avail / +ARRAY_SIZE (*avail);
For the consistency's sake use just plain 2 as initialisators do.
...
Nice catch! This seems to work because the page is present, but after this array it contains some garbage that doesn't induce page fault, otherwise this must have led to oops very easily (and no one reported it before).
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 1/1 iio: pressure: bmp280: fix out-of-bounds access in sampling frequency lookup
In reply to Hui SuThe sampling frequency tables store each frequency as an integer part and a fractional part in micro units. num_sampling_freq_avail is initialized to the number of flattened integer elements because read_avail() returns the table as a flat array.
bmp280_write_sampling_frequency(), however, indexes the same table as a two-dimensional array and uses num_sampling_freq_avail as the number of rows. Convert the flattened element count back to the number of rows before iterating over the table.
---
Changes in v2:
- Use plain 2 as the divisor, as suggested by Andy Shevchenko.
- Add Joshua Crofts' Reviewed-by tag.
drivers/iio/pressure/bmp280-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/pressure/bmp280-core.c +1 -1
@@ -836,7 +836,7 @@ static int bmp280_write_sampling_frequency(struct bmp280_data *data, int val, int val2) {const int (*avail)[2 ] = data->chip_info->sampling_freq_avail; -const int n = data->chip_info->num_sampling_freq_avail; +const int n = data->chip_info->num_sampling_freq_avail /2 ;int ret, prev;int i;
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerQuoted 10 lines · On Tue, 11 Aug 2026 10:52:53 +0800 Hui Su <[email protected]> wrote:
The sampling frequency tables store each frequency as an integer part and a fractional part in micro units.
num_sampling_freq_availis initialized to the number of flattened integer elements becauseread_avail()returns the table as a flat array.
bmp280_write_sampling_frequency(), however, indexes the same table as a two-dimensional array and usesnum_sampling_freq_availas the number of rows. Convert the flattened element count back to the number of rows before iterating over the table.
Quoted 4 lines
Fixes 10b40ffba2f9 ("iio: pressure: bmp280: Add more tunable config parameters for BMP380")Signed-off-by Hui Su <[email protected]>Reviewed-by Joshua Crofts <[email protected]>
Hi Hui Su
For future reference don't send a new version in reply to an older one. It rapidly looses all usefulness as the email threads get deeper and more complex.
LGTM Applied to the fixes-togreg branch of iio.git
Note I plan to rebase that once rc1 is out and a pull request will go upstream sometime after that.
Thanks,
Jonathan
Quoted 22 lines
---
Changes in v2:
- Use plain 2 as the divisor, as suggested by Andy Shevchenko.
- Add Joshua Crofts' Reviewed-by tag.drivers/iio/pressure/bmp280-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/pressure/bmp280-core.c +1
-1@@ -836,7 +836,7 @@ static int bmp280_write_sampling_frequency(struct bmp280_data *data, int val, int val2) {const int (*avail)[2 ] = data->chip_info->sampling_freq_avail; -const int n = data->chip_info->num_sampling_freq_avail; +const int n = data->chip_info->num_sampling_freq_avail /2 ;int ret, prev;int i;