Skip to contentSkip to conversation

kprobes: Rollback kprobe flags on failed arm_kprobe

PATCH Superseded Chuang 2 participants 3 messages Jun 2, 2022 – Jun 4, 2022 kprobes

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Chuang

author
#0 lore raw

PATCH 1/1 kprobes: Rollback kprobe flags on failed arm_kprobe

From: Chuang Wang <[email protected]>

In aggrprobe scenes, if arm_kprobe() returns an error(e.g. livepatch and kprobe are using the same function X), kprobe flags, while has been modified to ~KPROBE_FLAG_DISABLED, is not rollled back.

Then, __disable_kprobe() will be failed in __unregister_kprobe_top(), the kprobe list will be not removed from aggrprobe, memory leaks or illegal pointers will be caused.

WARN disarm_kprobe:
Failed to disarm kprobe-ftrace at 00000000c729fdbc (-2)

 RIP: 0010:disarm_kprobe+0xcc/0x110
 Call Trace:
  __disable_kprobe+0x78/0x90
  __unregister_kprobe_top+0x13/0x1b0
  ? _cond_resched+0x15/0x30
  unregister_kprobes+0x32/0x80
  unregister_kprobe+0x1a/0x20

Illegal Pointers:

 BUG: unable to handle kernel paging request at 0000000000656369
 RIP: 0010:__get_valid_kprobe+0x69/0x90
 Call Trace:
  register_kprobe+0x30/0x60
  __register_trace_kprobe.part.7+0x8b/0xc0
  create_local_trace_kprobe+0xd2/0x130
  perf_kprobe_init+0x83/0xd0
---
 kernel/kprobes.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/kprobes.c +3 -1
index f214f8c088ed..96c75e23113c 100644
@@ -2422,8 +2422,10 @@ int enable_kprobe(struct kprobe *kp)
 	if (!kprobes_all_disarmed && kprobe_disabled(p)) {
 		p->flags &= ~KPROBE_FLAG_DISABLED;
 		ret = arm_kprobe(p);
-		if (ret)
+		if (ret) {
 			p->flags |= KPROBE_FLAG_DISABLED;
+			kp->flags |= KPROBE_FLAG_DISABLED;
+		}
 	}
 out:
 	mutex_unlock(&kprobe_mutex);
Signature · 1 line
2.34.1
#1 lore raw

Hi Chuang,

Quoted 20 lines · On Thu, 2 Jun 2022 15:32:59 +0800 Chuang <[email protected]> wrote:

From: Chuang Wang <[email protected]>

In aggrprobe scenes, if arm_kprobe() returns an error(e.g. livepatch and kprobe are using the same function X), kprobe flags, while has been modified to ~KPROBE_FLAG_DISABLED, is not rollled back.

Then, __disable_kprobe() will be failed in __unregister_kprobe_top(), the kprobe list will be not removed from aggrprobe, memory leaks or illegal pointers will be caused.

WARN disarm_kprobe:
Failed to disarm kprobe-ftrace at 00000000c729fdbc (-2)

 RIP: 0010:disarm_kprobe+0xcc/0x110
 Call Trace:
  __disable_kprobe+0x78/0x90
  __unregister_kprobe_top+0x13/0x1b0
  ? _cond_resched+0x15/0x30
  unregister_kprobes+0x32/0x80
  unregister_kprobe+0x1a/0x20
Quoted 8 lines

Illegal Pointers:

 BUG: unable to handle kernel paging request at 0000000000656369
 RIP: 0010:__get_valid_kprobe+0x69/0x90
 Call Trace:
  register_kprobe+0x30/0x60
  __register_trace_kprobe.part.7+0x8b/0xc0
  create_local_trace_kprobe+0xd2/0x130
  perf_kprobe_init+0x83/0xd0

Oops, thanks for reporting!

Quoted 2 lines

This should go to stable, so add below tag. (No need to CC to stable)

Fixes 12310e343755 ("kprobes: Propagate error from arm_kprobe_ftrace()")

And could you also update this patch as below?

Quoted 9 lines
---
 kernel/kprobes.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/kprobes.c +0 -0
index f214f8c088ed..96c75e23113c 100644

kernel/kprobes.c int enable_kprobe(struct kprobe *kp) · line 2426

Quoted 6 lines
kernel/kprobes.c +1 -1
@@ -2422,8 +2422,10 @@ int enable_kprobe(struct kprobe *kp)
 	if (!kprobes_all_disarmed && kprobe_disabled(p)) {
 		p->flags &= ~KPROBE_FLAG_DISABLED;
 		ret = arm_kprobe(p);
-		if (ret)
+		if (ret) {
 			p->flags |= KPROBE_FLAG_DISABLED;

Here, can you add a check?

	if (p != kp) 

kernel/kprobes.c int enable_kprobe(struct kprobe *kp) · line 2427

Quoted 1 line
kernel/kprobes.c +1 -0
+			kp->flags |= KPROBE_FLAG_DISABLED;

Thus is is clear that this is corresponding to
---

        if (p != kp)
                kp->flags &= ~KPROBE_FLAG_DISABLED;

---

Thank you,

Quoted 7 lines
kernel/kprobes.c +1 -0
+		}
 	}
 out:
 	mutex_unlock(&kprobe_mutex);
Signature · 1 line
2.34.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author
#2 lore raw

Thanks for your quick reply.
I'm very, very sorry for sending multiple emails. I am submitting a patch for the first time.

Quoted 5 lines · Masami Hiramatsu

This should go to stable, so add below tag. (No need to CC to stable)

Fixes 12310e343755 ("kprobes: Propagate error from arm_kprobe_ftrace()")

Thanks for your kind reminder.

Quoted 21 lines

And could you also update this patch as below?

Quoted 9 lines
---
 kernel/kprobes.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/kprobes.c +0 -0
index f214f8c088ed..96c75e23113c 100644

kernel/kprobes.c int enable_kprobe(struct kprobe *kp) · line 2426

Quoted 6 lines
kernel/kprobes.c +1 -1
@@ -2422,8 +2422,10 @@ int enable_kprobe(struct kprobe *kp)
      if (!kprobes_all_disarmed && kprobe_disabled(p)) {
              p->flags &= ~KPROBE_FLAG_DISABLED;
              ret = arm_kprobe(p);
-             if (ret)
+             if (ret) {
                      p->flags |= KPROBE_FLAG_DISABLED;

Here, can you add a check?

Quoted 3 lines
        if (p != kp)
Quoted 1 line
kernel/kprobes.c +1 -0
+                     kp->flags |= KPROBE_FLAG_DISABLED;

Well, I also thought about it. This already covers conditions p == kp and p != kp:
---

                       kp->flags |= KPROBE_FLAG_DISABLED;

---

When p is equal to kp, kp->flags is assigned to KPROBE_FLAG_DISABLED twice. However, if you add a check that p is not equal to kp, this makes the code read more clearly. Anything is OK, what is your suggestion?

Quoted 18 lines

Thus is is clear that this is corresponding to
---

        if (p != kp)
                kp->flags &= ~KPROBE_FLAG_DISABLED;

---

Thank you,

Quoted 7 lines
kernel/kprobes.c +1 -1
+             }
      }
 out:
      mutex_unlock(&kprobe_mutex);
--

2.34.1

Quoted 2 lines

--
Masami Hiramatsu (Google) <[email protected]>

Have a wonderful day!