Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH 1/2] tracing: Clean up find_event_field() assignment-in-if and type truncation
@ 2026-12-04 16:36 EisenWalds
  2026-12-04 16:36 ` [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check EisenWalds
  2026-12-04 18:15 ` [PATCH 1/2] tracing: Clean up find_event_field() assignment-in-if and type truncation Steven Rostedt
  0 siblings, 2 replies; 5+ messages in thread
From: EisenWalds @ 2026-12-04 16:36 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu
  Cc: linux-trace-kernel, linux-kernel, EisenWalds

find_event_field() assigns str_has_prefix() to an int and uses the
assignment as an if condition. The int type truncates the size_t
returned by str_has_prefix(), and checkpatch warns about the
assignment-in-if pattern.

Separate the assignment from the condition and use size_t for len.
Also drop the redundant initializer of p, which is immediately
overwritten by the loop.

No functional change.

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

diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index 394d7cde1..b5cbdd31c 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -263,12 +263,14 @@ static struct trace_event_fields *find_event_field(const char *fmt,
 						   struct trace_event_call *call)
 {
 	struct trace_event_fields *field = call->class->fields_array;
-	const char *p = fmt;
-	int len;
+	const char *p;
+	size_t len;
 
-	if (!(len = str_has_prefix(fmt, "REC->")))
+	len = str_has_prefix(fmt, "REC->");
+	if (!len)
 		return NULL;
 	fmt += len;
+
 	for (p = fmt; *p; p++) {
 		if (!isalnum(*p) && *p != '_')
 			break;
-- 
2.56.1


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

* [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check
  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
  2026-12-04 16:55   ` 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
  1 sibling, 2 replies; 5+ messages in thread
From: EisenWalds @ 2026-12-04 16:36 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu
  Cc: linux-trace-kernel, linux-kernel, EisenWalds

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


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

* Re: [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check
  2026-12-04 16:36 ` [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check EisenWalds
@ 2026-12-04 16:55   ` sashiko-bot
  2026-12-04 18:21   ` Steven Rostedt
  1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 16:55 UTC (permalink / raw)
  To: EisenWalds; +Cc: linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] tracing: missing Fixes trailer for sanitize_field_type() bug
  fixes
- [Low] tracing: redundant unsigned char cast and inaccurate UB comment
  for isalnum()
--

commit fc0f6e4347c9a99f474f4909ae248ae5f2a89b9a
Author: EisenWalds <aerovynqueque@gmail.com>

> tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check
>
> 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>

[Severity: Low]
This fixes two bugs in sanitize_field_type() introduced by commit
1a967e92bf47 ("tracing: Remove "__attribute__()" from the type field of
event format").  Should this include:

  Fixes: 1a967e92bf47 ("tracing: Remove "__attribute__()" from the type field of event format")

> diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
> index 0e396c67a704b..ec0194d164d57 100644
> --- a/kernel/trace/trace_events.c
> +++ b/kernel/trace/trace_events.c
> @@ -3703,8 +3703,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;
>  		}

[Severity: Low]
This isn't a bug, but the (unsigned char) cast and the undefined behavior
note in this comment and the commit message are unnecessary in the kernel.
include/linux/ctype.h already casts the argument of isalnum() to unsigned
char in __ismask(), and the top-level Makefile builds with -funsigned-char.

Nearby callers such as update_event_printk() pass char directly to
isalnum() without a cast.  Can the cast and the undefined behavior note be
dropped from the comment and commit message?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204163630.17660-1-aerovynqueque@gmail.com?part=2

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

* Re: [PATCH 1/2] tracing: Clean up find_event_field() assignment-in-if and type truncation
  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 ` [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check EisenWalds
@ 2026-12-04 18:15 ` Steven Rostedt
  1 sibling, 0 replies; 5+ messages in thread
From: Steven Rostedt @ 2026-12-04 18:15 UTC (permalink / raw)
  To: EisenWalds; +Cc: Masami Hiramatsu, linux-trace-kernel, linux-kernel

On Fri,  4 Dec 2026 23:36:29 +0700
EisenWalds <aerovynqueque@gmail.com> wrote:

> find_event_field() assigns str_has_prefix() to an int and uses the
> assignment as an if condition. The int type truncates the size_t
> returned by str_has_prefix(), and checkpatch warns about the
> assignment-in-if pattern.

Checkpatch should never be used on code in the kernel.

> 
> Separate the assignment from the condition and use size_t for len.
> Also drop the redundant initializer of p, which is immediately
> overwritten by the loop.
> 
> No functional change.
> 
> Signed-off-by: EisenWalds <aerovynqueque@gmail.com>
> ---
>  kernel/trace/trace_events.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
> index 394d7cde1..b5cbdd31c 100644
> --- a/kernel/trace/trace_events.c
> +++ b/kernel/trace/trace_events.c
> @@ -263,12 +263,14 @@ static struct trace_event_fields *find_event_field(const char *fmt,
>  						   struct trace_event_call *call)
>  {
>  	struct trace_event_fields *field = call->class->fields_array;
> -	const char *p = fmt;
> -	int len;
> +	const char *p;
> +	size_t len;
>  
> -	if (!(len = str_has_prefix(fmt, "REC->")))

NACK!!!

This is exactly the use case I created str_has_prefix() for. It returns 0
or strlen("REC->") so there is no chance of size_t being truncated when
compared to int.

-- Steve



> +	len = str_has_prefix(fmt, "REC->");
> +	if (!len)
>  		return NULL;
>  	fmt += len;
> +
>  	for (p = fmt; *p; p++) {
>  		if (!isalnum(*p) && *p != '_')
>  			break;


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

* Re: [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check
  2026-12-04 16:36 ` [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check EisenWalds
  2026-12-04 16:55   ` sashiko-bot
@ 2026-12-04 18:21   ` Steven Rostedt
  1 sibling, 0 replies; 5+ messages in thread
From: Steven Rostedt @ 2026-12-04 18:21 UTC (permalink / raw)
  To: EisenWalds; +Cc: Masami Hiramatsu, linux-trace-kernel, linux-kernel

On Fri,  4 Dec 2026 23:36:30 +0700
EisenWalds <aerovynqueque@gmail.com> wrote:

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

Ug, more AI slop :-p

Is there an event with a field that requires this change?

> 
> 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__((...))".
> +		 *

Do you even understand what the above says?

> +		 * 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 '('. */

Useless change.

>  		depth = 1;
>  		next = attr + ATTRIBUTE_STR_LEN;
>  		do {
>  			tmp = strpbrk(next, "()");
> -			/* There is unbalanced parentheses */
> +			/* Unbalanced parentheses. */

Another useless change.

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

This is probably the only part that I may take, but unless you do this
yourself with NO HELP FROM AI, I'm not taking it from you.

-- Steve


>  		next = attr;
>  	}
>  	return ret;


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

end of thread, other threads:[~2026-12-04 18:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/2] tracing: Fix sanitize_field_type() overlapping strcpy() and word boundary check EisenWalds
2026-12-04 16:55   ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox