From patchwork Fri Jun 29 19:05:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laura Abbott X-Patchwork-Id: 10497337 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 C4EB3601C7 for ; Fri, 29 Jun 2018 19:06:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B2FAF28B5F for ; Fri, 29 Jun 2018 19:06:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A51D429963; Fri, 29 Jun 2018 19:06:22 +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 3706428B5F for ; Fri, 29 Jun 2018 19:06:22 +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: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:In-Reply-To: References:List-Owner; bh=r0OixINTtq4bSKZBvX15o9X2lWZLogZFVbl8fThqjXE=; b=RyO 7EIMJ6byNLV/m+p6x5fBQ2ea83nfwA4o8Xc0WqjTk1N+6miKxGMAcFggoocFFkU+wEJX8cNZzZh/O 6qWt/xygGUWL58ODwAn0+rQtemcf34GZ3bdqIqMW4beoh1iqmD00aC3Q545DEBpMWdsatq2+MXS5b PP0Hs8d1y0LGtquWQzH1WXxFFkt3a1XFjyjMZZZf77HcdLxRdfhnnPzjl5gp/LcQa1HmgpvzUTM7d bTuO5tOUx885WrkFo129pkUrvdHHKHouxhdgnygP9JhBR40W5QXBE7xac1wlNyZK44X+KyuuNUPgC zee2DJfvr7tBjGir3HGFEV+J0CxVZdw==; 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 1fYyim-0004g1-10; Fri, 29 Jun 2018 19:06:16 +0000 Received: from mail-ot0-f194.google.com ([74.125.82.194]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fYyih-0004eL-FO for linux-arm-kernel@lists.infradead.org; Fri, 29 Jun 2018 19:06:13 +0000 Received: by mail-ot0-f194.google.com with SMTP id b10-v6so9060185otl.10 for ; Fri, 29 Jun 2018 12:06:00 -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; bh=mHqkxXYYNn2Qo2hekLTJrHe3Xgnc4LY07wqGxKOcq5Y=; b=ou6aPZv6dgBBmhCiPbvMtRAhvYU7hPeibeaQCzaUIk/AYV8u7P2b0UBa1DwNDj99ul j30SLwuiM7wm2wJQaRmssnFc+1XHwM//WH2A28XrtJyZv8B5sNFwB/jvxPzK+Z0EgtRB vPEGkjBKDcVT2ymStU7Y0qfkbanMba1cQRPl6VcsIuiLX0t0VIHZ5LIhguAkQ21ENm1n TRTn37UmTE9XLf968q0PBc6v+x4vwCSgdHYqZLOTX0HNgOlecMcypL7OB6/wlAYUsUHo VCQsYOgoMZlgn8kfYowK/bcQ8EgVFXDkQtetNMUarfadi1zhrXpTjtjKbs+QGuLTOrMY AyrQ== X-Gm-Message-State: APt69E2kOiQ2ld+k1AyCn9jc/0OfOt1VQNmxXHrhG7ix2BvsdAlMZqo9 plkS7xHKzx4e7oG+FhKcYdxilA== X-Google-Smtp-Source: AAOMgpcpCl3E6W1KnDl8TNSJMW8z76diqFvNgxKlaeqpk9oFfTqv8XD7fAoFeDoaqlpwMOHekawQxg== X-Received: by 2002:a9d:2a81:: with SMTP id e1-v6mr8648850otb.36.1530299160204; Fri, 29 Jun 2018 12:06:00 -0700 (PDT) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc::f0c1]) by smtp.gmail.com with ESMTPSA id n20-v6sm4758504otf.35.2018.06.29.12.05.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 29 Jun 2018 12:05:58 -0700 (PDT) From: Laura Abbott To: Alexander Popov , Kees Cook , Mark Rutland , Ard Biesheuvel Subject: [PATCH] arm64: Clear the stack Date: Fri, 29 Jun 2018 12:05:53 -0700 Message-Id: <20180629190553.7282-1-labbott@redhat.com> X-Mailer: git-send-email 2.17.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180629_120611_524430_FEF50070 X-CRM114-Status: GOOD ( 15.70 ) 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: Laura Abbott , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel-hardening@lists.openwall.com 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 --- Changes since last time: - Minor name change in entry.S - Converted to use the generic interfaces so there's minimal additions. - Added the fast syscall path. - Addition of on_thread_stack and current_top_of_stack - Disable stackleak on hyp per suggestion - Added a define for check_alloca. I'm still not sure about keeping it since the x86 version got reworked? I've mostly kept this as one patch with a minimal commit text. I can split it up and elaborate more before final merging. --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/processor.h | 9 +++++++++ arch/arm64/kernel/entry.S | 7 +++++++ arch/arm64/kernel/process.c | 16 ++++++++++++++++ arch/arm64/kvm/hyp/Makefile | 3 ++- drivers/firmware/efi/libstub/Makefile | 3 ++- include/linux/stackleak.h | 1 + scripts/Makefile.gcc-plugins | 5 ++++- 8 files changed, 42 insertions(+), 3 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index eb2cf4938f6d..b0221db95dc9 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -92,6 +92,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 767598932549..73914fd7e142 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -248,5 +248,14 @@ void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused); #define SVE_SET_VL(arg) sve_set_current_vl(arg) #define SVE_GET_VL() sve_get_current_vl() +/* + * For 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() (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 ec2ee720e33e..31c9da7d401e 100644 --- a/arch/arm64/kernel/entry.S +++ b/arch/arm64/kernel/entry.S @@ -401,6 +401,11 @@ tsk .req x28 // current thread_info .text + .macro stackleak_erase +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + bl stackleak_erase_kstack +#endif + .endm /* * Exception vectors. */ @@ -880,6 +885,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 @@ -906,6 +912,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 f08a2ed9db0d..9f0f135f8b66 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 diff --git a/include/linux/stackleak.h b/include/linux/stackleak.h index e2da99b3a191..00d62b302efb 100644 --- a/include/linux/stackleak.h +++ b/include/linux/stackleak.h @@ -5,6 +5,7 @@ #include #include +#include /* * Check that the poison value points to the unused hole in the * virtual memory map for your platform. diff --git a/scripts/Makefile.gcc-plugins b/scripts/Makefile.gcc-plugins index a535742a1c06..972ce4ca7f6a 100644 --- a/scripts/Makefile.gcc-plugins +++ b/scripts/Makefile.gcc-plugins @@ -37,11 +37,14 @@ ifdef CONFIG_GCC_PLUGINS gcc-plugin-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak_plugin.so gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) += -DSTACKLEAK_PLUGIN -fplugin-arg-stackleak_plugin-track-min-size=$(CONFIG_STACKLEAK_TRACK_MIN_SIZE) + ifdef CONFIG_GCC_PLUGIN_STACKLEAK + DISABLE_STACKLEAK_PLUGIN += -fplugin-arg-stackleak_plugin-disable + endif GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y)) export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR - export SANCOV_PLUGIN DISABLE_LATENT_ENTROPY_PLUGIN + export SANCOV_PLUGIN DISABLE_LATENT_ENTROPY_PLUGIN DISABLE_STACKLEAK_PLUGIN ifneq ($(PLUGINCC),) # SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication.