The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@kernel.org>
To: linux-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Martin Kaiser <martin@kaiser.cx>, Vinod Koul <vkoul@kernel.org>
Subject: [for-next][PATCH 14/16] tracing: Warn when an event dereferences a pointer in TP_printk()
Date: Tue, 28 Jul 2026 20:05:37 -0400	[thread overview]
Message-ID: <20260729000557.354446823@kernel.org> (raw)
In-Reply-To: 20260729000523.093060274@kernel.org

From: Steven Rostedt <rostedt@goodmis.org>

Currently on boot up and when modules are loaded, the trace event
infrastructure will examine the TP_printk's of every event looking to see
if it dereferences pointers on the ring buffer via printk formats like
"%pB" and such. What it doesn't do is check if the arguments themselves
do a dereference from a pointer.

This was brought with a fix[1] to the fsl_edma event that had in the
arguments of the TP_printk(): "__entry->edma->membase"

The __entry->edma is a pointer saved in the ring buffer. The dereference
from TP_printk() happens when the user reads the "trace" file which can be
seconds, minutes, hours, days, weeks, or even months later! There is no
guarantee that the __entry->edma pointer will still be pointing to what it
was when it was recorded, and could crash the kernel when a user reads the
event.

Add logic to the test_event_printk() that also checks for this case and
warn if the event dereferences a pointer from the ring buffer.

[1] https://lore.kernel.org/all/20260630200022.1826420-1-martin@kaiser.cx/

Link: https://patch.msgid.link/20260630184836.74d477b6@gandalf.local.home
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Reviewed-by: Martin Kaiser <martin@kaiser.cx>
Reviewed-by: Vinod Koul <vkoul@kernel.org>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/trace_events.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)

diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index e0449e648871..714285e23a2e 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -401,6 +401,31 @@ static bool process_string(const char *fmt, int len, struct trace_event_call *ca
 	return true;
 }
 
+static void test_double_dereference(const char *str, int len,
+				    struct trace_event_call *call)
+{
+	const char *ptr;
+	const char *end = str + len;
+
+	ptr = strstr(str, "REC->");
+
+	while (ptr && ptr < end) {
+
+		ptr += 5;
+		for (; ptr < end; ptr++) {
+			if (ptr[0] == '-' && ptr[1] == '>') {
+				WARN_ONCE(1, "Event %s has double dereference in TP_printk: %.*s\n",
+					  trace_event_name(call), len, str);
+				return;
+			}
+			if (!isalnum(*ptr) && *ptr != '_')
+				break;
+		}
+
+		ptr = strstr(ptr, "REC->");
+	}
+}
+
 static void handle_dereference_arg(const char *arg_str, u64 string_flags, int len,
 				   u64 *dereference_flags, int arg,
 				   struct trace_event_call *call)
@@ -460,12 +485,6 @@ static void test_event_printk(struct trace_event_call *call)
 				if (in_quote) {
 					arg = 0;
 					first = false;
-					/*
-					 * If there was no %p* uses
-					 * the fmt is OK.
-					 */
-					if (!dereference_flags)
-						return;
 				}
 			}
 			if (in_quote) {
@@ -577,6 +596,8 @@ static void test_event_printk(struct trace_event_call *call)
 				continue;
 			}
 
+			test_double_dereference(fmt + start_arg, e - start_arg, call);
+
 			if (dereference_flags & (1ULL << arg)) {
 				handle_dereference_arg(fmt + start_arg, string_flags,
 						       e - start_arg,
@@ -590,6 +611,8 @@ 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)) {
 		handle_dereference_arg(fmt + start_arg, string_flags,
 				       i - start_arg,
-- 
2.53.0



  parent reply	other threads:[~2026-07-29  0:05 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-29  0:05 [for-next][PATCH 00/16] tracing: Updates for v7.3 Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 01/16] bpf: Make btf_get_module_btf() and btf_relocate_id() non-static Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 02/16] tracing: Expose tracepoint BTF ids via tracefs Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 03/16] selftests/bpf: Add test for tracepoint btf_ids tracefs file Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 04/16] tracing: Point constant hist field type to string literal Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 05/16] kernel/trace/trace_printk: Use kstrdup() instead of kmalloc() and strcpy() Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 06/16] tracing: Use __free() for expr_str() buffer Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 07/16] tracing: Return ERR_PTR() from expr_str() Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 08/16] tracing: Bound histogram expression strings with seq_buf Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 09/16] tracing/user_events: Use seq_putc() in two functions Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 10/16] tracing/user_events: Replace a seq_printf() call by seq_puts() in user_seq_show() Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 11/16] fgraph: Use trace_seq_putc() in print_graph_return() Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 12/16] tracing: Reject invalid preemptirq_delay_test CPU affinity Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 13/16] samples/ftrace: Prevent division by zero when nr_function_calls is zero Steven Rostedt
2026-07-29  0:05 ` Steven Rostedt [this message]
2026-07-29  0:05 ` [for-next][PATCH 15/16] tracing: Use strscpy() instead of strcpy() in trace_sched_switch Steven Rostedt
2026-07-29  0:05 ` [for-next][PATCH 16/16] tracing: Use seq_buf for string concatenation 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=20260729000557.354446823@kernel.org \
    --to=rostedt@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=martin@kaiser.cx \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=vkoul@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox