Skip to content

Commit a75bf27

Browse files
pa1guptahansendc
authored andcommitted
x86/its: Add support for ITS-safe return thunk
RETs in the lower half of cacheline may be affected by ITS bug, specifically when the RSB-underflows. Use ITS-safe return thunk for such RETs. RETs that are not patched: - RET in retpoline sequence does not need to be patched, because the sequence itself fills an RSB before RET. - RET in Call Depth Tracking (CDT) thunks __x86_indirect_{call|jump}_thunk and call_depth_return_thunk are not patched because CDT by design prevents RSB-underflow. - RETs in .init section are not reachable after init. - RETs that are explicitly marked safe with ANNOTATE_UNRET_SAFE. Signed-off-by: Pawan Gupta <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Reviewed-by: Josh Poimboeuf <[email protected]> Reviewed-by: Alexandre Chartre <[email protected]>
1 parent 8754e67 commit a75bf27

File tree

8 files changed

+57
-7
lines changed

8 files changed

+57
-7
lines changed

arch/x86/include/asm/alternative.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ static __always_inline int x86_call_depth_emit_accounting(u8 **pprog,
124124
}
125125
#endif
126126

127+
#if defined(CONFIG_MITIGATION_RETHUNK) && defined(CONFIG_OBJTOOL)
128+
extern bool cpu_wants_rethunk(void);
129+
extern bool cpu_wants_rethunk_at(void *addr);
130+
#else
131+
static __always_inline bool cpu_wants_rethunk(void)
132+
{
133+
return false;
134+
}
135+
static __always_inline bool cpu_wants_rethunk_at(void *addr)
136+
{
137+
return false;
138+
}
139+
#endif
140+
127141
#ifdef CONFIG_SMP
128142
extern void alternatives_smp_module_add(struct module *mod, char *name,
129143
void *locks, void *locks_end,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ static inline void srso_return_thunk(void) {}
367367
static inline void srso_alias_return_thunk(void) {}
368368
#endif
369369

370+
#ifdef CONFIG_MITIGATION_ITS
371+
extern void its_return_thunk(void);
372+
#else
373+
static inline void its_return_thunk(void) {}
374+
#endif
375+
370376
extern void retbleed_return_thunk(void);
371377
extern void srso_return_thunk(void);
372378
extern void srso_alias_return_thunk(void);

arch/x86/kernel/alternative.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,21 @@ void __init_or_module noinline apply_retpolines(s32 *start, s32 *end)
814814

815815
#ifdef CONFIG_MITIGATION_RETHUNK
816816

817+
bool cpu_wants_rethunk(void)
818+
{
819+
return cpu_feature_enabled(X86_FEATURE_RETHUNK);
820+
}
821+
822+
bool cpu_wants_rethunk_at(void *addr)
823+
{
824+
if (!cpu_feature_enabled(X86_FEATURE_RETHUNK))
825+
return false;
826+
if (x86_return_thunk != its_return_thunk)
827+
return true;
828+
829+
return !((unsigned long)addr & 0x20);
830+
}
831+
817832
/*
818833
* Rewrite the compiler generated return thunk tail-calls.
819834
*
@@ -830,7 +845,7 @@ static int patch_return(void *addr, struct insn *insn, u8 *bytes)
830845
int i = 0;
831846

832847
/* Patch the custom return thunks... */
833-
if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
848+
if (cpu_wants_rethunk_at(addr)) {
834849
i = JMP32_INSN_SIZE;
835850
__text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i);
836851
} else {
@@ -847,7 +862,7 @@ void __init_or_module noinline apply_returns(s32 *start, s32 *end)
847862
{
848863
s32 *s;
849864

850-
if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
865+
if (cpu_wants_rethunk())
851866
static_call_force_reinit();
852867

853868
for (s = start; s < end; s++) {

arch/x86/kernel/ftrace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
354354
goto fail;
355355

356356
ip = trampoline + size;
357-
if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
357+
if (cpu_wants_rethunk_at(ip))
358358
__text_gen_insn(ip, JMP32_INSN_OPCODE, ip, x86_return_thunk, JMP32_INSN_SIZE);
359359
else
360360
memcpy(ip, retq, sizeof(retq));

arch/x86/kernel/static_call.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void __ref __static_call_transform(void *insn, enum insn_type type,
8181
break;
8282

8383
case RET:
84-
if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
84+
if (cpu_wants_rethunk_at(insn))
8585
code = text_gen_insn(JMP32_INSN_OPCODE, insn, x86_return_thunk);
8686
else
8787
code = &retinsn;
@@ -90,7 +90,7 @@ static void __ref __static_call_transform(void *insn, enum insn_type type,
9090
case JCC:
9191
if (!func) {
9292
func = __static_call_return;
93-
if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
93+
if (cpu_wants_rethunk())
9494
func = x86_return_thunk;
9595
}
9696

arch/x86/kernel/vmlinux.lds.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ PROVIDE(__ref_stack_chk_guard = __stack_chk_guard);
503503
. = ASSERT(__x86_indirect_its_thunk_array == __x86_indirect_its_thunk_rax, "Gap in ITS thunk array");
504504
#endif
505505

506+
#if defined(CONFIG_MITIGATION_ITS) && !defined(CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B)
507+
. = ASSERT(its_return_thunk & 0x20, "its_return_thunk not in second half of cacheline");
508+
#endif
509+
506510
#endif /* CONFIG_X86_64 */
507511

508512
/*

arch/x86/lib/retpoline.S

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,18 @@ SYM_CODE_START(__x86_indirect_its_thunk_array)
393393
.align 64, 0xcc
394394
SYM_CODE_END(__x86_indirect_its_thunk_array)
395395

396-
#endif
396+
.align 64, 0xcc
397+
.skip 32, 0xcc
398+
SYM_CODE_START(its_return_thunk)
399+
UNWIND_HINT_FUNC
400+
ANNOTATE_NOENDBR
401+
ANNOTATE_UNRET_SAFE
402+
ret
403+
int3
404+
SYM_CODE_END(its_return_thunk)
405+
EXPORT_SYMBOL(its_return_thunk)
406+
407+
#endif /* CONFIG_MITIGATION_ITS */
397408

398409
/*
399410
* This function name is magical and is used by -mfunction-return=thunk-extern

arch/x86/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ static void emit_return(u8 **pprog, u8 *ip)
686686
{
687687
u8 *prog = *pprog;
688688

689-
if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
689+
if (cpu_wants_rethunk()) {
690690
emit_jump(&prog, x86_return_thunk, ip);
691691
} else {
692692
EMIT1(0xC3); /* ret */

0 commit comments

Comments
 (0)