Skip to contentSkip to conversation

riscv/kprobe: reclaim insn_slot on kprobe unregistration

PATCH In review Liao Chang 4 participants 5 messages May 25, 2022 – May 26, 2022

Subsystems (2)
lore
Share & tools
lore

Liao Chang

author
#0 lore raw

PATCH v2 1/1 riscv/kprobe: reclaim insn_slot on kprobe unregistration

On kprobe registration kernel allocate one insn_slot for new kprobe, but it forget to reclaim the insn_slot on unregistration, leading to a potential leakage.

---
v2:
Add Reported-by tag

 arch/riscv/kernel/probes/kprobes.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/probes/kprobes.c +4 -0
index e6e950b7cf32..f12eb1fbb52c 100644
@@ -110,6 +110,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+	if (p->ainsn.api.insn) {
+		free_insn_slot(p->ainsn.api.insn, 0);
+		p->ainsn.api.insn = NULL;
+	}
 }
 
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 19 lines · Liao Chang

On kprobe registration kernel allocate one insn_slot for new kprobe, but it forget to reclaim the insn_slot on unregistration, leading to a potential leakage.

---
v2:
Add Reported-by tag

 arch/riscv/kernel/probes/kprobes.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/probes/kprobes.c +0 -0
index e6e950b7cf32..f12eb1fbb52c 100644

arch/riscv/kernel/probes/kprobes.c void __kprobes arch_disarm_kprobe(struct kprobe *p) · line 116

Quoted 7 lines
arch/riscv/kernel/probes/kprobes.c +4 -0
@@ -110,6 +110,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+       if (p->ainsn.api.insn) {
+               free_insn_slot(p->ainsn.api.insn, 0);
+               p->ainsn.api.insn = NULL;
+       }

Thx reviewed-by: Guo Ren <[email protected]>

You also could give a fixup patch to csky, thx:

(quoted diff) +0 -0

b/arch/csky/kernel/probes/kprobes.c
index 42920f25e73c..661da54b418f 100644

arch/csky/kernel/probes/kprobes.c +4 -0
@@ -124,6 +124,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+       if (p->ainsn.api.insn) {
+               free_insn_slot(p->ainsn.api.insn, 0);
+               p->ainsn.api.insn = NULL;
+       }
 }
 
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Quoted 6 lines
arch/riscv/kernel/probes/kprobes.c +0 -1
 }
 
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
--

2.17.1

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#2 lore raw
Quoted 37 lines · Guo Ren
Quoted 19 lines · Liao Chang

On kprobe registration kernel allocate one insn_slot for new kprobe, but it forget to reclaim the insn_slot on unregistration, leading to a potential leakage.

---
v2:
Add Reported-by tag

 arch/riscv/kernel/probes/kprobes.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/probes/kprobes.c +0 -0
index e6e950b7cf32..f12eb1fbb52c 100644

arch/riscv/kernel/probes/kprobes.c void __kprobes arch_disarm_kprobe(struct kprobe *p) · line 116

Quoted 7 lines
arch/riscv/kernel/probes/kprobes.c +4 -0
@@ -110,6 +110,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+       if (p->ainsn.api.insn) {
+               free_insn_slot(p->ainsn.api.insn, 0);
+               p->ainsn.api.insn = NULL;
+       }

Thx reviewed-by: Guo Ren <[email protected]>

You also could give a fixup patch to csky, thx:

arch/csky/kernel/probes/kprobes.c +0 -0
index 42920f25e73c..661da54b418f 100644

arch/csky/kernel/probes/kprobes.c void __kprobes arch_disarm_kprobe(struct kprobe *p) · line 131

Quoted 8 lines
arch/csky/kernel/probes/kprobes.c +4 -0
@@ -124,6 +124,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+       if (p->ainsn.api.insn) {
+               free_insn_slot(p->ainsn.api.insn, 0);
+               p->ainsn.api.insn = NULL;
+       }
 }

Sure, I will, thanks.

Quoted 11 lines
arch/csky/kernel/probes/kprobes.c +0 -0
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
 
Quoted 6 lines
arch/riscv/kernel/probes/kprobes.c +0 -1
 }
 
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
--

2.17.1

Signature · 2 lines
BR,
Liao, Chang
Reviewed-by
#3 lore raw
In reply to Guo Ren
Quoted 8 lines · Guo Ren
Quoted 7 lines · Liao Chang

On kprobe registration kernel allocate one insn_slot for new kprobe, but it forget to reclaim the insn_slot on unregistration, leading to a potential leakage.

Quoted 53 lines
Quoted 12 lines

---
v2:
Add Reported-by tag

 arch/riscv/kernel/probes/kprobes.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/probes/kprobes.c +0 -0
index e6e950b7cf32..f12eb1fbb52c 100644

arch/riscv/kernel/probes/kprobes.c void __kprobes arch_disarm_kprobe(struct kprobe *p) · line 116

Quoted 7 lines
arch/riscv/kernel/probes/kprobes.c +4 -0
@@ -110,6 +110,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+       if (p->ainsn.api.insn) {
+               free_insn_slot(p->ainsn.api.insn, 0);
+               p->ainsn.api.insn = NULL;
+       }

Thx reviewed-by: Guo Ren <[email protected]>

You also could give a fixup patch to csky, thx:

arch/csky/kernel/probes/kprobes.c +4 -0
index 42920f25e73c..661da54b418f 100644
@@ -124,6 +124,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+       if (p->ainsn.api.insn) {
+               free_insn_slot(p->ainsn.api.insn, 0);
+               p->ainsn.api.insn = NULL;
+       }
 }
 
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Quoted 7 lines
arch/riscv/kernel/probes/kprobes.c +0 -1
 }
 
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
--

2.17.1

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
Reviewed-by on v2 1/1
#4 lore raw
In reply to Liao Chang
Quoted 6 lines · On Wed, 25 May 2022 09:44:24 +0800 Liao Chang <[email protected]> wrote:

On kprobe registration kernel allocate one insn_slot for new kprobe, but it forget to reclaim the insn_slot on unregistration, leading to a potential leakage.

Looks good to me.

Fixes c22b0bcb1dd0 ("riscv: Add kprobes supported")

Thank you,

Quoted 25 lines

---
v2:
Add Reported-by tag

 arch/riscv/kernel/probes/kprobes.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/probes/kprobes.c +4 -0
index e6e950b7cf32..f12eb1fbb52c 100644
@@ -110,6 +110,10 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
 
 void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
+	if (p->ainsn.api.insn) {
+		free_insn_slot(p->ainsn.api.insn, 0);
+		p->ainsn.api.insn = NULL;
+	}
 }
 
 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Signature · 1 line
2.17.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>