Skip to content

Commit

Permalink
nios2: Replace all non-returning 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().
No return values were used, so direct replacement is safe.

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

Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
Signed-off-by: Dinh Nguyen <dinguyen@kernel.org>
  • Loading branch information
azeemshaikh38 authored and Dinh Nguyen committed Jun 13, 2023
1 parent 6ebe94b commit 6a22e01
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion arch/nios2/kernel/cpuinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void __init setup_cpuinfo(void)

str = of_get_property(cpu, "altr,implementation", &len);
if (str)
strlcpy(cpuinfo.cpu_impl, str, sizeof(cpuinfo.cpu_impl));
strscpy(cpuinfo.cpu_impl, str, sizeof(cpuinfo.cpu_impl));
else
strcpy(cpuinfo.cpu_impl, "<unknown>");

Expand Down
6 changes: 3 additions & 3 deletions arch/nios2/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,18 @@ asmlinkage void __init nios2_boot_init(unsigned r4, unsigned r5, unsigned r6,
dtb_passed = r6;

if (r7)
strlcpy(cmdline_passed, (char *)r7, COMMAND_LINE_SIZE);
strscpy(cmdline_passed, (char *)r7, COMMAND_LINE_SIZE);
}
#endif

early_init_devtree((void *)dtb_passed);

#ifndef CONFIG_CMDLINE_FORCE
if (cmdline_passed[0])
strlcpy(boot_command_line, cmdline_passed, COMMAND_LINE_SIZE);
strscpy(boot_command_line, cmdline_passed, COMMAND_LINE_SIZE);
#ifdef CONFIG_NIOS2_CMDLINE_IGNORE_DTB
else
strlcpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
#endif
#endif

Expand Down

0 comments on commit 6a22e01

Please sign in to comment.