All of lore.kernel.org
 help / color / mirror / Atom feed
From: Romain Perier <romain.perier@gmail.com>
To: Kees Cook <keescook@chromium.org>,
	kernel-hardening@lists.openwall.com,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Tejun Heo <tj@kernel.org>
Cc: Romain Perier <romain.perier@gmail.com>, linux-kernel@vger.kernel.org
Subject: [PATCH 08/20] kernfs: Manual replacement of the deprecated strlcpy() with return values
Date: Mon, 22 Feb 2021 16:12:19 +0100	[thread overview]
Message-ID: <20210222151231.22572-9-romain.perier@gmail.com> (raw)
In-Reply-To: <20210222151231.22572-1-romain.perier@gmail.com>

The strlcpy() reads the entire source buffer first, it is dangerous if
the source buffer lenght is unbounded or possibility non NULL-terminated.
It can lead to linear read overflows, crashes, etc...

As recommended in the deprecated interfaces [1], it should be replaced
by strscpy.

This commit replaces all calls to strlcpy that handle the return values
by the corresponding strscpy calls with new handling of the return
values (as it is quite different between the two functions).

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy

Signed-off-by: Romain Perier <romain.perier@gmail.com>
---
 fs/kernfs/dir.c |   27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 7a53eed69fef..9e65b595d880 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -42,9 +42,9 @@ static bool kernfs_lockdep(struct kernfs_node *kn)
 static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
 {
 	if (!kn)
-		return strlcpy(buf, "(null)", buflen);
+		return strscpy(buf, "(null)", buflen);
 
-	return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
+	return strscpy(buf, kn->parent ? kn->name : "/", buflen);
 }
 
 /* kernfs_node_depth - compute depth from @from to @to */
