x86: Add straight-line-speculation mitigation
authorPeter Zijlstra <peterz@infradead.org>
Sat, 4 Dec 2021 13:43:44 +0000 (14:43 +0100)
committerBorislav Petkov <bp@suse.de>
Thu, 9 Dec 2021 12:32:25 +0000 (13:32 +0100)
Make use of an upcoming GCC feature to mitigate
straight-line-speculation for x86:

  https://gcc.gnu.org/g:53a643f8568067d7700a9f2facc8ba39974973d3
  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102952
  https://bugs.llvm.org/show_bug.cgi?id=52323

It's built tested on x86_64-allyesconfig using GCC-12 and GCC-11.

Maintenance overhead of this should be fairly low due to objtool
validation.

Size overhead of all these additional int3 instructions comes to:

     text    data     bss     dec     hex filename
  22267751 6933356 2011368 31212475 1dc43bb defconfig-build/vmlinux
  22804126 6933356 1470696 31208178 1dc32f2 defconfig-build/vmlinux.sls

Or roughly 2.4% additional text.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lore.kernel.org/r/20211204134908.140103474@infradead.org
arch/x86/Kconfig
arch/x86/Makefile
arch/x86/include/asm/linkage.h
arch/x86/include/asm/static_call.h
arch/x86/kernel/ftrace.c
arch/x86/kernel/static_call.c
arch/x86/lib/memmove_64.S
arch/x86/lib/retpoline.S
scripts/Makefile.build
scripts/link-vmlinux.sh

index 7399327d1eff79da273ba40c65f5e4f64c48c2e2..dd13ba82ce7ab6c3df836b3219fa998833d22bb3 100644 (file)
@@ -472,6 +472,18 @@ config RETPOLINE
          branches. Requires a compiler with -mindirect-branch=thunk-extern
          support for full protection. The kernel may run slower.
 
+config CC_HAS_SLS
+       def_bool $(cc-option,-mharden-sls=all)
+
+config SLS
+       bool "Mitigate Straight-Line-Speculation"
+       depends on CC_HAS_SLS && X86_64
+       default n
+       help
+         Compile the kernel with straight-line-speculation options to guard
+         against straight line speculation. The kernel image might be slightly
+         larger.
+
 config X86_CPU_RESCTRL
        bool "x86 CPU resource control support"
        depends on X86 && (CPU_SUP_INTEL || CPU_SUP_AMD)
index c38b6577c103da44f775a40a69506ae409c30c3f..e84cdd409b6462cc84b7e8d6ec1b13e2187d94a4 100644 (file)
@@ -191,6 +191,10 @@ ifdef CONFIG_RETPOLINE
   endif
 endif
 
+ifdef CONFIG_SLS
+  KBUILD_CFLAGS += -mharden-sls=all
+endif
+
 KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE)
 
 ifdef CONFIG_LTO_CLANG
index ebddec2f3ba86de5264ce2be379031dca83087bc..030907922bd07e49182563b3b89b92fac5c8f7dc 100644 (file)
 #define __ALIGN_STR    __stringify(__ALIGN)
 #endif
 
+#ifdef CONFIG_SLS
+#define RET    ret; int3
+#else
+#define RET    ret
+#endif
+
 #else /* __ASSEMBLY__ */
 
+#ifdef CONFIG_SLS
+#define ASM_RET        "ret; int3\n\t"
+#else
 #define ASM_RET        "ret\n\t"
+#endif
 
 #endif /* __ASSEMBLY__ */
 
index 39ebe05118691dcafbad974393b9559291cd3d5d..ed4f8bb6c2d9c293f2ca39c9917669afcef83f77 100644 (file)
@@ -36,7 +36,7 @@
        __ARCH_DEFINE_STATIC_CALL_TRAMP(name, ".byte 0xe9; .long " #func " - (. + 4)")
 
 #define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)                       \
-       __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "ret; nop; nop; nop; nop")
+       __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "ret; int3; nop; nop; nop")
 
 
 #define ARCH_ADD_TRAMP_KEY(name)                                       \
