Skip to content

Commit

Permalink
init/version.c: Replace strlcpy with strscpy
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
azeemshaikh38 authored and kees committed Sep 22, 2023
1 parent 215199e commit 8ebab15
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions init/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ static int __init early_hostname(char *arg)
{
size_t bufsize = sizeof(init_uts_ns.name.nodename);
size_t maxlen = bufsize - 1;
size_t arglen;
ssize_t arglen;

arglen = strlcpy(init_uts_ns.name.nodename, arg, bufsize);
if (arglen > maxlen) {
arglen = strscpy(init_uts_ns.name.nodename, arg, bufsize);
if (arglen < 0) {
pr_warn("hostname parameter exceeds %zd characters and will be truncated",
maxlen);
}
Expand Down

0 comments on commit 8ebab15

Please sign in to comment.