Skip to contentSkip to conversation

kprobes/x86: Another way to make insn buffer RO and cleanup

PATCH In review Masami Hiramatsu 2 participants 7 messages Aug 18, 2017 – Sep 28, 2017

Subsystems (2)
lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

v3 cover kprobes/x86: Another way to make insn buffer RO and cleanup

Hi,

This series modifies how to handle RO insn buffer and cleans up addressof operators.

The 1st patch changes the RO insn buffer handling: instead of using set_memory_ro/rw to modify the buffer, it prepares new instructions in another buffer and write it with text_poke() as suggested by Ingo Molnar (Thanks!). Since the text_poke() is safely modifying code by mapping alias pages, it can write RO pages.
This also override alloc_insn_page() so that it returns ROX page directly.

The 2nd one is not changed. It is a cleanup patch to remove addressof operators ("&") since
it is meaningless anymore.

V3 has just a following update:
- [1/2] Not to just add set_memory_ro(), introduce new patch to change the way to handle RO pages.

Thanks,

---

Masami Hiramatsu (2):
kprobes/x86: Make insn buffer always ROX and use text_poke kprobes/x86: Remove addressof operators

 arch/x86/include/asm/kprobes.h   |    4 +-
 arch/x86/kernel/kprobes/common.h |    6 ++-
 arch/x86/kernel/kprobes/core.c   |   61 +++++++++++++++++++++------------
 arch/x86/kernel/kprobes/opt.c    |   71 +++++++++++++++++++++-----------------
 kernel/kprobes.c                 |    2 +
 5 files changed, 86 insertions(+), 58 deletions(-)

--
Masami Hiramatsu <[email protected]>

author maintainer
#1 lore raw

PATCH v3 1/2 kprobes/x86: Make insn buffer always ROX and use text_poke

Make insn buffer always ROX and use text_poke() to write the copied instructions instead of set_memory_*(). This makes instruction buffer stronger against other kernel subsystems because there is no window time to modify the buffer.

---
 arch/x86/kernel/kprobes/common.h |    6 ++--
 arch/x86/kernel/kprobes/core.c   |   61 +++++++++++++++++++++++-------------
 arch/x86/kernel/kprobes/opt.c    |   65 ++++++++++++++++++++++----------------
 kernel/kprobes.c                 |    2 +
 4 files changed, 81 insertions(+), 53 deletions(-)
arch/x86/kernel/kprobes/common.h +3 -3
index db2182d63ed0..e2c2a1970869 100644
@@ -75,11 +75,11 @@ extern unsigned long recover_probed_instruction(kprobe_opcode_t *buf,
  * Copy an instruction and adjust the displacement if the instruction
  * uses the %rip-relative addressing mode.
  */
-extern int __copy_instruction(u8 *dest, u8 *src, struct insn *insn);
+extern int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn);
 
 /* Generate a relative-jump/call instruction */
-extern void synthesize_reljump(void *from, void *to);
-extern void synthesize_relcall(void *from, void *to);
+extern void synthesize_reljump(void *dest, void *from, void *to);
+extern void synthesize_relcall(void *dest, void *from, void *to);
 
 #ifdef	CONFIG_OPTPROBES
 extern int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter);
arch/x86/kernel/kprobes/core.c +40 -21

137 lines not on this page · raw message

arch/x86/kernel/kprobes/opt.c +37 -28

107 lines not on this page · raw message

kernel/kprobes.c +1 -1
index a1606a4224e1..15fba7fe57c8 100644
@@ -117,7 +117,7 @@ enum kprobe_slot_state {
 	SLOT_USED = 2,
 };
 
-static void *alloc_insn_page(void)
+void __weak *alloc_insn_page(void)
 {
 	return module_alloc(PAGE_SIZE);
 }
#2 lore raw

[tip:perf/core] kprobes/x86: Make insn buffer always ROX and use text_poke()

kprobes/x86: Make insn buffer always ROX and use text_poke()
Commit-ID:  63fef14fc98a8b4fad777fd3bef4d068802b3f14
Gitweb:     https://git.kernel.org/tip/63fef14fc98a8b4fad777fd3bef4d068802b3f14
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Fri, 18 Aug 2017 17:24:00 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 28 Sep 2017 09:23:03 +0200

kprobes/x86: Make insn buffer always ROX and use text_poke()

Make insn buffer always ROX and use text_poke() to write the copied instructions instead of set_memory_*(). This makes instruction buffer stronger against other kernel subsystems because there is no window time to modify the buffer.

---
 arch/x86/kernel/kprobes/common.h |  6 ++--
 arch/x86/kernel/kprobes/core.c   | 61 ++++++++++++++++++++++++-------------
 arch/x86/kernel/kprobes/opt.c    | 65 +++++++++++++++++++++++-----------------
 kernel/kprobes.c                 |  2 +-
 4 files changed, 81 insertions(+), 53 deletions(-)
