[PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check
From: EisenWalds
Date: Fri Dec 04 2026 - 11:36:50 EST
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@xxxxxxxxx>
---
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