Skip to contentSkip to conversation

kprobes/x86: opt: free optinsn cache when range check fails

PATCH In review Wang Nan 4 participants 10 messages Jul 28, 2014 – Sep 16, 2014 x86

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

PATCH 1/1 kprobes/x86: opt: free optinsn cache when range check fails

This patch frees optinsn slot when range check error to prevent memory leaks. Before this patch, cache entry in kprobe_insn_cache won't be freed if kprobe optimizing fails due to range check failure.

---
 arch/x86/kernel/kprobes/opt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
index f304773..f1314d0 100644
@@ -338,8 +338,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
 	 * a relative jump.
 	 */
 	rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
-	if (abs(rel) > 0x7fffffff)
+	if (abs(rel) > 0x7fffffff) {
+		__arch_remove_optimized_kprobe(op, 0);
 		return -ERANGE;
+	}
 
 	buf = (u8 *)op->optinsn.insn;
 
Signature · 1 line
1.8.4
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Wang Nan

This patch frees optinsn slot when range check error to prevent memory leaks. Before this patch, cache entry in kprobe_insn_cache won't be freed if kprobe optimizing fails due to range check failure.

Oops, thank you for finding it!

BTW, would you really have hit this error?
I'd like to know the case if this really happens.

Quoted 21 lines
---
 arch/x86/kernel/kprobes/opt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
index f304773..f1314d0 100644
@@ -338,8 +338,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
 	 * a relative jump.
 	 */
 	rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
-	if (abs(rel) > 0x7fffffff)
+	if (abs(rel) > 0x7fffffff) {
+		__arch_remove_optimized_kprobe(op, 0);
 		return -ERANGE;
+	}
 
 	buf = (u8 *)op->optinsn.insn;
 
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
author
#2 lore raw
Quoted 5 lines · Masami Hiramatsu
Quoted 4 lines · Wang Nan

This patch frees optinsn slot when range check error to prevent memory leaks. Before this patch, cache entry in kprobe_insn_cache won't be freed if kprobe optimizing fails due to range check failure.

Quoted 8 lines
Quoted 1 line

Oops, thank you for finding it!

BTW, would you really have hit this error?
I'd like to know the case if this really happens.

I'm not really hit it on x86_64. I found this problem when trying to implement kprobe opt on arm.

On arm, relative jump can only branch on/backward 64MB, which makes it a realistic problem.

Quoted 24 lines
Quoted 22 lines
---
 arch/x86/kernel/kprobes/opt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
index f304773..f1314d0 100644
@@ -338,8 +338,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
 	 * a relative jump.
 	 */
 	rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
-	if (abs(rel) > 0x7fffffff)
+	if (abs(rel) > 0x7fffffff) {
+		__arch_remove_optimized_kprobe(op, 0);
 		return -ERANGE;
+	}
 
 	buf = (u8 *)op->optinsn.insn;
 
#3 lore raw

Hi Wang,

Quoted 8 lines · Wang Nan
Quoted 7 lines · Masami Hiramatsu
Quoted 5 lines · Wang Nan

This patch frees optinsn slot when range check error to prevent memory leaks. Before this patch, cache entry in kprobe_insn_cache won't be freed if kprobe optimizing fails due to range check failure.

Quoted 8 lines
Quoted 6 lines

Oops, thank you for finding it!

BTW, would you really have hit this error?
I'd like to know the case if this really happens.

I'm not really hit it on x86_64. I found this problem when trying to implement kprobe opt on arm.

That's interesting :)

Quoted 2 lines

On arm, relative jump can only branch on/backward 64MB, which makes it a realistic problem.

Yeah, that is what I expected on RISC processor such as ARM.

Perhaps you'll need to overwrite 2 words, one is for "ldr pc, [pc, #-4]" and one is for the address data. In this case, you have no branch range limitation in 32bit mode. This requires branch destination checking for safety as x86 optprobe does.
Plus, you'll have to use same technique of x86 to make a detour code and deferred optimization for overwriting multiple instructions. Put a breakpoint at the probe point, wait for synchronize_sched(), put the 2nd instruction(.data) and overwrite the breakpoint with the "ldr". :)

However, that is only for arm32.
For ARM64, I'm not so sure about its ISA. I guess we need a scratchpad area for that..

