public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-hardening@vger.kernel.org, Kees Cook <kees@kernel.org>,
	Justin Stitt <justinstitt@google.com>
Subject: [for-next][PATCH 02/11] tracing: Replace multiple deprecated strncpy with memcpy
Date: Fri, 01 Nov 2024 06:36:49 -0400	[thread overview]
Message-ID: <20241101103707.130624298@goodmis.org> (raw)
In-Reply-To: 20241101103647.011707614@goodmis.org

From: Justin Stitt <justinstitt@google.com>

strncpy() is deprecated for use on NUL-terminated destination strings [1] and
as such we should prefer more robust and less ambiguous string interfaces.

String copy operations involving manual pointer offset and length
calculations followed by explicit NUL-byte assignments are best changed
to either strscpy or memcpy.

strscpy is not a drop-in replacement as @len would need a one subtracted
from it to avoid truncating the source string.

To not sabotage readability of the current code, use memcpy (retaining
the manual NUL assignment) as this unambiguously describes the desired
behavior.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://github.com/KSPP/linux/issues/90 [2]

Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: linux-hardening@vger.kernel.org
Link: https://lore.kernel.org/20241014-strncpy-kernel-trace-trace_events_filter-c-v2-1-d821e81e371e@google.com
Reviewed-by: Kees Cook <kees@kernel.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/trace/trace_events_filter.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
index 0c611b281a5b..78051de581e7 100644
--- a/kernel/trace/trace_events_filter.c
+++ b/kernel/trace/trace_events_filter.c
@@ -1616,7 +1616,7 @@ static int parse_pred(const char *str, void *data,
 				goto err_free;
 			}
 
-			strncpy(num_buf, str + s, len);
+			memcpy(num_buf, str + s, len);
 			num_buf[len] = 0;
 
 			ret = kstrtoul(num_buf, 0, &ip);
@@ -1694,7 +1694,7 @@ static int parse_pred(const char *str, void *data,
 		if (!pred->regex)
 			goto err_mem;
 		pred->regex->len = len;
-		strncpy(pred->regex->pattern, str + s, len);
+		memcpy(pred->regex->pattern, str + s, len);
 		pred->regex->pattern[len] = 0;
 
 	} else if (!strncmp(str + i, "CPUS", 4)) {
@@ -1859,7 +1859,7 @@ static int parse_pred(const char *str, void *data,
 		if (!pred->regex)
 			goto err_mem;
 		pred->regex->len = len;
-		strncpy(pred->regex->pattern, str + s, len);
+		memcpy(pred->regex->pattern, str + s, len);
 		pred->regex->pattern[len] = 0;
 
 		filter_build_regex(pred);
@@ -1919,7 +1919,7 @@ static int parse_pred(const char *str, void *data,
 			goto err_free;
 		}
 
-		strncpy(num_buf, str + s, len);
+		memcpy(num_buf, str + s, len);
 		num_buf[len] = 0;
 
 		/* Make sure it is a value */
-- 
2.45.2



  parent reply	other threads:[~2024-11-01 10:36 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-01 10:36 [for-next][PATCH 00/11] tracing: Updates for 6.13 Steven Rostedt
2024-11-01 10:36 ` [for-next][PATCH 01/11] tracing: Make percpu stack trace buffer invariant to PAGE_SIZE Steven Rostedt
2024-11-01 10:36 ` Steven Rostedt [this message]
2024-11-01 10:36 ` [for-next][PATCH 03/11] kdb: Replace the use of simple_strto with safer kstrto in kdb_main Steven Rostedt
2024-11-01 14:21   ` Doug Anderson
2024-11-01 14:31     ` Steven Rostedt
2024-11-01 18:22       ` Daniel Thompson
2024-11-01 18:36         ` Steven Rostedt
2024-11-01 18:42         ` Nir Lichtman
2024-11-01 18:54           ` Steven Rostedt
2024-11-01 19:00             ` Nir Lichtman
2024-11-01 10:36 ` [for-next][PATCH 04/11] trace: kdb: Replace simple_strtoul with kstrtoul in kdb_ftdump Steven Rostedt
2024-11-01 10:36 ` [for-next][PATCH 05/11] kdb: Remove fallback interpretation of arbitrary numbers as hex Steven Rostedt
2024-11-01 10:36 ` [for-next][PATCH 06/11] tracing: Remove TRACE_FLAG_IRQS_NOSUPPORT Steven Rostedt
2024-11-01 10:36 ` [for-next][PATCH 07/11] tracing: Introduce tracepoint extended structure Steven Rostedt
2024-11-01 10:36 ` [for-next][PATCH 08/11] tracing: Introduce tracepoint_is_faultable() Steven Rostedt
2024-11-01 10:36 ` [for-next][PATCH 09/11] tracing: Fix syscall tracepoint use-after-free Steven Rostedt
2024-11-01 10:36 ` [for-next][PATCH 10/11] tracing: Add might_fault() check in __DECLARE_TRACE_SYSCALL Steven Rostedt
2024-11-01 10:36 ` [for-next][PATCH 11/11] tracing: Replace strncpy() with strscpy() when copying comm Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241101103707.130624298@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=justinstitt@google.com \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox