Skip to content

Commit 1a7d089

Browse files
brenns10mhiramat
authored andcommitted
kprobe/ftrace: bail out if ftrace was killed
If an error happens in ftrace, ftrace_kill() will prevent disarming kprobes. Eventually, the ftrace_ops associated with the kprobes will be freed, yet the kprobes will still be active, and when triggered, they will use the freed memory, likely resulting in a page fault and panic. This behavior can be reproduced quite easily, by creating a kprobe and then triggering a ftrace_kill(). For simplicity, we can simulate an ftrace error with a kernel module like [1]: [1]: https://github.com/brenns10/kernel_stuff/tree/master/ftrace_killer sudo perf probe --add commit_creds sudo perf trace -e probe:commit_creds # In another terminal make sudo insmod ftrace_killer.ko # calls ftrace_kill(), simulating bug # Back to perf terminal # ctrl-c sudo perf probe --del commit_creds After a short period, a page fault and panic would occur as the kprobe continues to execute and uses the freed ftrace_ops. While ftrace_kill() is supposed to be used only in extreme circumstances, it is invoked in FTRACE_WARN_ON() and so there are many places where an unexpected bug could be triggered, yet the system may continue operating, possibly without the administrator noticing. If ftrace_kill() does not panic the system, then we should do everything we can to continue operating, rather than leave a ticking time bomb. Link: https://lore.kernel.org/all/20240501162956.229427-1-stephen.s.brennan@oracle.com/ Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Acked-by: Guo Ren <guoren@kernel.org> Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
1 parent b7bd96e commit 1a7d089

File tree

10 files changed

+35
-0
lines changed

10 files changed

+35
-0
lines changed

arch/csky/kernel/probes/ftrace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
1212
struct kprobe_ctlblk *kcb;
1313
struct pt_regs *regs;
1414

15+
if (unlikely(kprobe_ftrace_disabled))
16+
return;
17+
1518
bit = ftrace_test_recursion_trylock(ip, parent_ip);
1619
if (bit < 0)
1720
return;

arch/loongarch/kernel/ftrace_dyn.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
287287
struct kprobe *p;
288288
struct kprobe_ctlblk *kcb;
289289

290+
if (unlikely(kprobe_ftrace_disabled))
291+
return;
292+
290293
bit = ftrace_test_recursion_trylock(ip, parent_ip);
291294
if (bit < 0)
292295
return;

arch/parisc/kernel/ftrace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
206206
struct kprobe *p;
207207
int bit;
208208

209+
if (unlikely(kprobe_ftrace_disabled))
210+
return;
211+
209212
bit = ftrace_test_recursion_trylock(ip, parent_ip);
210213
if (bit < 0)
211214
return;

arch/powerpc/kernel/kprobes-ftrace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip,
2121
struct pt_regs *regs;
2222
int bit;
2323

24+
if (unlikely(kprobe_ftrace_disabled))
25+
return;
26+
2427
bit = ftrace_test_recursion_trylock(nip, parent_nip);
2528
if (bit < 0)
2629
return;

arch/riscv/kernel/probes/ftrace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
1111
struct kprobe_ctlblk *kcb;
1212
int bit;
1313

14+
if (unlikely(kprobe_ftrace_disabled))
15+
return;
16+
1417
bit = ftrace_test_recursion_trylock(ip, parent_ip);
1518
if (bit < 0)
1619
return;

arch/s390/kernel/ftrace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
296296
struct kprobe *p;
297297
int bit;
298298

299+
if (unlikely(kprobe_ftrace_disabled))
300+
return;
301+
299302
bit = ftrace_test_recursion_trylock(ip, parent_ip);
300303
if (bit < 0)
301304
return;

arch/x86/kernel/kprobes/ftrace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
2121
struct kprobe_ctlblk *kcb;
2222
int bit;
2323

24+
if (unlikely(kprobe_ftrace_disabled))
25+
return;
26+
2427
bit = ftrace_test_recursion_trylock(ip, parent_ip);
2528
if (bit < 0)
2629
return;

include/linux/kprobes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,15 @@ static inline void wait_for_kprobe_optimizer(void) { }
378378
extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
379379
struct ftrace_ops *ops, struct ftrace_regs *fregs);
380380
extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
381+
/* Set when ftrace has been killed: kprobes on ftrace must be disabled for safety */
382+
extern bool kprobe_ftrace_disabled __read_mostly;
383+
extern void kprobe_ftrace_kill(void);
381384
#else
382385
static inline int arch_prepare_kprobe_ftrace(struct kprobe *p)
383386
{
384387
return -EINVAL;
385388
}
389+
static inline void kprobe_ftrace_kill(void) {}
386390
#endif /* CONFIG_KPROBES_ON_FTRACE */
387391

388392
/* Get the kprobe at this addr (if any) - called with preemption disabled */
@@ -495,6 +499,9 @@ static inline void kprobe_flush_task(struct task_struct *tk)
495499
static inline void kprobe_free_init_mem(void)
496500
{
497501
}
502+
static inline void kprobe_ftrace_kill(void)
503+
{
504+
}
498505
static inline int disable_kprobe(struct kprobe *kp)
499506
{
500507
return -EOPNOTSUPP;

kernel/kprobes.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
10681068

10691069
static int kprobe_ipmodify_enabled;
10701070
static int kprobe_ftrace_enabled;
1071+
bool kprobe_ftrace_disabled;
10711072

10721073
static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
10731074
int *cnt)
@@ -1136,6 +1137,11 @@ static int disarm_kprobe_ftrace(struct kprobe *p)
11361137
ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
11371138
ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
11381139
}
1140+
1141+
void kprobe_ftrace_kill()
1142+
{
1143+
kprobe_ftrace_disabled = true;
1144+
}
11391145
#else /* !CONFIG_KPROBES_ON_FTRACE */
11401146
static inline int arm_kprobe_ftrace(struct kprobe *p)
11411147
{

kernel/trace/ftrace.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7895,6 +7895,7 @@ void ftrace_kill(void)
78957895
ftrace_disabled = 1;
78967896
ftrace_enabled = 0;
78977897
ftrace_trace_function = ftrace_stub;
7898+
kprobe_ftrace_kill();
78987899
}
78997900

79007901
/**

0 commit comments

Comments
 (0)