Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1688955pxb; Fri, 20 Aug 2021 11:22:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyG0J20OSqLifaXz9xndD8v4rl9AjBHKGf3q88CLZqBVJBhavG6GVAX156N4cIV/mM/VnJq X-Received: by 2002:a05:6e02:b48:: with SMTP id f8mr12338975ilu.25.1629483777764; Fri, 20 Aug 2021 11:22:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629483777; cv=none; d=google.com; s=arc-20160816; b=IHVXQYFH0xmJtPu+dPqo2zDbsxwM2Wk5Sp7s45GiVMiE37l7x9GHtXoEp9+sOAbTFr QDi1iEhH5jN8Cbomgh7WENLxxhbDvpdpjIhvG4BmqCsTZftXc+ta9mcKXkTybE3ldWTm yEKawwb+UcT8yogLS6KnBPkosBo8BMPWuN8ZndCeggRtHGwv5DtA9fzfOGLz9XFMErEp VkSvkO/UjJkQjMX9Fjl/yKc7sfkFudO24maEoDL0L19Kzs5D+u70Ixa597FuGNZKwH2Z b30plO6HOfC03OhkW0k+6iD8QLskP3QAFOXGSMqBllzevetx8P++xwerJFdThkKT5Vdj a2yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=R3mhe92+lHXaQI7Uw9GHc0ctlWF3jOQYwUAAVFUyLCk=; b=gD6XfWbDtjZdCceC3hw7PpVv5VzwGE3jYmwAjf3OUiCNejgfbpHwdXcOsy3KXJ/efK k0nOHJIMq/DDsWiM4OJHERwGl+K47gi/RcnU9NdyZ11B//Mx1SJIvvo2uudIE0TFbWwT zgAl5NoW2L4nv+a2epVqquk+ixojRgUPS6kVu3Z6RjeoxRzJpJ1maKH5XeYJrtWmhuNe +vn9WkmxFCDattZflwF9j0jXjLmhDxGC3H8uuG6lGLuy/MbtSLSlrvuyz2nlybs45HHT xnfA3AOjBocJw8NpVLyYKtL1f7DfvsFpOC8upaV2GDgbDFwyeLDyaXqKzoNFPOlL96eL aIoA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (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 az16si7324238jab.108.2021.08.20.11.22.45; Fri, 20 Aug 2021 11:22:57 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238564AbhHTSWZ (ORCPT + 99 others); Fri, 20 Aug 2021 14:22:25 -0400 Received: from mga05.intel.com ([192.55.52.43]:22456 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236037AbhHTSVu (ORCPT ); Fri, 20 Aug 2021 14:21:50 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10082"; a="302407841" X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="302407841" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:52 -0700 X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="533074777" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:51 -0700 From: Yu-cheng Yu 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 , Pengfei Xu , Haitao Huang , Rick P Edgecombe Cc: Yu-cheng Yu Subject: [PATCH v29 27/32] x86/cet/shstk: Handle signals for shadow stack Date: Fri, 20 Aug 2021 11:11:56 -0700 Message-Id: <20210820181201.31490-28-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210820181201.31490-1-yu-cheng.yu@intel.com> References: <20210820181201.31490-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A signal handler (if not changing ucontext) returns to the restorer, and the restorer calls sigreturn. Thus, when setting up a signal frame, the kernel: - installs a shadow stack restore token pointing to the current shadow stack address, and - installs the restorer address below the restore token. In sigreturn, the restore token is verified and shadow stack pointer is restored. Signed-off-by: Yu-cheng Yu Cc: Andy Lutomirski Cc: Cyrill Gorcunov Cc: Florian Weimer Cc: H. Peter Anvin Cc: Kees Cook --- v27: - Eliminate saving shadow stack pointer to signal context. v25: - Update commit log/comments for the sc_ext struct. - Use restorer address already calculated. - Change CONFIG_X86_CET to CONFIG_X86_SHADOW_STACK. - Change X86_FEATURE_CET to X86_FEATURE_SHSTK. - Eliminate writing to MSR_IA32_U_CET for shadow stack. - Change wrmsrl() to wrmsrl_safe() and handle error. --- arch/x86/ia32/ia32_signal.c | 25 +++++++++++++++++----- arch/x86/include/asm/cet.h | 4 ++++ arch/x86/kernel/shstk.c | 42 +++++++++++++++++++++++++++++++++++++ arch/x86/kernel/signal.c | 13 ++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) diff --git a/arch/x86/ia32/ia32_signal.c b/arch/x86/ia32/ia32_signal.c index 5e3d9b7fd5fb..d7a30bc98e66 100644 --- a/arch/x86/ia32/ia32_signal.c +++ b/arch/x86/ia32/ia32_signal.c @@ -35,6 +35,7 @@ #include #include #include +#include static inline void reload_segments(struct sigcontext_32 *sc) { @@ -113,6 +114,10 @@ COMPAT_SYSCALL_DEFINE0(sigreturn) if (ia32_restore_sigcontext(regs, &frame->sc)) goto badframe; + + if (restore_signal_shadow_stack()) + goto badframe; + return regs->ax; badframe: @@ -138,6 +143,9 @@ COMPAT_SYSCALL_DEFINE0(rt_sigreturn) if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext)) goto badframe; + if (restore_signal_shadow_stack()) + goto badframe; + if (compat_restore_altstack(&frame->uc.uc_stack)) goto badframe; @@ -262,6 +270,9 @@ int ia32_setup_frame(int sig, struct ksignal *ksig, restorer = &frame->retcode; } + if (setup_signal_shadow_stack(1, restorer)) + return -EFAULT; + if (!user_access_begin(frame, sizeof(*frame))) return -EFAULT; @@ -319,6 +330,15 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, frame = get_sigframe(ksig, regs, sizeof(*frame), &fp); + if (ksig->ka.sa.sa_flags & SA_RESTORER) + restorer = ksig->ka.sa.sa_restorer; + else + restorer = current->mm->context.vdso + + vdso_image_32.sym___kernel_rt_sigreturn; + + if (setup_signal_shadow_stack(1, restorer)) + return -EFAULT; + if (!user_access_begin(frame, sizeof(*frame))) return -EFAULT; @@ -334,11 +354,6 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, unsafe_put_user(0, &frame->uc.uc_link, Efault); unsafe_compat_save_altstack(&frame->uc.uc_stack, regs->sp, Efault); - if (ksig->ka.sa.sa_flags & SA_RESTORER) - restorer = ksig->ka.sa.sa_restorer; - else - restorer = current->mm->context.vdso + - vdso_image_32.sym___kernel_rt_sigreturn; unsafe_put_user(ptr_to_compat(restorer), &frame->pretcode, Efault); /* diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h index aa533700ba31..2f7940d68ce3 100644 --- a/arch/x86/include/asm/cet.h +++ b/arch/x86/include/asm/cet.h @@ -24,6 +24,8 @@ void shstk_disable(void); int shstk_setup_rstor_token(bool ia32, unsigned long restorer, unsigned long *new_ssp); int shstk_check_rstor_token(bool ia32, unsigned long *new_ssp); +int setup_signal_shadow_stack(int ia32, void __user *restorer); +int restore_signal_shadow_stack(void); #else static inline int shstk_setup(void) { return 0; } static inline int shstk_alloc_thread_stack(struct task_struct *p, @@ -35,6 +37,8 @@ static inline int shstk_setup_rstor_token(bool ia32, unsigned long restorer, unsigned long *new_ssp) { return 0; } static inline int shstk_check_rstor_token(bool ia32, unsigned long *new_ssp) { return 0; } +static inline int setup_signal_shadow_stack(int ia32, void __user *restorer) { return 0; } +static inline int restore_signal_shadow_stack(void) { return 0; } #endif #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 548d0552f9b3..b3105b7440a5 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -333,3 +333,45 @@ int shstk_check_rstor_token(bool proc32, unsigned long *new_ssp) return 0; } + +int setup_signal_shadow_stack(int ia32, void __user *restorer) +{ + struct thread_shstk *shstk = ¤t->thread.shstk; + unsigned long new_ssp; + int err; + + if (!cpu_feature_enabled(X86_FEATURE_SHSTK) || !shstk->size) + return 0; + + err = shstk_setup_rstor_token(ia32, (unsigned long)restorer, + &new_ssp); + if (err) + return err; + + start_update_msrs(); + err = wrmsrl_safe(MSR_IA32_PL3_SSP, new_ssp); + end_update_msrs(); + + return err; +} + +int restore_signal_shadow_stack(void) +{ + struct thread_shstk *shstk = ¤t->thread.shstk; + int ia32 = in_ia32_syscall(); + unsigned long new_ssp; + int err; + + if (!cpu_feature_enabled(X86_FEATURE_SHSTK) || !shstk->size) + return 0; + + err = shstk_check_rstor_token(ia32, &new_ssp); + if (err) + return err; + + start_update_msrs(); + err = wrmsrl_safe(MSR_IA32_PL3_SSP, new_ssp); + end_update_msrs(); + + return err; +} diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index f4d21e470083..661e46803b84 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -46,6 +46,7 @@ #include #include #include +#include #ifdef CONFIG_X86_64 /* @@ -471,6 +472,9 @@ static int __setup_rt_frame(int sig, struct ksignal *ksig, frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp); uc_flags = frame_uc_flags(regs); + if (setup_signal_shadow_stack(0, ksig->ka.sa.sa_restorer)) + return -EFAULT; + if (!user_access_begin(frame, sizeof(*frame))) return -EFAULT; @@ -576,6 +580,9 @@ static int x32_setup_rt_frame(struct ksignal *ksig, uc_flags = frame_uc_flags(regs); + if (setup_signal_shadow_stack(0, ksig->ka.sa.sa_restorer)) + return -EFAULT; + if (!user_access_begin(frame, sizeof(*frame))) return -EFAULT; @@ -674,6 +681,9 @@ SYSCALL_DEFINE0(rt_sigreturn) if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags)) goto badframe; + if (restore_signal_shadow_stack()) + goto badframe; + if (restore_altstack(&frame->uc.uc_stack)) goto badframe; @@ -932,6 +942,9 @@ COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn) if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags)) goto badframe; + if (restore_signal_shadow_stack()) + goto badframe; + if (compat_restore_altstack(&frame->uc.uc_stack)) goto badframe; -- 2.21.0