Skip to content

Commit 5100004

Browse files
pa1guptagregkh
authored andcommitted
x86/its: Add support for ITS-safe return thunk
commit a75bf27 upstream. 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]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 16a7d5b commit 5100004

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
@@ -134,6 +134,20 @@ static __always_inline int x86_call_depth_emit_accounting(u8 **pprog,
134134
}
135135
#endif
136136

137+
#if defined(CONFIG_MITIGATION_RETHUNK) && defined(CONFIG_OBJTOOL)
138+
extern bool cpu_wants_rethunk(void);
139+
extern bool cpu_wants_rethunk_at(void *addr);
140+
#else
141+
static __always_inline bool cpu_wants_rethunk(void)
142+
{
143+
return false;
144+
}
145+
static __always_inline bool cpu_wants_rethunk_at(void *addr)
146+
{
147+
return false;
148+
}
149+
#endif
150+
137151
#ifdef CONFIG_SMP
138152
extern void alternatives_smp_module_add(struct module *mod, char *name,
139153
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
@@ -386,6 +386,12 @@ static inline void srso_return_thunk(void) {}
386386
static inline void srso_alias_return_thunk(void) {}
387387
#endif
388388

389+
#ifdef CONFIG_MITIGATION_ITS
390+
extern void its_return_thunk(void);
391+
#else
392+
static inline void its_return_thunk(void) {}
393+
#endif
394+
389395
extern void retbleed_return_thunk(void);
390396
extern void srso_return_thunk(void);
391397
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
@@ -809,6 +809,21 @@ void __init_or_module noinline apply_retpolines(s32 *start, s32 *end)
809809

810810
#ifdef CONFIG_MITIGATION_RETHUNK
811811

812+
bool cpu_wants_rethunk(void)
813+
{
814+
return cpu_feature_enabled(X86_FEATURE_RETHUNK);
815+
}
816+
817+
bool cpu_wants_rethunk_at(void *addr)
818+
{
819+
if (!cpu_feature_enabled(X86_FEATURE_RETHUNK))
820+
return false;
821+
if (x86_return_thunk != its_return_thunk)
822+
return true;
823+
824+
return !((unsigned long)addr & 0x20);
825+
}
826+
812827
/*
813828
* Rewrite the compiler generated return thunk tail-calls.
814829
*
@@ -825,7 +840,7 @@ static int patch_return(void *addr, struct insn *insn, u8 *bytes)
825840
int i = 0;
826841

827842
/* Patch the custom return thunks... */
828-
if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
843+
if (cpu_wants_rethunk_at(addr)) {
829844
i = JMP32_INSN_SIZE;
830845
__text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i);
831846
} else {
@@ -842,7 +857,7 @@ void __init_or_module noinline apply_returns(s32 *start, s32 *end)
842857
{
843858
s32 *s;
844859

845-
if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
860+
if (cpu_wants_rethunk())
846861
static_call_force_reinit();
847862

848863
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
@@ -536,4 +536,8 @@ INIT_PER_CPU(irq_stack_backing_store);
536536
. = ASSERT(__x86_indirect_its_thunk_array == __x86_indirect_its_thunk_rax, "Gap in ITS thunk array");
537537
#endif
538538

539+
#if defined(CONFIG_MITIGATION_ITS) && !defined(CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B)
540+
. = ASSERT(its_return_thunk & 0x20, "its_return_thunk not in second half of cacheline");
541+
#endif
542+
539543
#endif /* CONFIG_X86_64 */

arch/x86/lib/retpoline.S

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

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

397408
/*
398409
* 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
@@ -664,7 +664,7 @@ static void emit_return(u8 **pprog, u8 *ip)
664664
{
665665
u8 *prog = *pprog;
666666

667-
if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
667+
if (cpu_wants_rethunk()) {
668668
emit_jump(&prog, x86_return_thunk, ip);
669669
} else {
670670
EMIT1(0xC3); /* ret */

0 commit comments

Comments
 (0)