Skip to contentSkip to conversation

tracing: reject trace events with an empty TP_STRUCT__entry

PATCH In review Haoran Wang 3 participants 3 messages Oct 16 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: reject trace events with an empty TP_STRUCT__entry

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Haoran Wang <[email protected]>
Date: Fri, 16 Oct 2026 15:10:00 +0800
Subject: [PATCH] tracing: reject trace events with an empty TP_STRUCT__entry

trace_event_get_offsets() unconditionally dereferences the first entry of an event class's fields list through list_first_entry(). A tracepoint that declares an empty TP_STRUCT__entry never populates that list. Its fields_array holds only the terminating sentinel, so list_first_entry() returns the list head itself and the following tail->offset + tail->size reads memory past struct trace_event_class. The corrupted result is then used as the BPF context-size bound in __perf_event_set_bpf_prog(), which lets context-reading BPF programs attach to a zero-field tracepoint when they should be rejected with -EACCES.

Reject such events at registration to fix this bug. event_init() is the universal chokepoint invoked for both early-boot and module events, and its return is enforced by __register_event(), which refuses to add the call to ftrace_events. An event whose fields_array[0].type is NULL (the empty-TP_STRUCT__entry sentinel) is rejected with -EINVAL before trace_event_get_offsets() can be reached.

---
 kernel/trace/trace_events.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/trace/trace_events.c +4 -0
@@ -3279,6 +3279,10 @@ static int event_init(struct trace_event_call *call)
 	if (WARN_ON(!name))
 		return -EINVAL;
 
+	if (call->class->fields_array &&
+	    !call->class->fields_array[0].type)
+		return -EINVAL;
+
 	if (call->class->raw_init) {
 		ret = call->class->raw_init(call);
 		if (ret < 0 && ret != -ENOSYS)
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] tracing: user_events broken by fields_array[0].type check --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] tracing: user_events broken by fields_array[0].type check
--

commit e8b9d44518a8f72216cba6b41f5a36c08fdcef3c
Author: Haoran Wang <[email protected]>

tracing: reject trace events with an empty TP_STRUCT__entry

This patch adds a check in event_init() to reject trace events that have an empty TP_STRUCT__entry by returning -EINVAL if the first element of fields_array has a NULL type.

kernel/trace/trace_events.c static int event_init(struct trace_event_call *call) · line 3411

Quoted 11 lines
kernel/trace/trace_events.c +3 -0
index 30c0ddf90887f..9f79fbdf40a20 100644
@@ -3406,6 +3406,10 @@ static int event_init(struct trace_event_call *call)
 	if (WARN_ON(!name))
 		return -EINVAL;
 
+	if (call->class->fields_array &&
+	    !call->class->fields_array[0].type)
+		return -EINVAL;

[Severity: High]
Does this inadvertently break the user_events subsystem?

In kernel/trace/trace_events_user.c, user_events are assigned a statically initialized dummy array:

kernel/trace/trace_events_user.c:
static struct trace_event_fields user_event_fields_array[1];

Because this array is size 1 and zero-initialized, its first element's type field is implicitly NULL. When user_events are registered from userspace via the ABI, __register_event() calls event_init(), which will evaluate this new check to true and unconditionally return -EINVAL.

Can we find another way to handle empty TP_STRUCT__entry events that won't cause a feature regression for user_events?

Quoted 4 lines
kernel/trace/trace_events.c +1 -0
+
 	if (call->class->raw_init) {
 		ret = call->class->raw_init(call);
 		if (ret < 0 && ret != -ENOSYS)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Haoran Wang
Quoted 15 lines · On Fri, 16 Oct 2026 14:57:22 +0800 Haoran Wang <[email protected]> wrote:

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Haoran Wang <[email protected]>
Date: Fri, 16 Oct 2026 15:10:00 +0800
Subject: [PATCH] tracing: reject trace events with an empty TP_STRUCT__entry

trace_event_get_offsets() unconditionally dereferences the first entry of an event class's fields list through list_first_entry(). A tracepoint that declares an empty TP_STRUCT__entry never populates that list. Its fields_array holds only the terminating sentinel, so list_first_entry() returns the list head itself and the following tail->offset + tail->size reads memory past struct trace_event_class. The corrupted result is then used as the BPF context-size bound in __perf_event_set_bpf_prog(), which lets context-reading BPF programs attach to a zero-field tracepoint when they should be rejected with -EACCES.

Quoted 7 lines

Reject such events at registration to fix this bug. event_init() is the universal chokepoint invoked for both early-boot and module events, and its return is enforced by __register_event(), which refuses to add the call to ftrace_events. An event whose fields_array[0].type is NULL (the empty-TP_STRUCT__entry sentinel) is rejected with -EINVAL before trace_event_get_offsets() can be reached.

BTW, what bug are you trying to solve? Just some theoretical bug you found from a static analyzer or AI?

-- Steve