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