Skip to content

Commit 297f923

Browse files
Jessica YuIngo Molnar
authored andcommitted
kprobes: Propagate error from disarm_kprobe_ftrace()
Improve error handling when disarming ftrace-based kprobes. Like with arm_kprobe_ftrace(), propagate any errors from disarm_kprobe_ftrace() so that we do not disable/unregister kprobes that are still armed. In other words, unregister_kprobe() and disable_kprobe() should not report success if the kprobe could not be disarmed. disarm_all_kprobes() keeps its current behavior and attempts to disarm all kprobes. It returns the last encountered error and gives a warning if not all probes could be disarmed. This patch is based on Petr Mladek's original patchset (patches 2 and 3) back in 2015, which improved kprobes error handling, found here: https://lkml.org/lkml/2015/2/26/452 However, further work on this had been paused since then and the patches were not upstreamed. Based-on-patches-by: Petr Mladek <[email protected]> Signed-off-by: Jessica Yu <[email protected]> Acked-by: Masami Hiramatsu <[email protected]> Cc: Ananth N Mavinakayanahalli <[email protected]> Cc: Anil S Keshavamurthy <[email protected]> Cc: David S . Miller <[email protected]> Cc: Jiri Kosina <[email protected]> Cc: Joe Lawrence <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Miroslav Benes <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Petr Mladek <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 12310e3 commit 297f923

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

kernel/kprobes.c

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,23 +1011,27 @@ static int arm_kprobe_ftrace(struct kprobe *p)
10111011
}
10121012

10131013
/* Caller must lock kprobe_mutex */
1014-
static void disarm_kprobe_ftrace(struct kprobe *p)
1014+
static int disarm_kprobe_ftrace(struct kprobe *p)
10151015
{
1016-
int ret;
1016+
int ret = 0;
10171017

1018-
kprobe_ftrace_enabled--;
1019-
if (kprobe_ftrace_enabled == 0) {
1018+
if (kprobe_ftrace_enabled == 1) {
10201019
ret = unregister_ftrace_function(&kprobe_ftrace_ops);
1021-
WARN(ret < 0, "Failed to init kprobe-ftrace (%d)\n", ret);
1020+
if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
1021+
return ret;
10221022
}
1023+
1024+
kprobe_ftrace_enabled--;
1025+
10231026
ret = ftrace_set_filter_ip(&kprobe_ftrace_ops,
10241027
(unsigned long)p->addr, 1, 0);
10251028
WARN(ret < 0, "Failed to disarm kprobe-ftrace at %p (%d)\n", p->addr, ret);
1029+
return ret;
10261030
}
10271031
#else /* !CONFIG_KPROBES_ON_FTRACE */
10281032
#define prepare_kprobe(p) arch_prepare_kprobe(p)
10291033
#define arm_kprobe_ftrace(p) (-ENODEV)
1030-
#define disarm_kprobe_ftrace(p) do {} while (0)
1034+
#define disarm_kprobe_ftrace(p) (-ENODEV)
10311035
#endif
10321036

10331037
/* Arm a kprobe with text_mutex */
@@ -1046,18 +1050,18 @@ static int arm_kprobe(struct kprobe *kp)
10461050
}
10471051

10481052
/* Disarm a kprobe with text_mutex */
1049-
static void disarm_kprobe(struct kprobe *kp, bool reopt)
1053+
static int disarm_kprobe(struct kprobe *kp, bool reopt)
10501054
{
1051-
if (unlikely(kprobe_ftrace(kp))) {
1052-
disarm_kprobe_ftrace(kp);
1053-
return;
1054-
}
1055+
if (unlikely(kprobe_ftrace(kp)))
1056+
return disarm_kprobe_ftrace(kp);
10551057

10561058
cpus_read_lock();
10571059
mutex_lock(&text_mutex);
10581060
__disarm_kprobe(kp, reopt);
10591061
mutex_unlock(&text_mutex);
10601062
cpus_read_unlock();
1063+
1064+
return 0;
10611065
}
10621066

10631067
/*
@@ -1639,11 +1643,12 @@ static int aggr_kprobe_disabled(struct kprobe *ap)
16391643
static struct kprobe *__disable_kprobe(struct kprobe *p)
16401644
{
16411645
struct kprobe *orig_p;
1646+
int ret;
16421647

16431648
/* Get an original kprobe for return */
16441649
orig_p = __get_valid_kprobe(p);
16451650
if (unlikely(orig_p == NULL))
1646-
return NULL;
1651+
return ERR_PTR(-EINVAL);
16471652