arch/x86/kernel/kprobes/common.h +3 -3
index db2182d..e2c2a19 100644
@@ -75,11 +75,11 @@ extern unsigned long recover_probed_instruction(kprobe_opcode_t *buf,
  * Copy an instruction and adjust the displacement if the instruction
  * uses the %rip-relative addressing mode.
  */
-extern int __copy_instruction(u8 *dest, u8 *src, struct insn *insn);
+extern int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn);
 
 /* Generate a relative-jump/call instruction */
-extern void synthesize_reljump(void *from, void *to);
-extern void synthesize_relcall(void *from, void *to);
+extern void synthesize_reljump(void *dest, void *from, void *to);
+extern void synthesize_relcall(void *dest, void *from, void *to);
 
 #ifdef	CONFIG_OPTPROBES
 extern int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter);
arch/x86/kernel/kprobes/core.c +40 -21

137 lines not on this page · raw message

arch/x86/kernel/kprobes/opt.c +37 -28

107 lines not on this page · raw message

kernel/kprobes.c +1 -1
index a1606a4..15fba7f 100644
@@ -117,7 +117,7 @@ enum kprobe_slot_state {
 	SLOT_USED = 2,
 };
 
-static void *alloc_insn_page(void)
+void __weak *alloc_insn_page(void)
 {
 	return module_alloc(PAGE_SIZE);
 }
author maintainer
#3 lore raw

PATCH v3 2/2 kprobes/x86: Remove addressof operators

In reply to Masami Hiramatsu

Since commit 54a7d50b9205 ("x86: mark kprobe templates as character arrays, not single characters") changes optprobe_template_* to arrays, we can remove addressof operators from those symbols.

---
 arch/x86/include/asm/kprobes.h |    4 ++--
 arch/x86/kernel/kprobes/opt.c  |    8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/include/asm/kprobes.h +2 -2
index 6cf65437b5e5..9f2e3102e0bb 100644
@@ -58,8 +58,8 @@ extern __visible kprobe_opcode_t optprobe_template_call[];
 extern __visible kprobe_opcode_t optprobe_template_end[];
 #define MAX_OPTIMIZED_LENGTH (MAX_INSN_SIZE + RELATIVE_ADDR_SIZE)
 #define MAX_OPTINSN_SIZE 				\
-	(((unsigned long)&optprobe_template_end -	\
-	  (unsigned long)&optprobe_template_entry) +	\
+	(((unsigned long)optprobe_template_end -	\
+	  (unsigned long)optprobe_template_entry) +	\
 	 MAX_OPTIMIZED_LENGTH + RELATIVEJUMP_SIZE)
 
 extern const int kretprobe_blacklist_size;
arch/x86/kernel/kprobes/opt.c +4 -4
index 22e65f0b8b34..0cae7c0f32ec 100644
@@ -142,11 +142,11 @@ void optprobe_template_func(void);
 STACK_FRAME_NON_STANDARD(optprobe_template_func);
 
 #define TMPL_MOVE_IDX \
-	((long)&optprobe_template_val - (long)&optprobe_template_entry)
+	((long)optprobe_template_val - (long)optprobe_template_entry)
 #define TMPL_CALL_IDX \
-	((long)&optprobe_template_call - (long)&optprobe_template_entry)
+	((long)optprobe_template_call - (long)optprobe_template_entry)
 #define TMPL_END_IDX \
-	((long)&optprobe_template_end - (long)&optprobe_template_entry)
+	((long)optprobe_template_end - (long)optprobe_template_entry)
 
 #define INT3_SIZE sizeof(kprobe_opcode_t)
 
@@ -371,7 +371,7 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
 	}
 
 	/* Copy arch-dep-instance from template */
-	memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
+	memcpy(buf, optprobe_template_entry, TMPL_END_IDX);
 
 	/* Copy instructions into the out-of-line buffer */
 	ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr,
#4 lore raw

[tip:perf/core] kprobes/x86: Remove addressof() operators

kprobes/x86: Remove addressof() operators
Commit-ID:  a8976fc84b644e3b567ea2bafad3b53b21ed6b6c
Gitweb:     https://git.kernel.org/tip/a8976fc84b644e3b567ea2bafad3b53b21ed6b6c
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Fri, 18 Aug 2017 17:25:08 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 28 Sep 2017 09:23:03 +0200

kprobes/x86: Remove addressof() operators

The following commit:

54a7d50b9205 ("x86: mark kprobe templates as character arrays, not single characters")

changed optprobe_template_* to arrays, so we can remove the addressof() operators from those symbols.

---
 arch/x86/include/asm/kprobes.h | 4 ++--
 arch/x86/kernel/kprobes/opt.c  | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/include/asm/kprobes.h +2 -2
index 6cf6543..9f2e310 100644
@@ -58,8 +58,8 @@ extern __visible kprobe_opcode_t optprobe_template_call[];
 extern __visible kprobe_opcode_t optprobe_template_end[];
 #define MAX_OPTIMIZED_LENGTH (MAX_INSN_SIZE + RELATIVE_ADDR_SIZE)
 #define MAX_OPTINSN_SIZE 				\
