From patchwork Thu Jul 19 23:28:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laura Abbott X-Patchwork-Id: 10535701 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9082D600D0 for ; Thu, 19 Jul 2018 23:29:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8600B29AC6 for ; Thu, 19 Jul 2018 23:29:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 651C529B04; Thu, 19 Jul 2018 23:29:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 056CC29AC6 for ; Thu, 19 Jul 2018 23:29:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=rUIOZ03IiehCW0pfA8qd91pDsHmIcMWZi9bYTVMeDHw=; b=qwpslolZUoCDEbWL1tFnDtr6Oy UDcuvc1QfBGZ6IJWFkX3IfwrPMJdfT1BcHYkceFT34lcUNCXaS0n1VUYx4FPSSQdeGuuXtxsB/+OF F+PRTURBO7TBxCpkH3pT9ldbApk0+bH1yYt+DmX0vs+Ryl15OwgA59E33UOawnC4+9EddB6wNy78k CIWZZBeYYzRAt6BH2HcVJvlORVaktjkkog1J5lkjc5fVK6Iz8uSk3U3gnNwsZIuIagygvZTlZvf+O BD0roORCrlo9TixH2uU7xCab3i1cs5SQLQOA+qRjkGVberMbOZoElA9ZlJTgRPjxt/J09MxVwtQDf oWQ5z2Rg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fgIMg-0008Eh-Ao; Thu, 19 Jul 2018 23:29:42 +0000 Received: from mail-qt0-f195.google.com ([209.85.216.195]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fgILV-0006bE-BF for linux-arm-kernel@lists.infradead.org; Thu, 19 Jul 2018 23:28:35 +0000 Received: by mail-qt0-f195.google.com with SMTP id c15-v6so8806749qtp.0 for ; Thu, 19 Jul 2018 16:28:19 -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=gN8QL/Y11A7E6FhcHbLbCidBJaN3nqe2yD2uQB8Up3Q=; b=HqpUJRudjgmpdb5+bhasMVtNaX9+UGT7cNAZAxbcvaqX/pMMACJnx5vS4GIxR4n+D7 8ktjvQ2256y0N2CjbbDUuwzmkYXFW7hN8bn9MFX6sI1D64CilO0PUcDtuyfvTv31EaYO Dv1Mr1TnirBrJJB6bugjhD6GzaOJ4CQwAt+zxi9MhksysAZ4pyZCqND4AfW7r4OytcX/ 2/T6UaKidCFUZD/BhCvqGopQY1pGoKw1poUwt2mCC2N0RSZpfpM5YsBDqEhudFvA4/ZU Ra6pPyc0NqgpgLgeSjc3BCqR+JQM38YHeIzLD3xIefygacEhGN6sglJ5M6CT9FKulzVw U2fQ== X-Gm-Message-State: AOUpUlEgMX2FMHcWJ6TrV8Vii7TmYaFhjaCQ/0m1y1bYOcDR6lkDfkcJ pdp9fJgyDJoWB+CuWCzVbXT5SQ== X-Google-Smtp-Source: AAOMgpeENeJlWCbP9LStrn6WbJsxLuOQmabTiD2Wpd5UEKVOSqTSuEMDpYJcOE6qksHsR0zQRWMRbg== X-Received: by 2002:a0c:d8d8:: with SMTP id w24-v6mr7599521qvj.49.1532042898149; Thu, 19 Jul 2018 16:28:18 -0700 (PDT) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc:4eb2:6dae:ab32:e5b0]) by smtp.gmail.com with ESMTPSA id f70-v6sm258205qke.77.2018.07.19.16.28.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 19 Jul 2018 16:28:17 -0700 (PDT) From: Laura Abbott To: Alexander Popov , Kees Cook , Mark Rutland , Ard Biesheuvel Subject: [PATCHv2 2/2] arm64: Clear the stack Date: Thu, 19 Jul 2018 16:28:06 -0700 Message-Id: <20180719232806.3397-3-labbott@redhat.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180719232806.3397-1-labbott@redhat.com> References: <20180719232806.3397-1-labbott@redhat.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180719_162830_176003_0FCA610E X-CRM114-Status: GOOD ( 15.74 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kernel-hardening@lists.openwall.com, Catalin Marinas , Will Deacon , linux-kernel@vger.kernel.org, Laura Abbott , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Implementation of stackleak based heavily on the x86 version Signed-off-by: Laura Abbott Reviewed-by: Kees Cook Reviewed-by: Mark Rutlamd --- v2: Convert to adjusted on_acessible_stack APIs. Fixed alloca check to just panic. Dropped the extra include per Kees. I also didn't add the Reviewed-by since the APIs did change and I wanted another pass. --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/processor.h | 15 +++++++++++++++ arch/arm64/kernel/entry.S | 7 +++++++ arch/arm64/kernel/process.c | 17 +++++++++++++++++ arch/arm64/kvm/hyp/Makefile | 3 ++- drivers/firmware/efi/libstub/Makefile | 3 ++- 6 files changed, 44 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..2724e4d31b16 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -493,3 +493,20 @@ 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; + + 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