kprobes/x86: Another way to make insn buffer RO and cleanup
Masami Hiramatsu
maintainerv3 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]>
Masami Hiramatsu
maintainerPATCH 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
@@ -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
@@ -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 ); }
tip-bot for Masami Hiramatsu
merge bot[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
@@ -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
@@ -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 ); }
Masami Hiramatsu
maintainerPATCH v3 2/2 kprobes/x86: Remove addressof operators
In reply to Masami HiramatsuSince 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
@@ -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
@@ -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,
tip-bot for Masami Hiramatsu
merge bot[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
@@ -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
@@ -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,
Masami Hiramatsu
maintainerHi 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 thetext_poke()is safely modifying code by mapping alias pages, it can write RO pages.
This also overridealloc_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 addset_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 usetext_pokekprobes/x86: Remove addressof operatorsarch/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]>
Masami Hiramatsu
maintainerHello 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 viatext_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 thetext_poke()is safely modifying code by mapping alias pages, it can write RO pages.
This also overridealloc_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 addset_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 usetext_pokekprobes/x86: Remove addressof operatorsarch/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]>