All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4] drivers/iio: Remove all strcpy() uses
@ 2021-08-14 13:55 Len Baker
  2021-08-14 19:36 ` Andy Shevchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Len Baker @ 2021-08-14 13:55 UTC (permalink / raw)
  To: Jonathan Cameron, Lars-Peter Clausen
  Cc: Len Baker, Andy Shevchenko, David Laight, Kees Cook,
	linux-hardening, linux-iio, linux-kernel

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

Signed-off-by: Len Baker <len.baker@gmx.com>
---
The previous versions can be found in:

v1
https://lore.kernel.org/linux-hardening/20210801171157.17858-1-len.baker@gmx.com/

v2
https://lore.kernel.org/linux-hardening/20210807152225.9403-1-len.baker@gmx.com/

v3
https://lore.kernel.org/linux-hardening/20210814090618.8920-1-len.baker@gmx.com/

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(-)

diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c
index f282e9cc34c5..7eceae0012c9 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c
@@ -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


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-08-15 17:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-14 13:55 [PATCH v4] drivers/iio: Remove all strcpy() uses Len Baker
2021-08-14 19:36 ` Andy Shevchenko
2021-08-15  8:19   ` Len Baker
2021-08-15 14:45     ` Jonathan Cameron
2021-08-15 15:59       ` Jonathan Cameron
2021-08-15 15:06   ` Joe Perches
2021-08-15 16:36     ` Len Baker
2021-08-15 16:58       ` Joe Perches
2021-08-15 17:12         ` Len Baker

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.