Skip to content

Commit bfb336c

Browse files
committed
ftrace: Also allocate and copy hash for reading of filter files
Currently the reader of set_ftrace_filter and set_ftrace_notrace just adds the pointer to the global tracer hash to its iterator. Unlike the writer that allocates a copy of the hash, the reader keeps the pointer to the filter hashes. This is problematic because this pointer is static across function calls that release the locks that can update the global tracer hashes. This can cause UAF and similar bugs. Allocate and copy the hash for reading the filter files like it is done for the writers. This not only fixes UAF bugs, but also makes the code a bit simpler as it doesn't have to differentiate when to free the iterator's hash between writers and readers. Cc: [email protected] Cc: Masami Hiramatsu <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Linus Torvalds <[email protected]> Link: https://lore.kernel.org/[email protected] Fixes: c20489d ("ftrace: Assign iter->hash to filter or notrace hashes on seq read") Closes: https://lore.kernel.org/all/[email protected]/ Closes: https://lore.kernel.org/all/20250822192437.GA458494@ax162/ Reported-by: Tengda Wu <[email protected]> Tested-by: Tengda Wu <[email protected]> Tested-by: Nathan Chancellor <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 4013aef commit bfb336c

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

kernel/trace/ftrace.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4661,13 +4661,17 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
46614661
} else {
46624662
iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
46634663
}
4664+
} else {
4665+
if (hash)
4666+
iter->hash = alloc_and_copy_ftrace_hash(hash->size_bits, hash);
4667+
else
4668+
iter->hash = EMPTY_HASH;
4669+
}
46644670

4665-
if (!iter->hash) {
4666-
trace_parser_put(&iter->parser);
4667-
goto out_unlock;
4668-
}
4669-
} else
4670-
iter->hash = hash;
4671+
if (!iter->hash) {
4672+
trace_parser_put(&iter->parser);
4673+
goto out_unlock;
4674+
}
46714675

46724676
ret = 0;
46734677

@@ -6543,9 +6547,6 @@ int ftrace_regex_release(struct inode *inode, struct file *file)
65436547
ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
65446548
iter->hash, filter_hash);
65456549
mutex_unlock(&ftrace_lock);
6546-
} else {
6547-
/* For read only, the hash is the ops hash */
6548-
iter->hash = NULL;
65496550
}
65506551

65516552
mutex_unlock(&iter->ops->func_hash->regex_lock);

0 commit comments

Comments
 (0)