2019-05-27 08:55:05 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-09-28 17:15:20 +09:00
|
|
|
/*
|
|
|
|
* Dynamic Ftrace based Kprobes Optimization
|
|
|
|
*
|
|
|
|
* Copyright (C) Hitachi Ltd., 2012
|
|
|
|
*/
|
|
|
|
#include <linux/kprobes.h>
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/hardirq.h>
|
|
|
|
#include <linux/preempt.h>
|
|
|
|
#include <linux/ftrace.h>
|
2024-10-21 21:39:22 +09:00
|
|
|
#include <asm/text-patching.h>
|
2012-09-28 17:15:20 +09:00
|
|
|
|
2012-09-28 17:15:22 +09:00
|
|
|
#include "common.h"
|
2012-09-28 17:15:20 +09:00
|
|
|
|
2021-03-18 15:28:01 +01:00
|
|
|
/* Ftrace callback handler for kprobes -- called under preempt disabled */
|
2014-04-17 17:18:14 +09:00
|
|
|
void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
|
2020-10-28 17:42:17 -04:00
|
|
|
struct ftrace_ops *ops, struct ftrace_regs *fregs)
|
2012-09-28 17:15:20 +09:00
|
|
|
{
|
2020-10-28 17:42:17 -04:00
|
|
|
struct pt_regs *regs = ftrace_get_regs(fregs);
|
2012-09-28 17:15:20 +09:00
|
|
|
struct kprobe *p;
|
|
|
|
struct kprobe_ctlblk *kcb;
|
2020-11-05 21:32:40 -05:00
|
|
|
int bit;
|
2012-09-28 17:15:20 +09:00
|
|
|
|
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>
2024-05-01 09:29:56 -07:00
|
|
|
if (unlikely(kprobe_ftrace_disabled))
|
|
|
|
return;
|
|
|
|
|
2020-11-05 21:32:46 -05:00
|
|
|
bit = ftrace_test_recursion_trylock(ip, parent_ip);
|
2020-11-05 21:32:40 -05:00
|
|
|
if (bit < 0)
|
|
|
|
return;
|
|
|
|
|
2012-09-28 17:15:20 +09:00
|
|
|
p = get_kprobe((kprobe_opcode_t *)ip);
|
|
|
|
if (unlikely(!p) || kprobe_disabled(p))
|
2020-11-05 21:32:40 -05:00
|
|
|
goto out;
|
2012-09-28 17:15:20 +09:00
|
|
|
|
|
|
|
kcb = get_kprobe_ctlblk();
|
|
|
|
if (kprobe_running()) {
|
|
|
|
kprobes_inc_nmissed_count(p);
|
|
|
|
} else {
|
2024-10-21 21:39:22 +09:00
|
|
|
unsigned long orig_ip = instruction_pointer(regs);
|
|
|
|
|
2012-09-28 17:15:20 +09:00
|
|
|
/* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
|
2024-10-21 21:39:22 +09:00
|
|
|
instruction_pointer_set(regs, ip + INT3_INSN_SIZE);
|
2012-09-28 17:15:20 +09:00
|
|
|
|
|
|
|
__this_cpu_write(current_kprobe, p);
|
|
|
|
kcb->kprobe_status = KPROBE_HIT_ACTIVE;
|
2017-09-19 19:01:40 +09:00
|
|
|
if (!p->pre_handler || !p->pre_handler(p, regs)) {
|
2018-06-20 01:10:55 +09:00
|
|
|
if (unlikely(p->post_handler)) {
|
2024-10-21 21:39:22 +09:00
|
|
|
/*
|
|
|
|
* Emulate singlestep (and also recover regs->ip)
|
|
|
|
* as if there is a 5byte nop
|
|
|
|
*/
|
|
|
|
instruction_pointer_set(regs, ip + MCOUNT_INSN_SIZE);
|
2018-06-20 01:10:55 +09:00
|
|
|
kcb->kprobe_status = KPROBE_HIT_SSDONE;
|
|
|
|
p->post_handler(p, regs, 0);
|
|
|
|
}
|
2024-10-21 21:39:22 +09:00
|
|
|
/* Recover IP address */
|
|
|
|
instruction_pointer_set(regs, orig_ip);
|
2017-09-19 19:01:40 +09:00
|
|
|
}
|
2012-09-28 17:15:20 +09:00
|
|
|
/*
|
2018-06-20 01:15:45 +09:00
|
|
|
* If pre_handler returns !0, it changes regs->ip. We have to
|
|
|
|
* skip emulating post_handler.
|
2012-09-28 17:15:20 +09:00
|
|
|
*/
|
2018-06-20 01:15:45 +09:00
|
|
|
__this_cpu_write(current_kprobe, NULL);
|
2012-09-28 17:15:20 +09:00
|
|
|
}
|
2020-11-05 21:32:40 -05:00
|
|
|
out:
|
|
|
|
ftrace_test_recursion_unlock(bit);
|
2012-09-28 17:15:20 +09:00
|
|
|
}
|
2014-04-17 17:18:14 +09:00
|
|
|
NOKPROBE_SYMBOL(kprobe_ftrace_handler);
|
2012-09-28 17:15:20 +09:00
|
|
|
|
2014-04-17 17:17:47 +09:00
|
|
|
int arch_prepare_kprobe_ftrace(struct kprobe *p)
|
2012-09-28 17:15:20 +09:00
|
|
|
{
|
|
|
|
p->ainsn.insn = NULL;
|
2017-03-29 14:01:35 +09:00
|
|
|
p->ainsn.boostable = false;
|
2012-09-28 17:15:20 +09:00
|
|
|
return 0;
|
|
|
|
}
|