Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp620222pxb; Tue, 1 Feb 2022 07:08:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJwEaYb2EJ2kwHcoo4wd3j5ihTRUC7tmbWxV0UyWi5+sKjvv7RoV2+Q4ebsBR17SLsKm4KEq X-Received: by 2002:a17:906:5d0f:: with SMTP id g15mr21751418ejt.751.1643728114954; Tue, 01 Feb 2022 07:08:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728114; cv=none; d=google.com; s=arc-20160816; b=urFZRZR96MSkudLnhNL6uJWQu4rwQXTj/+TVRbxqe+YM5aW3yKw5vgc+YYJDkppTZz 70seW/hxEcgfWTy1hz1o4V+OQ/UCuR1b4W6yJy8AOjL21XVkfA7lmlHW1DUJEoSaJ6yY PrwYrR6AGQbuFv8Jw8iOOqEveBs/uk5iXV7IE/OofeVFBNAh0CCdBqYURkpEUnlDsFla C+xOL8in/ohNb8AwOkOtbBSV2ygfnwfqmdWGFbEEIVhbjwr3/9SzzCm4rz4LPZPNfAIS OsKV7EU/k7weAO9X+3kDeRn2/bS5aF0PYDgRwhggMYOgn7h1SIAORmxE1ydzicK79s4v vZ9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=gsSmHEUJ8386kiSCf/KgYxmhk1pG0Jj+dX9ROhYIiiM=; b=rVAuaW+PDZD2NjW6Mks1waVrYtj+0pmcSzYwqYpMtNjDzNhCzXooERfO7bRhRFjuKg tcvM9LhNbqi69ZQJII9xKsLvhY/tFx03Lb6E+4sErvQWVVNVS4KX2YzvUCZXNdUlG7a9 b5ZHB7UGD9pbJGIcp5KiZmnuGePIVKPP9rK61xVrcZJvBWOdBEE4osKWVR8dwy90Sn8B PERveK58MFHiJ3UClen0/KRBvmgDZm7BgydSBPqzXrSEQLJxLX0wKOS8I+WngsbOdWeV 0mY1rVlwXk4fWXP/xr8ZUkHh2tpS/qstooP9b0zu4jdYR76SMUPRQQ4aJu5H3UozPH5X oKZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gIY6kBLk; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 27si9257837eja.214.2022.02.01.07.08.09; Tue, 01 Feb 2022 07:08:34 -0800 (PST) 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=@intel.com header.s=Intel header.b=gIY6kBLk; 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=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240196AbiA3VZp (ORCPT + 99 others); Sun, 30 Jan 2022 16:25:45 -0500 Received: from mga06.intel.com ([134.134.136.31]:52029 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356951AbiA3VY2 (ORCPT ); Sun, 30 Jan 2022 16:24:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643577868; x=1675113868; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=nKIQtVTcJcJc4kRj9os/sAR9Nug8XbfOG4wi9emXZyE=; b=gIY6kBLkheGSetWY1Ar+Na5gX4QHM+BozZI73MFRP5QWTRvT3+oIvadC OHR5ecKzxyEjSbGCXicJi46Vw+FcUDUSySYrASm9ZZ64NwYEFaYc/kM+3 zgZmhN0+DGuTec1D509KLqW/2gv7IY2izzmvpwRZsp4lzCgva6LpaJbNj cBI6aowr7jMiIwUuRFYbVR255289+d2IyWlnk8T2sS9EZM55wuzDvhKsl ihGrsD6fWSq8WdSg+G6KyP9ZjssdvrBYs70lS6k/Zy9IJty2dnb+MbpvZ +EBJmndfsfbi8SRK0vLA9lid30D+rD5Ec9kdyupyR8kDZsq0Djzna/XU3 Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="308104968" X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="308104968" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:22:06 -0800 X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="536856894" Received: from avmallar-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.123.171]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:22:05 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Dave Martin , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH 25/35] x86/cet/shstk: Add user-mode shadow stack support Date: Sun, 30 Jan 2022 13:18:28 -0800 Message-Id: <20220130211838.8382-26-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220130211838.8382-1-rick.p.edgecombe@intel.com> References: <20220130211838.8382-1-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yu-cheng Yu Introduce basic shadow stack enabling/disabling/allocation routines. A task's shadow stack is allocated from memory with VM_SHADOW_STACK flag and has a fixed size of min(RLIMIT_STACK, 4GB). Add the user shadow stack MSRs to the xsave helpers, so they can be used to implement the functionality. Keep the task's shadow stack address and size in thread_struct. This will be copied when cloning new threads, but needs to be cleared during exec, so add a function to do this. Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe Cc: Kees Cook --- v1: - Switch to xsave helpers. - Expand commit log. Yu-cheng v30: - Remove superfluous comments for struct thread_shstk. - Replace 'populate' with 'unused'. Yu-cheng v28: - Update shstk_setup() with wrmsrl_safe(), returns success when shadow stack feature is not present (since this is a setup function). Yu-cheng v27: - Change 'struct cet_status' to 'struct thread_shstk', and change member types from unsigned long to u64. - Re-order local variables in reverse order of length. - WARN_ON_ONCE() when vm_munmap() fails. arch/x86/include/asm/cet.h | 29 ++++++ arch/x86/include/asm/processor.h | 5 ++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/fpu/xstate.c | 5 +- arch/x86/kernel/process_64.c | 2 + arch/x86/kernel/shstk.c | 149 +++++++++++++++++++++++++++++++ 6 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/cet.h create mode 100644 arch/x86/kernel/shstk.c diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h new file mode 100644 index 000000000000..de90e4ae083a --- /dev/null +++ b/arch/x86/include/asm/cet.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_CET_H +#define _ASM_X86_CET_H + +#ifndef __ASSEMBLY__ +#include + +struct task_struct; + +struct thread_shstk { + u64 base; + u64 size; +}; + +#ifdef CONFIG_X86_SHADOW_STACK +int shstk_setup(void); +void shstk_free(struct task_struct *p); +int shstk_disable(void); +void reset_thread_shstk(void); +#else +static inline void shstk_setup(void) {} +static inline void shstk_free(struct task_struct *p) {} +static inline void shstk_disable(void) {} +static inline void reset_thread_shstk(void) {} +#endif /* CONFIG_X86_SHADOW_STACK */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_X86_CET_H */ diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index 2c5f12ae7d04..a9f4e9c4ca81 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -27,6 +27,7 @@ struct vm86; #include #include #include +#include #include #include @@ -528,6 +529,10 @@ struct thread_struct { */ u32 pkru; +#ifdef CONFIG_X86_SHADOW_STACK + struct thread_shstk shstk; +#endif + /* Floating point and extended processor state */ struct fpu fpu; /* diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 6aef9ee28a39..d60ae6c365c7 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -153,6 +153,7 @@ obj-$(CONFIG_AMD_MEM_ENCRYPT) += sev.o obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += cc_platform.o +obj-$(CONFIG_X86_SHADOW_STACK) += shstk.o ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y) diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index c5e20e0d0725..25b1b0c417fd 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1871,7 +1871,10 @@ int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, static u64 *__get_xsave_member(void *xstate, u32 msr) { switch (msr) { - /* Currently there are no MSR's supported */ + case MSR_IA32_PL3_SSP: + return &((struct cet_user_state *)xstate)->user_ssp; + case MSR_IA32_U_CET: + return &((struct cet_user_state *)xstate)->user_cet; default: WARN_ONCE(1, "x86/fpu: unsupported xstate msr (%u)\n", msr); return NULL; diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 3402edec236c..f05fe27d4967 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -514,6 +514,8 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip, load_gs_index(__USER_DS); } + reset_thread_shstk(); + loadsegment(fs, 0); loadsegment(es, _ds); loadsegment(ds, _ds); diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c new file mode 100644 index 000000000000..4e8686ed885f --- /dev/null +++ b/arch/x86/kernel/shstk.c @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * shstk.c - Intel shadow stack support + * + * Copyright (c) 2021, Intel Corporation. + * Yu-cheng Yu + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static unsigned long alloc_shstk(unsigned long size) +{ + int flags = MAP_ANONYMOUS | MAP_PRIVATE; + struct mm_struct *mm = current->mm; + unsigned long addr, unused; + + mmap_write_lock(mm); + addr = do_mmap(NULL, 0, size, PROT_READ, flags, VM_SHADOW_STACK, 0, + &unused, NULL); + mmap_write_unlock(mm); + + return addr; +} + +static void unmap_shadow_stack(u64 base, u64 size) +{ + while (1) { + int r; + + r = vm_munmap(base, size); + + /* + * vm_munmap() returns -EINTR when mmap_lock is held by + * something else, and that lock should not be held for a + * long time. Retry it for the case. + */ + if (r == -EINTR) { + cond_resched(); + continue; + } + + /* + * For all other types of vm_munmap() failure, either the + * system is out of memory or there is bug. + */ + WARN_ON_ONCE(r); + break; + } +} + +int shstk_setup(void) +{ + struct thread_shstk *shstk = ¤t->thread.shstk; + unsigned long addr, size; + void *xstate; + int err; + + if (!cpu_feature_enabled(X86_FEATURE_SHSTK) || + shstk->size || + shstk->base) + return 1; + + size = PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); + addr = alloc_shstk(size); + if (IS_ERR_VALUE(addr)) + return 1; + + xstate = start_update_xsave_msrs(XFEATURE_CET_USER); + err = xsave_wrmsrl(xstate, MSR_IA32_PL3_SSP, addr + size); + if (!err) + err = xsave_wrmsrl(xstate, MSR_IA32_U_CET, CET_SHSTK_EN); + end_update_xsave_msrs(); + + if (err) { + /* + * Don't leak shadow stack if something went wrong with writing the + * msrs. Warn about it because things may be in a weird state. + */ + WARN_ON_ONCE(1); + unmap_shadow_stack(addr, size); + return 1; + } + + shstk->base = addr; + shstk->size = size; + return 0; +} + +void reset_thread_shstk(void) +{ + memset(¤t->thread.shstk, 0, sizeof(struct thread_shstk)); +} + +void shstk_free(struct task_struct *tsk) +{ + struct thread_shstk *shstk = &tsk->thread.shstk; + + if (!cpu_feature_enabled(X86_FEATURE_SHSTK) || + !shstk->size || + !shstk->base) + return; + + if (!tsk->mm) + return; + + unmap_shadow_stack(shstk->base, shstk->size); + + shstk->base = 0; + shstk->size = 0; +} + +int shstk_disable(void) +{ + struct thread_shstk *shstk = ¤t->thread.shstk; + void *xstate; + int err; + + if (!cpu_feature_enabled(X86_FEATURE_SHSTK) || + !shstk->size || + !shstk->base) + return 1; + + xstate = start_update_xsave_msrs(XFEATURE_CET_USER); + err = xsave_set_clear_bits_msrl(xstate, MSR_IA32_U_CET, 0, CET_SHSTK_EN); + if (!err) + err = xsave_wrmsrl(xstate, MSR_IA32_PL3_SSP, 0); + end_update_xsave_msrs(); + + if (err) + return 1; + + shstk_free(current); + return 0; +} -- 2.17.1