Skip to content

Commit 0db0934

Browse files
mhiramatrostedt
authored andcommitted
tracing: fgraph: Protect return handler from recursion loop
function_graph_enter_regs() prevents itself from recursion by ftrace_test_recursion_trylock(), but __ftrace_return_to_handler(), which is called at the exit, does not prevent such recursion. Therefore, while it can prevent recursive calls from fgraph_ops::entryfunc(), it is not able to prevent recursive calls to fgraph from fgraph_ops::retfunc(), resulting in a recursive loop. This can lead an unexpected recursion bug reported by Menglong. is_endbr() is called in __ftrace_return_to_handler -> fprobe_return -> kprobe_multi_link_exit_handler -> is_endbr. To fix this issue, acquire ftrace_test_recursion_trylock() in the __ftrace_return_to_handler() after unwind the shadow stack to mark this section must prevent recursive call of fgraph inside user-defined fgraph_ops::retfunc(). This is essentially a fix to commit 4346ba1 ("fprobe: Rewrite fprobe on function-graph tracer"), because before that fgraph was only used from the function graph tracer. Fprobe allowed user to run any callbacks from fgraph after that commit. Reported-by: Menglong Dong <menglong8.dong@gmail.com> Closes: https://lore.kernel.org/all/20250918120939.1706585-1-dongml2@chinatelecom.cn/ Fixes: 4346ba1 ("fprobe: Rewrite fprobe on function-graph tracer") Cc: stable@vger.kernel.org Cc: Peter Zijlstra <peterz@infradead.org> Link: https://lore.kernel.org/175852292275.307379.9040117316112640553.stgit@devnote2 Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Acked-by: Jiri Olsa <jolsa@kernel.org> Tested-by: Menglong Dong <menglong8.dong@gmail.com> Acked-by: Menglong Dong <menglong8.dong@gmail.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 1da3f14 commit 0db0934

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

kernel/trace/fgraph.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ __ftrace_return_to_handler(struct ftrace_regs *fregs, unsigned long frame_pointe
815815
unsigned long bitmap;
816816
unsigned long ret;
817817
int offset;
818+
int bit;
818819
int i;
819820

820821
ret_stack = ftrace_pop_return_trace(&trace, &ret, frame_pointer, &offset);
@@ -829,6 +830,15 @@ __ftrace_return_to_handler(struct ftrace_regs *fregs, unsigned long frame_pointe
829830
if (fregs)
830831
ftrace_regs_set_instruction_pointer(fregs, ret);
831832

833+
bit = ftrace_test_recursion_trylock(trace.func, ret);
834+
/*
835+
* This can fail because ftrace_test_recursion_trylock() allows one nest
836+
* call. If we are already in a nested call, then we don't probe this and
837+
* just return the original return address.
838+
*/
839+
if (unlikely(bit < 0))
840+
goto out;
841+
832842
#ifdef CONFIG_FUNCTION_GRAPH_RETVAL
833843
trace.retval = ftrace_regs_get_return_value(fregs);
834844
#endif
@@ -852,6 +862,8 @@ __ftrace_return_to_handler(struct ftrace_regs *fregs, unsigned long frame_pointe
852862
}
853863
}
854864

865+
ftrace_test_recursion_unlock(bit);
866+
out:
855867
/*
856868
* The ftrace_graph_return() may still access the current
857869
* ret_stack structure, we need to make sure the update of

0 commit comments

Comments
 (0)