* [PATCH] kallsyms: replace deprecated strncpy with strscpy
@ 2024-04-12 18:53 Justin Stitt
2024-04-12 20:20 ` Luis Chamberlain
0 siblings, 1 reply; 2+ messages in thread
From: Justin Stitt @ 2024-04-12 18:53 UTC (permalink / raw)
To: Luis Chamberlain, Nathan Chancellor, Nick Desaulniers,
Bill Wendling
Cc: linux-modules, linux-kernel, llvm, linux-hardening, Justin Stitt
strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces. The goal is to remove its use completely [2].
namebuf is eventually cleaned of any trailing llvm suffixes using
strstr(). This hints that namebuf should be NUL-terminated.
static void cleanup_symbol_name(char *s)
{
char *res;
...
res = strstr(s, ".llvm.");
...
}
Due to this, use strscpy() over strncpy() as it guarantees
NUL-termination on the destination buffer. Drop the -1 from the length
calculation as it is no longer needed to ensure NUL-termination.
Link: https://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
Link: https://github.com/KSPP/linux/issues/90 [2]
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
---
kernel/module/kallsyms.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index ef73ae7c8909..62fb57bb9f16 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -348,7 +348,7 @@ const char *module_address_lookup(unsigned long addr,
}
/* Make a copy in here where it's safe */
if (ret) {
- strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
+ strscpy(namebuf, ret, KSYM_NAME_LEN);
ret = namebuf;
}
preempt_enable();
---
base-commit: d7ad0581567927c433918bb5f06f3d29f89807d3
change-id: 20240412-strncpy-kernel-module-kallsyms-c-8539c5e7bf57
Best regards,
--
Justin Stitt <justinstitt@google.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] kallsyms: replace deprecated strncpy with strscpy
2024-04-12 18:53 [PATCH] kallsyms: replace deprecated strncpy with strscpy Justin Stitt
@ 2024-04-12 20:20 ` Luis Chamberlain
0 siblings, 0 replies; 2+ messages in thread
From: Luis Chamberlain @ 2024-04-12 20:20 UTC (permalink / raw)
To: Justin Stitt
Cc: Nathan Chancellor, Nick Desaulniers, Bill Wendling, linux-modules,
linux-kernel, llvm, linux-hardening
On Fri, Apr 12, 2024 at 06:53:47PM +0000, Justin Stitt wrote:
> strncpy() is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces. The goal is to remove its use completely [2].
>
> namebuf is eventually cleaned of any trailing llvm suffixes using
> strstr(). This hints that namebuf should be NUL-terminated.
>
> static void cleanup_symbol_name(char *s)
> {
> char *res;
> ...
> res = strstr(s, ".llvm.");
> ...
> }
>
> Due to this, use strscpy() over strncpy() as it guarantees
> NUL-termination on the destination buffer. Drop the -1 from the length
> calculation as it is no longer needed to ensure NUL-termination.
>
> Link: https://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
> Link: https://github.com/KSPP/linux/issues/90 [2]
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
Thanks! Queued up on modules-next.
Luis
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-04-12 20:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-12 18:53 [PATCH] kallsyms: replace deprecated strncpy with strscpy Justin Stitt
2024-04-12 20:20 ` Luis Chamberlain
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).