@@ -125,17 +125,18 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
 {
 	struct kernfs_node *kn, *common;
 	const char parent_str[] = "/..";
-	size_t depth_from, depth_to, len = 0;
+	size_t depth_from, depth_to;
+	ssize_t len = 0;
 	int i, j;
 
 	if (!kn_to)
-		return strlcpy(buf, "(null)", buflen);
+		return strscpy(buf, "(null)", buflen);
 
 	if (!kn_from)
 		kn_from = kernfs_root(kn_to)->kn;
 
 	if (kn_from == kn_to)
-		return strlcpy(buf, "/", buflen);
+		return strscpy(buf, "/", buflen);
 
 	if (!buf)
 		return -EINVAL;
@@ -150,16 +151,16 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
 	buf[0] = '\0';
 
 	for (i = 0; i < depth_from; i++)
-		len += strlcpy(buf + len, parent_str,
+		len += strscpy(buf + len, parent_str,
 			       len < buflen ? buflen - len : 0);
 
 	/* Calculate how many bytes we need for the rest */
 	for (i = depth_to - 1; i >= 0; i--) {
 		for (kn = kn_to, j = 0; j < i; j++)
 			kn = kn->parent;
-		len += strlcpy(buf + len, "/",
+		len += strscpy(buf + len, "/",
 			       len < buflen ? buflen - len : 0);
-		len += strlcpy(buf + len, kn->name,
+		len += strscpy(buf + len, kn->name,
 			       len < buflen ? buflen - len : 0);
 	}
 
@@ -173,8 +174,8 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
  * @buflen: size of @buf
  *
  * Copies the name of @kn into @buf of @buflen bytes.  The behavior is
- * similar to strlcpy().  It returns the length of @kn's name and if @buf
- * isn't long enough, it's filled upto @buflen-1 and nul terminated.
+ * similar to strscpy().  It returns the length of @kn's name and if @buf
+ * isn't long enough or @buflen is 0, it returns -E2BIG.
  *
  * Fills buffer with "(null)" if @kn is NULL.
  *
@@ -858,7 +859,7 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
 					  const unsigned char *path,
 					  const void *ns)
 {
-	size_t len;
+	ssize_t len;
 	char *p, *name;
 
 	lockdep_assert_held(&kernfs_mutex);
@@ -866,9 +867,9 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
 	/* grab kernfs_rename_lock to piggy back on kernfs_pr_cont_buf */
 	spin_lock_irq(&kernfs_rename_lock);
 
-	len = strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
+	len = strscpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
 
-	if (len >= sizeof(kernfs_pr_cont_buf)) {
+	if (len == -E2BIG) {
 		spin_unlock_irq(&kernfs_rename_lock);
 		return NULL;
 	}


  parent reply	other threads:[~2021-02-22 15:18 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-22 15:12 [PATCH 00/20] Manual replacement of all strlcpy in favor of strscpy Romain Perier
2021-02-22 15:12 ` Romain Perier
2021-02-22 15:12 ` Romain Perier
2021-02-22 15:12 ` Romain Perier
2021-02-22 15:12 ` [PATCH 01/20] cgroup: Manual replacement of the deprecated strlcpy() with return values Romain Perier
2021-02-23 16:13   ` Michal Koutný
2021-02-23 16:13     ` Michal Koutný
2021-02-23 17:49     ` Romain Perier
2021-02-22 15:12 ` [PATCH 02/20] crypto: " Romain Perier
2021-03-04  4:37   ` Herbert Xu
2021-03-04 10:08     ` Romain Perier
2021-02-22 15:12 ` [PATCH 03/20] devlink: " Romain Perier
2021-02-23  0:56   ` Jakub Kicinski
2021-02-22 15:12 ` [PATCH 04/20] dma-buf: " Romain Perier
2021-02-22 15:12   ` Romain Perier
2021-02-22 15:12 ` [PATCH 05/20] kobject: " Romain Perier
2021-02-22 15:12 ` [PATCH 06/20] ima: " Romain Perier
2021-03-02 13:29   ` Mimi Zohar
2021-02-22 15:12 ` [PATCH 07/20] SUNRPC: " Romain Perier
2021-03-01 18:25   ` Chuck Lever
2021-03-01 18:25     ` Chuck Lever
2021-03-02  6:43     ` Romain Perier
2021-02-22 15:12 ` Romain Perier [this message]
2021-02-22 15:12 ` [PATCH 09/20] m68k/atari: " Romain Perier
2021-02-22 15:12 ` [PATCH 10/20] module: " Romain Perier
2021-02-22 15:12 ` [PATCH 11/20] hwmon: " Romain Perier
2021-02-22 15:46   ` Guenter Roeck
2021-02-23  9:43     ` Romain Perier
2021-02-28 11:50     ` Joe Perches
2021-02-28 11:50       ` Joe Perches
2021-02-22 15:12 ` [PATCH 12/20] s390/hmcdrv: " Romain Perier
2021-02-22 15:12 ` [PATCH 13/20] scsi: zfcp: " Romain Perier
2021-02-22 16:04   ` Benjamin Block
2021-02-22 15:12 ` [PATCH 14/20] target: " Romain Perier
2021-02-22 16:00   ` Bodo Stroesser
2021-02-22 18:09   ` kernel test robot
2021-02-22 18:09     ` kernel test robot
2021-02-22 15:12 ` [PATCH 15/20] ALSA: usb-audio: " Romain Perier
2021-02-22 15:12   ` Romain Perier
2021-02-22 15:39   ` Takashi Iwai
2021-02-22 15:39     ` Takashi Iwai
2021-02-22 15:12 ` [PATCH 16/20] tracing/probe: " Romain Perier
2021-02-22 17:49   ` Steven Rostedt
2021-02-25  8:38     ` Romain Perier
2021-02-22 15:12 ` [PATCH 17/20] vt: " Romain Perier
2021-02-26  9:49   ` Jiri Slaby
2021-02-26 15:09     ` Romain Perier
2021-02-22 15:12 ` [PATCH 18/20] usb: gadget: f_midi: " Romain Perier
2021-02-22 15:12 ` [PATCH 19/20] usbip: usbip_host: " Romain Perier
2021-02-22 16:21   ` Shuah Khan
2021-02-28  9:03   ` Sergei Shtylyov
2021-02-28 16:00   ` Andy Shevchenko
2021-02-22 15:12 ` [PATCH 20/20] s390/watchdog: " Romain Perier
2021-02-22 15:55   ` Guenter Roeck
2021-02-22 16:36 ` [PATCH 00/20] Manual replacement of all strlcpy in favor of strscpy Shuah Khan
2021-02-22 16:36   ` Shuah Khan
2021-02-22 16:36   ` Shuah Khan
2021-02-22 16:36   ` Shuah Khan
2021-02-23  9:31   ` Romain Perier
2021-02-23  9:31     ` Romain Perier
2021-02-23  9:31     ` Romain Perier
2021-02-23  9:31     ` Romain Perier

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=20210222151231.22572-9-romain.perier@gmail.com \
    --to=romain.perier@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=keescook@chromium.org \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tj@kernel.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.