1/* SPDX-License-Identifier: GPL-2.0 */ 2 3#include <linux/export.h> 4#include <linux/stringify.h> 5#include <linux/linkage.h> 6#include <asm/dwarf2.h> 7#include <asm/cpufeatures.h> 8#include <asm/alternative.h> 9#include <asm/asm-offsets.h> 10#include <asm/nospec-branch.h> 11#include <asm/unwind_hints.h> 12#include <asm/percpu.h> 13#include <asm/frame.h> 14#include <asm/nops.h> 15 16 .section .text..__x86.indirect_thunk 17 18 19.macro POLINE reg 20 ANNOTATE_INTRA_FUNCTION_CALL 21 call .Ldo_rop_\@ 22 int3 23.Ldo_rop_\@: 24 mov %\reg, (%_ASM_SP) 25 UNWIND_HINT_FUNC 26.endm 27 28.macro RETPOLINE reg 29 POLINE \reg 30 RET 31.endm 32 33.macro THUNK reg 34 35 .align RETPOLINE_THUNK_SIZE 36SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM_L_GLOBAL) 37 UNWIND_HINT_UNDEFINED 38 ANNOTATE_NOENDBR 39 40 ALTERNATIVE_2 __stringify(RETPOLINE \reg), \ 41 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg; int3), X86_FEATURE_RETPOLINE_LFENCE, \ 42 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), ALT_NOT(X86_FEATURE_RETPOLINE) 43SYM_PIC_ALIAS(__x86_indirect_thunk_\reg) 44 45.endm 46 47/* 48 * Despite being an assembler file we can't just use .irp here 49 * because __KSYM_DEPS__ only uses the C preprocessor and would 50 * only see one instance of "__x86_indirect_thunk_\reg" rather 51 * than one per register with the correct names. So we do it 52 * the simple and nasty way... 53 * 54 * Worse, you can only have a single EXPORT_SYMBOL per line, 55 * and CPP can't insert newlines, so we have to repeat everything 56 * at least twice. 57 */ 58 59#define __EXPORT_THUNK(sym) _ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym) 60 61 .align RETPOLINE_THUNK_SIZE 62SYM_CODE_START(__x86_indirect_thunk_array) 63 64#define GEN(reg) THUNK reg 65#include <asm/GEN-for-each-reg.h> 66#undef GEN 67 68 .align RETPOLINE_THUNK_SIZE 69SYM_CODE_END(__x86_indirect_thunk_array) 70 71#define GEN(reg) __EXPORT_THUNK(__x86_indirect_thunk_ ## reg) 72#include <asm/GEN-for-each-reg.h> 73#undef GEN 74 75#ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING 76.macro CALL_THUNK reg 77 .align RETPOLINE_THUNK_SIZE 78 79SYM_INNER_LABEL(__x86_indirect_call_thunk_\reg, SYM_L_GLOBAL) 80 UNWIND_HINT_UNDEFINED 81 ANNOTATE_NOENDBR 82 83 CALL_DEPTH_ACCOUNT 84 POLINE \reg 85 ANNOTATE_UNRET_SAFE 86 ret 87 int3 88.endm 89 90 .align RETPOLINE_THUNK_SIZE 91SYM_CODE_START(__x86_indirect_call_thunk_array) 92 93#define GEN(reg) CALL_THUNK reg 94#include <asm/GEN-for-each-reg.h> 95#undef GEN 96 97 .align RETPOLINE_THUNK_SIZE 98SYM_CODE_END(__x86_indirect_call_thunk_array) 99 100#define GEN(reg) __EXPORT_THUNK(__x86_indirect_call_thunk_ ## reg) 101#include <asm/GEN-for-each-reg.h> 102#undef GEN 103 104.macro JUMP_THUNK reg 105 .align RETPOLINE_THUNK_SIZE 106 107SYM_INNER_LABEL(__x86_indirect_jump_thunk_\reg, SYM_L_GLOBAL) 108 UNWIND_HINT_UNDEFINED 109 ANNOTATE_NOENDBR 110 POLINE \reg 111 ANNOTATE_UNRET_SAFE 112 ret 113 int3 114.endm 115 116 .align RETPOLINE_THUNK_SIZE 117SYM_CODE_START(__x86_indirect_jump_thunk_array) 118 119#define GEN(reg) JUMP_THUNK reg 120#include <asm/GEN-for-each-reg.h> 121#undef GEN 122 123 .align RETPOLINE_THUNK_SIZE 124SYM_CODE_END(__x86_indirect_jump_thunk_array) 125 126#define GEN(reg) __EXPORT_THUNK(__x86_indirect_jump_thunk_ ## reg) 127#include <asm/GEN-for-each-reg.h> 128#undef GEN 129#endif 130 131#ifdef CONFIG_MITIGATION_RETHUNK 132 133/* 134 * Be careful here: that label cannot really be removed because in 135 * some configurations and toolchains, the JMP __x86_return_thunk the 136 * compiler issues is either a short one or the compiler doesn't use 137 * relocations for same-section JMPs and that breaks the returns 138 * detection logic in apply_returns() and in objtool. 139 */ 140 .section .text..__x86.return_thunk 141 142#ifdef CONFIG_MITIGATION_SRSO 143 144/* 145 * srso_alias_untrain_ret() and srso_alias_safe_ret() are placed at 146 * special addresses: 147 * 148 * - srso_alias_untrain_ret() is 2M aligned 149 * - srso_alias_safe_ret() is also in the same 2M page but bits 2, 8, 14 150 * and 20 in its virtual address are set (while those bits in the 151 * srso_alias_untrain_ret() function are cleared). 152 * 153 * This guarantees that those two addresses will alias in the branch 154 * target buffer of Zen3/4 generations, leading to any potential 155 * poisoned entries at that BTB slot to get evicted. 156 * 157 * As a result, srso_alias_safe_ret() becomes a safe return. 158 */ 159 .pushsection .text..__x86.rethunk_untrain 160SYM_CODE_START_NOALIGN(srso_alias_untrain_ret) 161 UNWIND_HINT_FUNC 162 ANNOTATE_NOENDBR 163 ASM_NOP2 164 lfence 165 jmp srso_alias_return_thunk 166SYM_FUNC_END(srso_alias_untrain_ret) 167__EXPORT_THUNK(srso_alias_untrain_ret) 168 .popsection 169 170 .pushsection .text..__x86.rethunk_safe 171SYM_CODE_START_NOALIGN(srso_alias_safe_ret) 172 lea 8(%_ASM_SP), %_ASM_SP 173 UNWIND_HINT_FUNC 174 ANNOTATE_UNRET_SAFE 175 ret 176 int3 177SYM_FUNC_END(srso_alias_safe_ret) 178 179SYM_CODE_START_NOALIGN(srso_alias_return_thunk) 180 UNWIND_HINT_FUNC 181 ANNOTATE_NOENDBR 182 call srso_alias_safe_ret 183 ud2 184SYM_CODE_END(srso_alias_return_thunk) 185 .popsection 186 187/* 188 * SRSO untraining sequence for Zen1/2, similar to retbleed_untrain_ret() 189 * above. On kernel entry, srso_untrain_ret() is executed which is a 190 * 191 * movabs $0xccccc30824648d48,%rax 192 * 193 * and when the return thunk executes the inner label srso_safe_ret() 194 * later, it is a stack manipulation and a RET which is mispredicted and 195 * thus a "safe" one to use. 196 */ 197 .align 64 198 .skip 64 - (srso_safe_ret - srso_untrain_ret), 0xcc 199SYM_CODE_START_LOCAL_NOALIGN(srso_untrain_ret) 200 ANNOTATE_NOENDBR 201 .byte 0x48, 0xb8 202 203/* 204 * This forces the function return instruction to speculate into a trap 205 * (UD2 in srso_return_thunk() below). This RET will then mispredict 206 * and execution will continue at the return site read from the top of 207 * the stack. 208 */ 209SYM_INNER_LABEL(srso_safe_ret, SYM_L_GLOBAL) 210 lea 8(%_ASM_SP), %_ASM_SP 211 ret 212 int3 213 int3 214 /* end of movabs */ 215 lfence 216 call srso_safe_ret 217 ud2 218SYM_CODE_END(srso_safe_ret) 219SYM_FUNC_END(srso_untrain_ret) 220 221SYM_CODE_START(srso_return_thunk) 222 UNWIND_HINT_FUNC 223 ANNOTATE_NOENDBR 224 call srso_safe_ret 225 ud2 226SYM_CODE_END(srso_return_thunk) 227 228#define JMP_SRSO_UNTRAIN_RET "jmp srso_untrain_ret" 229#else /* !CONFIG_MITIGATION_SRSO */ 230/* Dummy for the alternative in CALL_UNTRAIN_RET. */ 231SYM_CODE_START(srso_alias_untrain_ret) 232 ANNOTATE_UNRET_SAFE 233 ANNOTATE_NOENDBR 234 ret 235 int3 236SYM_FUNC_END(srso_alias_untrain_ret) 237__EXPORT_THUNK(srso_alias_untrain_ret) 238#define JMP_SRSO_UNTRAIN_RET "ud2" 239#endif /* CONFIG_MITIGATION_SRSO */ 240 241#ifdef CONFIG_MITIGATION_UNRET_ENTRY 242 243/* 244 * Some generic notes on the untraining sequences: 245 * 246 * They are interchangeable when it comes to flushing potentially wrong 247 * RET predictions from the BTB. 248 * 249 * The SRSO Zen1/2 (MOVABS) untraining sequence is longer than the 250 * Retbleed sequence because the return sequence done there 251 * (srso_safe_ret()) is longer and the return sequence must fully nest 252 * (end before) the untraining sequence. Therefore, the untraining 253 * sequence must fully overlap the return sequence. 254 * 255 * Regarding alignment - the instructions which need to be untrained, 256 * must all start at a cacheline boundary for Zen1/2 generations. That 257 * is, instruction sequences starting at srso_safe_ret() and 258 * the respective instruction sequences at retbleed_return_thunk() 259 * must start at a cacheline boundary. 260 */ 261 262/* 263 * Safety details here pertain to the AMD Zen{1,2} microarchitecture: 264 * 1) The RET at retbleed_return_thunk must be on a 64 byte boundary, for 265 * alignment within the BTB. 266 * 2) The instruction at retbleed_untrain_ret must contain, and not 267 * end with, the 0xc3 byte of the RET. 268 * 3) STIBP must be enabled, or SMT disabled, to prevent the sibling thread 269 * from re-poisioning the BTB prediction. 270 */ 271 .align 64 272 .skip 64 - (retbleed_return_thunk - retbleed_untrain_ret), 0xcc 273SYM_CODE_START_LOCAL_NOALIGN(retbleed_untrain_ret) 274 ANNOTATE_NOENDBR 275 /* 276 * As executed from retbleed_untrain_ret, this is: 277 * 278 * TEST $0xcc, %bl 279 * LFENCE 280 * JMP retbleed_return_thunk 281 * 282 * Executing the TEST instruction has a side effect of evicting any BTB 283 * prediction (potentially attacker controlled) attached to the RET, as 284 * retbleed_return_thunk + 1 isn't an instruction boundary at the moment. 285 */ 286 .byte 0xf6 287 288 /* 289 * As executed from retbleed_return_thunk, this is a plain RET. 290 * 291 * As part of the TEST above, RET is the ModRM byte, and INT3 the imm8. 292 * 293 * We subsequently jump backwards and architecturally execute the RET. 294 * This creates a correct BTB prediction (type=ret), but in the 295 * meantime we suffer Straight Line Speculation (because the type was 296 * no branch) which is halted by the INT3. 297 * 298 * With SMT enabled and STIBP active, a sibling thread cannot poison 299 * RET's prediction to a type of its choice, but can evict the 300 * prediction due to competitive sharing. If the prediction is 301 * evicted, retbleed_return_thunk will suffer Straight Line Speculation 302 * which will be contained safely by the INT3. 303 */ 304SYM_INNER_LABEL(retbleed_return_thunk, SYM_L_GLOBAL) 305 ret 306 int3 307SYM_CODE_END(retbleed_return_thunk) 308 309 /* 310 * Ensure the TEST decoding / BTB invalidation is complete. 311 */ 312 lfence 313 314 /* 315 * Jump back and execute the RET in the middle of the TEST instruction. 316 * INT3 is for SLS protection. 317 */ 318 jmp retbleed_return_thunk 319 int3 320SYM_FUNC_END(retbleed_untrain_ret) 321 322#define JMP_RETBLEED_UNTRAIN_RET "jmp retbleed_untrain_ret" 323#else /* !CONFIG_MITIGATION_UNRET_ENTRY */ 324#define JMP_RETBLEED_UNTRAIN_RET "ud2" 325#endif /* CONFIG_MITIGATION_UNRET_ENTRY */ 326 327#if defined(CONFIG_MITIGATION_UNRET_ENTRY) || defined(CONFIG_MITIGATION_SRSO) 328 329SYM_FUNC_START(entry_untrain_ret) 330 ANNOTATE_NOENDBR 331 ALTERNATIVE JMP_RETBLEED_UNTRAIN_RET, JMP_SRSO_UNTRAIN_RET, X86_FEATURE_SRSO 332SYM_FUNC_END(entry_untrain_ret) 333__EXPORT_THUNK(entry_untrain_ret) 334 335#endif /* CONFIG_MITIGATION_UNRET_ENTRY || CONFIG_MITIGATION_SRSO */ 336 337#ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING 338 339 .align 64 340SYM_FUNC_START(call_depth_return_thunk) 341 ANNOTATE_NOENDBR 342 /* 343 * Keep the hotpath in a 16byte I-fetch for the non-debug 344 * case. 345 */ 346 CALL_THUNKS_DEBUG_INC_RETS 347 shlq $5, PER_CPU_VAR(__x86_call_depth) 348 jz 1f 349 ANNOTATE_UNRET_SAFE 350 ret 351 int3 3521: 353 CALL_THUNKS_DEBUG_INC_STUFFS 354 .rept 16 355 ANNOTATE_INTRA_FUNCTION_CALL 356 call 2f 357 int3 3582: 359 .endr 360 add $(8*16), %rsp 361 362 CREDIT_CALL_DEPTH 363 364 ANNOTATE_UNRET_SAFE 365 ret 366 int3 367SYM_FUNC_END(call_depth_return_thunk) 368 369#endif /* CONFIG_MITIGATION_CALL_DEPTH_TRACKING */ 370 371#ifdef CONFIG_MITIGATION_ITS 372 373.macro ITS_THUNK reg 374 375/* 376 * If CFI paranoid is used then the ITS thunk starts with opcodes (0xea; jne 1b) 377 * that complete the fineibt_paranoid caller sequence. 378 */ 3791: .byte 0xea 380SYM_INNER_LABEL(__x86_indirect_paranoid_thunk_\reg, SYM_L_GLOBAL) 381 UNWIND_HINT_UNDEFINED 382 ANNOTATE_NOENDBR 383 jne 1b 384SYM_INNER_LABEL(__x86_indirect_its_thunk_\reg, SYM_L_GLOBAL) 385 UNWIND_HINT_UNDEFINED 386 ANNOTATE_NOENDBR 387 ANNOTATE_RETPOLINE_SAFE 388 jmp *%\reg 389 int3 390 .align 32, 0xcc /* fill to the end of the line */ 391 .skip 32 - (__x86_indirect_its_thunk_\reg - 1b), 0xcc /* skip to the next upper half */ 392.endm 393 394/* ITS mitigation requires thunks be aligned to upper half of cacheline */ 395.align 64, 0xcc 396.skip 29, 0xcc 397 398#define GEN(reg) ITS_THUNK reg 399#include <asm/GEN-for-each-reg.h> 400#undef GEN 401 402 .align 64, 0xcc 403SYM_FUNC_ALIAS(__x86_indirect_its_thunk_array, __x86_indirect_its_thunk_rax) 404SYM_CODE_END(__x86_indirect_its_thunk_array) 405 406.align 64, 0xcc 407.skip 32, 0xcc 408SYM_CODE_START(its_return_thunk) 409 UNWIND_HINT_FUNC 410 ANNOTATE_NOENDBR 411 ANNOTATE_UNRET_SAFE 412 ret 413 int3 414SYM_CODE_END(its_return_thunk) 415EXPORT_SYMBOL(its_return_thunk) 416 417#endif /* CONFIG_MITIGATION_ITS */ 418 419/* 420 * This function name is magical and is used by -mfunction-return=thunk-extern 421 * for the compiler to generate JMPs to it. 422 * 423 * This code is only used during kernel boot or module init. All 424 * 'JMP __x86_return_thunk' sites are changed to something else by 425 * apply_returns(). 426 * 427 * The ALTERNATIVE below adds a really loud warning to catch the case 428 * where the insufficient default return thunk ends up getting used for 429 * whatever reason like miscompilation or failure of 430 * objtool/alternatives/etc to patch all the return sites. 431 */ 432SYM_CODE_START(__x86_return_thunk) 433 UNWIND_HINT_FUNC 434 ANNOTATE_NOENDBR 435#if defined(CONFIG_MITIGATION_UNRET_ENTRY) || \ 436 defined(CONFIG_MITIGATION_SRSO) || \ 437 defined(CONFIG_MITIGATION_CALL_DEPTH_TRACKING) 438 ALTERNATIVE __stringify(ANNOTATE_UNRET_SAFE; ret), \ 439 "jmp warn_thunk_thunk", X86_FEATURE_ALWAYS 440#else 441 ANNOTATE_UNRET_SAFE 442 ret 443#endif 444 int3 445SYM_CODE_END(__x86_return_thunk) 446SYM_PIC_ALIAS(__x86_return_thunk) 447EXPORT_SYMBOL(__x86_return_thunk) 448 449#endif /* CONFIG_MITIGATION_RETHUNK */ 450