* [PATCH v3] ftrace: Add global mutex to serialize trace_parser access
@ 2026-07-23 13:34 Tengda Wu
2026-07-24 22:47 ` Steven Rostedt
0 siblings, 1 reply; 3+ messages in thread
From: Tengda Wu @ 2026-07-23 13:34 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mark Rutland, Mathieu Desnoyers, linux-trace-kernel,
linux-kernel, Tengda Wu
In ftrace, the trace_parser structure is allocated and initialized when
a trace file is opened, and is subsequently used across write and release
handlers to parse user input.
The affected handler paths and their specific functions are:
- Open paths: ftrace_regex_open(), ftrace_graph_open()
- Write paths: ftrace_regex_write(), ftrace_graph_write()
- Release paths: ftrace_regex_release(), ftrace_graph_release()
If userspace opens a trace file descriptor and shares it across multiple
threads, concurrent write calls will race on the parser's internal state,
specifically the 'idx', 'cont', and 'buffer' fields, leading to corrupted
input or undefined behavior.
Fix this by adding a global mutex, parser_lock, to serialize all access
to trace_parser across write and release paths, preventing concurrent
corruption of parser state.
Fixes: e704eff3ff51 ("ftrace: Have set_graph_function handle multiple functions in one write")
Fixes: 689fd8b65d66 ("tracing: trace parser support for function and graph")
Cc: stable@vger.kernel.org
Signed-off-by: Tengda Wu <wutengda@huaweicloud.com>
---
v3: Switch from per-parser lock to global parser_lock (v1 approach).
v2: https://lore.kernel.org/all/20260715081937.1469757-1-wutengda@huaweicloud.com/
v1: https://lore.kernel.org/all/20260713134640.708323-1-wutengda@huaweicloud.com/
kernel/trace/ftrace.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index f93e34dd2328..ba27f31c6ff4 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -1097,6 +1097,12 @@ struct ftrace_ops global_ops = {
FTRACE_OPS_FL_PID,
};
+/*
+ * parser_lock - Protects trace_parser state against concurrent operations.
+ * Held across trace_get_user() and subsequent buffer parsing to prevent races.
+ */
+DEFINE_MUTEX(parser_lock);
+
/*
* Used by the stack unwinder to know about dynamic ftrace trampolines.
*/
@@ -5842,6 +5848,8 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
/* iter->hash is a local copy, so we don't need regex_lock */
parser = &iter->parser;
+
+ guard(mutex)(&parser_lock);
read = trace_get_user(parser, ubuf, cnt, ppos);
if (read >= 0 && trace_parser_loaded(parser) &&
@@ -6984,12 +6992,14 @@ int ftrace_regex_release(struct inode *inode, struct file *file)
iter = file->private_data;
parser = &iter->parser;
+ mutex_lock(&parser_lock);
if (trace_parser_loaded(parser)) {
int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
ftrace_process_regex(iter, parser->buffer,
parser->idx, enable);
}
+ mutex_unlock(&parser_lock);
trace_parser_put(parser);
@@ -7321,10 +7331,12 @@ ftrace_graph_release(struct inode *inode, struct file *file)
parser = &fgd->parser;
+ mutex_lock(&parser_lock);
if (trace_parser_loaded((parser))) {
ret = ftrace_graph_set_hash(fgd->new_hash,
parser->buffer);
}
+ mutex_unlock(&parser_lock);
trace_parser_put(parser);
@@ -7437,6 +7449,7 @@ ftrace_graph_write(struct file *file, const char __user *ubuf,
parser = &fgd->parser;
+ guard(mutex)(&parser_lock);
read = trace_get_user(parser, ubuf, cnt, ppos);
if (read >= 0 && trace_parser_loaded(parser) &&
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v3] ftrace: Add global mutex to serialize trace_parser access
2026-07-23 13:34 [PATCH v3] ftrace: Add global mutex to serialize trace_parser access Tengda Wu
@ 2026-07-24 22:47 ` Steven Rostedt
2026-07-25 0:50 ` Tengda Wu
0 siblings, 1 reply; 3+ messages in thread
From: Steven Rostedt @ 2026-07-24 22:47 UTC (permalink / raw)
To: Tengda Wu
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers,
linux-trace-kernel, linux-kernel
On Thu, 23 Jul 2026 13:34:04 +0000
Tengda Wu <wutengda@huaweicloud.com> wrote:
> --- a/kernel/trace/ftrace.c
> +++ b/kernel/trace/ftrace.c
> @@ -1097,6 +1097,12 @@ struct ftrace_ops global_ops = {
> FTRACE_OPS_FL_PID,
> };
>
> +/*
> + * parser_lock - Protects trace_parser state against concurrent operations.
> + * Held across trace_get_user() and subsequent buffer parsing to prevent races.
> + */
> +DEFINE_MUTEX(parser_lock);
> +
Sashiko pointed out[1] that this should be static. And it should. Could you
send a v4?
static DEFINE_MUTEX(parser_lock);
Note, you can ignore the other Sashiko comments.
-- Steve
[1] https://sashiko.dev/#/patchset/20260723133404.1926732-1-wutengda%40huaweicloud.com
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v3] ftrace: Add global mutex to serialize trace_parser access
2026-07-24 22:47 ` Steven Rostedt
@ 2026-07-25 0:50 ` Tengda Wu
0 siblings, 0 replies; 3+ messages in thread
From: Tengda Wu @ 2026-07-25 0:50 UTC (permalink / raw)
To: Steven Rostedt
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers,
linux-trace-kernel, linux-kernel
On 2026/7/25 6:47, Steven Rostedt wrote:
> On Thu, 23 Jul 2026 13:34:04 +0000
> Tengda Wu <wutengda@huaweicloud.com> wrote:
>
>> --- a/kernel/trace/ftrace.c
>> +++ b/kernel/trace/ftrace.c
>> @@ -1097,6 +1097,12 @@ struct ftrace_ops global_ops = {
>> FTRACE_OPS_FL_PID,
>> };
>>
>> +/*
>> + * parser_lock - Protects trace_parser state against concurrent operations.
>> + * Held across trace_get_user() and subsequent buffer parsing to prevent races.
>> + */
>> +DEFINE_MUTEX(parser_lock);
>> +
>
> Sashiko pointed out[1] that this should be static. And it should. Could you
> send a v4?
>
> static DEFINE_MUTEX(parser_lock);
>
> Note, you can ignore the other Sashiko comments.
>
> -- Steve
>
> [1] https://sashiko.dev/#/patchset/20260723133404.1926732-1-wutengda%40huaweicloud.com
Sorry, I overlooked that. Will add static and send v4 right away.
Thanks,
Tengda
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-07-25 0:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-23 13:34 [PATCH v3] ftrace: Add global mutex to serialize trace_parser access Tengda Wu
2026-07-24 22:47 ` Steven Rostedt
2026-07-25 0:50 ` Tengda Wu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®