Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp380722pxf; Thu, 8 Apr 2021 05:15:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDjsfcdcx2jTtMHypIVb6StDijhdT1QyK3392axCl31EcMREJxt6Du6LUjIL6ASvLoi3Ty X-Received: by 2002:a17:906:d284:: with SMTP id ay4mr10019372ejb.448.1617884106804; Thu, 08 Apr 2021 05:15:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617884106; cv=none; d=google.com; s=arc-20160816; b=aRc6EKdYptNFtQafAoJRqSA0CH2LlElJWV2t7qDXEyLE5Ikng1IfDiu+OJUfUt3V0m 69+7DqelmzZyWebqa8QG9qtZy/DxJ21B62+zhQd2BF3cIWM5/25LIFQZlNMFNPxmmNW7 fSvI4JAZq2JufZ6bpw/gkYFJKEa/Oe/fiROlnMNsHxRKDzjKaS7l45Td1jc05g/dau5s WMq3Bclr8hYJr1JcDXmbyNYCzUBVllOyvBBobXxa0TAioHmGuqxEe951IAUN8k9qHkwZ 3QTeWk+81cdUR9hqZ3al5sGpxWSh0hcwVj2SMjgYNfliXY8OX5oS4bNPnqwUifn64R0R sdCg== 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=olPAXa5CH+vfmVzU0F/dY3rs0n1BKBcsUFV7ULj+VBU=; b=ezhe5jtBMixPYQ1FzlyTYvyj5oLEkgvHeI9ojWnnSDjRWgw4arQ3MaEIpOJckxJNT2 1kZNasJ1AcJ55C1O0vcGgOgXlvOawc1zGDIy7F7YuSiQT3fLtZvIHUau47ewR6pcuuWX bIp3cFo3ZNr9ccUxMGDGeIQhSADLJeTk0JdwSJtr5uXKNqGoKgb0hs3zidIo4Az+VT3R V4U9+wJ/dksXki3q6zSX3gJn7+6B2ccW+IVIXZqI6XHchoQSUQZjOaJoWQNOr3Ioj1MO x6Hwp5NygVbshLUcvs5F4o6qSaQupKF+K0cgRL5bArhtttVqQTbw2S5OWwoq0GL/8BFI SZ8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=vG6qfS8+; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=tHl7j4FD; 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 z1si22401973edc.76.2021.04.08.05.14.43; Thu, 08 Apr 2021 05:15:06 -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=vG6qfS8+; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=tHl7j4FD; 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 S231516AbhDHMN5 (ORCPT + 99 others); Thu, 8 Apr 2021 08:13:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231481AbhDHMNz (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 59D7AC061760; 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=olPAXa5CH+vfmVzU0F/dY3rs0n1BKBcsUFV7ULj+VBU=; b=vG6qfS8+MWGvLtd//0AenJfh8sKFAFq7uesMz3Tfw3yNHsoesUc3kQVF0LsEFp7YA3dvKt JgGN8Ft8VkJWghM0bqQsM5wyxrcaj3D/lNe55BQi62FP9dfHm1M8fcx/ysie/kBNj2jNok 2fHFSaDRzLGm45vJ/Uo7awx96NFKh5FGMTj8pIkPwj3+PTFC6RHzgOs8hNZiNcmA7IHHEy yNcq75Tk4SHhwleZkLjl6HGZ4UcoCCSf5e6NPJYBgvzudvSMaHqvFUPZ5YP2/oLBCoTlR3 m9KAj3HHdZ8aja/r6878I65Ao525IVZUecgaDWRhA90/Co7RsRmjbGs5rRPEPQ== 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=olPAXa5CH+vfmVzU0F/dY3rs0n1BKBcsUFV7ULj+VBU=; b=tHl7j4FDK8H1P279fcDEcV3usjRe/F3zWDKNno6gAqYIL9m7na8PAFh+eK0ac6eOJpDajK A/Hzqfg6y6i8gHAg== 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] x86/entry: Enable random_kstack_offset support Cc: Kees Cook , Thomas Gleixner , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20210401232347.2791257-5-keescook@chromium.org> References: <20210401232347.2791257-5-keescook@chromium.org> MIME-Version: 1.0 Message-ID: <161788402091.29796.12870482466677100771.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: fe950f6020338c8ac668ef823bb692d36b7542a2 Gitweb: https://git.kernel.org/tip/fe950f6020338c8ac668ef823bb692d36b7542a2 Author: Kees Cook AuthorDate: Thu, 01 Apr 2021 16:23:45 -07:00 Committer: Thomas Gleixner CommitterDate: Thu, 08 Apr 2021 14:05:20 +02:00 x86/entry: Enable random_kstack_offset support Allow for a randomized stack offset on a per-syscall basis, with roughly 5-6 bits of entropy, depending on compiler and word size. Since the method of offsetting uses macros, this cannot live in the common entry code (the stack offset needs to be retained for the life of the syscall, which means it needs to happen at the actual entry point). Signed-off-by: Kees Cook Signed-off-by: Thomas Gleixner Reviewed-by: Thomas Gleixner Link: https://lore.kernel.org/r/20210401232347.2791257-5-keescook@chromium.org --- arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 3 +++ arch/x86/include/asm/entry-common.h | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 2792879..4b4ad8e 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -165,6 +165,7 @@ config X86 select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if X86_64 select HAVE_ARCH_USERFAULTFD_WP if X86_64 && USERFAULTFD select HAVE_ARCH_VMAP_STACK if X86_64 + select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET select HAVE_ARCH_WITHIN_STACK_FRAMES select HAVE_ASM_MODVERSIONS select HAVE_CMPXCHG_DOUBLE diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c index 4efd39a..7b2542b 100644 --- a/arch/x86/entry/common.c +++ b/arch/x86/entry/common.c @@ -38,6 +38,7 @@ #ifdef CONFIG_X86_64 __visible noinstr void do_syscall_64(unsigned long nr, struct pt_regs *regs) { + add_random_kstack_offset(); nr = syscall_enter_from_user_mode(regs, nr); instrumentation_begin(); @@ -83,6 +84,7 @@ __visible noinstr void do_int80_syscall_32(struct pt_regs *regs) { unsigned int nr = syscall_32_enter(regs); + add_random_kstack_offset(); /* * Subtlety here: if ptrace pokes something larger than 2^32-1 into * orig_ax, the unsigned int return value truncates it. This may @@ -102,6 +104,7 @@ static noinstr bool __do_fast_syscall_32(struct pt_regs *regs) unsigned int nr = syscall_32_enter(regs); int res; + add_random_kstack_offset(); /* * This cannot use syscall_enter_from_user_mode() as it has to * fetch EBP before invoking any of the syscall entry work diff --git a/arch/x86/include/asm/entry-common.h b/arch/x86/include/asm/entry-common.h index 2b87b19..14ebd21 100644 --- a/arch/x86/include/asm/entry-common.h +++ b/arch/x86/include/asm/entry-common.h @@ -2,6 +2,7 @@ #ifndef _ASM_X86_ENTRY_COMMON_H #define _ASM_X86_ENTRY_COMMON_H +#include #include #include @@ -70,6 +71,21 @@ static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs, */ current_thread_info()->status &= ~(TS_COMPAT | TS_I386_REGS_POKED); #endif + + /* + * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(), + * but not enough for x86 stack utilization comfort. To keep + * reasonable stack head room, reduce the maximum offset to 8 bits. + * + * The actual entropy will be further reduced by the compiler when + * applying stack alignment constraints (see cc_stack_align4/8 in + * arch/x86/Makefile), which will remove the 3 (x86_64) or 2 (ia32) + * low bits from any entropy chosen here. + * + * Therefore, final stack offset entropy will be 5 (x86_64) or + * 6 (ia32) bits. + */ + choose_random_kstack_offset(rdtsc() & 0xFF); } #define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_prepare