Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3332562imm; Fri, 20 Jul 2018 14:43:11 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdZMFWepwRTqnOeOi1Mk+HPnBEqSXfPVPDT0OST3NjAXgO+lOvXxARqv/RWLcz9+w1eqZIP X-Received: by 2002:a63:943:: with SMTP id 64-v6mr3473200pgj.368.1532122991048; Fri, 20 Jul 2018 14:43:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532122991; cv=none; d=google.com; s=arc-20160816; b=shmqiGJDztEvrFXfVaokiBup9L/iL1AQYlNPsqHztSt5oCkrz5ZZIfEDSPPRNWdIFJ nGEfTlK4bnX0jJVJQAMs2NxCpSQWCvy3BpEBV7dGd1TBy4RLCiCfGcgbl27QWjgpXtwi 4/MEa+ehb/mnGuDYFtB2U2ycNDnBGfoekX0dM1TD52cuzFxm8nKdexTyUUTtGhijgTFA /Z9aUDszLfrq8qmcHT/YbwtgJDE4Cn1QM82Am4r5i07Ak7a91/9TiusbuIrh+n9TEN83 DNuqVoH4/3b1i9PKKCX0T5spy+B/mh+Py1ULJREq+ZitNj67YLf7HMsjSl18ETjq/LsN Ykkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=/Ld8wjs7KxCcl4zSvDzg4dEg33msapg+r54ggPPmxJE=; b=bbDxlAqzhgRNXBcmkjzhk3ez2fEzANanU8PC/bKxFmzhyL66uJPtgjYfTPyOO5Fpp5 CZ+bqx0D72gq2mwDohVaHMMsSpewC/nq7MWx641k6OyUu/NgxKwcSodLcbyD8ACKBQOu bomyM2vNARqyaNDuKCnIl+MC4gWE4LyNeJkt5BwEF1buBnMEaAZD+0zMcmewZULHw4+C sPx7kbDF4trwlBEmWOTyI7ObZLymGPZF/qcqJUQyBImBwthbW/kPv+1s1a/UYsBFIPz7 vn1FiePwz5fIf51XAf8EV4mut94BrGInRQ959X4Jl97H/B9WEkiElBUZ97D0sXTVcdSj kpOg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l21-v6si2619526pgo.272.2018.07.20.14.42.56; Fri, 20 Jul 2018 14:43:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728857AbeGTWcN (ORCPT + 99 others); Fri, 20 Jul 2018 18:32:13 -0400 Received: from mail-qt0-f196.google.com ([209.85.216.196]:35514 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727749AbeGTWcM (ORCPT ); Fri, 20 Jul 2018 18:32:12 -0400 Received: by mail-qt0-f196.google.com with SMTP id a5-v6so11582701qtp.2 for ; Fri, 20 Jul 2018 14:42:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=/Ld8wjs7KxCcl4zSvDzg4dEg33msapg+r54ggPPmxJE=; b=EXGjTE3dv6v6+5VGRI8o8lIjRFY+SB5W8jqOUdi3z/UQK7K6Mt1Y36TceAGycpDSL9 T08eLATpSbTaC6+ktRlCt+1R1IvlX3UKYh3jpleUM8MmJQjt9H/4YuHmhg75tgX6jyj8 yYWpYP0GCNSsnlMvy75W82WGqRu1gpdpnEqXXMQoeCekekGCLp8OGbk1KiV+ZwrqeshK oIbTyngZ1v3eVWeFg+Egc/E3ch9rfXexPFkRvLuDUZDrM9wQU12GXZjkaCdua4OZNFbw T02fL/ZoMLObyrISzf/0DlQhHfM2sCtbS6DpNbtiSQD+aWtMKOJ+svRLB/j8ITpAKe+s MRhQ== X-Gm-Message-State: AOUpUlH39J8gXSd8cX605YEPZQjR+h8o4g6TXMox3kxj9TodRsgxdAOI ZcFn0f9r4iBfUXWGPylDI1Ioyw== X-Received: by 2002:ac8:42b:: with SMTP id v43-v6mr3455714qtg.319.1532122924952; Fri, 20 Jul 2018 14:42:04 -0700 (PDT) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc:4eb2:6dae:ab32:e5b0]) by smtp.gmail.com with ESMTPSA id r4-v6sm1617385qtm.10.2018.07.20.14.42.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 20 Jul 2018 14:42:03 -0700 (PDT) From: Laura Abbott To: Alexander Popov , Kees Cook , Mark Rutland , Ard Biesheuvel Cc: Laura Abbott , kernel-hardening@lists.openwall.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Will Deacon , Catalin Marinas Subject: [PATCHv3 2/2] arm64: Add support for STACKLEAK gcc plugin Date: Fri, 20 Jul 2018 14:41:54 -0700 Message-Id: <20180720214154.2940-3-labbott@redhat.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180720214154.2940-1-labbott@redhat.com> References: <20180720214154.2940-1-labbott@redhat.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Reviewed-by: Mark Rutland Reviewed-by: Kees Cook Signed-off-by: Laura Abbott --- v3: Actual commit text courtesy of Kees. A comment explaining why we panic --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/processor.h | 15 +++++++++++++++ arch/arm64/kernel/entry.S | 7 +++++++ arch/arm64/kernel/process.c | 22 ++++++++++++++++++++++ arch/arm64/kvm/hyp/Makefile | 3 ++- drivers/firmware/efi/libstub/Makefile | 3 ++- 6 files changed, 49 insertions(+), 2 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 42c090cf0292..216d36a49ab5 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -96,6 +96,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 diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index a73ae1e49200..0061450a793b 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -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 */ diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S index 28ad8799406f..67d12016063d 100644 --- a/arch/arm64/kernel/entry.S +++ b/arch/arm64/kernel/entry.S @@ -431,6 +431,11 @@ tsk .req x28 // current thread_info .text + .macro stackleak_erase +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + bl stackleak_erase +#endif + .endm /* * Exception vectors. */ @@ -910,6 +915,7 @@ ret_fast_syscall: and x2, x1, #_TIF_WORK_MASK cbnz x2, work_pending enable_step_tsk x1, x2 + stackleak_erase kernel_exit 0 ret_fast_syscall_trace: enable_daif @@ -936,6 +942,7 @@ ret_to_user: cbnz x2, work_pending finish_ret_to_user: enable_step_tsk x1, x2 + stackleak_erase kernel_exit 0 ENDPROC(ret_to_user) diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index e10bc363f533..f0ad00fb6a71 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -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 diff --git a/arch/arm64/kvm/hyp/Makefile b/arch/arm64/kvm/hyp/Makefile index 4313f7475333..2fabc2dc1966 100644 --- a/arch/arm64/kvm/hyp/Makefile +++ b/arch/arm64/kvm/hyp/Makefile @@ -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 diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index a34e9290a699..25dd2a14560d 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -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 -- 2.17.1