Skip to contentSkip to conversation

drivers/iio: Remove all strcpy() uses

PATCH Superseded Len Baker 2 participants 2 messages Aug 14, 2021

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Len Baker

author
#0 lore raw

PATCH v3 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

---
Hi,

This patch doesn't change the logic. I think it is better to use the current logic and not use always the plus and minus signs as suggested in the previous version. I don't like the idea that 0 has sign.

Thanks,
Len

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]/

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.

 drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c | 32 ++++++++++++++--------
 1 file changed, 20 insertions(+), 12 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +20 -12
index f282e9cc34c5..9c359f894631 100644
@@ -264,6 +264,7 @@ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st)
 	const char *orient;
 	char *str;
 	int i;
+	struct device *dev;
 
 	/* fill magnetometer orientation */
 	switch (st->chip_type) {
@@ -279,21 +280,28 @@ int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st)
 		st->magn_orient.rotation[4] = st->orientation.rotation[1];
 		st->magn_orient.rotation[5] = st->orientation.rotation[2];
 		/* z <- -z */
+		dev = regmap_get_device(st->map);
 		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 inverted according to the following
+			 * rules:
+			 *
+			 * 1) Drop leading minus.
+			 * 2) Add leading minus.
+			 * 3) Leave 0 as is.
+			 */
+			if (orient[0] == '-')
+				str = devm_kstrdup(dev, orient + 1, GFP_KERNEL);
+			else if (orient[0] != '0' || orient[1] != '\0')
+				str = devm_kasprintf(dev, GFP_KERNEL, "-%s", orient);
+			else
+				str = devm_kstrdup(dev, orient, GFP_KERNEL);
+
+			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

#1 lore raw
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 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

Thanks for an update, my comments below.

...

Quoted 3 lines

This patch doesn't change the logic. I think it is better to use the current logic and not use always the plus and minus signs as suggested in the previous version. I don't like the idea that 0 has sign.

Agree on that, the safest way to go with.

...

Quoted 4 lines
        const char *orient;
        char *str;
        int i;

+ struct device *dev;

Please, keep this in reversed xmas tree order (longer lines first).

...

Quoted 1 line

+ dev = regmap_get_device(st->map);

I haven't checked the code in between, but maybe it's possible to move an assignment directly to the definition block above.

...

drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) · line 288

Quoted 2 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +2 -0
+                       /*
+                        * The value is inverted according to the following

"to one of the"
And technically speaking "inversion" is not the same as negation (which is "sign inversion").

drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) · line 298

Quoted 11 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +10 -0
+                        * rules:
+                        *
+                        * 1) Drop leading minus.
+                        * 2) Add leading minus.
+                        * 3) Leave 0 as is.
+                        */
+                       if (orient[0] == '-')
+                               str = devm_kstrdup(dev, orient + 1, GFP_KERNEL);
 
+                       else if (orient[0] != '0' || orient[1] != '\0')
+                               str = devm_kasprintf(dev, GFP_KERNEL, "-%s", orient);

I would go with the logic I suggested later on, i.e.

                       else if (orient[0] == '0' && orient[1] == '\0')
                               str = devm_kstrdup(dev, orient, GFP_KERNEL);

and below changed accordingly. It will clarify the "0" check.

drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) · line 301

Quoted 4 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c +3 -0
+                       else
+                               str = devm_kstrdup(dev, orient, GFP_KERNEL);
 
+

Redundant blank line.

Quoted 2 lines

+ if (!str)

                                return -ENOMEM;
Signature · 2 lines
With Best Regards,
Andy Shevchenko