16481653
if (!kprobe_disabled(p)) {
16491654
/* Disable probe if it is a child probe */
@@ -1657,8 +1662,13 @@ static struct kprobe *__disable_kprobe(struct kprobe *p)
16571662
* should have already been disarmed, so
16581663
* skip unneed disarming process.
16591664
*/
1660-
if (!kprobes_all_disarmed)
1661-
disarm_kprobe(orig_p, true);
1665+
if (!kprobes_all_disarmed) {
1666+
ret = disarm_kprobe(orig_p, true);
1667+
if (ret) {
1668+
p->flags &= ~KPROBE_FLAG_DISABLED;
1669+
return ERR_PTR(ret);
1670+
}
1671+
}
16621672
orig_p->flags |= KPROBE_FLAG_DISABLED;
16631673
}
16641674
}
@@ -1675,8 +1685,8 @@ static int __unregister_kprobe_top(struct kprobe *p)
16751685

16761686
/* Disable kprobe. This will disarm it if needed. */
16771687
ap = __disable_kprobe(p);
1678-
if (ap == NULL)
1679-
return -EINVAL;
1688+
if (IS_ERR(ap))
1689+
return PTR_ERR(ap);
16801690

16811691
if (ap == p)
16821692
/*
@@ -2109,12 +2119,14 @@ static void kill_kprobe(struct kprobe *p)
21092119
int disable_kprobe(struct kprobe *kp)
21102120
{
21112121
int ret = 0;
2122+
struct kprobe *p;
21122123

21132124
mutex_lock(&kprobe_mutex);
21142125

21152126
/* Disable this kprobe */
2116-
if (__disable_kprobe(kp) == NULL)
2117-
ret = -EINVAL;
2127+
p = __disable_kprobe(kp);
2128+
if (IS_ERR(p))
2129+
ret = PTR_ERR(p);
21182130

21192131
mutex_unlock(&kprobe_mutex);
21202132
return ret;
@@ -2486,34 +2498,50 @@ static int arm_all_kprobes(void)
24862498
return ret;
24872499
}
24882500

2489-
static void disarm_all_kprobes(void)
2501+
static int disarm_all_kprobes(void)
24902502
{
24912503
struct hlist_head *head;
24922504
struct kprobe *p;
2493-
unsigned int i;
2505+
unsigned int i, total = 0, errors = 0;
2506+
int err, ret = 0;
24942507

24952508
mutex_lock(&kprobe_mutex);
24962509

24972510
/* If kprobes are already disarmed, just return */
24982511
if (kprobes_all_disarmed) {
24992512
mutex_unlock(&kprobe_mutex);
2500-
return;
2513+
return 0;
25012514
}
25022515

25032516
kprobes_all_disarmed = true;
2504-
printk(KERN_INFO "Kprobes globally disabled\n");
25052517

25062518
for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
25072519
head = &kprobe_table[i];
2520+
/* Disarm all kprobes on a best-effort basis */
25082521
hlist_for_each_entry_rcu(p, head, hlist) {
2509-
if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
2510-
disarm_kprobe(p, false);
2522+
if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2523+
err = disarm_kprobe(p, false);
2524+
if (err) {
2525+
errors++;
2526+
ret = err;
2527+
}
2528+
total++;
2529+
}
25112530
}
25122531
}
2532+
2533+
if (errors)
2534+
pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2535+
errors, total);
2536+
else
2537+
pr_info("Kprobes globally disabled\n");
2538+
25132539
mutex_unlock(&kprobe_mutex);
25142540

25152541
/* Wait for disarming all kprobes by optimizer */
25162542
wait_for_kprobe_optimizer();
2543+
2544+
return ret;
25172545
}
25182546

25192547
/*
@@ -2556,7 +2584,7 @@ static ssize_t write_enabled_file_bool(struct file *file,
25562584
case 'n':
25572585
case 'N':
25582586
case '0':
2559-
disarm_all_kprobes();
2587+
ret = disarm_all_kprobes();
25602588
break;
25612589
default:
25622590
return -EINVAL;

0 commit comments

Comments
 (0)