index c39f906cdc4ea3c21dc367ac603696f7e415daee..7cc540e6de0c33f0a2c988e5c1a99d12f784c037 100644 (file)
@@ -303,7 +303,7 @@ union ftrace_op_code_union {
        } __attribute__((packed));
 };
 
-#define RET_SIZE               1
+#define RET_SIZE               1 + IS_ENABLED(CONFIG_SLS)
 
 static unsigned long
 create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
index 9c407a33a77413466ea928648a29c83a250013c5..531fb4cbb63fd1d1e29ef2cb80d4b27eb0b8ed0c 100644 (file)
@@ -17,6 +17,8 @@ enum insn_type {
  */
 static const u8 xor5rax[] = { 0x66, 0x66, 0x48, 0x31, 0xc0 };
 
+static const u8 retinsn[] = { RET_INSN_OPCODE, 0xcc, 0xcc, 0xcc, 0xcc };
+
 static void __ref __static_call_transform(void *insn, enum insn_type type, void *func)
 {
        const void *emulate = NULL;
@@ -42,8 +44,7 @@ static void __ref __static_call_transform(void *insn, enum insn_type type, void
                break;
 
        case RET:
-               code = text_gen_insn(RET_INSN_OPCODE, insn, func);
-               size = RET_INSN_SIZE;
+               code = &retinsn;
                break;
        }
 
index e84d649620c43c6204b0d5fcf05bf7e20b06ed7a..50ea390df7128d6b6cd063e1b6f1839600349537 100644 (file)
@@ -40,7 +40,7 @@ SYM_FUNC_START(__memmove)
        /* FSRM implies ERMS => no length checks, do the copy directly */
 .Lmemmove_begin_forward:
        ALTERNATIVE "cmp $0x20, %rdx; jb 1f", "", X86_FEATURE_FSRM
-       ALTERNATIVE "", "movq %rdx, %rcx; rep movsb; RET", X86_FEATURE_ERMS
+       ALTERNATIVE "", __stringify(movq %rdx, %rcx; rep movsb; RET), X86_FEATURE_ERMS
 
        /*
         * movsq instruction have many startup latency
index a842866062c8f321b77719538415c0e856c79fa6..89b3fb244e159e63a1c510e23adc8a8219233282 100644 (file)
@@ -34,7 +34,7 @@ SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM_L_GLOBAL)
 
        ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), \
                      __stringify(RETPOLINE \reg), X86_FEATURE_RETPOLINE, \
-                     __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), X86_FEATURE_RETPOLINE_AMD
+                     __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg; int3), X86_FEATURE_RETPOLINE_AMD
 
 .endm
 
index 78656b527fe59a2b944b29ce808d8842f3c87bea..a4b89b7572871fdbb8f74212a94a3cf72202be98 100644 (file)
@@ -234,7 +234,8 @@ objtool_args =                                                              \
        $(if $(CONFIG_GCOV_KERNEL)$(CONFIG_LTO_CLANG), --no-unreachable)\
        $(if $(CONFIG_RETPOLINE), --retpoline)                          \
        $(if $(CONFIG_X86_SMAP), --uaccess)                             \
-       $(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount)
+       $(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount)             \
+       $(if $(CONFIG_SLS), --sls)
 
 cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool_args) $@)
 cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
index 5cdd9bc5c3852b8972774259a8aba06e7a52ce3c..9716f285e404c17656fbfcc462957881f9e09895 100755 (executable)
@@ -139,6 +139,9 @@ objtool_link()
                if [ -n "${CONFIG_X86_SMAP}" ]; then
                        objtoolopt="${objtoolopt} --uaccess"
                fi
+               if [ -n "${CONFIG_SLS}" ]; then
+                       objtoolopt="${objtoolopt} --sls"
+               fi
                info OBJTOOL ${1}
                tools/objtool/objtool ${objtoolcmd} ${objtoolopt} ${1}
        fi