From patchwork Thu Jul 12 00:03:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10520707 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 50147603D7 for ; Thu, 12 Jul 2018 00:04:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 39E09286AE for ; Thu, 12 Jul 2018 00:04:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2D256293CA; Thu, 12 Jul 2018 00:04:00 +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 B8F1E286AE for ; Thu, 12 Jul 2018 00:03:59 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Subject:To:From :Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=LN8xCwOsQxju82ljG29kuEF3JoZhbBbohpZXPRkTWVI=; b=nl5bvU0W+GTOhZ WW+4iNJ3GfuCJFoX5IfQmMoXwkjvawVEL19nqf0jNkD1YLeWAgUoz4tVn8wAUxjsGY3F1uQKJdfYV d03KHlJpLIwAm2HRTFl4Qf5ampsKSUeua3WmTMSMgxAo6y1djiYThAg82Iyk0+AjqVb6TNOoe0301 M9C6k2I6g1z2sBkSXC9fnHm0oMDjtnPzn447nwTAV1o04iqGLFljniWuAUpvWfTMR2oFY2yKqcI2n fiZsQB5x12Esr5GSUv2lKP1kEKD3TLZ/do8MK9FDEdVVPyVJxTb97K9cZIiVdzoKoHu0AkSpvQrb2 kJICC2E66IQvn0ykIXJA==; 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 1fdP5O-0005QL-Va; Thu, 12 Jul 2018 00:03:54 +0000 Received: from mail-pl0-x243.google.com ([2607:f8b0:400e:c01::243]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fdP5M-0005PG-3J for linux-arm-kernel@lists.infradead.org; Thu, 12 Jul 2018 00:03:53 +0000 Received: by mail-pl0-x243.google.com with SMTP id w8-v6so9855856ply.8 for ; Wed, 11 Jul 2018 17:03:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=jih1a/l/LOpgoTURjBQgUE+HeOEANJI4RzwO5+5TF+A=; b=Y8BnAM4+9hyhvcrlhP3PnPJP1w6aMNonqFkbf1TFun/neUEkHyLwmRVdo1bbBISUva wRCCmpN9dUuWFuoLqNonQxBeuDaA+F1+WAAw/diHqZpk5d6WmcvAHWfL14BLBPvyp0OI U6fEeH5GNtm9+V3aa+qgBfuG7PXUzdrF8ysBw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=jih1a/l/LOpgoTURjBQgUE+HeOEANJI4RzwO5+5TF+A=; b=WoXV8L5rgxvgel8gFrl+a73vPbUW3sMuosC7ieazbf7ksVxSQk9H2lVT90ONPINgzG HBrRwntlEM9tETj78wjxkBfCm4G3GOXxUXT7qeR3owSlksv3g/9auz98b+/qoWhDaE66 El/s6J0CWbZWgiZxc+6+8CcrWTIDPaB17/91ASnTcqrq8t/ckLgigb1vKMB1awpG4PPL ZqeflYMw4OvpMaED71qM0UtLhdJBw7E40tJ8rQ5/5Ati8oil9LcpY+pxGdh5Mco1L5bq 029taV5X7nph7kguDP1bNXnrnGdtuibCQ+k/CrBx01ORqle4FK4NqY4GhLj81RRDP2p7 NNUQ== X-Gm-Message-State: AOUpUlGk+JIid1S//6SpRk4CnZPH+lLtnCoCyEvpGfpE1uQ64yzdTFZW v1wr/aTFwAg0mIV3ezQPe+p72w== X-Google-Smtp-Source: AAOMgpd12JTXx3x3HCSZz9JISOEjQlMAn6TOFSrAmCllgQSiT3XT66pxu94NXvOHW7hKyRXxN2ekjA== X-Received: by 2002:a17:902:b587:: with SMTP id a7-v6mr639096pls.225.1531353820623; Wed, 11 Jul 2018 17:03:40 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id z11-v6sm16927245pff.162.2018.07.11.17.03.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 11 Jul 2018 17:03:38 -0700 (PDT) Date: Wed, 11 Jul 2018 17:03:37 -0700 From: Kees Cook To: Will Deacon Subject: [PATCH] arm64: Add support for STACKLEAK gcc plugin Message-ID: <20180712000337.GA4022@beast> MIME-Version: 1.0 Content-Disposition: inline X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180711_170352_171005_74C78766 X-CRM114-Status: GOOD ( 17.56 ) 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: Mark Rutland , Kernel Hardening , Catalin Marinas , Ard Biesheuvel , LKML , Laura Abbott , linux-arm-kernel , Alexander Popov 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 From: Laura Abbott 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. Signed-off-by: Laura Abbott [kees: add cast to current_top_of_stack(), tweak commit log & comments] Signed-off-by: Kees Cook --- This is tweaked to be stand-alone from Alexander's series so it can land via the arm64 tree. (Alexander's v14 pulled one change out already, and I've lifted the last remaining: the newly needed include in stackleak.h) --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/processor.h | 10 ++++++++++ arch/arm64/kernel/entry.S | 7 +++++++ arch/arm64/kernel/process.c | 16 ++++++++++++++++ arch/arm64/kvm/hyp/Makefile | 3 ++- drivers/firmware/efi/libstub/Makefile | 3 ++- 6 files changed, 38 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..ca856bda2051 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -266,5 +266,15 @@ 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 the STACKLEAK gcc plugin. + * + * 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() ((unsigned long)task_stack_page(current) + \ + THREAD_SIZE) +#define on_thread_stack() (on_task_stack(current, current_stack_pointer)) + #endif /* __ASSEMBLY__ */ #endif /* __ASM_PROCESSOR_H */ diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S index 28ad8799406f..80bc93d971f7 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_kstack +#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..d99281b476b0 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -493,3 +493,19 @@ void arch_setup_new_exec(void) { current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0; } + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +#define MIN_STACK_LEFT 256 + +void __used stackleak_check_alloca(unsigned long size) +{ + unsigned long sp, stack_left; + + sp = current_stack_pointer; + + stack_left = sp & (THREAD_SIZE - 1); + BUG_ON(stack_left < MIN_STACK_LEFT || + size >= stack_left - MIN_STACK_LEFT); +} +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