-	(((unsigned long)&optprobe_template_end -	\
-	  (unsigned long)&optprobe_template_entry) +	\
+	(((unsigned long)optprobe_template_end -	\
+	  (unsigned long)optprobe_template_entry) +	\
 	 MAX_OPTIMIZED_LENGTH + RELATIVEJUMP_SIZE)
 
 extern const int kretprobe_blacklist_size;
arch/x86/kernel/kprobes/opt.c +4 -4
index 22e65f0..0cae7c0 100644
@@ -142,11 +142,11 @@ void optprobe_template_func(void);
 STACK_FRAME_NON_STANDARD(optprobe_template_func);
 
 #define TMPL_MOVE_IDX \
-	((long)&optprobe_template_val - (long)&optprobe_template_entry)
+	((long)optprobe_template_val - (long)optprobe_template_entry)
 #define TMPL_CALL_IDX \
-	((long)&optprobe_template_call - (long)&optprobe_template_entry)
+	((long)optprobe_template_call - (long)optprobe_template_entry)
 #define TMPL_END_IDX \
-	((long)&optprobe_template_end - (long)&optprobe_template_entry)
+	((long)optprobe_template_end - (long)optprobe_template_entry)
 
 #define INT3_SIZE sizeof(kprobe_opcode_t)
 
@@ -371,7 +371,7 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
 	}
 
 	/* Copy arch-dep-instance from template */
-	memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
+	memcpy(buf, optprobe_template_entry, TMPL_END_IDX);
 
 	/* Copy instructions into the out-of-line buffer */
 	ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr,
author maintainer
#5 lore raw
In reply to Masami Hiramatsu

Hi Ingo,

What would you think about fixing this way?
This makes the instruction buffer always RO and
poke it via text_poke.

Thank you,

Quoted 40 lines · On Fri, 18 Aug 2017 17:22:54 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi,

This series modifies how to handle RO insn buffer and cleans up addressof operators.

The 1st patch changes the RO insn buffer handling: instead of using set_memory_ro/rw to modify the buffer, it prepares new instructions in another buffer and write it with text_poke() as suggested by Ingo Molnar (Thanks!). Since the text_poke() is safely modifying code by mapping alias pages, it can write RO pages.
This also override alloc_insn_page() so that it returns ROX page directly.

The 2nd one is not changed. It is a cleanup patch to remove addressof operators ("&") since
it is meaningless anymore.

V3 has just a following update:
- [1/2] Not to just add set_memory_ro(), introduce new patch to change the way to handle RO pages.

Thanks,

---

Masami Hiramatsu (2):
kprobes/x86: Make insn buffer always ROX and use text_poke kprobes/x86: Remove addressof operators

 arch/x86/include/asm/kprobes.h   |    4 +-
 arch/x86/kernel/kprobes/common.h |    6 ++-
 arch/x86/kernel/kprobes/core.c   |   61 +++++++++++++++++++++------------
 arch/x86/kernel/kprobes/opt.c    |   71 +++++++++++++++++++++-----------------
 kernel/kprobes.c                 |    2 +
 5 files changed, 86 insertions(+), 58 deletions(-)

--
Masami Hiramatsu <[email protected]>

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#6 lore raw

Hello Ingo,

Ping?

Could you give me a comment? Since current code has an obvious bug, it should be fixed anyway.

Thank you,

Quoted 55 lines · On Mon, 28 Aug 2017 11:28:04 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Ingo,

What would you think about fixing this way?
This makes the instruction buffer always RO and
poke it via text_poke.

Thank you,

Quoted 40 lines · On Fri, 18 Aug 2017 17:22:54 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi,

This series modifies how to handle RO insn buffer and cleans up addressof operators.

The 1st patch changes the RO insn buffer handling: instead of using set_memory_ro/rw to modify the buffer, it prepares new instructions in another buffer and write it with text_poke() as suggested by Ingo Molnar (Thanks!). Since the text_poke() is safely modifying code by mapping alias pages, it can write RO pages.
This also override alloc_insn_page() so that it returns ROX page directly.

The 2nd one is not changed. It is a cleanup patch to remove addressof operators ("&") since
it is meaningless anymore.

V3 has just a following update:
- [1/2] Not to just add set_memory_ro(), introduce new patch to change the way to handle RO pages.

Thanks,

---

Masami Hiramatsu (2):
kprobes/x86: Make insn buffer always ROX and use text_poke kprobes/x86: Remove addressof operators

 arch/x86/include/asm/kprobes.h   |    4 +-
 arch/x86/kernel/kprobes/common.h |    6 ++-
 arch/x86/kernel/kprobes/core.c   |   61 +++++++++++++++++++++------------
 arch/x86/kernel/kprobes/opt.c    |   71 +++++++++++++++++++++-----------------
 kernel/kprobes.c                 |    2 +
 5 files changed, 86 insertions(+), 58 deletions(-)

--
Masami Hiramatsu <[email protected]>

Signature · 1 line
Masami Hiramatsu <[email protected]>
Signature · 1 line
Masami Hiramatsu <[email protected]>