Skip to content

Commit 8ebab15

Browse files
azeemshaikh38kees
authored andcommitted
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] KSPP#89 Signed-off-by: Azeem Shaikh <[email protected]> Reviewed-by: Justin Stitt <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 215199e commit 8ebab15

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

init/version.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ static int __init early_hostname(char *arg)
2121
{
2222
size_t bufsize = sizeof(init_uts_ns.name.nodename);
2323
size_t maxlen = bufsize - 1;
24-
size_t arglen;
24+
ssize_t arglen;
2525

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) {
2828
pr_warn("hostname parameter exceeds %zd characters and will be truncated",
2929
maxlen);
3030
}

0 commit comments

Comments
 (0)