All of lore.kernel.org
 help / color / mirror / Atom feed
From: EisenWalds <aerovynqueque@gmail.com>
To: Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>
Cc: linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org,
	EisenWalds <aerovynqueque@gmail.com>
Subject: [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check
Date: Fri,  4 Dec 2026 23:36:30 +0700	[thread overview]
Message-ID: <20261204163630.17660-2-aerovynqueque@gmail.com> (raw)
In-Reply-To: <20261204163630.17660-1-aerovynqueque@gmail.com>

sanitize_field_type() removes __attribute__((...)) from a type
string. It has two issues:

1. strcpy(attr, next) is called with overlapping source and
   destination in the same buffer. Overlapping strcpy() is undefined
   behavior per the C standard; use memmove() instead.

2. The word boundary check used isspace(attr[-1]), which skipped
   attributes that follow punctuation such as "*__attribute__((...))"
   or "(__attribute__((...))". Check for identifier characters
   (alnum or '_') instead, so those forms are sanitized correctly.

While here, compare against the buffer start (ret) instead of the
moving search position (next), so the previous-character check is
skipped only when attr is at the very beginning of the buffer.
Also cast to unsigned char before isalnum() to avoid undefined
behavior on signed char.

Signed-off-by: EisenWalds <aerovynqueque@gmail.com>
---
 kernel/trace/trace_events.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index b5cbdd31c..d7637eb08 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -3605,8 +3605,19 @@ static char *sanitize_field_type(const char *type)
 
 	next = (char *)type;
 	while ((attr = strstr(next, ATTRIBUTE_STR))) {
-		/* Retry if "__attribute__(" is a part of another word. */
-		if (attr != next && !isspace(attr[-1])) {
+		/*
+		 * Reject if "__attribute__(" is part of another identifier,
+		 * e.g. "my__attribute__(" or "_foo__attribute__(".
+		 * Checking the previous character is enough: alnum or '_'
+		 * means it is still part of an identifier. Other characters
+		 * (space, '*', '(', etc.) are valid because C allows an
+		 * attribute to follow punctuation, e.g. "*__attribute__((...))".
+		 *
+		 * Cast to unsigned char to avoid UB when char is signed and
+		 * the byte is non-ASCII.
+		 */
+		if (attr != ret &&
+		    (isalnum((unsigned char)attr[-1]) || attr[-1] == '_')) {
 			next = attr + ATTRIBUTE_STR_LEN;
 			continue;
 		}
@@ -3618,12 +3629,12 @@ static char *sanitize_field_type(const char *type)
 			attr = ret + (attr - type);
 		}
 
-		/* the ATTRIBUTE_STR already has the first '(' */
+		/* ATTRIBUTE_STR already contains the first '('. */
 		depth = 1;
 		next = attr + ATTRIBUTE_STR_LEN;
 		do {
 			tmp = strpbrk(next, "()");
-			/* There is unbalanced parentheses */
+			/* Unbalanced parentheses. */
 			if (WARN_ON_ONCE(!tmp)) {
 				kfree(ret);
 				return (char *)type;
@@ -3636,7 +3647,13 @@ static char *sanitize_field_type(const char *type)
 			next = tmp + 1;
 		} while (depth > 0);
 		next = skip_spaces(next);
-		strcpy(attr, next);
+		/*
+		 * Use memmove(), not strcpy():
+		 * source (next) and destination (attr) are in the same buffer
+		 * and overlap. strcpy() with overlapping objects is undefined
+		 * behavior according to the C standard.
+		 */
+		memmove(attr, next, strlen(next) + 1);
 		next = attr;
 	}
 	return ret;
-- 
2.56.1


  reply	other threads:[~2026-12-04 16:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:36 [PATCH 1/2] tracing: Clean up find_event_field() assignment-in-if and type truncation EisenWalds
2026-12-04 16:36 ` EisenWalds [this message]
2026-12-04 16:55   ` [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check sashiko-bot
2026-12-04 18:21   ` Steven Rostedt
2026-12-04 18:15 ` [PATCH 1/2] tracing: Clean up find_event_field() assignment-in-if and type truncation 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=20261204163630.17660-2-aerovynqueque@gmail.com \
    --to=aerovynqueque@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.