linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tracing/synthetic: Replace deprecated strncpy() with strscpy()
@ 2025-03-10 20:03 Thorsten Blum
  2025-03-11 12:37 ` Steven Rostedt
  0 siblings, 1 reply; 2+ messages in thread
From: Thorsten Blum @ 2025-03-10 20:03 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers
  Cc: Thorsten Blum, linux-hardening, linux-kernel, linux-trace-kernel

strncpy() is deprecated for NUL-terminated destination buffers; use
strscpy() instead and remove the manual NUL-termination.

The destination buffer 'buf' is used only with kstrtouint() and must be
NUL-terminated, but not NUL-padded. The source string 'start' is also
guaranteed to be NUL-terminated and meets the __must_be_cstr()
requirement of strscpy().

No functional changes intended.

Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 kernel/trace/trace_events_synth.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
index e3f7d09e5512..4b51f74897b4 100644
--- a/kernel/trace/trace_events_synth.c
+++ b/kernel/trace/trace_events_synth.c
@@ -207,8 +207,7 @@ static int synth_field_string_size(char *type)
 	if (len == 0)
 		return 0; /* variable-length string */
 
-	strncpy(buf, start, len);
-	buf[len] = '\0';
+	strscpy(buf, start, len + 1);
 
 	err = kstrtouint(buf, 0, &size);
 	if (err)
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] tracing/synthetic: Replace deprecated strncpy() with strscpy()
  2025-03-10 20:03 [PATCH] tracing/synthetic: Replace deprecated strncpy() with strscpy() Thorsten Blum
@ 2025-03-11 12:37 ` Steven Rostedt
  0 siblings, 0 replies; 2+ messages in thread
From: Steven Rostedt @ 2025-03-11 12:37 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Masami Hiramatsu, Mathieu Desnoyers, linux-hardening,
	linux-kernel, linux-trace-kernel

On Mon, 10 Mar 2025 21:03:58 +0100
Thorsten Blum <thorsten.blum@linux.dev> wrote:

> diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
> index e3f7d09e5512..4b51f74897b4 100644
> --- a/kernel/trace/trace_events_synth.c
> +++ b/kernel/trace/trace_events_synth.c
> @@ -207,8 +207,7 @@ static int synth_field_string_size(char *type)
>  	if (len == 0)
>  		return 0; /* variable-length string */
>  
> -	strncpy(buf, start, len);
> -	buf[len] = '\0';
> +	strscpy(buf, start, len + 1);
>  
>  	err = kstrtouint(buf, 0, &size);
>  	if (err)
> -

Looking at the code, it calculates the start and end by scanning the
string. I think it's better to just use memcpy() instead. It's not like
it's copying a random string passed in.

-- Steve

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-03-11 12:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-10 20:03 [PATCH] tracing/synthetic: Replace deprecated strncpy() with strscpy() Thorsten Blum
2025-03-11 12:37 ` Steven Rostedt

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).