linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Len Baker <len.baker@gmx.com>
To: Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>
Cc: Len Baker <len.baker@gmx.com>,
	Andy Shevchenko <andy.shevchenko@gmail.com>,
	David Laight <David.Laight@ACULAB.COM>,
	Kees Cook <keescook@chromium.org>,
	linux-hardening@vger.kernel.org, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v3] drivers/iio: Remove all strcpy() uses
Date: Sat, 14 Aug 2021 11:06:18 +0200	[thread overview]
Message-ID: <20210814090618.8920-1-len.baker@gmx.com> (raw)

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>
---
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/20210801171157.17858-1-len.baker@gmx.com/

v2
https://lore.kernel.org/linux-hardening/20210807152225.9403-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.

 drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c | 32 ++++++++++++++--------
 1 file changed, 20 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..9c359f894631 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c
@@ -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


             reply	other threads:[~2021-08-14  9:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-14  9:06 Len Baker [this message]
2021-08-14 10:08 ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210814090618.8920-1-len.baker@gmx.com \
    --to=len.baker@gmx.com \
    --cc=David.Laight@ACULAB.COM \
    --cc=andy.shevchenko@gmail.com \
    --cc=jic23@kernel.org \
    --cc=keescook@chromium.org \
    --cc=lars@metafoo.de \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: [PATCH v3] drivers/iio: Remove all strcpy() uses' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).