linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Justin Stitt <justinstitt@google.com>
To: Phillip Lougher <phillip@squashfs.org.uk>
Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org,
	 Justin Stitt <justinstitt@google.com>
Subject: [PATCH] Squashfs: replace deprecated strncpy with strscpy
Date: Thu, 28 Mar 2024 21:52:59 +0000	[thread overview]
Message-ID: <20240328-strncpy-fs-squashfs-namei-c-v1-1-5c7bcbbeb675@google.com> (raw)

strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

The previous code took special care of NUL-terminating the destination
buffer via a manual assignment. As such, there is no bug in the current
implementation. However, in an effort to rid the kernel of strscpy()
[2], Let's instead use strscpy() which guarantees this behavior [3]. To
ensure we can copy the same number of bytes as before, add 1 to the
length argument provided to strscpy().

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://github.com/KSPP/linux/issues/90 [2]
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html  [3]
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: build-tested only.

Found with: $ rg "strncpy\("
---
 fs/squashfs/namei.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fs/squashfs/namei.c b/fs/squashfs/namei.c
index 11e4539b9eae..6c4704ba8f42 100644
--- a/fs/squashfs/namei.c
+++ b/fs/squashfs/namei.c
@@ -80,8 +80,7 @@ static int get_dir_index_using_name(struct super_block *sb,
 	}
 
 	str = &index->name[SQUASHFS_NAME_LEN + 1];
-	strncpy(str, name, len);
-	str[len] = '\0';
+	strscpy(str, name, len + 1);
 
 	for (i = 0; i < i_count; i++) {
 		err = squashfs_read_metadata(sb, index, &index_start,

---
base-commit: 928a87efa42302a23bb9554be081a28058495f22
change-id: 20240328-strncpy-fs-squashfs-namei-c-9d01b8975e53

Best regards,
--
Justin Stitt <justinstitt@google.com>


             reply	other threads:[~2024-03-28 21:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-28 21:52 Justin Stitt [this message]
2024-03-29  3:48 ` [PATCH] Squashfs: replace deprecated strncpy with strscpy Kees Cook
2024-04-03 19:31 ` Phillip Lougher
2024-04-04  0:30   ` Justin Stitt
2024-04-04  0:38     ` Phillip Lougher

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=20240328-strncpy-fs-squashfs-namei-c-v1-1-5c7bcbbeb675@google.com \
    --to=justinstitt@google.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=phillip@squashfs.org.uk \
    /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 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).