Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

init/version.c: Replace strlcpy with strscpy

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].
In an effort to remove strlcpy() completely [2], replace
strlcpy() here with strscpy().

Direct replacement is safe here since return value of -errno
is used to check for truncation instead of sizeof(dest).

[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>
Reviewed-by: Justin Stitt <justinstitt@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20230830160806.3821893-1-azeemshaikh38@gmail.com
Signed-off-by: Kees Cook <keescook@chromium.org>

authored by

Azeem Shaikh and committed by
Kees Cook
8ebab155 215199e3

+3 -3
+3 -3
init/version.c
··· 21 21 { 22 22 size_t bufsize = sizeof(init_uts_ns.name.nodename); 23 23 size_t maxlen = bufsize - 1; 24 - size_t arglen; 24 + ssize_t arglen; 25 25 26 - arglen = strlcpy(init_uts_ns.name.nodename, arg, bufsize); 27 - if (arglen > maxlen) { 26 + arglen = strscpy(init_uts_ns.name.nodename, arg, bufsize); 27 + if (arglen < 0) { 28 28 pr_warn("hostname parameter exceeds %zd characters and will be truncated", 29 29 maxlen); 30 30 }