Anyway, please CC to me when you've done the prototyping and sending RFC. I'll review and test it. :)

Thank you,

Quoted 33 lines
Quoted 20 lines
Quoted 20 lines

---
 arch/x86/kernel/kprobes/opt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
index f304773..f1314d0 100644
@@ -338,8 +338,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
 	 * a relative jump.
 	 */
 	rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
-	if (abs(rel) > 0x7fffffff)
+	if (abs(rel) > 0x7fffffff) {
+		__arch_remove_optimized_kprobe(op, 0);
 		return -ERANGE;
+	}
 
Quoted 5 lines

arch/x86/kernel/kprobes/opt.c int arch_prepare_optimized_kprobe(struct optimized_kprobe *op) · line 347

Quoted 3 lines
arch/x86/kernel/kprobes/opt.c +0 -0
 	buf = (u8 *)op->optinsn.insn;
 

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
author
#4 lore raw
Quoted 35 lines · Masami Hiramatsu

Hi Wang,

Quoted 8 lines · Wang Nan
Quoted 7 lines · Masami Hiramatsu
Quoted 5 lines · Wang Nan

This patch frees optinsn slot when range check error to prevent memory leaks. Before this patch, cache entry in kprobe_insn_cache won't be freed if kprobe optimizing fails due to range check failure.

Quoted 8 lines
Quoted 6 lines

Oops, thank you for finding it!

BTW, would you really have hit this error?
I'd like to know the case if this really happens.

I'm not really hit it on x86_64. I found this problem when trying to implement kprobe opt on arm.

That's interesting :)

Quoted 2 lines

On arm, relative jump can only branch on/backward 64MB, which makes it a realistic problem.

Yeah, that is what I expected on RISC processor such as ARM.

Perhaps you'll need to overwrite 2 words, one is for "ldr pc, [pc, #-4]" and one is for the address data. In this case, you have no branch range limitation in 32bit mode. This requires branch destination checking for safety as x86 optprobe does.
Plus, you'll have to use same technique of x86 to make a detour code and deferred optimization for overwriting multiple instructions. Put a breakpoint at the probe point, wait for synchronize_sched(), put the 2nd instruction(.data) and overwrite the breakpoint with the "ldr". :)

Quoted 8 lines

However, that is only for arm32.
For ARM64, I'm not so sure about its ISA. I guess we need a scratchpad area for that..

Anyway, please CC to me when you've done the prototyping and sending RFC. I'll review and test it. :)

Thank you,

Hi Masami,

I have posted my RFC patch on LKML and ARM mailing list, and also CC you. Please see:

http://lists.infradead.org/pipermail/linux-arm-kernel/2014-August/277809.html

Please help me review my patch, Thank you!

Quoted 35 lines
Quoted 33 lines
Quoted 20 lines
Quoted 20 lines

---
 arch/x86/kernel/kprobes/opt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
index f304773..f1314d0 100644
@@ -338,8 +338,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
 	 * a relative jump.
 	 */
 	rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
-	if (abs(rel) > 0x7fffffff)
+	if (abs(rel) > 0x7fffffff) {
+		__arch_remove_optimized_kprobe(op, 0);
 		return -ERANGE;
+	}
 
Quoted 5 lines

arch/x86/kernel/kprobes/opt.c int arch_prepare_optimized_kprobe(struct optimized_kprobe *op) · line 347

Quoted 3 lines
arch/x86/kernel/kprobes/opt.c +0 -0
 	buf = (u8 *)op->optinsn.insn;
 

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author
#5 lore raw
In reply to Masami Hiramatsu
Quoted 11 lines · Masami Hiramatsu
Quoted 5 lines · Wang Nan

This patch frees optinsn slot when range check error to prevent memory leaks. Before this patch, cache entry in kprobe_insn_cache won't be freed if kprobe optimizing fails due to range check failure.

Oops, thank you for finding it!

Hi Masami Hiramatsu,

I don't find this patch in newest upstream. Is there any problem?

Quoted 26 lines

BTW, would you really have hit this error?
I'd like to know the case if this really happens.

Quoted 21 lines
---
 arch/x86/kernel/kprobes/opt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
index f304773..f1314d0 100644
@@ -338,8 +338,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
 	 * a relative jump.
 	 */
 	rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
