* [PATCH] bpftool: Replace strncpy with strscpy
@ 2025-02-28 18:18 Michael Estner
2025-02-28 21:37 ` Quentin Monnet
0 siblings, 1 reply; 2+ messages in thread
From: Michael Estner @ 2025-02-28 18:18 UTC (permalink / raw)
To: qmo, ast, daniel; +Cc: bpf, linux-kernel, linux-hardening, Michael Estner
strncpy() is deprecated for NUL-terminated destination buffers. Use
strscpy() instead and remove the manual NUL-termination.
Compile-tested only.
Link: https://github.com/KSPP/linux/issues/90
Signed-off-by: Michael Estner <michaelestner@web.de>
---
tools/bpf/bpftool/xlated_dumper.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
index d0094345fb2b..60dbe48a91a3 100644
--- a/tools/bpf/bpftool/xlated_dumper.c
+++ b/tools/bpf/bpftool/xlated_dumper.c
@@ -135,8 +135,7 @@ print_insn_json(void *private_data, const char *fmt, ...)
va_start(args, fmt);
if (l > 0) {
- strncpy(chomped_fmt, fmt, l - 1);
- chomped_fmt[l - 1] = '\0';
+ strscpy(chomped_fmt, fmt);
}
jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
va_end(args);
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] bpftool: Replace strncpy with strscpy
2025-02-28 18:18 [PATCH] bpftool: Replace strncpy with strscpy Michael Estner
@ 2025-02-28 21:37 ` Quentin Monnet
0 siblings, 0 replies; 2+ messages in thread
From: Quentin Monnet @ 2025-02-28 21:37 UTC (permalink / raw)
To: Michael Estner, ast, daniel; +Cc: bpf, linux-kernel, linux-hardening
2025-02-28 19:18 UTC+0100 ~ Michael Estner <michaelestner@web.de>
> strncpy() is deprecated for NUL-terminated destination buffers. Use
> strscpy() instead and remove the manual NUL-termination.
>
> Compile-tested only.
How? The change does _not_ compile in my case:
$ cd tool/bpf/bpftool
$ make -j
[...]
/usr/bin/ld: xlated_dumper.o: in function `print_insn_json':
xlated_dumper.c:(.text+0x1f6): undefined reference to `strscpy'
collect2: error: ld returned 1 exit status
make: *** [Makefile:254: bpftool] Error 1
(Besides, this code should be rather easy to test, so running it is
appreciated.)
strscpy() has been proposed for bpftool a few times in the past, but
bpftool is a user space utility and does not currently #include header
linux/string.h. If we wanted to use strscpy(), we'd likely need to use
this header, and also to copy the definition of the function to the
GitHub mirror. Given that - as far as I know - the current use of
strncpy() is not broken, I'm not sure this is worth the effort.
> Link: https://github.com/KSPP/linux/issues/90
I note that this Issue provides a command for looking for strncpy()
instances to replace, but this command filters out occurrences that are
under tools/:
"git grep ... | grep -vE '^(Documentation|tools|...) ..."
Thanks,
Quentin
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-02-28 21:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-28 18:18 [PATCH] bpftool: Replace strncpy with strscpy Michael Estner
2025-02-28 21:37 ` Quentin Monnet
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox