tracing: Fix UBSAN shift-out-of-bounds in test_event_printk()
Subsystems (3)
PATCH 1/1 tracing: Fix UBSAN shift-out-of-bounds in test_event_printk()
Commit b5cc230af5e5 ("tracing: Warn when an event dereferences a pointer in TP_printk()") removed the early return when dereference_flags is zero so that test_double_dereference() can check all event arguments.
As a result, trace events with more than 64 arguments in TP_printk() now always reach the argument-parsing loop and evaluate (1ULL << arg) when arg >= 64, triggering a UBSAN shift-out-of-bounds trap.
Additionally, in the format string parsing loop, the bounds check only tested 'arg == 63'. If the first 64 specifiers are non-pointer and non-string formats (e.g., %d) and a subsequent specifier (arg >= 64) is %s or %p*, 'arg == 63' evaluates to false and '1ULL << arg' also shifts out of bounds.
Fix both issues by changing the upper bound check to 'arg >= 63' and guarding the '(1ULL << arg)' checks in the argument loop with 'arg < 64'.
--- kernel/trace/trace_events.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/trace/trace_events.c +4 -4
@@ -526,7 +526,7 @@ static void test_event_printk(struct trace_event_call *call)case 'a' :case 'd' :case 'D' :case 'g' :case 't' :case 'C' :case 'O' :case 'f' : -if (WARN_ONCE (arg ==63 , +if (WARN_ONCE (arg >=63 ,"Too many args for event: %s" ,trace_event_name (call)))return ;
@@ -556,7 +556,7 @@ static void test_event_printk(struct trace_event_call *call)if ((fmt[i + j] =='s' )) {if (star) arg++; -if (WARN_ONCE (arg ==63 , +if (WARN_ONCE (arg >=63 ,"Too many args for event: %s" ,trace_event_name (call)))return ;
@@ -610,7 +610,7 @@ static void test_event_printk(struct trace_event_call *call)test_double_dereference (fmt + start_arg, e - start_arg, call); -if (dereference_flags & (1ULL << arg)) { +if (arg <64 && (dereference_flags & (1ULL << arg))) {handle_dereference_arg (fmt + start_arg, string_flags, e - start_arg, &dereference_flags, arg, call);
@@ -625,7 +625,7 @@ static void test_event_printk(struct trace_event_call *call)test_double_dereference (fmt + start_arg, i - start_arg, call); -if (dereference_flags & (1ULL << arg)) { +if (arg <64 && (dereference_flags & (1ULL << arg))) {handle_dereference_arg (fmt + start_arg, string_flags, i - start_arg, &dereference_flags, arg, call);
Signature · 1 line
2.45.2
Steven Rostedt
maintainerQuoted 14 lines · On Wed, 25 Nov 2026 16:03:56 +0800 Kuyo Chang <[email protected]> wrote:
Commit
b5cc230af5e5("tracing: Warn when an event dereferences a pointer inTP_printk()") removed the early return whendereference_flagsis zero so thattest_double_dereference()can check all event arguments.As a result, trace events with more than 64 arguments in
TP_printk()now always reach the argument-parsing loop and evaluate (1ULL << arg) when arg >= 64, triggering a UBSAN shift-out-of-bounds trap.Additionally, in the format string parsing loop, the bounds check only tested 'arg == 63'. If the first 64 specifiers are non-pointer and non-string formats (e.g., %d) and a subsequent specifier (arg >= 64) is %s or %p*, 'arg == 63' evaluates to false and '1ULL << arg' also shifts out of bounds.
Quoted 6 lines
Fix both issues by changing the upper bound check to 'arg >= 63' and guarding the '(1ULL << arg)' checks in the argument loop with 'arg < 64'.
Fixes b5cc230af5e5 ("tracing: Warn when an event dereferences a pointer in TP_printk()")
I'll add this as a cleanup but I'm nuking the above tags. There is no event with anywhere close to 64 args. These checks were simply a failsafe in case they ever happened (which they likely will never do).
-- Steve
Steven Rostedt
maintainerQuoted 26 lines · On Wed, 25 Nov 2026 16:03:56 +0800 Kuyo Chang <[email protected]> wrote:
kernel/trace/trace_events.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)kernel/trace/trace_events.c +2
-2@@ -526,7 +526,7 @@ static void test_event_printk(struct trace_event_call *call)case 'a' :case 'd' :case 'D' :case 'g' :case 't' :case 'C' :case 'O' :case 'f' : -if (WARN_ONCE (arg ==63 , +if (WARN_ONCE (arg >=63 ,"Too many args for event: %s" ,trace_event_name (call)))return ;@@ -556,7 +556,7 @@ static void test_event_printk(struct trace_event_call *call)if ((fmt[i + j] =='s' )) {if (star) arg++; -if (WARN_ONCE (arg ==63 , +if (WARN_ONCE (arg >=63 ,"Too many args for event: %s" ,trace_event_name (call)))return ;
kernel/trace/trace_events.c static void test_event_printk(struct trace_event_call *call) · line 613
Quoted 5 lines
kernel/trace/trace_events.c +1
-1@@ -610,7 +610,7 @@ static void test_event_printk(struct trace_event_call *call)test_double_dereference (fmt + start_arg, e - start_arg, call); -if (dereference_flags & (1ULL << arg)) { +if (arg <64 && (dereference_flags & (1ULL << arg))) {
The above should never happen. If anything, arg >= 64 should exit this function immediately.
-- Steve
Quoted 12 lines
kernel/trace/trace_events.c +1
-1handle_dereference_arg (fmt + start_arg, string_flags, e - start_arg, &dereference_flags, arg, call);@@ -625,7 +625,7 @@ static void test_event_printk(struct trace_event_call *call)test_double_dereference (fmt + start_arg, i - start_arg, call); -if (dereference_flags & (1ULL << arg)) { +if (arg <64 && (dereference_flags & (1ULL << arg))) {handle_dereference_arg (fmt + start_arg, string_flags, i - start_arg, &dereference_flags, arg, call);