-	if (abs(rel) > 0x7fffffff)
+	if (abs(rel) > 0x7fffffff) {
+		__arch_remove_optimized_kprobe(op, 0);
 		return -ERANGE;
+	}
 
 	buf = (u8 *)op->optinsn.insn;
 
#6 lore raw

Hi Ingo,

Could you pull this for a bugfix of a memory leak?

Quoted 46 lines · Wang Nan
Quoted 11 lines · Masami Hiramatsu
Quoted 5 lines · Wang Nan

This patch frees optinsn slot when range check error to prevent memory leaks. Before this patch, cache entry in kprobe_insn_cache won't be freed if kprobe optimizing fails due to range check failure.

Oops, thank you for finding it!

Hi Masami Hiramatsu,

I don't find this patch in newest upstream. Is there any problem?

Quoted 26 lines

BTW, would you really have hit this error?
I'd like to know the case if this really happens.

Quoted 21 lines
---
 arch/x86/kernel/kprobes/opt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
index f304773..f1314d0 100644
@@ -338,8 +338,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
 	 * a relative jump.
 	 */
 	rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
-	if (abs(rel) > 0x7fffffff)
+	if (abs(rel) > 0x7fffffff) {
+		__arch_remove_optimized_kprobe(op, 0);
 		return -ERANGE;
+	}
 
 	buf = (u8 *)op->optinsn.insn;
 
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
author
#7 lore raw

Hi Ingo and Masami,

I still unable to find this bugfix in mainline code. Is there any problem?

Thank you!

Quoted 54 lines · Masami Hiramatsu

Hi Ingo,

Could you pull this for a bugfix of a memory leak?

Quoted 46 lines · Wang Nan
Quoted 11 lines · Masami Hiramatsu
Quoted 5 lines · Wang Nan

This patch frees optinsn slot when range check error to prevent memory leaks. Before this patch, cache entry in kprobe_insn_cache won't be freed if kprobe optimizing fails due to range check failure.

Oops, thank you for finding it!

Hi Masami Hiramatsu,

I don't find this patch in newest upstream. Is there any problem?

Quoted 26 lines

BTW, would you really have hit this error?
I'd like to know the case if this really happens.

Quoted 21 lines
---
 arch/x86/kernel/kprobes/opt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
index f304773..f1314d0 100644
@@ -338,8 +338,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
 	 * a relative jump.
 	 */
 	rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
-	if (abs(rel) > 0x7fffffff)
+	if (abs(rel) > 0x7fffffff) {
+		__arch_remove_optimized_kprobe(op, 0);
 		return -ERANGE;
+	}
 
 	buf = (u8 *)op->optinsn.insn;
 
#8 lore raw
Quoted 3 lines · Wang Nan

Hi Ingo and Masami,

I still unable to find this bugfix in mainline code. Is there any problem?

It's in tip:perf/urgent, on its way to Linus.

Thanks,

Ingo

#9 lore raw

[tip:perf/urgent] kprobes/x86: Free 'optinsn' cache when range check fails

In reply to Wang Nan
kprobes/x86: Free 'optinsn' cache when range check fails
Commit-ID:  256aae5eac6d328067d1a986a7c5df6f19bdc8b4
Gitweb:     http://git.kernel.org/tip/256aae5eac6d328067d1a986a7c5df6f19bdc8b4
Author:     Wang Nan <[email protected]>
AuthorDate: Mon, 28 Jul 2014 20:20:19 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 27 Aug 2014 20:24:32 +0200

kprobes/x86: Free 'optinsn' cache when range check fails

This patch frees the 'optinsn' slot when we get a range check error, to prevent memory leaks.

Before this patch, cache entry in kprobe_insn_cache() won't be freed if kprobe optimizing fails due to range check failure.

---
 arch/x86/kernel/kprobes/opt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/opt.c +3 -1
index f304773..f1314d0 100644
@@ -338,8 +338,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
 	 * a relative jump.
 	 */
 	rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
-	if (abs(rel) > 0x7fffffff)
+	if (abs(rel) > 0x7fffffff) {
+		__arch_remove_optimized_kprobe(op, 0);
 		return -ERANGE;
+	}
 
 	buf = (u8 *)op->optinsn.insn;