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 36 SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM 37 UNWIND_HINT_UNDEFINED 38 ANNOTATE_NOENDBR 39 40 ALTERNATIVE_2 __stringify(RETPOLINE \r 41 __stringify(lfence; ANNO 42 __stringify(ANNOTATE_RET 43 44 .endm 45 46 /* 47 * Despite being an assembler file we can't ju 48 * because __KSYM_DEPS__ only uses the C prepr 49 * only see one instance of "__x86_indirect_th 50 * than one per register with the correct name 51 * the simple and nasty way... 52 * 53 * Worse, you can only have a single EXPORT_SY 54 * and CPP can't insert newlines, so we have t 55 * at least twice. 56 */ 57 58 #define __EXPORT_THUNK(sym) _ASM_NOKPROBE( 59 60 .align RETPOLINE_THUNK_SIZE 61 SYM_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 68 SYM_CODE_END(__x86_indirect_thunk_array) 69 70 #define GEN(reg) __EXPORT_THUNK(__x86_indirect 71 #include <asm/GEN-for-each-reg.h> 72 #undef GEN 73 74 #ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING 75 .macro CALL_THUNK reg 76 .align RETPOLINE_THUNK_SIZE 77 78 SYM_INNER_LABEL(__x86_indirect_call_thunk_\reg 79 UNWIND_HINT_UNDEFINED 80 ANNOTATE_NOENDBR 81 82 CALL_DEPTH_ACCOUNT 83 POLINE \reg 84 ANNOTATE_UNRET_SAFE 85 ret 86 int3 87 .endm 88 89 .align RETPOLINE_THUNK_SIZE 90 SYM_CODE_START(__x86_indirect_call_thunk_array 91 92 #define GEN(reg) CALL_THUNK reg 93 #include <asm/GEN-for-each-reg.h> 94 #undef GEN 95 96 .align RETPOLINE_THUNK_SIZE 97 SYM_CODE_END(__x86_indirect_call_thunk_array) 98 99 #define GEN(reg) __EXPORT_THUNK(__x86_indirect 100 #include <asm/GEN-for-each-reg.h> 101 #undef GEN 102 103 .macro JUMP_THUNK reg 104 .align RETPOLINE_THUNK_SIZE 105 106 SYM_INNER_LABEL(__x86_indirect_jump_thunk_\reg 107 UNWIND_HINT_UNDEFINED 108 ANNOTATE_NOENDBR 109 POLINE \reg 110 ANNOTATE_UNRET_SAFE 111 ret 112 int3 113 .endm 114 115 .align RETPOLINE_THUNK_SIZE 116 SYM_CODE_START(__x86_indirect_jump_thunk_array 117 118 #define GEN(reg) JUMP_THUNK reg 119 #include <asm/GEN-for-each-reg.h> 120 #undef GEN 121 122 .align RETPOLINE_THUNK_SIZE 123 SYM_CODE_END(__x86_indirect_jump_thunk_array) 124 125 #define GEN(reg) __EXPORT_THUNK(__x86_indirect 126 #include <asm/GEN-for-each-reg.h> 127 #undef GEN 128 #endif 129 130 #ifdef CONFIG_MITIGATION_RETHUNK 131 132 /* 133 * Be careful here: that label cannot really b 134 * some configurations and toolchains, the JMP 135 * compiler issues is either a short one or th 136 * relocations for same-section JMPs and that 137 * detection logic in apply_returns() and in o 138 */ 139 .section .text..__x86.return_thunk 140 141 #ifdef CONFIG_MITIGATION_SRSO 142 143 /* 144 * srso_alias_untrain_ret() and srso_alias_saf 145 * special addresses: 146 * 147 * - srso_alias_untrain_ret() is 2M aligned 148 * - srso_alias_safe_ret() is also in the same 149 * and 20 in its virtual address are set (whil 150 * srso_alias_untrain_ret() function are clear 151 * 152 * This guarantees that those two addresses wi 153 * target buffer of Zen3/4 generations, leadin 154 * poisoned entries at that BTB slot to get ev 155 * 156 * As a result, srso_alias_safe_ret() becomes 157 */ 158 .pushsection .text..__x86.rethunk_untr 159 SYM_CODE_START_NOALIGN(srso_alias_untrain_ret) 160 UNWIND_HINT_FUNC 161 ANNOTATE_NOENDBR 162 ASM_NOP2 163 lfence 164 jmp srso_alias_return_thunk 165 SYM_FUNC_END(srso_alias_untrain_ret) 166 __EXPORT_THUNK(srso_alias_untrain_ret) 167 .popsection 168 169 .pushsection .text..__x86.rethunk_safe 170 SYM_CODE_START_NOALIGN(srso_alias_safe_ret) 171 lea 8(%_ASM_SP), %_ASM_SP 172 UNWIND_HINT_FUNC 173 ANNOTATE_UNRET_SAFE 174 ret 175 int3 176 SYM_FUNC_END(srso_alias_safe_ret) 177 178 SYM_CODE_START_NOALIGN(srso_alias_return_thunk 179 UNWIND_HINT_FUNC 180 ANNOTATE_NOENDBR 181 call srso_alias_safe_ret 182 ud2 183 SYM_CODE_END(srso_alias_return_thunk) 184 .popsection 185 186 /* 187 * SRSO untraining sequence for Zen1/2, simila 188 * above. On kernel entry, srso_untrain_ret() 189 * 190 * movabs $0xccccc30824648d48,%rax 191 * 192 * and when the return thunk executes the inne 193 * later, it is a stack manipulation and a RET 194 * thus a "safe" one to use. 195 */ 196 .align 64 197 .skip 64 - (srso_safe_ret - srso_untra 198 SYM_CODE_START_LOCAL_NOALIGN(srso_untrain_ret) 199 ANNOTATE_NOENDBR 200 .byte 0x48, 0xb8 201 202 /* 203 * This forces the function return instruction 204 * (UD2 in srso_return_thunk() below). This R 205 * and execution will continue at the return s 206 * the stack. 207 */ 208 SYM_INNER_LABEL(srso_safe_ret, SYM_L_GLOBAL) 209 lea 8(%_ASM_SP), %_ASM_SP 210 ret 211 int3 212 int3 213 /* end of movabs */ 214 lfence 215 call srso_safe_ret 216 ud2 217 SYM_CODE_END(srso_safe_ret) 218 SYM_FUNC_END(srso_untrain_ret) 219 220 SYM_CODE_START(srso_return_thunk) 221 UNWIND_HINT_FUNC 222 ANNOTATE_NOENDBR 223 call srso_safe_ret 224 ud2 225 SYM_CODE_END(srso_return_thunk) 226 227 #define JMP_SRSO_UNTRAIN_RET "jmp srso_untrain 228 #else /* !CONFIG_MITIGATION_SRSO */ 229 /* Dummy for the alternative in CALL_UNTRAIN_R 230 SYM_CODE_START(srso_alias_untrain_ret) 231 ANNOTATE_UNRET_SAFE 232 ANNOTATE_NOENDBR 233 ret 234 int3 235 SYM_FUNC_END(srso_alias_untrain_ret) 236 __EXPORT_THUNK(srso_alias_untrain_ret) 237 #define JMP_SRSO_UNTRAIN_RET "ud2" 238 #endif /* CONFIG_MITIGATION_SRSO */ 239 240 #ifdef CONFIG_MITIGATION_UNRET_ENTRY 241 242 /* 243 * Some generic notes on the untraining sequen 244 * 245 * They are interchangeable when it comes to f 246 * RET predictions from the BTB. 247 * 248 * The SRSO Zen1/2 (MOVABS) untraining sequenc 249 * Retbleed sequence because the return sequen 250 * (srso_safe_ret()) is longer and the return 251 * (end before) the untraining sequence. There 252 * sequence must fully overlap the return sequ 253 * 254 * Regarding alignment - the instructions whic 255 * must all start at a cacheline boundary for 256 * is, instruction sequences starting at srso_ 257 * the respective instruction sequences at ret 258 * must start at a cacheline boundary. 259 */ 260 261 /* 262 * Safety details here pertain to the AMD Zen{ 263 * 1) The RET at retbleed_return_thunk must be 264 * alignment within the BTB. 265 * 2) The instruction at retbleed_untrain_ret 266 * end with, the 0xc3 byte of the RET. 267 * 3) STIBP must be enabled, or SMT disabled, 268 * from re-poisioning the BTB prediction. 269 */ 270 .align 64 271 .skip 64 - (retbleed_return_thunk - re 272 SYM_CODE_START_LOCAL_NOALIGN(retbleed_untrain_ 273 ANNOTATE_NOENDBR 274 /* 275 * As executed from retbleed_untrain_r 276 * 277 * TEST $0xcc, %bl 278 * LFENCE 279 * JMP retbleed_return_thunk 280 * 281 * Executing the TEST instruction has 282 * prediction (potentially attacker co 283 * retbleed_return_thunk + 1 isn't an 284 */ 285 .byte 0xf6 286 287 /* 288 * As executed from retbleed_return_th 289 * 290 * As part of the TEST above, RET is t 291 * 292 * We subsequently jump backwards and 293 * This creates a correct BTB predicti 294 * meantime we suffer Straight Line Sp 295 * no branch) which is halted by the I 296 * 297 * With SMT enabled and STIBP active, 298 * RET's prediction to a type of its c 299 * prediction due to competitive shari 300 * evicted, retbleed_return_thunk will 301 * which will be contained safely by t 302 */ 303 SYM_INNER_LABEL(retbleed_return_thunk, SYM_L_G 304 ret 305 int3 306 SYM_CODE_END(retbleed_return_thunk) 307 308 /* 309 * Ensure the TEST decoding / BTB inva 310 */ 311 lfence 312 313 /* 314 * Jump back and execute the RET in th 315 * INT3 is for SLS protection. 316 */ 317 jmp retbleed_return_thunk 318 int3 319 SYM_FUNC_END(retbleed_untrain_ret) 320 321 #define JMP_RETBLEED_UNTRAIN_RET "jmp retbleed 322 #else /* !CONFIG_MITIGATION_UNRET_ENTRY */ 323 #define JMP_RETBLEED_UNTRAIN_RET "ud2" 324 #endif /* CONFIG_MITIGATION_UNRET_ENTRY */ 325 326 #if defined(CONFIG_MITIGATION_UNRET_ENTRY) || 327 328 SYM_FUNC_START(entry_untrain_ret) 329 ALTERNATIVE JMP_RETBLEED_UNTRAIN_RET, 330 SYM_FUNC_END(entry_untrain_ret) 331 __EXPORT_THUNK(entry_untrain_ret) 332 333 #endif /* CONFIG_MITIGATION_UNRET_ENTRY || CON 334 335 #ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING 336 337 .align 64 338 SYM_FUNC_START(call_depth_return_thunk) 339 ANNOTATE_NOENDBR 340 /* 341 * Keep the hotpath in a 16byte I-fetc 342 * case. 343 */ 344 CALL_THUNKS_DEBUG_INC_RETS 345 shlq $5, PER_CPU_VAR(pcpu_hot + X86 346 jz 1f 347 ANNOTATE_UNRET_SAFE 348 ret 349 int3 350 1: 351 CALL_THUNKS_DEBUG_INC_STUFFS 352 .rept 16 353 ANNOTATE_INTRA_FUNCTION_CALL 354 call 2f 355 int3 356 2: 357 .endr 358 add $(8*16), %rsp 359 360 CREDIT_CALL_DEPTH 361 362 ANNOTATE_UNRET_SAFE 363 ret 364 int3 365 SYM_FUNC_END(call_depth_return_thunk) 366 367 #endif /* CONFIG_MITIGATION_CALL_DEPTH_TRACKIN 368 369 /* 370 * This function name is magical and is used b 371 * for the compiler to generate JMPs to it. 372 * 373 * This code is only used during kernel boot o 374 * 'JMP __x86_return_thunk' sites are changed 375 * apply_returns(). 376 * 377 * The ALTERNATIVE below adds a really loud wa 378 * where the insufficient default return thunk 379 * whatever reason like miscompilation or fail 380 * objtool/alternatives/etc to patch all the r 381 */ 382 SYM_CODE_START(__x86_return_thunk) 383 UNWIND_HINT_FUNC 384 ANNOTATE_NOENDBR 385 #if defined(CONFIG_MITIGATION_UNRET_ENTRY) || 386 defined(CONFIG_MITIGATION_SRSO) || \ 387 defined(CONFIG_MITIGATION_CALL_DEPTH_TRACK 388 ALTERNATIVE __stringify(ANNOTATE_UNRET 389 "jmp warn_thunk_thunk", X86 390 #else 391 ANNOTATE_UNRET_SAFE 392 ret 393 #endif 394 int3 395 SYM_CODE_END(__x86_return_thunk) 396 EXPORT_SYMBOL(__x86_return_thunk) 397 398 #endif /* CONFIG_MITIGATION_RETHUNK */
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.