Skip to content

Commit 45c23bf

Browse files
mhiramatAlexei Starovoitov
authored andcommitted
x86,kprobes: Fix optprobe trampoline to generate complete pt_regs
Currently the optprobe trampoline template code ganerate an almost complete pt_regs on-stack, everything except regs->ss. The 'regs->ss' points to the top of stack, which is not a valid segment decriptor. As same as the rethook does, complete the job by also pushing ss. Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/164826166027.2455864.14759128090648961900.stgit@devnote2
1 parent 0ef6f5c commit 45c23bf

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

  • arch/x86/kernel/kprobes

arch/x86/kernel/kprobes/opt.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ asm (
106106
".global optprobe_template_entry\n"
107107
"optprobe_template_entry:\n"
108108
#ifdef CONFIG_X86_64
109-
/* We don't bother saving the ss register */
109+
" pushq $" __stringify(__KERNEL_DS) "\n"
110+
/* Save the 'sp - 8', this will be fixed later. */
110111
" pushq %rsp\n"
111112
" pushfq\n"
112113
".global optprobe_template_clac\n"
@@ -121,14 +122,17 @@ asm (
121122
".global optprobe_template_call\n"
122123
"optprobe_template_call:\n"
123124
ASM_NOP5
124-
/* Move flags to rsp */
125+
/* Copy 'regs->flags' into 'regs->ss'. */
125126
" movq 18*8(%rsp), %rdx\n"
126-
" movq %rdx, 19*8(%rsp)\n"
127+
" movq %rdx, 20*8(%rsp)\n"
127128
RESTORE_REGS_STRING
128-
/* Skip flags entry */
129-
" addq $8, %rsp\n"
129+
/* Skip 'regs->flags' and 'regs->sp'. */
130+
" addq $16, %rsp\n"
131+
/* And pop flags register from 'regs->ss'. */
130132
" popfq\n"
131133
#else /* CONFIG_X86_32 */
134+
" pushl %ss\n"
135+
/* Save the 'sp - 4', this will be fixed later. */
132136
" pushl %esp\n"
133137
" pushfl\n"
134138
".global optprobe_template_clac\n"
@@ -142,12 +146,13 @@ asm (
142146
".global optprobe_template_call\n"
143147
"optprobe_template_call:\n"
144148
ASM_NOP5
145-
/* Move flags into esp */
149+
/* Copy 'regs->flags' into 'regs->ss'. */
146150
" movl 14*4(%esp), %edx\n"
147-
" movl %edx, 15*4(%esp)\n"
151+
" movl %edx, 16*4(%esp)\n"
148152
RESTORE_REGS_STRING
149-
/* Skip flags entry */
150-
" addl $4, %esp\n"
153+
/* Skip 'regs->flags' and 'regs->sp'. */
154+
" addl $8, %esp\n"
155+
/* And pop flags register from 'regs->ss'. */
151156
" popfl\n"
152157
#endif
153158
".global optprobe_template_end\n"
@@ -179,6 +184,8 @@ optimized_callback(struct optimized_kprobe *op, struct pt_regs *regs)
179184
kprobes_inc_nmissed_count(&op->kp);
180185
} else {
181186
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
187+
/* Adjust stack pointer */
188+
regs->sp += sizeof(long);
182189
/* Save skipped registers */
183190
regs->cs = __KERNEL_CS;
184191
#ifdef CONFIG_X86_32

0 commit comments

Comments
 (0)