Skip to content

Commit 343f3e3

Browse files
committed
x86: Add magic AMD return-thunk
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2090231 CVE: CVE-2022-23816 CVE: CVE-2022-23825 CVE: CVE-2022-29900 CVE: CVE-2022-29901 Conflicts: 1) Merge conflict in the add_jump_destinations() hunk of tools/objtool/check.c due to missing upstream commit 34c861e ("objtool: Fix sibling call detection in alternatives") and commit 26ff604 ("objtool: Don't set 'jump_dest' for sibling calls"). 2) Merge conflicts in arch/x86/entry/entry_64.S and arch/x86/entry/entry_64_compat.S due to missing upstream commit e8d61bd ("x86/ibt,sev: Annotations"). 3) Merge conflict in the __x86_return_thunk hunk of arch/x86/lib/retpoline.S due to missing upstream commit c8c301a ("x86/ibt: Add ANNOTATE_NOENDBR"). commit a149180 Author: Peter Zijlstra <peterz@infradead.org> Date: Tue, 14 Jun 2022 23:15:48 +0200 x86: Add magic AMD return-thunk Note: needs to be in a section distinct from Retpolines such that the Retpoline RET substitution cannot possibly use immediate jumps. ORC unwinding for zen_untrain_ret() and __x86_return_thunk() is a little tricky but works due to the fact that zen_untrain_ret() doesn't have any stack ops and as such will emit a single ORC entry at the start (+0x3f). Meanwhile, unwinding an IP, including the __x86_return_thunk() one (+0x40) will search for the largest ORC entry smaller or equal to the IP, these will find the one ORC entry (+0x3f) and all works. [ Alexandre: SVM part. ] [ bp: Build fix, massages. ] Suggested-by: Andrew Cooper <Andrew.Cooper3@citrix.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Josh Poimboeuf <jpoimboe@kernel.org> Signed-off-by: Borislav Petkov <bp@suse.de> Signed-off-by: Waiman Long <longman@redhat.com>
1 parent ade6c94 commit 343f3e3

File tree

9 files changed

+127
-8
lines changed

9 files changed

+127
-8
lines changed

arch/x86/entry/entry_64.S

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ SYM_CODE_START(entry_SYSCALL_64)
9595
movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
9696

9797
SYM_INNER_LABEL(entry_SYSCALL_64_safe_stack, SYM_L_GLOBAL)
98+
UNTRAIN_RET
9899

99100
/* Construct struct pt_regs on stack */
100101
pushq $__USER_DS /* pt_regs->ss */
@@ -699,6 +700,7 @@ native_irq_return_ldt:
699700
pushq %rdi /* Stash user RDI */
700701
swapgs /* to kernel GS */
701702
SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */
703+
UNTRAIN_RET
702704

703705
movq PER_CPU_VAR(espfix_waddr), %rdi
704706
movq %rax, (0*8)(%rdi) /* user RAX */
@@ -894,6 +896,7 @@ SYM_CODE_START_LOCAL(paranoid_entry)
894896
* be retrieved from a kernel internal table.
895897
*/
896898
SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
899+
UNTRAIN_RET
897900

898901
/*
899902
* Handling GSBASE depends on the availability of FSGSBASE.
@@ -1003,6 +1006,7 @@ SYM_CODE_START_LOCAL(error_entry)
10031006
FENCE_SWAPGS_USER_ENTRY
10041007
/* We have user CR3. Change to kernel CR3. */
10051008
SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
1009+
UNTRAIN_RET
10061010

10071011
leaq 8(%rsp), %rdi /* arg0 = pt_regs pointer */
10081012
.Lerror_entry_from_usermode_after_swapgs:
@@ -1055,6 +1059,7 @@ SYM_CODE_START_LOCAL(error_entry)
10551059
SWAPGS
10561060
FENCE_SWAPGS_USER_ENTRY
10571061
SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
1062+
UNTRAIN_RET
10581063

10591064
/*
10601065
* Pretend that the exception came from user mode: set up pt_regs
@@ -1150,6 +1155,7 @@ SYM_CODE_START(asm_exc_nmi)
11501155
movq %rsp, %rdx
11511156
movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
11521157
UNWIND_HINT_IRET_REGS base=%rdx offset=8
1158+
UNTRAIN_RET
11531159
pushq 5*8(%rdx) /* pt_regs->ss */
11541160
pushq 4*8(%rdx) /* pt_regs->rsp */
11551161
pushq 3*8(%rdx) /* pt_regs->flags */

arch/x86/entry/entry_64_compat.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <asm/irqflags.h>
1515
#include <asm/asm.h>
1616
#include <asm/smap.h>
17+
#include <asm/nospec-branch.h>
1718
#include <linux/linkage.h>
1819
#include <linux/err.h>
1920

@@ -72,6 +73,7 @@ SYM_CODE_START(entry_SYSENTER_compat)
7273
pushq $__USER32_CS /* pt_regs->cs */
7374
pushq $0 /* pt_regs->ip = 0 (placeholder) */
7475
SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)
76+
UNTRAIN_RET
7577

7678
/*
7779
* User tracing code (ptrace or signal handlers) might assume that
@@ -189,6 +191,7 @@ SYM_CODE_START(entry_SYSCALL_compat)
189191
movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
190192

191193
SYM_INNER_LABEL(entry_SYSCALL_compat_safe_stack, SYM_L_GLOBAL)
194+
UNTRAIN_RET
192195

193196
/* Construct struct pt_regs on stack */
194197
pushq $__USER32_DS /* pt_regs->ss */
@@ -326,6 +329,7 @@ SYM_CODE_START(entry_INT80_compat)
326329
pushq 0*8(%rax) /* regs->orig_ax */
327330
.Lint80_keep_stack:
328331

332+
UNTRAIN_RET
329333
PUSH_AND_CLEAR_REGS rax=$-ENOSYS
330334
UNWIND_HINT_REGS
331335

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
#define X86_FEATURE_RETPOLINE (11*32+12) /* "" Generic Retpoline mitigation for Spectre variant 2 */
303303
#define X86_FEATURE_RETPOLINE_LFENCE (11*32+13) /* "" Use LFENCE for Spectre variant 2 */
304304
#define X86_FEATURE_RETHUNK (11*32+14) /* "" Use REturn THUNK */
305+
#define X86_FEATURE_UNRET (11*32+15) /* "" AMD BTB untrain return */
305306

306307
/* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */
307308
#define X86_FEATURE_AVX_VNNI (12*32+ 4) /* AVX VNNI instructions */

arch/x86/include/asm/disabled-features.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
#else
6262
# define DISABLE_RETPOLINE ((1 << (X86_FEATURE_RETPOLINE & 31)) | \
6363
(1 << (X86_FEATURE_RETPOLINE_LFENCE & 31)) | \
64-
(1 << (X86_FEATURE_RETHUNK & 31)))
64+
(1 << (X86_FEATURE_RETHUNK & 31)) | \
65+
(1 << (X86_FEATURE_UNRET & 31)))
6566
#endif
6667

6768
#ifdef CONFIG_INTEL_IOMMU_SVM

arch/x86/include/asm/nospec-branch.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@
112112
#endif
113113
.endm
114114

115+
/*
116+
* Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
117+
* return thunk isn't mapped into the userspace tables (then again, AMD
118+
* typically has NO_MELTDOWN).
119+
*
120+
* Doesn't clobber any registers but does require a stable stack.
121+
*
122+
* As such, this must be placed after every *SWITCH_TO_KERNEL_CR3 at a point
123+
* where we have a stack but before any RET instruction.
124+
*/
125+
.macro UNTRAIN_RET
126+
#ifdef CONFIG_RETPOLINE
127+
ALTERNATIVE "", "call zen_untrain_ret", X86_FEATURE_UNRET
128+
#endif
129+
.endm
130+
115131
#else /* __ASSEMBLY__ */
116132

117133
#define ANNOTATE_RETPOLINE_SAFE \
@@ -124,6 +140,7 @@ typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
124140
extern retpoline_thunk_t __x86_indirect_thunk_array[];
125141

126142
extern void __x86_return_thunk(void);
143+
extern void zen_untrain_ret(void);
127144

128145
#ifdef CONFIG_RETPOLINE
129146

arch/x86/kernel/vmlinux.lds.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ SECTIONS
142142

143143
#ifdef CONFIG_RETPOLINE
144144
__indirect_thunk_start = .;
145-
*(.text.__x86.indirect_thunk)
145+
*(.text.__x86.*)
146146
__indirect_thunk_end = .;
147147
#endif
148148
} :text =0xcccc

arch/x86/kvm/svm/vmenter.S

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ SYM_FUNC_START(__svm_vcpu_run)
110110
mov %r15, VCPU_R15(%_ASM_AX)
111111
#endif
112112

113+
/*
114+
* Mitigate RETBleed for AMD/Hygon Zen uarch. RET should be
115+
* untrained as soon as we exit the VM and are back to the
116+
* kernel. This should be done before re-enabling interrupts
117+
* because interrupt handlers won't sanitize 'ret' if the return is
118+
* from the kernel.
119+
*/
120+
UNTRAIN_RET
121+
113122
/*
114123
* Clear all general purpose registers except RSP and RAX to prevent
115124
* speculative use of the guest's values, even those that are reloaded
@@ -190,6 +199,15 @@ SYM_FUNC_START(__svm_sev_es_vcpu_run)
190199
FILL_RETURN_BUFFER %_ASM_AX, RSB_CLEAR_LOOPS, X86_FEATURE_RETPOLINE
191200
#endif
192201

202+
/*
203+
* Mitigate RETBleed for AMD/Hygon Zen uarch. RET should be
204+
* untrained as soon as we exit the VM and are back to the
205+
* kernel. This should be done before re-enabling interrupts
206+
* because interrupt handlers won't sanitize RET if the return is
207+
* from the kernel.
208+
*/
209+
UNTRAIN_RET
210+
193211
pop %_ASM_BX
194212

195213
#ifdef CONFIG_X86_64

