Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp382522pxf; Thu, 8 Apr 2021 05:17:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzk4KwE/UN5AIWVyj+CD7iOU6dniLcD90CDP99K+bRgOKq42Ko+Hlw6fGzKfleDxaN7sx6P X-Received: by 2002:a05:6402:42c8:: with SMTP id i8mr11007711edc.386.1617884227540; Thu, 08 Apr 2021 05:17:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617884227; cv=none; d=google.com; s=arc-20160816; b=GpMsMoo1/4sr5XYTfWCKZmVvkV+N7ozpR0xJ1ynTKT6+25ie33SrWCmiYR8Lr2mEjA /fz8JJBviWj+0FTbb0bD2Ko79LVcUONeA03GYi/g5U1diPWlBjHAydDO5Ytv2B4jzaqA TkJarCClBoOg/Rhtg8beFXXhGr1BZE7cnnYIlmyyymfVAzc4ZfVcZIXULhEDzaQnpoRh jDskW9wqvEAx8WvIdBeml26yEB14VsFhHhrAyrwogtkOGSur0wV90+/VFKVSpMp4jDsL YTlbIEGEW43raD92hesWpcmIGxUomFeCnT2G1VD6zOOS5ABZL8dXxKp7Yz/6Nn553Fgq aYSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=mb3zMgvlnGxL7DKphWI0Z11QYg4DNvzY1OA8OqKJB9Q=; b=DoSyzkWZlDFHnY213ok5RyOYx5/m23lzG0rmChLsDHk+eHRsJabXSNKyFkRlYwJVmQ k4s8K5wt3TSqcoAx69P55UL15Ic94bL25VygfGsvemyLADfqw/8WiRK6H+mnoOfA1xa0 6T5/VDfZWJVgMndixLBqnSPw2J6za8lnevyGxsAfr5o+XyEP2po0gN0lvvNbR72fB1Q8 988mE+1Dwe6KUV0NfB5Tm+2GILDaIQctdyU0HZYrkWS1YYWl31CizT+E8QmpaWNEtyMQ pjVVF8yLQsYTupi3OZtbCcpk43K2u7EYZxrkdN7qQq60mfQcdIPMukWDIq5GdXwDfqdT rrUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hlu92RIm; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o10si15605609ejr.361.2021.04.08.05.16.44; Thu, 08 Apr 2021 05:17:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hlu92RIm; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231508AbhDHMN6 (ORCPT + 99 others); Thu, 8 Apr 2021 08:13:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229837AbhDHMNz (ORCPT ); Thu, 8 Apr 2021 08:13:55 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 626AFC061761; Thu, 8 Apr 2021 05:13:44 -0700 (PDT) Date: Thu, 08 Apr 2021 12:13:40 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1617884021; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mb3zMgvlnGxL7DKphWI0Z11QYg4DNvzY1OA8OqKJB9Q=; b=hlu92RImu4S4cLSFL6iPTQpxfzLiGJtT6rTBXIvg2qX0fPMXYrHWb6FMwtoiroo424/APy JONFR/DId3ihfeUODDoXnRPh3iT30zmjWL2gfyK7hSqC6cZNZFrI45VNzK0u9kiKC/dR5H sCAr8VgaMFqnjvLExlaWoEozPjddgCJOSonAeK/l9MpZjemrdkNwZdES5vCG5zS7Phb5/R WQ4DkWdPahKsOjnN/o6iMfK+BNxV6+7DYHgwHQAanYgvMP7O/Sd5t/JF7+iIGWogY9L48T o4TyBvJORVlLIb5S+o9+L2EPz/CoRupOmJHJIExac3gDWbJF33bnhxD+7RypTg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1617884021; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mb3zMgvlnGxL7DKphWI0Z11QYg4DNvzY1OA8OqKJB9Q=; b=/89g/qvTUencI3aUG4Ts+hVdTib74dAjEBxCJlvO1WEAgZe8dXWeQfNs1iKFN5MmukF71j simNHYCvnrm6tsDA== From: "tip-bot2 for Kees Cook" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/entry] arm64: entry: Enable random_kstack_offset support Cc: Kees Cook , Thomas Gleixner , Will Deacon , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20210401232347.2791257-6-keescook@chromium.org> References: <20210401232347.2791257-6-keescook@chromium.org> MIME-Version: 1.0 Message-ID: <161788402015.29796.16586093156076434452.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/entry branch of tip: Commit-ID: 70918779aec9bd01d16f4e6e800ffe423d196021 Gitweb: https://git.kernel.org/tip/70918779aec9bd01d16f4e6e800ffe423d196021 Author: Kees Cook AuthorDate: Thu, 01 Apr 2021 16:23:46 -07:00 Committer: Thomas Gleixner CommitterDate: Thu, 08 Apr 2021 14:12:19 +02:00 arm64: entry: Enable random_kstack_offset support Allow for a randomized stack offset on a per-syscall basis, with roughly 5 bits of entropy. (And include AAPCS rationale AAPCS thanks to Mark Rutland.) In order to avoid unconditional stack canaries on syscall entry (due to the use of alloca()), also disable stack protector to avoid triggering needless checks and slowing down the entry path. As there is no general way to control stack protector coverage with a function attribute[1], this must be disabled at the compilation unit level. This isn't a problem here, though, since stack protector was not triggered before: examining the resulting syscall.o, there are no changes in canary coverage (none before, none now). [1] a working __attribute__((no_stack_protector)) has been added to GCC and Clang but has not been released in any version yet: https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=346b302d09c1e6db56d9fe69048acb32fbb97845 https://reviews.llvm.org/rG4fbf84c1732fca596ad1d6e96015e19760eb8a9b Signed-off-by: Kees Cook Signed-off-by: Thomas Gleixner Acked-by: Will Deacon Link: https://lore.kernel.org/r/20210401232347.2791257-6-keescook@chromium.org --- arch/arm64/Kconfig | 1 + arch/arm64/kernel/Makefile | 5 +++++ arch/arm64/kernel/syscall.c | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index e4e1b65..4640d25 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -146,6 +146,7 @@ config ARM64 select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT select HAVE_ARCH_PFN_VALID select HAVE_ARCH_PREL32_RELOCATIONS + select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_STACKLEAK select HAVE_ARCH_THREAD_STRUCT_WHITELIST diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index ed65576..6cc9773 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -9,6 +9,11 @@ CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_insn.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) +# Remove stack protector to avoid triggering unneeded stack canary +# checks due to randomize_kstack_offset. +CFLAGS_REMOVE_syscall.o = -fstack-protector -fstack-protector-strong +CFLAGS_syscall.o += -fno-stack-protector + # Object file lists. obj-y := debug-monitors.o entry.o irq.o fpsimd.o \ entry-common.o entry-fpsimd.o process.o ptrace.o \ diff --git a/arch/arm64/kernel/syscall.c b/arch/arm64/kernel/syscall.c index b9cf12b..263d6c1 100644 --- a/arch/arm64/kernel/syscall.c +++ b/arch/arm64/kernel/syscall.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -43,6 +44,8 @@ static void invoke_syscall(struct pt_regs *regs, unsigned int scno, { long ret; + add_random_kstack_offset(); + if (scno < sc_nr) { syscall_fn_t syscall_fn; syscall_fn = syscall_table[array_index_nospec(scno, sc_nr)]; @@ -55,6 +58,19 @@ static void invoke_syscall(struct pt_regs *regs, unsigned int scno, ret = lower_32_bits(ret); regs->regs[0] = ret; + + /* + * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(), + * but not enough for arm64 stack utilization comfort. To keep + * reasonable stack head room, reduce the maximum offset to 9 bits. + * + * The actual entropy will be further reduced by the compiler when + * applying stack alignment constraints: the AAPCS mandates a + * 16-byte (i.e. 4-bit) aligned SP at function boundaries. + * + * The resulting 5 bits of entropy is seen in SP[8:4]. + */ + choose_random_kstack_offset(get_random_int() & 0x1FF); } static inline bool has_syscall_work(unsigned long flags)