* [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds
@ 2020-04-09 8:58 Alexandre Bard
2020-04-09 9:17 ` Andy Shevchenko
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Alexandre Bard @ 2020-04-09 8:58 UTC (permalink / raw)
To: lorenzo.bianconi83; +Cc: linux-iio, Alexandre Bard
Former code was iterating through all possible IDs whereas only a few
per settings array are really available. Leading to several out of
bounds readings.
Line is now longer than 80 characters. But since it is a classic for
loop I think it is better to keep it like this than splitting it.
Signed-off-by: Alexandre Bard <alexandre.bard@netmodule.com>
---
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
index 84d219ae6aee..be8882ff30eb 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
@@ -1350,7 +1350,7 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
int err, i, j, data;
for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
- for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
+ for (j = 0; j < ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id); j++) {
if (st_lsm6dsx_sensor_settings[i].id[j].name &&
id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
break;
--
2.20.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds 2020-04-09 8:58 [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds Alexandre Bard @ 2020-04-09 9:17 ` Andy Shevchenko 2020-04-09 11:01 ` Stephan Gerhold 2020-04-09 16:44 ` Martin Kepplinger 2 siblings, 0 replies; 8+ messages in thread From: Andy Shevchenko @ 2020-04-09 9:17 UTC (permalink / raw) To: Alexandre Bard; +Cc: Lorenzo Bianconi, linux-iio On Thu, Apr 9, 2020 at 12:00 PM Alexandre Bard <alexandre.bard@netmodule.com> wrote: > > Former code was iterating through all possible IDs whereas only a few > per settings array are really available. Leading to several out of > bounds readings. > > Line is now longer than 80 characters. But since it is a classic for > loop I think it is better to keep it like this than splitting it. Agree. Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> > Signed-off-by: Alexandre Bard <alexandre.bard@netmodule.com> > --- > drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > index 84d219ae6aee..be8882ff30eb 100644 > --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > @@ -1350,7 +1350,7 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, > int err, i, j, data; > > for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) { > - for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) { > + for (j = 0; j < ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id); j++) { > if (st_lsm6dsx_sensor_settings[i].id[j].name && > id == st_lsm6dsx_sensor_settings[i].id[j].hw_id) > break; > -- > 2.20.1 > -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds 2020-04-09 8:58 [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds Alexandre Bard 2020-04-09 9:17 ` Andy Shevchenko @ 2020-04-09 11:01 ` Stephan Gerhold 2020-04-09 11:50 ` Alexandre Bard 2020-04-09 16:44 ` Martin Kepplinger 2 siblings, 1 reply; 8+ messages in thread From: Stephan Gerhold @ 2020-04-09 11:01 UTC (permalink / raw) To: Alexandre Bard; +Cc: lorenzo.bianconi83, linux-iio On Thu, Apr 09, 2020 at 10:58:18AM +0200, Alexandre Bard wrote: > Former code was iterating through all possible IDs whereas only a few > per settings array are really available. Leading to several out of > bounds readings. > > Line is now longer than 80 characters. But since it is a classic for > loop I think it is better to keep it like this than splitting it. > > Signed-off-by: Alexandre Bard <alexandre.bard@netmodule.com> > --- > drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > index 84d219ae6aee..be8882ff30eb 100644 > --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > @@ -1350,7 +1350,7 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, > int err, i, j, data; > > for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) { > - for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) { > + for (j = 0; j < ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id); j++) { id in st_lsm6dsx_settings is declared as: struct { enum st_lsm6dsx_hw_id hw_id; const char *name; } id[ST_LSM6DSX_MAX_ID]; so it's always ST_LSM6DSX_MAX_ID long (additional entries are just zero-initialized). Isn't ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id) == ST_LSM6DSX_MAX_ID in this case? > if (st_lsm6dsx_sensor_settings[i].id[j].name && > id == st_lsm6dsx_sensor_settings[i].id[j].hw_id) > break; > -- > 2.20.1 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds 2020-04-09 11:01 ` Stephan Gerhold @ 2020-04-09 11:50 ` Alexandre Bard 2020-04-09 11:58 ` Lorenzo Bianconi 2020-04-09 12:09 ` Stephan Gerhold 0 siblings, 2 replies; 8+ messages in thread From: Alexandre Bard @ 2020-04-09 11:50 UTC (permalink / raw) To: Stephan Gerhold; +Cc: lorenzo.bianconi83, linux-iio Le 09.04.20 à 13:01, Stephan Gerhold a écrit : > On Thu, Apr 09, 2020 at 10:58:18AM +0200, Alexandre Bard wrote: >> Former code was iterating through all possible IDs whereas only a few >> per settings array are really available. Leading to several out of >> bounds readings. >> >> Line is now longer than 80 characters. But since it is a classic for >> loop I think it is better to keep it like this than splitting it. >> >> Signed-off-by: Alexandre Bard <alexandre.bard@netmodule.com> >> --- >> drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c >> index 84d219ae6aee..be8882ff30eb 100644 >> --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c >> +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c >> @@ -1350,7 +1350,7 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, >> int err, i, j, data; >> >> for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) { >> - for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) { >> + for (j = 0; j < ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id); j++) { > id in st_lsm6dsx_settings is declared as: > > struct { > enum st_lsm6dsx_hw_id hw_id; > const char *name; > } id[ST_LSM6DSX_MAX_ID]; > > so it's always ST_LSM6DSX_MAX_ID long > (additional entries are just zero-initialized). > > Isn't ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id) == ST_LSM6DSX_MAX_ID > in this case? Yes, you are right, I missed that. But there is still a problem : parsing 0-initialized fields can lead to a false positive when looking for the value ST_LSM6DS3_ID which is the first element of an enum. So either the enum must be patched to start at 1 or the length of valid ids in a settings must be retrieved somehow. Or is there another way ? Or am I wrong ? > >> if (st_lsm6dsx_sensor_settings[i].id[j].name && >> id == st_lsm6dsx_sensor_settings[i].id[j].hw_id) >> break; >> -- >> 2.20.1 >> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds 2020-04-09 11:50 ` Alexandre Bard @ 2020-04-09 11:58 ` Lorenzo Bianconi 2020-04-09 12:09 ` Stephan Gerhold 1 sibling, 0 replies; 8+ messages in thread From: Lorenzo Bianconi @ 2020-04-09 11:58 UTC (permalink / raw) To: Alexandre Bard; +Cc: Stephan Gerhold, lorenzo.bianconi83, linux-iio [-- Attachment #1: Type: text/plain, Size: 2337 bytes --] > Le 09.04.20 à 13:01, Stephan Gerhold a écrit : > > On Thu, Apr 09, 2020 at 10:58:18AM +0200, Alexandre Bard wrote: > >> Former code was iterating through all possible IDs whereas only a few > >> per settings array are really available. Leading to several out of > >> bounds readings. > >> > >> Line is now longer than 80 characters. But since it is a classic for > >> loop I think it is better to keep it like this than splitting it. > >> > >> Signed-off-by: Alexandre Bard <alexandre.bard@netmodule.com> > >> --- > >> drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- > >> 1 file changed, 1 insertion(+), 1 deletion(-) > >> > >> diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > >> index 84d219ae6aee..be8882ff30eb 100644 > >> --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > >> +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > >> @@ -1350,7 +1350,7 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, > >> int err, i, j, data; > >> > >> for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) { > >> - for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) { > >> + for (j = 0; j < ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id); j++) { > > id in st_lsm6dsx_settings is declared as: > > > > struct { > > enum st_lsm6dsx_hw_id hw_id; > > const char *name; > > } id[ST_LSM6DSX_MAX_ID]; > > > > so it's always ST_LSM6DSX_MAX_ID long > > (additional entries are just zero-initialized). > > > > Isn't ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id) == ST_LSM6DSX_MAX_ID > > in this case? > Yes, you are right, I missed that. But there is still a problem : > parsing 0-initialized fields can lead to a false positive when looking for the > value ST_LSM6DS3_ID which is the first element of an enum. So either the enum > must be patched to start at 1 or the length of valid ids in a settings must be > retrieved somehow. for un-initialized entries st_lsm6dsx_sensor_settings[i].id[j].name will be NULL so I guess there is no issue there. Am I missing something? Regards, Lorenzo > > Or is there another way ? Or am I wrong ? > > > >> if (st_lsm6dsx_sensor_settings[i].id[j].name && > >> id == st_lsm6dsx_sensor_settings[i].id[j].hw_id) > >> break; > >> -- > >> 2.20.1 > >> [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 228 bytes --] ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds 2020-04-09 11:50 ` Alexandre Bard 2020-04-09 11:58 ` Lorenzo Bianconi @ 2020-04-09 12:09 ` Stephan Gerhold 2020-04-09 12:14 ` Alexandre Bard 1 sibling, 1 reply; 8+ messages in thread From: Stephan Gerhold @ 2020-04-09 12:09 UTC (permalink / raw) To: Alexandre Bard; +Cc: lorenzo.bianconi83, linux-iio On Thu, Apr 09, 2020 at 01:50:24PM +0200, Alexandre Bard wrote: > Le 09.04.20 à 13:01, Stephan Gerhold a écrit : > > On Thu, Apr 09, 2020 at 10:58:18AM +0200, Alexandre Bard wrote: > >> Former code was iterating through all possible IDs whereas only a few > >> per settings array are really available. Leading to several out of > >> bounds readings. > >> > >> Line is now longer than 80 characters. But since it is a classic for > >> loop I think it is better to keep it like this than splitting it. > >> > >> Signed-off-by: Alexandre Bard <alexandre.bard@netmodule.com> > >> --- > >> drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- > >> 1 file changed, 1 insertion(+), 1 deletion(-) > >> > >> diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > >> index 84d219ae6aee..be8882ff30eb 100644 > >> --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > >> +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > >> @@ -1350,7 +1350,7 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, > >> int err, i, j, data; > >> > >> for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) { > >> - for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) { > >> + for (j = 0; j < ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id); j++) { > > id in st_lsm6dsx_settings is declared as: > > > > struct { > > enum st_lsm6dsx_hw_id hw_id; > > const char *name; > > } id[ST_LSM6DSX_MAX_ID]; > > > > so it's always ST_LSM6DSX_MAX_ID long > > (additional entries are just zero-initialized). > > > > Isn't ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id) == ST_LSM6DSX_MAX_ID > > in this case? > Yes, you are right, I missed that. But there is still a problem : > parsing 0-initialized fields can lead to a false positive when looking for the > value ST_LSM6DS3_ID which is the first element of an enum. So either the enum > must be patched to start at 1 or the length of valid ids in a settings must be > retrieved somehow. > > Or is there another way ? Or am I wrong ? ST_LSM6DS3_ID was indeed broken, which is why I added a .name != NULL check in commit fb4fbc8904e7 ("iio: imu: st_lsm6dsx: Fix selection of ST_LSM6DS3_ID"). .name is only set for properly initialized IDs, so this ensures that we do not match any zero-initialized entries. :) > > > >> if (st_lsm6dsx_sensor_settings[i].id[j].name && > >> id == st_lsm6dsx_sensor_settings[i].id[j].hw_id) > >> break; > >> -- > >> 2.20.1 > >> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds 2020-04-09 12:09 ` Stephan Gerhold @ 2020-04-09 12:14 ` Alexandre Bard 0 siblings, 0 replies; 8+ messages in thread From: Alexandre Bard @ 2020-04-09 12:14 UTC (permalink / raw) To: Stephan Gerhold; +Cc: lorenzo.bianconi83, linux-iio > On Thu, Apr 09, 2020 at 01:50:24PM +0200, Alexandre Bard wrote: >> Le 09.04.20 à 13:01, Stephan Gerhold a écrit : >>> On Thu, Apr 09, 2020 at 10:58:18AM +0200, Alexandre Bard wrote: >>>> Former code was iterating through all possible IDs whereas only a few >>>> per settings array are really available. Leading to several out of >>>> bounds readings. >>>> >>>> Line is now longer than 80 characters. But since it is a classic for >>>> loop I think it is better to keep it like this than splitting it. >>>> >>>> Signed-off-by: Alexandre Bard <alexandre.bard@netmodule.com> >>>> --- >>>> drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- >>>> 1 file changed, 1 insertion(+), 1 deletion(-) >>>> >>>> diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c >>>> index 84d219ae6aee..be8882ff30eb 100644 >>>> --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c >>>> +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c >>>> @@ -1350,7 +1350,7 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, >>>> int err, i, j, data; >>>> >>>> for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) { >>>> - for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) { >>>> + for (j = 0; j < ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id); j++) { >>> id in st_lsm6dsx_settings is declared as: >>> >>> struct { >>> enum st_lsm6dsx_hw_id hw_id; >>> const char *name; >>> } id[ST_LSM6DSX_MAX_ID]; >>> >>> so it's always ST_LSM6DSX_MAX_ID long >>> (additional entries are just zero-initialized). >>> >>> Isn't ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id) == ST_LSM6DSX_MAX_ID >>> in this case? >> Yes, you are right, I missed that. But there is still a problem : >> parsing 0-initialized fields can lead to a false positive when looking for the >> value ST_LSM6DS3_ID which is the first element of an enum. So either the enum >> must be patched to start at 1 or the length of valid ids in a settings must be >> retrieved somehow. >> >> Or is there another way ? Or am I wrong ? > ST_LSM6DS3_ID was indeed broken, which is why I added a .name != NULL > check in commit fb4fbc8904e7 ("iio: imu: st_lsm6dsx: Fix selection of ST_LSM6DS3_ID"). > > .name is only set for properly initialized IDs, so this ensures that we > do not match any zero-initialized entries. :) Right, I actually fell on this problem in an older version where .name did not exist and I did not understand that it was added for this purpose when I checked out the master branch. Looks alright then. Thanks for the feedback. Best regards, Alexandre Bard > >>>> if (st_lsm6dsx_sensor_settings[i].id[j].name && >>>> id == st_lsm6dsx_sensor_settings[i].id[j].hw_id) >>>> break; >>>> -- >>>> 2.20.1 >>>> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds 2020-04-09 8:58 [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds Alexandre Bard 2020-04-09 9:17 ` Andy Shevchenko 2020-04-09 11:01 ` Stephan Gerhold @ 2020-04-09 16:44 ` Martin Kepplinger 2 siblings, 0 replies; 8+ messages in thread From: Martin Kepplinger @ 2020-04-09 16:44 UTC (permalink / raw) To: Alexandre Bard, lorenzo.bianconi83; +Cc: linux-iio On 09.04.20 10:58, Alexandre Bard wrote: > Former code was iterating through all possible IDs whereas only a few > per settings array are really available. Leading to several out of > bounds readings. > > Line is now longer than 80 characters. But since it is a classic for > loop I think it is better to keep it like this than splitting it. > > Signed-off-by: Alexandre Bard <alexandre.bard@netmodule.com> > --- > drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > index 84d219ae6aee..be8882ff30eb 100644 > --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > @@ -1350,7 +1350,7 @@ static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, > int err, i, j, data; > > for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) { > - for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) { > + for (j = 0; j < ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id); j++) { > if (st_lsm6dsx_sensor_settings[i].id[j].name && > id == st_lsm6dsx_sensor_settings[i].id[j].hw_id) > break; > Tested-by: Martin Kepplinger <martin.kepplinger@puri.sm> thanks ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2020-04-09 16:44 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2020-04-09 8:58 [PATCH] iio: imu: st_lsm6dsx: Fix reading array out of bounds Alexandre Bard 2020-04-09 9:17 ` Andy Shevchenko 2020-04-09 11:01 ` Stephan Gerhold 2020-04-09 11:50 ` Alexandre Bard 2020-04-09 11:58 ` Lorenzo Bianconi 2020-04-09 12:09 ` Stephan Gerhold 2020-04-09 12:14 ` Alexandre Bard 2020-04-09 16:44 ` Martin Kepplinger
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox