Skip to content

Commit 4461438

Browse files
jpoimboebp3tk0v
authored andcommitted
x86/retpoline: Ensure default return thunk isn't used at runtime
Make sure the default return thunk is not used after all return instructions have been patched by the alternatives because the default return thunk is insufficient when it comes to mitigating Retbleed or SRSO. Fix based on an earlier version by David Kaplan <david.kaplan@amd.com>. [ bp: Fix the compilation error of warn_thunk_thunk being an invisible symbol, hoist thunk macro into calling.h ] Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org> Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/r/20231010171020.462211-4-david.kaplan@amd.com Link: https://lore.kernel.org/r/20240104132446.GEZZaxnrIgIyat0pqf@fat_crate.local
1 parent 0911b8c commit 4461438

File tree

7 files changed

+85
-68
lines changed

7 files changed

+85
-68
lines changed

arch/x86/entry/calling.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,63 @@ For 32-bit we have the following conventions - kernel is built with
426426
.endm
427427

428428
#endif /* CONFIG_SMP */
429+
430+
#ifdef CONFIG_X86_64
431+
432+
/* rdi: arg1 ... normal C conventions. rax is saved/restored. */
433+
.macro THUNK name, func
434+
SYM_FUNC_START(\name)
435+
pushq %rbp
436+
movq %rsp, %rbp
437+
438+
pushq %rdi
439+
pushq %rsi
440+
pushq %rdx
441+
pushq %rcx
442+
pushq %rax
443+
pushq %r8
444+
pushq %r9
445+
pushq %r10
446+
pushq %r11
447+
448+
call \func
449+
450+
popq %r11
451+
popq %r10
452+
popq %r9
453+
popq %r8
454+
popq %rax
455+
popq %rcx
456+
popq %rdx
457+
popq %rsi
458+
popq %rdi
459+
popq %rbp
460+
RET
461+
SYM_FUNC_END(\name)
462+
_ASM_NOKPROBE(\name)
463+
.endm
464+
465+
#else /* CONFIG_X86_32 */
466+
467+
/* put return address in eax (arg1) */
468+
.macro THUNK name, func, put_ret_addr_in_eax=0
469+
SYM_CODE_START_NOALIGN(\name)
470+
pushl %eax
471+
pushl %ecx
472+
pushl %edx
473+
474+
.if \put_ret_addr_in_eax
475+
/* Place EIP in the arg1 */
476+
movl 3*4(%esp), %eax
477+
.endif
478+
479+
call \func
480+
popl %edx
481+
popl %ecx
482+
popl %eax
483+
RET
484+
_ASM_NOKPROBE(\name)
485+
SYM_CODE_END(\name)
486+
.endm
487+
488+
#endif

arch/x86/entry/entry.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <linux/linkage.h>
88
#include <asm/msr-index.h>
99

10+
#include "calling.h"
11+
1012
.pushsection .noinstr.text, "ax"
1113

1214
SYM_FUNC_START(entry_ibpb)
@@ -20,3 +22,5 @@ SYM_FUNC_END(entry_ibpb)
2022
EXPORT_SYMBOL_GPL(entry_ibpb);
2123

2224
.popsection
25+
26+
THUNK warn_thunk_thunk, __warn_thunk

arch/x86/entry/thunk_32.S

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,15 @@
44
* Copyright 2008 by Steven Rostedt, Red Hat, Inc
55
* (inspired by Andi Kleen's thunk_64.S)
66
*/
7-
#include <linux/export.h>
8-
#include <linux/linkage.h>
9-
#include <asm/asm.h>
107

11-
/* put return address in eax (arg1) */
12-
.macro THUNK name, func, put_ret_addr_in_eax=0
13-
SYM_CODE_START_NOALIGN(\name)
14-
pushl %eax
15-
pushl %ecx
16-
pushl %edx
8+
#include <linux/export.h>
9+
#include <linux/linkage.h>
10+
#include <asm/asm.h>
1711

18-
.if \put_ret_addr_in_eax
19-
/* Place EIP in the arg1 */
20-
movl 3*4(%esp), %eax
21-
.endif
12+
#include "calling.h"
2213

23-
call \func
24-
popl %edx
25-
popl %ecx
26-
popl %eax
27-
RET
28-
_ASM_NOKPROBE(\name)
29-
SYM_CODE_END(\name)
30-
.endm
31-
32-
THUNK preempt_schedule_thunk, preempt_schedule
33-
THUNK preempt_schedule_notrace_thunk, preempt_schedule_notrace
34-
EXPORT_SYMBOL(preempt_schedule_thunk)
35-
EXPORT_SYMBOL(preempt_schedule_notrace_thunk)
14+
THUNK preempt_schedule_thunk, preempt_schedule
15+
THUNK preempt_schedule_notrace_thunk, preempt_schedule_notrace
16+
EXPORT_SYMBOL(preempt_schedule_thunk)
17+
EXPORT_SYMBOL(preempt_schedule_notrace_thunk)
3618

arch/x86/entry/thunk_64.S

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,6 @@
99
#include "calling.h"
1010
#include <asm/asm.h>
1111

12-
/* rdi: arg1 ... normal C conventions. rax is saved/restored. */
13-
.macro THUNK name, func
14-
SYM_FUNC_START(\name)
15-
pushq %rbp
16-
movq %rsp, %rbp
17-
18-
pushq %rdi
19-
pushq %rsi
20-
pushq %rdx
21-
pushq %rcx
22-
pushq %rax
23-
pushq %r8
24-
pushq %r9
25-
pushq %r10
26-
pushq %r11
27-
28-
call \func
29-
30-
popq %r11
31-
popq %r10
32-
popq %r9
33-
popq %r8
34-
popq %rax
35-
popq %rcx
36-
popq %rdx
37-
popq %rsi
38-
popq %rdi
39-
popq %rbp
40-
RET
41-
SYM_FUNC_END(\name)
42-
_ASM_NOKPROBE(\name)
43-
.endm
44-
4512
THUNK preempt_schedule_thunk, preempt_schedule
4613
THUNK preempt_schedule_notrace_thunk, preempt_schedule_notrace
4714
EXPORT_SYMBOL(preempt_schedule_thunk)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ extern void entry_ibpb(void);
357357

358358
extern void (*x86_return_thunk)(void);
359359

360+
extern void __warn_thunk(void);
361+
360362
#ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING
361363
extern void call_depth_return_thunk(void);
362364

arch/x86/kernel/cpu/bugs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,3 +2850,8 @@ ssize_t cpu_show_gds(struct device *dev, struct device_attribute *attr, char *bu
28502850
return cpu_show_common(dev, attr, buf, X86_BUG_GDS);
28512851
}
28522852
#endif
2853+
2854+
void __warn_thunk(void)
2855+
{
2856+
WARN_ONCE(1, "Unpatched return thunk in use. This should not happen!\n");
2857+
}

arch/x86/lib/retpoline.S

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,19 +369,16 @@ SYM_FUNC_END(call_depth_return_thunk)
369369
* 'JMP __x86_return_thunk' sites are changed to something else by
370370
* apply_returns().
371371
*
372-
* This should be converted eventually to call a warning function which
373-
* should scream loudly when the default return thunk is called after
374-
* alternatives have been applied.
375-
*
376-
* That warning function cannot BUG() because the bug splat cannot be
377-
* displayed in all possible configurations, leading to users not really
378-
* knowing why the machine froze.
372+
* The ALTERNATIVE below adds a really loud warning to catch the case
373+
* where the insufficient default return thunk ends up getting used for
374+
* whatever reason like miscompilation or failure of
375+
* objtool/alternatives/etc to patch all the return sites.
379376
*/
380377
SYM_CODE_START(__x86_return_thunk)
381378
UNWIND_HINT_FUNC
382379
ANNOTATE_NOENDBR
383-
ANNOTATE_UNRET_SAFE
384-
ret
380+
ALTERNATIVE __stringify(ANNOTATE_UNRET_SAFE; ret), \
381+
"jmp warn_thunk_thunk", X86_FEATURE_ALWAYS
385382
int3
386383
SYM_CODE_END(__x86_return_thunk)
387384
EXPORT_SYMBOL(__x86_return_thunk)

0 commit comments

Comments
 (0)