tracing: Replace deprecated strncpy() with memcpy() for stack_trace_filter_buf
PATCH 1/1 tracing: Replace deprecated strncpy() with memcpy() for stack_trace_filter_buf
strncpy() is deprecated for NUL-terminated destination buffers and must be replaced by memcpy() for length bounded buffers.
See issue: https://github.com/KSPP/linux/issues/90
--- kernel/trace/trace_stack.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/trace/trace_stack.c +6 -3
@@ -3,6 +3,7 @@* Copyright (C) 2008 Steven Rostedt <[email protected]> * */ +#include <linux/compiler_attributes.h> #include <linux/sched/task_stack.h> #include <linux/stacktrace.h> #include <linux/security.h>
@@ -537,14 +538,16 @@ stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,return ret; } -static char stack_trace_filter_buf[COMMAND_LINE_SIZE +1 ]__initdata ; +static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata __nonstring;static __init int enable_stacktrace (char *str) {int len; -if ((len =str_has_prefix (str,"_filter=" ))) -strncpy (stack_trace_filter_buf, str + len,COMMAND_LINE_SIZE ); + len =str_has_prefix (str,"_filter=" ); + +if (len) +memcpy (stack_trace_filter_buf, str + len,sizeof (stack_trace_filter_buf)); stack_tracer_enabled =1 ;return 1 ;
Signature · 1 line
2.47.1
Steven Rostedt
maintainerQuoted 7 lines · On Fri, 4 Apr 2025 00:43:40 +0530 Devaansh Kumar <[email protected]>…
kernel/trace/trace_stack.c +1
-1@@ -537,14 +538,16 @@ stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,return ret; } -static char stack_trace_filter_buf[COMMAND_LINE_SIZE +1 ]__initdata ; +static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata __nonstring;
kernel/trace/trace_stack.c stack_trace_sysctl(struct ctl_table *table, int write, void *buffer, · line 550
Quoted 10 lines
kernel/trace/trace_stack.c +4
-2static __init int enable_stacktrace (char *str) {int len; -if ((len =str_has_prefix (str,"_filter=" ))) -strncpy (stack_trace_filter_buf, str + len,COMMAND_LINE_SIZE ); + len =str_has_prefix (str,"_filter=" ); + +if (len) +memcpy (stack_trace_filter_buf, str + len,sizeof (stack_trace_filter_buf));
Hmm, this location looks like it can just use strscpy().
-- Steve
Quoted 4 lines
kernel/trace/trace_stack.c +0
-0stack_tracer_enabled =1 ;return 1 ;
Quoted 15 lines · Steven Rostedt
Quoted 11 lines · On Fri, 4 Apr 2025 00:43:40 +0530 Devaansh Kumar <[email protected]>…
kernel/trace/trace_stack.c +1
-1@@ -537,14 +538,16 @@ stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,return ret; } -static char stack_trace_filter_buf[COMMAND_LINE_SIZE +1 ]__initdata ; +static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata __nonstring;static __init int enable_stacktrace (char *str) {int len;
Quoted 8 lines
kernel/trace/trace_stack.c stack_trace_sysctl(struct ctl_table *table, int write, void *buffer, · line 550
Quoted 6 lines
kernel/trace/trace_stack.c +4
-2-if ((len =str_has_prefix (str,"_filter=" ))) -strncpy (stack_trace_filter_buf, str + len,COMMAND_LINE_SIZE ); + len =str_has_prefix (str,"_filter=" ); + +if (len) +memcpy (stack_trace_filter_buf, str + len,sizeof (stack_trace_filter_buf));Hmm, this location looks like it can just use
strscpy().
Yes strscpy() also works. But since stack_trace_filter_buf is length bounded, shouldn't memcpy be the right choice?
-- Devaansh Kumar
Mathieu Desnoyers
reviewerQuoted 25 lines · Devaansh Kumar
Quoted 15 lines · Steven Rostedt
Quoted 11 lines · On Fri, 4 Apr 2025 00:43:40 +0530 Devaansh Kumar <[email protected]>…
kernel/trace/trace_stack.c +1
-1@@ -537,14 +538,16 @@ stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,return ret; } -static char stack_trace_filter_buf[COMMAND_LINE_SIZE +1 ]__initdata ; +static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata __nonstring;static __init int enable_stacktrace (char *str) {int len;Quoted 8 lines
kernel/trace/trace_stack.c stack_trace_sysctl(struct ctl_table *table, int write, void *buffer, · line 550
Quoted 6 lines
kernel/trace/trace_stack.c +4
-2-if ((len =str_has_prefix (str,"_filter=" ))) -strncpy (stack_trace_filter_buf, str + len,COMMAND_LINE_SIZE ); + len =str_has_prefix (str,"_filter=" ); + +if (len) +memcpy (stack_trace_filter_buf, str + len,sizeof (stack_trace_filter_buf));Hmm, this location looks like it can just use
strscpy().
Quoted 2 lines
Yes
strscpy()also works. But sincestack_trace_filter_bufis length bounded, shouldn't memcpy be the right choice?
It's not only about the destination, but also about the source length.
AFAIU, turning a strncpy into a memcpy here will overflow reading the input @str if the input string is smaller than sizeof(stack_trace_filter_buf) + len.
This can trigger page faults or make KASAN unhappy.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 7 lines · Mathieu Desnoyers
It's not only about the destination, but also about the source length.
AFAIU, turning a strncpy into a memcpy here will overflow reading the input @str if the input string is smaller than
sizeof(stack_trace_filter_buf)+ len.This can trigger page faults or make KASAN unhappy.
I see. I will create a new patch to fix this then
Steven Rostedt
maintainerQuoted 7 lines · On Fri, 4 Apr 2025 08:54:33 -0400 Mathieu Desnoyers…
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
kernel/trace/trace_stack.c +4
-2-if ((len =str_has_prefix (str,"_filter=" ))) -strncpy (stack_trace_filter_buf, str + len,COMMAND_LINE_SIZE ); + len =str_has_prefix (str,"_filter=" ); + +if (len) +memcpy (stack_trace_filter_buf, str + len,sizeof (stack_trace_filter_buf));
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Hmm, this location looks like it can just use
strscpy().Yes
strscpy()also works. But sincestack_trace_filter_bufis length bounded, shouldn't memcpy be the right choice?It's not only about the destination, but also about the source length.
Correct.
Quoted 4 lines
AFAIU, turning a strncpy into a memcpy here will overflow reading the input @str if the input string is smaller than
sizeof(stack_trace_filter_buf)+ len.
The old code just read str + len and what was after it until it hit a '\0' or the COMMAND_LINE_SIZE limit.
memcpy() always reads COMMAND_LINE_SIZE (which is sizeof(stack_trace_filter_buf)) and will read more of the source "str" than may exist. Which as Mathieu pointed out, is a bug.
-- Steve