arch/x86/lib/retpoline.S

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,67 @@ SYM_CODE_END(__x86_indirect_thunk_array)
7272
* This function name is magical and is used by -mfunction-return=thunk-extern
7373
* for the compiler to generate JMPs to it.
7474
*/
75-
SYM_CODE_START(__x86_return_thunk)
76-
UNWIND_HINT_EMPTY
75+
.section .text.__x86.return_thunk
76+
77+
/*
78+
* Safety details here pertain to the AMD Zen{1,2} microarchitecture:
79+
* 1) The RET at __x86_return_thunk must be on a 64 byte boundary, for
80+
* alignment within the BTB.
81+
* 2) The instruction at zen_untrain_ret must contain, and not
82+
* end with, the 0xc3 byte of the RET.
83+
* 3) STIBP must be enabled, or SMT disabled, to prevent the sibling thread
84+
* from re-poisioning the BTB prediction.
85+
*/
86+
.align 64
87+
.skip 63, 0xcc
88+
SYM_FUNC_START_NOALIGN(zen_untrain_ret);
89+
90+
/*
91+
* As executed from zen_untrain_ret, this is:
92+
*
93+
* TEST $0xcc, %bl
94+
* LFENCE
95+
* JMP __x86_return_thunk
96+
*
97+
* Executing the TEST instruction has a side effect of evicting any BTB
98+
* prediction (potentially attacker controlled) attached to the RET, as
99+
* __x86_return_thunk + 1 isn't an instruction boundary at the moment.
100+
*/
101+
.byte 0xf6
102+
103+
/*
104+
* As executed from __x86_return_thunk, this is a plain RET.
105+
*
106+
* As part of the TEST above, RET is the ModRM byte, and INT3 the imm8.
107+
*
108+
* We subsequently jump backwards and architecturally execute the RET.
109+
* This creates a correct BTB prediction (type=ret), but in the
110+
* meantime we suffer Straight Line Speculation (because the type was
111+
* no branch) which is halted by the INT3.
112+
*
113+
* With SMT enabled and STIBP active, a sibling thread cannot poison
114+
* RET's prediction to a type of its choice, but can evict the
115+
* prediction due to competitive sharing. If the prediction is
116+
* evicted, __x86_return_thunk will suffer Straight Line Speculation
117+
* which will be contained safely by the INT3.
118+
*/
119+
SYM_INNER_LABEL(__x86_return_thunk, SYM_L_GLOBAL)
77120
ret
78121
int3
79122
SYM_CODE_END(__x86_return_thunk)
80123

81-
__EXPORT_THUNK(__x86_return_thunk)
124+
/*
125+
* Ensure the TEST decoding / BTB invalidation is complete.
126+
*/
127+
lfence
128+
129+
/*
130+
* Jump back and execute the RET in the middle of the TEST instruction.
131+
* INT3 is for SLS protection.
132+
*/
133+
jmp __x86_return_thunk
134+
int3
135+
SYM_FUNC_END(zen_untrain_ret)
136+
__EXPORT_THUNK(zen_untrain_ret)
137+
138+
EXPORT_SYMBOL(__x86_return_thunk)

tools/objtool/check.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ static void add_retpoline_call(struct objtool_file *file, struct instruction *in
12221222
annotate_call_site(file, insn, false);
12231223
}
12241224

1225-
static void add_return_call(struct objtool_file *file, struct instruction *insn)
1225+
static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add)
12261226
{
12271227
/*
12281228
* Return thunk tail calls are really just returns in disguise,
@@ -1231,7 +1231,8 @@ static void add_return_call(struct objtool_file *file, struct instruction *insn)
12311231
insn->type = INSN_RETURN;
12321232
insn->retpoline_safe = true;
12331233

1234-
list_add_tail(&insn->call_node, &file->return_thunk_list);
1234+
if (add)
1235+
list_add_tail(&insn->call_node, &file->return_thunk_list);
12351236
}
12361237

12371238
/*
@@ -1259,7 +1260,7 @@ static int add_jump_destinations(struct objtool_file *file)
12591260
add_retpoline_call(file, insn);
12601261
continue;
12611262
} else if (reloc->sym->return_thunk) {
1262-
add_return_call(file, insn);
1263+
add_return_call(file, insn, true);
12631264
continue;
12641265
} else if (insn->func) {
12651266
/* internal or external sibling call (with reloc) */
@@ -1276,6 +1277,20 @@ static int add_jump_destinations(struct objtool_file *file)
12761277

12771278
insn->jump_dest = find_insn(file, dest_sec, dest_off);
12781279
if (!insn->jump_dest) {
1280+
struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off);
1281+
1282+
/*
1283+
* This is a special case for zen_untrain_ret().
1284+
* It jumps to __x86_return_thunk(), but objtool
1285+
* can't find the thunk's starting RET
1286+
* instruction, because the RET is also in the
1287+
* middle of another instruction. Objtool only
1288+
* knows about the outer instruction.
1289+
*/
1290+
if (sym && sym->return_thunk) {
1291+
add_return_call(file, insn, false);
1292+
continue;
1293+
}
12791294

12801295
/*
12811296
* This is a special case where an alt instruction

0 commit comments

Comments
 (0)