From patchwork Wed Feb 21 01:13:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laura Abbott X-Patchwork-Id: 10231143 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 8E8F060209 for ; Wed, 21 Feb 2018 01:14:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F91828A02 for ; Wed, 21 Feb 2018 01:14:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 73AAF28A05; Wed, 21 Feb 2018 01:14:52 +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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID 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 C935628A02 for ; Wed, 21 Feb 2018 01:14:49 +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=05y813dp5igS2XGf6eNOtJY0i+HRUgo0glA9Qv7dv3E=; b=jM+Owz0CJ3EIbuGWEKjbBwXfIg tIhoxxsh1LsnuaxyyJXaeTk1iv2qShrYPmQ9omSKSR4G3chzGMHqmz3XmPd5UHEzK3m1lg5Gszc3I pBx8T3SE1UYPutvfwz50Qq6SGBeHP7JAJ5Ab1A/MxpV8zAK8QlgCZ58AL5nisgUAxOy5Z2bO5P7+I NbdZPVM9RaD+NmKz3jispWB8hjOBo5qq5tKLhO1RQC3zO0KpEvu9Eflgfa3p2OSPbMCx37HZbxTiD 5jTg7qCEw0ARuXFGD5MxIi1ZaZ6sfgayUQP1oG8O4fJ1Vv3PwtPHpDUEnXyyeMv7a3V+5xPNtxGJ2 nkV70eyw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1eoIzX-00057J-Hf; Wed, 21 Feb 2018 01:14:39 +0000 Received: from mail-oi0-f65.google.com ([209.85.218.65]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1eoIyN-0004Oq-Uo for linux-arm-kernel@lists.infradead.org; Wed, 21 Feb 2018 01:13:29 +0000 Received: by mail-oi0-f65.google.com with SMTP id y4so27190oix.2 for ; Tue, 20 Feb 2018 17:13:17 -0800 (PST) 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=lVX409BQ8EYHSIz7eCEkfJMjQYGkylcLquYnJBNCYCc=; b=aLlBvZT2ybXtrNdF0mFrXQNjRLxAC14u7jrfeaRAL3DvmdO8SykdS42UZbZ8Q33O6S hLt/XtLL+KtvX2VRRzYJXmiTL8xF96AEH9Ity+VbcPEMPpm9UZbfYgLXvAdRwF5ToEkL s1DBZgVmmb8hLsTzWGvJ/8/7tyrHy7idCWr7QRVg/0YhdiFPXd/RzHdftsR2f8ILgAWr NzMLCn/heOkSPLqFxXXQAmMqxiI/6HNvnHaU2RJrAwEAMWzLW3juj2rNuBm4pRNH7y3a ehDbrrZ7DsUFNX0Urmk2q6F9CXbDuH/5KUm4jik3M33/68+GbTDO+DH37mFEm5JU5p4y /tZg== X-Gm-Message-State: APf1xPDTWZVMn6KAw2b8yaR+SGHO85/u53AqpVZEayXgaR1UVyiBfBk+ sGut3jJYjTcMp75E1b/N5ptOzw== X-Google-Smtp-Source: AH8x224UY59zyJ7xGNfj+WoEJMHGdcEgXWdfqAxy93Sa3OSDQ4zYnPIpz24en6763hglkL9qI7NwVg== X-Received: by 10.202.91.84 with SMTP id p81mr987243oib.232.1519175596878; Tue, 20 Feb 2018 17:13:16 -0800 (PST) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc::f21a]) by smtp.gmail.com with ESMTPSA id 125sm13664765oih.57.2018.02.20.17.13.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 20 Feb 2018 17:13:15 -0800 (PST) From: Laura Abbott To: Alexander Popov , Kees Cook , Mark Rutland , Ard Biesheuvel Subject: [PATCH 2/2] arm64: Clear the stack Date: Tue, 20 Feb 2018 17:13:03 -0800 Message-Id: <20180221011303.20392-3-labbott@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180221011303.20392-1-labbott@redhat.com> References: <20180221011303.20392-1-labbott@redhat.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180220_171328_046714_14729582 X-CRM114-Status: GOOD ( 21.03 ) 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 --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/processor.h | 6 ++ arch/arm64/kernel/asm-offsets.c | 3 + arch/arm64/kernel/entry.S | 108 ++++++++++++++++++++++++++++++++++ arch/arm64/kernel/process.c | 16 +++++ drivers/firmware/efi/libstub/Makefile | 3 +- scripts/Makefile.gcc-plugins | 5 +- 7 files changed, 140 insertions(+), 2 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 7381eeb7ef8e..dcadcae674a7 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 fce604e3e599..4b309101ac83 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -114,6 +114,12 @@ struct thread_struct { unsigned long fault_address; /* fault info */ unsigned long fault_code; /* ESR_EL1 value */ struct debug_info debug; /* debugging */ +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + unsigned long lowest_stack; +#ifdef CONFIG_STACKLEAK_METRICS + unsigned long prev_lowest_stack; +#endif +#endif }; /* diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c index 1303e04110cd..b5c6100e8b14 100644 --- a/arch/arm64/kernel/asm-offsets.c +++ b/arch/arm64/kernel/asm-offsets.c @@ -45,6 +45,9 @@ int main(void) DEFINE(TSK_TI_TTBR0, offsetof(struct task_struct, thread_info.ttbr0)); #endif DEFINE(TSK_STACK, offsetof(struct task_struct, stack)); +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + DEFINE(TSK_TI_LOWEST_STACK, offsetof(struct task_struct, thread.lowest_stack)); +#endif BLANK(); DEFINE(THREAD_CPU_CONTEXT, offsetof(struct task_struct, thread.cpu_context)); BLANK(); diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S index ec2ee720e33e..b909b436293a 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 erase_kstack +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + bl __erase_kstack +#endif + .endm /* * Exception vectors. */ @@ -901,6 +906,7 @@ work_pending: */ ret_to_user: disable_daif + erase_kstack ldr x1, [tsk, #TSK_TI_FLAGS] and x2, x1, #_TIF_WORK_MASK cbnz x2, work_pending @@ -1337,3 +1343,105 @@ alternative_else_nop_endif ENDPROC(__sdei_asm_handler) NOKPROBE(__sdei_asm_handler) #endif /* CONFIG_ARM_SDE_INTERFACE */ + +/* + * This is what the stack looks like + * + * +---+ <- task_stack_page(p) + THREAD_SIZE + * | | + * +---+ <- task_stack_page(p) + THREAD_START_SP + * | | + * | | + * +---+ <- task_pt_regs(p) + * | | + * | | + * | | <- current_sp + * ~~~~~ + * + * ~~~~~ + * | | <- lowest_stack + * | | + * | | + * +---+ <- task_stack_page(p) + * + * This function is desgned to poison the memory between the lowest_stack + * and the current stack pointer. After clearing the stack, the lowest + * stack is reset. + */ + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +ENTRY(__erase_kstack) + mov x10, x0 // save x0 for the fast path + + get_thread_info x0 + ldr x1, [x0, #TSK_TI_LOWEST_STACK] + + /* get the number of bytes to check for lowest stack */ + mov x3, x1 + and x3, x3, #THREAD_SIZE - 1 + lsr x3, x3, #3 + + /* generate addresses from the bottom of the stack */ + mov x4, sp + movn x2, #THREAD_SIZE - 1 + and x1, x4, x2 + + mov x2, #STACKLEAK_POISON + + mov x5, #0 +1: + /* + * As borrowed from the x86 logic, start from the lowest_stack + * and go to the bottom to find the poison value. + * The check of 16 is to hopefully avoid false positives. + */ + cbz x3, 4f + ldr x4, [x1, x3, lsl #3] + cmp x4, x2 + csinc x5, xzr, x5, ne + tbnz x5, #STACKLEAK_POISON_CHECK_DEPTH/4, 4f // found 16 poisons? + sub x3, x3, #1 + b 1b + +4: + /* total number of bytes to poison */ + add x5, x1, x3, lsl #3 + mov x4, sp + sub x8, x4, x5 + + cmp x8, #THREAD_SIZE // sanity check the range + b.lo 5f + ASM_BUG() + +5: + /* + * We may have hit a path where the stack did not get used, + * no need to do anything here + */ + cbz x8, 7f + + sub x8, x8, #1 // don't poison the current stack pointer + + lsr x8, x8, #3 + add x3, x3, x8 + + /* + * The logic of this loop ensures the last stack word isn't + * ovewritten. + */ +6: + cbz x8, 7f + str x2, [x1, x3, lsl #3] + sub x3, x3, #1 + sub x8, x8, #1 + b 6b + + /* Reset the lowest stack to the top of the stack */ +7: + mov x1, sp + str x1, [x0, #TSK_TI_LOWEST_STACK] + + mov x0, x10 + ret +ENDPROC(__erase_kstack) +#endif diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index ad8aeb098b31..fd0528db6772 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -357,6 +357,9 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start, p->thread.cpu_context.pc = (unsigned long)ret_from_fork; p->thread.cpu_context.sp = (unsigned long)childregs; +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + p->thread.lowest_stack = (unsigned long)task_stack_page(p); +#endif ptrace_hw_copy_thread(p); return 0; @@ -486,3 +489,16 @@ void arch_setup_new_exec(void) { current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0; } + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +void __used check_alloca(unsigned long size) +{ + unsigned long sp, stack_left; + + sp = current_stack_pointer; + + stack_left = sp & (THREAD_SIZE - 1); + BUG_ON(stack_left < 256 || size >= stack_left - 256); +} +EXPORT_SYMBOL(check_alloca); +#endif diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index 7b3ba40f0745..35ebbc1b17ff 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/scripts/Makefile.gcc-plugins b/scripts/Makefile.gcc-plugins index 8d6070fc538f..6cc0e35d3324 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.