iio: imu: st_lsm6dsx: Fix reading array out of bounds
Subsystems (2)
PATCH 1/1 iio: imu: st_lsm6dsx: Fix reading array out of bounds
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.
--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +1 -1
@@ -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 ;
Signature · 1 line
2.20.1
Quoted 7 lines · 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.
Agree.
Quoted 21 lines
Signed-off-by Alexandre Bard <[email protected]>--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +1
-2@@ -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
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 17 lines · 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 <[email protected]>--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, · line 1353
Quoted 5 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +1
-1@@ -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?
Quoted 6 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0if (st_lsm6dsx_sensor_settings[i].id[j].name && id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)break ;Signature · 1 line
2.20.1
Le 09.04.20 à 13:01, Stephan Gerhold a écrit :
Quoted 30 lines · Alexandre Bard
Quoted 17 lines
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 <[email protected]>--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, · line 1353
Quoted 5 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +1
-1@@ -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_settingsis declared as:struct { enum st_lsm6dsx_hw_id hw_id; const char *name; } id[ST_LSM6DSX_MAX_ID];
Quoted 5 lines
so it's always
ST_LSM6DSX_MAX_IDlong
(additional entries are just zero-initialized).Isn't
ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id)==ST_LSM6DSX_MAX_IDin 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 ?
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0if (st_lsm6dsx_sensor_settings[i].id[j].name && id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)break ;Signature · 1 line
2.20.1
Lorenzo Bianconi
maintainerQuoted 34 lines
Le 09.04.20 à 13:01, Stephan Gerhold a écrit :
Quoted 33 lines · Alexandre Bard
Quoted 17 lines
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 <[email protected]>--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, · line 1353
Quoted 5 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +1
-1@@ -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_settingsis 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_IDlong
(additional entries are just zero-initialized).
Quoted 7 lines
Quoted 2 lines
Isn't
ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id)==ST_LSM6DSX_MAX_IDin 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 valueST_LSM6DS3_IDwhich 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
Quoted 9 lines
Or is there another way ? Or am I wrong ?
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0if (st_lsm6dsx_sensor_settings[i].id[j].name && id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)break ;Signature · 1 line
2.20.1
Quoted 36 lines · Alexandre Bard
Le 09.04.20 à 13:01, Stephan Gerhold a écrit :
Quoted 30 lines · Alexandre Bard
Quoted 17 lines
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 <[email protected]>--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, · line 1353
Quoted 5 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +1
-1@@ -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_settingsis declared as:struct { enum st_lsm6dsx_hw_id hw_id; const char *name; } id[ST_LSM6DSX_MAX_ID];Quoted 5 lines
so it's always
ST_LSM6DSX_MAX_IDlong
(additional entries are just zero-initialized).Isn't
ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id)==ST_LSM6DSX_MAX_IDin this case?
Quoted 7 lines
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 valueST_LSM6DS3_IDwhich 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. :)
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0if (st_lsm6dsx_sensor_settings[i].id[j].name && id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)break ;Signature · 1 line
2.20.1
Quoted 44 lines · Alexandre Bard
Quoted 42 lines
Le 09.04.20 à 13:01, Stephan Gerhold a écrit :
Quoted 30 lines · Alexandre Bard
Quoted 17 lines
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 <[email protected]>--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, · line 1353
Quoted 5 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +1
-1@@ -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_settingsis declared as:struct { enum st_lsm6dsx_hw_id hw_id; const char *name; } id[ST_LSM6DSX_MAX_ID];Quoted 5 lines
so it's always
ST_LSM6DSX_MAX_IDlong
(additional entries are just zero-initialized).Isn't
ARRAY_SIZE(st_lsm6dsx_sensor_settings[i].id)==ST_LSM6DSX_MAX_IDin 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 valueST_LSM6DS3_IDwhich 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.
Quoted 6 lines
Quoted 1 line
Or is there another way ? Or am I wrong ?
ST_LSM6DS3_IDwas indeed broken, which is why I added a .name != NULL check in commitfb4fbc8904e7("iio: imu:st_lsm6dsx: Fix selection ofST_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
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0if (st_lsm6dsx_sensor_settings[i].id[j].name && id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)break ;Signature · 1 line
2.20.1
Quoted 18 lines · 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 <[email protected]>--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +0
-0
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id, · line 1356
Quoted 8 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +1
-1@@ -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 ;
thanks