From: "Darrick J. Wong" <djwong@kernel.org>
To: Marcelo Moreira <marcelomoreira1905@gmail.com>
Cc: cem@kernel.org, linux-xfs@vger.kernel.org,
linux-kernel@vger.kernel.org, skhan@linuxfoundation.org,
linux-kernel-mentees@lists.linuxfoundation.org
Subject: Re: [PATCH] xfs: Replace strncpy with strscpy
Date: Thu, 17 Jul 2025 09:39:16 -0700 [thread overview]
Message-ID: <20250717163916.GR2672049@frogsfrogsfrogs> (raw)
In-Reply-To: <20250716182220.203631-1-marcelomoreira1905@gmail.com>
On Wed, Jul 16, 2025 at 03:20:37PM -0300, Marcelo Moreira wrote:
> The `strncpy` function is deprecated for NUL-terminated strings as
> explained in the "strncpy() on NUL-terminated strings" section of
> Documentation/process/deprecated.rst.
>
> In `xrep_symlink_salvage_inline()`, the `target_buf` (which is `sc->buf`)
> is intended to hold a NUL-terminated symlink path. The original code
> used `strncpy(target_buf, ifp->if_data, nr)`, where `nr` is the maximum
> number of bytes to copy. This approach is problematic because `strncpy()`
> does not guarantee NUL-termination if the source string is truncated
> exactly at `nr` bytes, which can lead to out-of-bounds read issues
> if the buffer is later treated as a NUL-terminated string.
> Evidence from `fs/xfs/scrub/symlink.c` (e.g., `strnlen(sc->buf,
> XFS_SYMLINK_MAXLEN)`) confirms that `sc->buf` is indeed expected to be
> NUL-terminated. Furthermore, `sc->buf` is allocated with
> `kvzalloc(XFS_SYMLINK_MAXLEN + 1, ...)`, explicitly reserving space for
> the NUL terminator.
>
> `strscpy()` is the proper replacement because it guarantees NUL-termination
> of the destination buffer, correctly handles the copy limit, and aligns
> with current kernel string-copying best practices.
> Other recommended functions like `strscpy_pad()`, `memcpy()`, or
> `memcpy_and_pad()` were not used because:
> - `strscpy_pad()` would unnecessarily zero-pad the entire buffer beyond the
> NUL terminator, which is not required as the function returns `nr` bytes.
> - `memcpy()` and `memcpy_and_pad()` do not guarantee NUL-termination, which
> is critical given `target_buf` is used as a NUL-terminated string.
>
> This change improves code safety and clarity by using a safer function for
> string copying.
Did you find an actual bug in online fsck, or is this just
s/strncpy/strscpy/ ? If the code already works correctly, please leave
it alone. Unless you want to take on all the online fsck and fuzz
testing to make sure the changes don't break anything.
--D
> Signed-off-by: Marcelo Moreira <marcelomoreira1905@gmail.com>
> ---
> fs/xfs/scrub/symlink_repair.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/xfs/scrub/symlink_repair.c b/fs/xfs/scrub/symlink_repair.c
> index 953ce7be78dc..ce21c7f0ef54 100644
> --- a/fs/xfs/scrub/symlink_repair.c
> +++ b/fs/xfs/scrub/symlink_repair.c
> @@ -185,7 +185,7 @@ xrep_symlink_salvage_inline(
> return 0;
>
> nr = min(XFS_SYMLINK_MAXLEN, xfs_inode_data_fork_size(ip));
> - strncpy(target_buf, ifp->if_data, nr);
> + strscpy(target_buf, ifp->if_data, XFS_SYMLINK_MAXLEN + 1);
> return nr;
> }
>
> --
> 2.50.0
>
>
next prev parent reply other threads:[~2025-07-17 16:39 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-16 18:20 [PATCH] xfs: Replace strncpy with strscpy Marcelo Moreira
2025-07-16 23:52 ` Dave Chinner
2025-07-17 17:34 ` Marcelo Moreira
2025-07-18 11:16 ` Dave Chinner
2025-07-18 19:10 ` Marcelo Moreira
2025-07-20 13:35 ` Carlos Maiolino
2025-07-20 22:24 ` Marcelo Moreira
2025-07-17 16:39 ` Darrick J. Wong [this message]
2025-07-17 17:36 ` Marcelo Moreira
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=20250717163916.GR2672049@frogsfrogsfrogs \
--to=djwong@kernel.org \
--cc=cem@kernel.org \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=marcelomoreira1905@gmail.com \
--cc=skhan@linuxfoundation.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.