linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] NFS: Prefer strscpy over strlcpy calls
@ 2023-05-12 15:57 Azeem Shaikh
  2023-05-16 21:04 ` Kees Cook
  2023-05-22 19:39 ` Kees Cook
  0 siblings, 2 replies; 4+ messages in thread
From: Azeem Shaikh @ 2023-05-12 15:57 UTC (permalink / raw)
  To: Trond Myklebust, Anna Schumaker
  Cc: linux-hardening, Azeem Shaikh, linux-nfs, linux-kernel

strlcpy() reads the entire source buffer first.
This read may exceed the destination size limit.
This is both inefficient and can lead to linear read
overflows if a source string is not NUL-terminated [1].
Check for strscpy()'s return value of -E2BIG on truncate for safe
replacement with strlcpy().

This is part of a tree-wide cleanup to remove the strlcpy() function
entirely from the kernel [2].

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89

Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
---
Note to reviewers: the one case where this patch would modify existing behavior
is when strlen(src)==destlen==0. Current behavior returns 0, with this patch it
would return -1.

Not sure what the implication of this updated behavior would be,
so bringing it to your attention.

 fs/nfs/nfsroot.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/nfs/nfsroot.c b/fs/nfs/nfsroot.c
index 620329b7e6ae..7600100ba26f 100644
--- a/fs/nfs/nfsroot.c
+++ b/fs/nfs/nfsroot.c
@@ -164,7 +164,7 @@ __setup("nfsroot=", nfs_root_setup);
 static int __init root_nfs_copy(char *dest, const char *src,
 				     const size_t destlen)
 {
-	if (strlcpy(dest, src, destlen) > destlen)
+	if (strscpy(dest, src, destlen) == -E2BIG)
 		return -1;
 	return 0;
 }
-- 
2.40.1.606.ga4b1b128d6-goog



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

* Re: [PATCH] NFS: Prefer strscpy over strlcpy calls
  2023-05-12 15:57 [PATCH] NFS: Prefer strscpy over strlcpy calls Azeem Shaikh
@ 2023-05-16 21:04 ` Kees Cook
  2023-05-22 16:19   ` Azeem Shaikh
  2023-05-22 19:39 ` Kees Cook
  1 sibling, 1 reply; 4+ messages in thread
From: Kees Cook @ 2023-05-16 21:04 UTC (permalink / raw)
  To: Azeem Shaikh
  Cc: Trond Myklebust, Anna Schumaker, linux-hardening, linux-nfs,
	linux-kernel

On Fri, May 12, 2023 at 03:57:49PM +0000, Azeem Shaikh wrote:
> strlcpy() reads the entire source buffer first.
> This read may exceed the destination size limit.
> This is both inefficient and can lead to linear read
> overflows if a source string is not NUL-terminated [1].
> Check for strscpy()'s return value of -E2BIG on truncate for safe
> replacement with strlcpy().
> 
> This is part of a tree-wide cleanup to remove the strlcpy() function
> entirely from the kernel [2].
> 
> [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
> [2] https://github.com/KSPP/linux/issues/89
> 
> Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
> ---
> Note to reviewers: the one case where this patch would modify existing behavior
> is when strlen(src)==destlen==0. Current behavior returns 0, with this patch it
> would return -1.
> 
> Not sure what the implication of this updated behavior would be,
> so bringing it to your attention.

I'm not sure either, but I would prefer non-terminated strings produce
an error, which this change does. :)

Reviewed-by: Kees Cook <keescook@chromium.org>

-Kees

> 
>  fs/nfs/nfsroot.c |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/nfs/nfsroot.c b/fs/nfs/nfsroot.c
> index 620329b7e6ae..7600100ba26f 100644
> --- a/fs/nfs/nfsroot.c
> +++ b/fs/nfs/nfsroot.c
> @@ -164,7 +164,7 @@ __setup("nfsroot=", nfs_root_setup);
>  static int __init root_nfs_copy(char *dest, const char *src,
>  				     const size_t destlen)
>  {
> -	if (strlcpy(dest, src, destlen) > destlen)
> +	if (strscpy(dest, src, destlen) == -E2BIG)
>  		return -1;
>  	return 0;
>  }
> -- 
> 2.40.1.606.ga4b1b128d6-goog
> 
> 

-- 
Kees Cook

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

* Re: [PATCH] NFS: Prefer strscpy over strlcpy calls
  2023-05-16 21:04 ` Kees Cook
@ 2023-05-22 16:19   ` Azeem Shaikh
  0 siblings, 0 replies; 4+ messages in thread
From: Azeem Shaikh @ 2023-05-22 16:19 UTC (permalink / raw)
  To: Kees Cook
  Cc: Trond Myklebust, Anna Schumaker, linux-hardening, linux-nfs,
	linux-kernel

> > ---
> > Note to reviewers: the one case where this patch would modify existing behavior
> > is when strlen(src)==destlen==0. Current behavior returns 0, with this patch it
> > would return -1.
> >
> > Not sure what the implication of this updated behavior would be,
> > so bringing it to your attention.
>
> I'm not sure either, but I would prefer non-terminated strings produce
> an error, which this change does. :)
>
> Reviewed-by: Kees Cook <keescook@chromium.org>
>

Friendly ping.

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

* Re: [PATCH] NFS: Prefer strscpy over strlcpy calls
  2023-05-12 15:57 [PATCH] NFS: Prefer strscpy over strlcpy calls Azeem Shaikh
  2023-05-16 21:04 ` Kees Cook
@ 2023-05-22 19:39 ` Kees Cook
  1 sibling, 0 replies; 4+ messages in thread
From: Kees Cook @ 2023-05-22 19:39 UTC (permalink / raw)
  To: azeemshaikh38, anna, trond.myklebust
  Cc: Kees Cook, linux-nfs, linux-kernel, linux-hardening

On Fri, 12 May 2023 15:57:49 +0000, Azeem Shaikh wrote:
> strlcpy() reads the entire source buffer first.
> This read may exceed the destination size limit.
> This is both inefficient and can lead to linear read
> overflows if a source string is not NUL-terminated [1].
> Check for strscpy()'s return value of -E2BIG on truncate for safe
> replacement with strlcpy().
> 
> [...]

Applied to for-next/hardening, thanks!

[1/1] NFS: Prefer strscpy over strlcpy calls
      https://git.kernel.org/kees/c/8ca25e00cf81

-- 
Kees Cook


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

end of thread, other threads:[~2023-05-22 19:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-12 15:57 [PATCH] NFS: Prefer strscpy over strlcpy calls Azeem Shaikh
2023-05-16 21:04 ` Kees Cook
2023-05-22 16:19   ` Azeem Shaikh
2023-05-22 19:39 ` Kees Cook

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