arm64: Add support for STACKLEAK gcc plugin
authorLaura Abbott <labbott@redhat.com>
Fri, 20 Jul 2018 21:41:54 +0000 (14:41 -0700)
committerWill Deacon <will.deacon@arm.com>
Thu, 26 Jul 2018 10:36:34 +0000 (11:36 +0100)
This adds support for the STACKLEAK gcc plugin to arm64 by implementing
stackleak_check_alloca(), based heavily on the x86 version, and adding the
two helpers used by the stackleak common code: current_top_of_stack() and
on_thread_stack(). The stack erasure calls are made at syscall returns.
Additionally, this disables the plugin in hypervisor and EFI stub code,
which are out of scope for the protection.

Acked-by: Alexander Popov <alex.popov@linux.com>
Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Laura Abbott <labbott@redhat.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
arch/arm64/Kconfig
arch/arm64/include/asm/processor.h
arch/arm64/kernel/entry.S
arch/arm64/kernel/process.c
arch/arm64/kvm/hyp/Makefile
drivers/firmware/efi/libstub/Makefile

index 27fd50e..1494d5c 100644 (file)
@@ -108,6 +108,7 @@ config ARM64
        select HAVE_ARCH_MMAP_RND_BITS
        select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT
        select HAVE_ARCH_SECCOMP_FILTER
+       select HAVE_ARCH_STACKLEAK
        select HAVE_ARCH_THREAD_STRUCT_WHITELIST
        select HAVE_ARCH_TRACEHOOK
        select HAVE_ARCH_TRANSPARENT_HUGEPAGE
index e026121..79657ad 100644 (file)
@@ -266,5 +266,20 @@ extern void __init minsigstksz_setup(void);
 #define SVE_SET_VL(arg)        sve_set_current_vl(arg)
 #define SVE_GET_VL()   sve_get_current_vl()
 
+/*
+ * For CONFIG_GCC_PLUGIN_STACKLEAK
+ *
+ * These need to be macros because otherwise we get stuck in a nightmare
+ * of header definitions for the use of task_stack_page.
+ */
+
+#define current_top_of_stack()                                                 \
+({                                                                             \
+       struct stack_info _info;                                                \
+       BUG_ON(!on_accessible_stack(current, current_stack_pointer, &_info));   \
+       _info.high;                                                             \
+})
+#define on_thread_stack()      (on_task_stack(current, current_stack_pointer, NULL))
+
 #endif /* __ASSEMBLY__ */
 #endif /* __ASM_PROCESSOR_H */
index d1440f8..09dbea2 100644 (file)
@@ -902,6 +902,9 @@ ret_to_user:
        cbnz    x2, work_pending
 finish_ret_to_user:
        enable_step_tsk x1, x2
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+       bl      stackleak_erase
+#endif
        kernel_exit 0
 ENDPROC(ret_to_user)
 
index 740b31f..7f1628e 100644 (file)
@@ -493,3 +493,25 @@ void arch_setup_new_exec(void)
 {
        current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0;
 }
+
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+void __used stackleak_check_alloca(unsigned long size)
+{
+       unsigned long stack_left;
+       unsigned long current_sp = current_stack_pointer;
+       struct stack_info info;
+
+       BUG_ON(!on_accessible_stack(current, current_sp, &info));
+
+       stack_left = current_sp - info.low;
+
+       /*
+        * There's a good chance we're almost out of stack space if this
+        * is true. Using panic() over BUG() is more likely to give
+        * reliable debugging output.
+        */
+       if (size >= stack_left)
+               panic("alloca() over the kernel stack boundary\n");
+}
+EXPORT_SYMBOL(stackleak_check_alloca);
+#endif
index 4313f74..2fabc2d 100644 (file)
@@ -3,7 +3,8 @@
 # Makefile for Kernel-based Virtual Machine module, HYP part
 #
 
-ccflags-y += -fno-stack-protector -DDISABLE_BRANCH_PROFILING
+ccflags-y += -fno-stack-protector -DDISABLE_BRANCH_PROFILING \
+               $(DISABLE_STACKLEAK_PLUGIN)
 
 KVM=../../../../virt/kvm
 
index a34e929..25dd2a1 100644 (file)
@@ -20,7 +20,8 @@ cflags-$(CONFIG_EFI_ARMSTUB)  += -I$(srctree)/scripts/dtc/libfdt
 KBUILD_CFLAGS                  := $(cflags-y) -DDISABLE_BRANCH_PROFILING \
                                   -D__NO_FORTIFY \
                                   $(call cc-option,-ffreestanding) \
-                                  $(call cc-option,-fno-stack-protector)
+                                  $(call cc-option,-fno-stack-protector) \
+                                  $(DISABLE_STACKLEAK_PLUGIN)
 
 GCOV_PROFILE                   := n
 KASAN_SANITIZE                 := n