Skip to content

Commit d232606

Browse files
JustinStittwilldeacon
authored andcommitted
arm64/sysreg: refactor deprecated strncpy
`strncpy` is deprecated for use on NUL-terminated destination strings [1]. Which seems to be the case here due to the forceful setting of `buf`'s tail to 0. A suitable replacement is `strscpy` [2] due to the fact that it guarantees NUL-termination on its destination buffer argument which is _not_ the case for `strncpy`! In this case, we can simplify the logic and also check for any silent truncation by using `strscpy`'s return value. This should have no functional change and yet uses a more robust and less ambiguous interface whilst reducing code complexity. Link: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1] Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] Link: KSPP#90 Suggested-by: Kees Cook <keescook@chromium.org> Cc: linux-hardening@vger.kernel.org Signed-off-by: Justin Stitt <justinstitt@google.com> Link: https://lore.kernel.org/r/20230811-strncpy-arch-arm64-v2-1-ba84eabffadb@google.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent 18b8f57 commit d232606

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

arch/arm64/kernel/idreg-override.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
262262
if (!len)
263263
return;
264264

265-
len = min(len, ARRAY_SIZE(buf) - 1);
266-
strncpy(buf, cmdline, len);
267-
buf[len] = 0;
265+
len = strscpy(buf, cmdline, ARRAY_SIZE(buf));
266+
if (len == -E2BIG)
267+
len = ARRAY_SIZE(buf) - 1;
268268

269269
if (strcmp(buf, "--") == 0)
270270
return;

0 commit comments

Comments
 (0)