drivers/iio: Remove all strcpy() uses
Subsystems (2)
PATCH v4 1/1 drivers/iio: Remove all strcpy() uses
strcpy() performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. So, remove all the uses and add devm_kstrdup() or devm_kasprintf() instead.
This patch is an effort to clean up the proliferation of str*() functions in the kernel and a previous step in the path to remove the strcpy function from the kernel entirely [1].
[1] https://github.com/KSPP/linux/issues/88
---
The previous versions can be found in:
v1
https://lore.kernel.org/linux-hardening/[email protected]/
v2
https://lore.kernel.org/linux-hardening/[email protected]/
v3
https://lore.kernel.org/linux-hardening/[email protected]/
Changelog v1 -> v2
- Modify the commit changelog to inform that the motivation of this patch is to remove the strcpy() function from the kernel entirely (Jonathan Cameron).
Changelog v2 -> v3
- Rewrite the code using devm_kstrdup() and devm_kasprintf() functions (Andy Shevchenko).
- Rebase against v5.14-rc5.
Changelog v3 -> v4
- Reorder the variables (Andy Shevchenko).
- Get the device in the definition block (Andy Shevchenko).
- Reword the comment (Andy Shevchenko).
- Change the conditions in the "if" statement to clarify the "0" check (Andy Shevchenko).
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +18 -12
@@ -261,6 +261,7 @@ int inv_mpu_magn_set_rate(const struct inv_mpu6050_state *st, int fifo_rate)*/ int inv_mpu_magn_set_orient (struct inv_mpu6050_state *st) { +struct device *dev =regmap_get_device (st->map);const char *orient;char *str;int i;
@@ -281,19 +282,24 @@ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st)/* z <- -z */ for (i =0 ; i <3 ; ++i) { orient = st->orientation.rotation[6 + i]; -/* use length + 2 for adding minus sign if needed */ - str =devm_kzalloc (regmap_get_device (st->map), -strlen (orient) +2 ,GFP_KERNEL ); -if (str ==NULL ) + +/* +* The value is negated according to one of the following +* rules: +* +* 1) Drop leading minus. +* 2) Leave 0 as is. +* 3) Add leading minus. +*/ +if (orient[0 ] =='-' ) + str =devm_kstrdup (dev, orient +1 ,GFP_KERNEL ); +else if (orient[0 ] =='0' && orient[1 ] =='\0' ) + str =devm_kstrdup (dev, orient,GFP_KERNEL ); +else + str =devm_kasprintf (dev,GFP_KERNEL ,"-%s" , orient); +if (!str)return -ENOMEM ; -if (strcmp (orient,"0" ) ==0 ) { -strcpy (str, orient); - }else if (orient[0 ] =='-' ) { -strcpy (str, &orient[1 ]); - }else { - str[0 ] ='-' ; -strcpy (&str[1 ], orient); - } + st->magn_orient.rotation[6 + i] = str; }break ;
--
2.25.1
Quoted 11 lines · Len Baker
strcpy()performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. So, remove all the uses and adddevm_kstrdup()ordevm_kasprintf()instead.This patch is an effort to clean up the proliferation of str*() functions in the kernel and a previous step in the path to remove the strcpy function from the kernel entirely [1].
Thank you very much for doing this!
Now I like the result,
Quoted 85 lines
Signed-off-by Len Baker <[email protected]>---
The previous versions can be found in:v1
https://lore.kernel.org/linux-hardening/[email protected]/v2
https://lore.kernel.org/linux-hardening/[email protected]/v3
https://lore.kernel.org/linux-hardening/[email protected]/Changelog v1 -> v2
- Modify the commit changelog to inform that the motivation of this patch is to remove thestrcpy()function from the kernel entirely (Jonathan Cameron).Changelog v2 -> v3
- Rewrite the code usingdevm_kstrdup()anddevm_kasprintf()functions (Andy Shevchenko).
- Rebase against v5.14-rc5.Changelog v3 -> v4
- Reorder the variables (Andy Shevchenko).
- Get the device in the definition block (Andy Shevchenko).
- Reword the comment (Andy Shevchenko).
- Change the conditions in the "if" statement to clarify the "0" check (Andy Shevchenko).drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-)drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +18
-13@@ -261,6 +261,7 @@ int inv_mpu_magn_set_rate(const struct inv_mpu6050_state *st, int fifo_rate)*/ int inv_mpu_magn_set_orient (struct inv_mpu6050_state *st) { +struct device *dev =regmap_get_device (st->map);const char *orient;char *str;int i;@@ -281,19 +282,24 @@ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st)/* z <- -z */ for (i =0 ; i <3 ; ++i) { orient = st->orientation.rotation[6 + i]; -/* use length + 2 for adding minus sign if needed */ - str =devm_kzalloc (regmap_get_device (st->map), -strlen (orient) +2 ,GFP_KERNEL ); -if (str ==NULL ) + +/* +* The value is negated according to one of the following +* rules: +* +* 1) Drop leading minus. +* 2) Leave 0 as is. +* 3) Add leading minus. +*/ +if (orient[0 ] =='-' ) + str =devm_kstrdup (dev, orient +1 ,GFP_KERNEL ); +else if (orient[0 ] =='0' && orient[1 ] =='\0' ) + str =devm_kstrdup (dev, orient,GFP_KERNEL ); +else + str =devm_kasprintf (dev,GFP_KERNEL ,"-%s" , orient); +if (!str)return -ENOMEM ; -if (strcmp (orient,"0" ) ==0 ) { -strcpy (str, orient); - }else if (orient[0 ] =='-' ) { -strcpy (str, &orient[1 ]); - }else { - str[0 ] ='-' ; -strcpy (&str[1 ], orient); - } + st->magn_orient.rotation[6 + i] = str; }break ; --2.25.1
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi Andy,
Quoted 11 lines · Andy Shevchenko
Quoted 10 lines · Len Baker
strcpy()performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. So, remove all the uses and adddevm_kstrdup()ordevm_kasprintf()instead.This patch is an effort to clean up the proliferation of str*() functions in the kernel and a previous step in the path to remove the strcpy function from the kernel entirely [1].
Quoted 5 lines
Quoted 1 line
Thank you very much for doing this!
Now I like the result,Reviewed-by Andy Shevchenko <[email protected]>
Thank you too Andy (and folks) for your help on this.
Regards,
Len
Jonathan Cameron
maintainerQuoted 18 lines · On Sun, 15 Aug 2021 10:19:49 +0200 Len Baker <[email protected]> wrote:
Hi Andy,
Quoted 15 lines · Andy Shevchenko
Quoted 11 lines · Len Baker
strcpy()performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. So, remove all the uses and adddevm_kstrdup()ordevm_kasprintf()instead.This patch is an effort to clean up the proliferation of str*() functions in the kernel and a previous step in the path to remove the strcpy function from the kernel entirely [1].
Thank you very much for doing this!
Now I like the result,
Agreed and applied to the togreg branch of iio.git, pushed out as testing for 0-day to poke at it and see if we missed anything.
Thanks,
Jonathan
Quoted 7 lines
Quoted 1 line
Reviewed-by Andy Shevchenko <[email protected]>Thank you too Andy (and folks) for your help on this.
Regards,
Len
Jonathan Cameron
maintainerQuoted 17 lines · On Sun, 15 Aug 2021 15:45:55 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 14 lines · On Sun, 15 Aug 2021 10:19:49 +0200 Len Baker <[email protected]> wrote:
Hi Andy,
Quoted 11 lines · Andy Shevchenko
Quoted 10 lines · Len Baker
strcpy()performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. So, remove all the uses and adddevm_kstrdup()ordevm_kasprintf()instead.This patch is an effort to clean up the proliferation of str*() functions in the kernel and a previous step in the path to remove the strcpy function from the kernel entirely [1].
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Thank you very much for doing this!
Now I like the result,Agreed and applied to the togreg branch of iio.git, pushed out as testing for 0-day to poke at it and see if we missed anything.
Dropped it for now so that Joe's comment can be addressed / discussed.
Jonathan
Quoted 12 lines
Thanks,
Jonathan
Quoted 7 lines
Quoted 1 line
Reviewed-by Andy Shevchenko <[email protected]>Thank you too Andy (and folks) for your help on this.
Regards,
Len
Quoted 11 lines · Andy Shevchenko
Quoted 10 lines · Len Baker
strcpy()performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. So, remove all the uses and adddevm_kstrdup()ordevm_kasprintf()instead.This patch is an effort to clean up the proliferation of str*() functions in the kernel and a previous step in the path to remove the strcpy function from the kernel entirely [1].
Quoted 5 lines
Quoted 1 line
Thank you very much for doing this!
Now I like the result,Reviewed-by Andy Shevchenko <[email protected]>
[]
Quoted 1 line
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c
Quoted 1 line
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +0
-0
[]
Quoted 20 lines
Quoted 20 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +5
-4@@ -261,6 +261,7 @@ int inv_mpu_magn_set_rate(const struct inv_mpu6050_state *st, int fifo_rate)*/ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) { +struct device *dev =regmap_get_device (st->map); const char *orient; char *str; int i;@@ -281,19 +282,24 @@ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) /* z <- -z */ for (i = 0; i < 3; ++i) { orient = st->orientation.rotation[6 + i]; -/* use length + 2 for adding minus sign if needed */ - str =devm_kzalloc (regmap_get_device (st->map), -strlen (orient) +2 ,GFP_KERNEL ); -if (str ==NULL ) + +/* +* The value is negated according to one of the following +* rules:
Quoted 8 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) · line 296
Quoted 8 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +8
-0+* +* 1) Drop leading minus. +* 2) Leave 0 as is. +* 3) Add leading minus. +*/ +if (orient[0 ] =='-' ) + str =devm_kstrdup (dev, orient +1 ,GFP_KERNEL ); +else if (orient[0 ] =='0' && orient[1 ] =='\0' )
bikeshed:
I think this change is less intelligible than the original strcmp.
And separately, perhaps for loop would be more readable as
for (i = 6; i < 9; i++)
converting the 6 + i uses to just i.
Quoted 17 lines
Quoted 17 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +5
-8+ str =devm_kstrdup (dev, orient,GFP_KERNEL ); + else + str =devm_kasprintf (dev,GFP_KERNEL ,"-%s" , orient); +if (!str) return -ENOMEM; -if (strcmp (orient,"0" ) ==0 ) { -strcpy (str, orient); - }else if (orient[0 ] =='-' ) { -strcpy (str, &orient[1 ]); - }else { - str[0 ] ='-' ; -strcpy (&str[1 ], orient); - } + st->magn_orient.rotation[6 + i] = str; } break;
Hi Joe,
Quoted 51 lines · Joe Perches
Quoted 11 lines · Andy Shevchenko
Quoted 10 lines · Len Baker
strcpy()performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. So, remove all the uses and adddevm_kstrdup()ordevm_kasprintf()instead.This patch is an effort to clean up the proliferation of str*() functions in the kernel and a previous step in the path to remove the strcpy function from the kernel entirely [1].
Quoted 5 lines
Quoted 1 line
Thank you very much for doing this!
Now I like the result,Reviewed-by Andy Shevchenko <[email protected]>[]
Quoted 1 line
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c
Quoted 1 line
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +0
-0[]
Quoted 20 lines
Quoted 20 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +5
-4@@ -261,6 +261,7 @@ int inv_mpu_magn_set_rate(const struct inv_mpu6050_state *st, int fifo_rate)*/ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) { +struct device *dev =regmap_get_device (st->map); const char *orient; char *str; int i;@@ -281,19 +282,24 @@ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) /* z <- -z */ for (i = 0; i < 3; ++i) { orient = st->orientation.rotation[6 + i]; -/* use length + 2 for adding minus sign if needed */ - str =devm_kzalloc (regmap_get_device (st->map), -strlen (orient) +2 ,GFP_KERNEL ); -if (str ==NULL ) + +/* +* The value is negated according to one of the following +* rules: Quoted 8 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) · line 296
Quoted 8 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +8
-0+* +* 1) Drop leading minus. +* 2) Leave 0 as is. +* 3) Add leading minus. +*/ +if (orient[0 ] =='-' ) + str =devm_kstrdup (dev, orient +1 ,GFP_KERNEL ); +else if (orient[0 ] =='0' && orient[1 ] =='\0' )bikeshed:
Quoted 1 line
I think this change is less intelligible than the original strcmp.
So, if I understand correctly you suggest to change the above line for:
else if (strcmp(orient, "0") == 0)
And yes, this line will be more readable ;)
Quoted 6 lines
And separately, perhaps for loop would be more readable as
for (i = 6; i < 9; i++)converting the 6 + i uses to just i.
Ok, no problem. If there are no objections I will send a new version with these 2 changes.
Regards,
Len
Quoted 20 lines
Quoted 18 lines
Quoted 18 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +5
-8+ str =devm_kstrdup (dev, orient,GFP_KERNEL ); + else + str =devm_kasprintf (dev,GFP_KERNEL ,"-%s" , orient); +if (!str) return -ENOMEM; -if (strcmp (orient,"0" ) ==0 ) { -strcpy (str, orient); - }else if (orient[0 ] =='-' ) { -strcpy (str, &orient[1 ]); - }else { - str[0 ] ='-' ; -strcpy (&str[1 ], orient); - } + st->magn_orient.rotation[6 + i] = str; } break;
Quoted 1 line · Len Baker
Hi Joe,
Hello Len.
Don't take this advice too seriously, it's just bikeshedding but it seems to me an unexpected use of a strcmp equivalent in a non performance sensitive path.
Quoted 1 line · Joe Perches
[]
Quoted 6 lines
Quoted 3 lines
bikeshed:
I think this change is less intelligible than the original strcmp.
So, if I understand correctly you suggest to change the above line for:
else if (strcmp(orient, "0")== 0)
Yes.
In kernel sources it's about 2:1 in favor of '!strcmp()' over 'strcmp() == 0'
$ git grep -P '\!\s*strcmp\b' | wc -l 3457 $ git grep -P '\bstrcmp\s*\([^\)]+\)\s*==\s*0\b' | wc -l 1719
And it's your choice to use one or the other or just your V4 patch.
btw, according to godbolt:
gcc -O2 doesn't call strcmp and produces the same object code as your byte comparisons. clang 11 calls strcmp regardless of optimization level.
Hi,
Quoted 20 lines · Joe Perches
Quoted 1 line · Len Baker
Hi Joe,
Hello Len.
Don't take this advice too seriously, it's just bikeshedding but it seems to me an unexpected use of a strcmp equivalent in a non performance sensitive path.
Quoted 1 line · Joe Perches
[]
Quoted 6 lines
Quoted 3 lines
bikeshed:
I think this change is less intelligible than the original strcmp.
So, if I understand correctly you suggest to change the above line for:
else if (strcmp(orient, "0")== 0)Yes.
Quoted 8 lines
In kernel sources it's about 2:1 in favor of '!strcmp()' over '
strcmp()== 0'$ git grep -P '\!\s*strcmp\b' | wc -l 3457 $ git grep -P '\bstrcmp\s*\([^\)]+\)\s*==\s*0\b' | wc -l 1719And it's your choice to use one or the other or just your V4 patch.
I will increase the !strcmp() statistics ;)
Quoted 7 lines
btw, according to godbolt:
gcc -O2 doesn't call strcmp and produces the same object code as your byte comparisons. clang 11 calls strcmp regardless of optimization level.
Thanks for the feedback,
Len