NFS: Prefer strscpy over strlcpy calls
Subsystems (2)
PATCH 1/1 NFS: Prefer strscpy over strlcpy calls
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
---
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(-)
fs/nfs/nfsroot.c +1 -1
@@ -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 ; }
Signature · 1 line
2.40.1.606.ga4b1b128d6-goog
Quoted 13 lines · Azeem Shaikh
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-E2BIGon truncate for safe replacement withstrlcpy().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
Quoted 8 lines
Signed-off-by Azeem Shaikh <[email protected]>---
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. :)
-Kees
Quoted 21 lines
fs/nfs/nfsroot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/nfs/nfsroot.c +1
-1@@ -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 ; }Signature · 1 line
2.40.1.606.ga4b1b128d6-goog
Signature · 1 line
Kees Cook
Quoted 5 lines
Quoted 5 lines
---
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.
Quoted 8 lines
Quoted 2 lines
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 <[email protected]>
Friendly ping.
Kees Cook
AppliedQuoted 8 lines · Azeem Shaikh
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-E2BIGon truncate for safe replacement withstrlcpy().[...]
Applied to for-next/hardening, thanks!
[1/1] NFS: Prefer strscpy over strlcpy calls
https://git.kernel.org/kees/c/8ca25e00cf81
Signature · 1 line
Kees Cook