Skip to content

Commit d8a7386

Browse files
author
Peter Zijlstra
committed
x86/optprobe: Fix OPTPROBE vs UACCESS
While looking at an objtool UACCESS warning, it suddenly occurred to me that it is entirely possible to have an OPTPROBE right in the middle of an UACCESS region. In this case we must of course clear FLAGS.AC while running the KPROBE. Luckily the trampoline already saves/restores [ER]FLAGS, so all we need to do is inject a CLAC. Unfortunately we cannot use ALTERNATIVE() in the trampoline text, so we have to frob that manually. Fixes: ca0bbc70f147 ("sched/x86_64: Don't save flags on context switch") Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Masami Hiramatsu <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent d1c9f7d commit d8a7386

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

arch/x86/include/asm/kprobes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef u8 kprobe_opcode_t;
3636

3737
/* optinsn template addresses */
3838
extern __visible kprobe_opcode_t optprobe_template_entry[];
39+
extern __visible kprobe_opcode_t optprobe_template_clac[];
3940
extern __visible kprobe_opcode_t optprobe_template_val[];
4041
extern __visible kprobe_opcode_t optprobe_template_call[];
4142
extern __visible kprobe_opcode_t optprobe_template_end[];

arch/x86/kernel/kprobes/opt.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsigned long addr)
7171
return (unsigned long)buf;
7272
}
7373

74+
static void synthesize_clac(kprobe_opcode_t *addr)
75+
{
76+
/*
77+
* Can't be static_cpu_has() due to how objtool treats this feature bit.
78+
* This isn't a fast path anyway.
79+
*/
80+
if (!boot_cpu_has(X86_FEATURE_SMAP))
81+
return;
82+
83+
/* Replace the NOP3 with CLAC */
84+
addr[0] = 0x0f;
85+
addr[1] = 0x01;
86+
addr[2] = 0xca;
87+
}
88+
7489
/* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
7590
static void synthesize_set_arg1(kprobe_opcode_t *addr, unsigned long val)
7691
{
@@ -92,6 +107,9 @@ asm (
92107
/* We don't bother saving the ss register */
93108
" pushq %rsp\n"
94109
" pushfq\n"
110+
".global optprobe_template_clac\n"
111+
"optprobe_template_clac:\n"
112+
ASM_NOP3
95113
SAVE_REGS_STRING
96114
" movq %rsp, %rsi\n"
97115
".global optprobe_template_val\n"
@@ -111,6 +129,9 @@ asm (
111129
#else /* CONFIG_X86_32 */
112130
" pushl %esp\n"
113131
" pushfl\n"
132+
".global optprobe_template_clac\n"
133+
"optprobe_template_clac:\n"
134+
ASM_NOP3
114135
SAVE_REGS_STRING
115136
" movl %esp, %edx\n"
116137
".global optprobe_template_val\n"
@@ -134,6 +155,8 @@ asm (
134155
void optprobe_template_func(void);
135156
STACK_FRAME_NON_STANDARD(optprobe_template_func);
136157

158+
#define TMPL_CLAC_IDX \
159+
((long)optprobe_template_clac - (long)optprobe_template_entry)
137160
#define TMPL_MOVE_IDX \
138161
((long)optprobe_template_val - (long)optprobe_template_entry)
139162
#define TMPL_CALL_IDX \
@@ -389,6 +412,8 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
389412
op->optinsn.size = ret;
390413
len = TMPL_END_IDX + op->optinsn.size;
391414

415+
synthesize_clac(buf + TMPL_CLAC_IDX);
416+
392417
/* Set probe information */
393418
synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
394419

0 commit comments

Comments
 (0)