From patchwork Mon Feb 27 22:29:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13154263 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA16DC64ED9 for ; Mon, 27 Feb 2023 22:32:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0F1D36B00A4; Mon, 27 Feb 2023 17:31:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 07C136B00A5; Mon, 27 Feb 2023 17:31:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE8856B00A6; Mon, 27 Feb 2023 17:31:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C85626B00A4 for ; Mon, 27 Feb 2023 17:31:58 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6B07216029E for ; Mon, 27 Feb 2023 22:31:58 +0000 (UTC) X-FDA: 80514520716.29.8724D0E Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf07.hostedemail.com (Postfix) with ESMTP id 79D6540011 for ; Mon, 27 Feb 2023 22:31:56 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FogXzCaT; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677537116; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=TY2JtR8ifKge5ml0A1+y7lAR6dgyU2LGxzFL7xG1GFc=; b=bQtujuQ4sUK76VXF3fnqN449f/nuMUSxGzMWtQZOjZXV5D0ialrYDwEZdK3IYYU6+ao8hN KIYN0yUUJKLgP/7RLYTfvf9mGHVNQ/oH+XhDRSOpyC2kr9U/KJqylD8RFJ++D47JMlIt98 B7wc0Ojipf8MbNCsGfk1t1GAKZC1JFY= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FogXzCaT; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677537116; a=rsa-sha256; cv=none; b=hpwNFbXCQJ7r6d7lkrg99fhmtWQmUOk1NLx39zGvILqfp7/B5cf6SBnBoPrFkyrUIwRd57 86G8r8WQeRKHPBiEzjqlfou8kpcguK2nP3BRm8XDoMu8BLuEMniJhBQ9HEgzeJNrFcxi/c AbnBu9LQ1Gzndcs7TOD8w6n6tiTBbgw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537116; x=1709073116; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=lP5MPQ2jM0Cm31tDJBvrQ8+bAxBLar4e9NDXZhzO2V4=; b=FogXzCaTjX0THiLfHwnE9Th1K1DmMo0jYEM9JHrEoS3blvZ7BdhOFsLf Pf0xN0atPx5y1uHgKdny8/vmuxhD2ANf3BXB2vcEe4GUE1aeunNE8n28E Yfo8OC4UoptUIDP/SIjkcelFUhuKpQynYPesOXSn2aVBJV67vXSJmHUIG gu1bxRiaPsNDaKeY0qiWi3x5lSayvI+nE405q+BoLc7hqgaM4dljHCk3/ hhcQnbWBDSwifJwLaTUeXV+rHR8fBp7vZem+w6jFmx3pOwfpHuwx6yvEU HrAsf2UOES48dhX7dp1LIJShmgUM4mPZCKXDFbsHhQ7WBpayWiLU18hi1 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657774" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657774" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:32 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024758" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024758" Received: from leonqu-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.72.19]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:31 -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 , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v7 32/41] x86/shstk: Handle signals for shadow stack Date: Mon, 27 Feb 2023 14:29:48 -0800 Message-Id: <20230227222957.24501-33-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230227222957.24501-1-rick.p.edgecombe@intel.com> References: <20230227222957.24501-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 79D6540011 X-Stat-Signature: gt1hhxu6bngzu8w94jpptayne86co1z4 X-HE-Tag: 1677537116-263128 X-HE-Meta: U2FsdGVkX1/+M3VCUQTYohjsA3DttMfyF/5lDaO55nwi4F8dBcQnGmYjp//0f6pewjjujRrGgpZe4siH8Ghb1h6rKqT9NBEbmmNQLS1k28ad6jfAxzawyXlQw755WzgTvVOpgsvHTck+XJ9cQbs/4/YOS1tJ2Na5M8E/o9LZGcql2YlWbPMXsakBH2OzQHRRkh74Dsnq71XZD4HQs6PGOc7794bAoENKTPghU4XrzLUe8WkudAqYdh/kWbDxan2vxddCy+JQCFXDQiNgVLzUVXqlbsMMeWlk+5QlOZluohRfz+kMDBLCgKU8Sb+dM+oAG0hf6M1Jb1WESA/EOxYVGYpHpxAYR9tQZhMR4sdyYYpgTew7g1mDpEhDzmm6JpvyZ2xFSlHaHZXY3JBN7b2A+SA7gclq4CHzzrP4rFO1OchVuyvP/+Ib8PqmVeJr/U4E/qh+q5tIvqVMHmsNehzVb7OQTld3pkhjUEf4jr9qyyIO3Fb9ntFO+xc1L4vNLIU3q7Iu0coZJEXgFN0/YRcwChK4+mvxsdRTYtGLfNTj3j1OXvPg1g4tAOiA6pVFNq/9PpIOc0uz8ur4Q4JwHy+XhRMM327xpBVCiNZcLUGwfnrQjAqeLLiHaU4lk9Qdwtt3/F1ySH3dsluR4FFw9vL67Pd4861mX5vW2yNE/7/W/HjUpyCH7fEcSzSj8pnuo9U6384gAal2IYexpjEH2LX27jvQQdHdaURhUNoxEW4GVhRi/SzxrbSI2zOQGqNjwvVSCe0GcaeaaNBoarsC86V/XeVi7HoySs2dQ22LQQEkY2HCeYHuGxxg8sADqXuT6wHLqo7xKJ5XlkBkwD4Y9bteGqFXH2HnTqUVsD8zjgVYjdUfyrJ1sw5kBnMrP+RzKVtwEnLl+ornhKKlcj9fbsZ+zrYdfir35dTx72Cw79CATHDhElLf73HrXANrij6BNktLtFfRkBY9pWDwhVSYrGp +JObXw04 JZc5iZbxzQY2BNa9bq6MkuKAyvcrzMJty4XR25YCx80B0iaIdEPr7nUAITcwxC0cuLdsp8AYyeb+0sPTZvwnsE07sM15t2/4myfII+yej77gwrrIb9DCq7OZI+JUir5CYDBWhYNCBd3XdnsvOqJ+owwgKWiks1ZP9Hj1Fiw2sS3vC1O5bgz7ussF9yVt4PCbI+E6k7DnxTb9W213syZY8wBtAikAnQm16/a99M7zOWWKg/kgKeQfzb37BlNcLzyajZ24p0dX7sfm5B5CxIAIkpSBcJs5N7fjbVNB2x/QgOrF9L+mtQtTQN2WqLns2S2exrJuBu+qRR4GKjKoOGVXK1e+8vlYJ23Vyrz+JqeVNov1iiyQmnV1gOfUDViUTZdymG6Qom5LLAYnXSaXewrAwq3IeAfW5FfW+uqqTgbq9ba8d/KygcQPmHX8+p1EOiHRSvQrQ8/ZU5M/imIg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Yu-cheng Yu When a signal is handled normally the context is pushed to the stack before handling it. For shadow stacks, since the shadow stack only track's return addresses, there isn't any state that needs to be pushed. However, there are still a few things that need to be done. These things are userspace visible and which will be kernel ABI for shadow stacks. One is to make sure the restorer address is written to shadow stack, since the signal handler (if not changing ucontext) returns to the restorer, and the restorer calls sigreturn. So add the restorer on the shadow stack before handling the signal, so there is not a conflict when the signal handler returns to the restorer. The other thing to do is to place some type of checkable token on the thread's shadow stack before handling the signal and check it during sigreturn. This is an extra layer of protection to hamper attackers calling sigreturn manually as in SROP-like attacks. For this token we can use the shadow stack data format defined earlier. Have the data pushed be the previous SSP. In the future the sigreturn might want to return back to a different stack. Storing the SSP (instead of a restore offset or something) allows for future functionality that may want to restore to a different stack. So, when handling a signal push - the SSP pointing in the shadow stack data format - the restorer address below the restore token. In sigreturn, verify SSP is stored in the data format and pop the shadow stack. Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Acked-by: Mike Rapoport (IBM) Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Andy Lutomirski Cc: Cyrill Gorcunov Cc: Florian Weimer Cc: H. Peter Anvin Cc: Kees Cook --- v3: - Drop shstk_setup_rstor_token() (Kees) - Drop x32 signal support, since x32 support is dropped v2: - Switch to new shstk signal format v1: - Use xsave helpers. - Expand commit log. Yu-cheng v27: - Eliminate saving shadow stack pointer to signal context. --- arch/x86/include/asm/shstk.h | 5 ++ arch/x86/kernel/shstk.c | 98 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/signal.c | 1 + arch/x86/kernel/signal_64.c | 6 +++ 4 files changed, 110 insertions(+) diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index 1399f4df098b..acee68d30a07 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -6,6 +6,7 @@ #include struct task_struct; +struct ksignal; #ifdef CONFIG_X86_USER_SHADOW_STACK struct thread_shstk { @@ -19,6 +20,8 @@ int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, unsigned long stack_size, unsigned long *shstk_addr); void shstk_free(struct task_struct *p); +int setup_signal_shadow_stack(struct ksignal *ksig); +int restore_signal_shadow_stack(void); #else static inline long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) { return -EINVAL; } @@ -28,6 +31,8 @@ static inline int shstk_alloc_thread_stack(struct task_struct *p, unsigned long stack_size, unsigned long *shstk_addr) { return 0; } static inline void shstk_free(struct task_struct *p) {} +static inline int setup_signal_shadow_stack(struct ksignal *ksig) { return 0; } +static inline int restore_signal_shadow_stack(void) { return 0; } #endif /* CONFIG_X86_USER_SHADOW_STACK */ #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 13c02747386f..40f0a55762a9 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -232,6 +232,104 @@ static int get_shstk_data(unsigned long *data, unsigned long __user *addr) return 0; } +static int shstk_push_sigframe(unsigned long *ssp) +{ + unsigned long target_ssp = *ssp; + + /* Token must be aligned */ + if (!IS_ALIGNED(*ssp, 8)) + return -EINVAL; + + if (!IS_ALIGNED(target_ssp, 8)) + return -EINVAL; + + *ssp -= SS_FRAME_SIZE; + if (put_shstk_data((void *__user)*ssp, target_ssp)) + return -EFAULT; + + return 0; +} + +static int shstk_pop_sigframe(unsigned long *ssp) +{ + unsigned long token_addr; + int err; + + err = get_shstk_data(&token_addr, (unsigned long __user *)*ssp); + if (unlikely(err)) + return err; + + /* Restore SSP aligned? */ + if (unlikely(!IS_ALIGNED(token_addr, 8))) + return -EINVAL; + + /* SSP in userspace? */ + if (unlikely(token_addr >= TASK_SIZE_MAX)) + return -EINVAL; + + *ssp = token_addr; + + return 0; +} + +int setup_signal_shadow_stack(struct ksignal *ksig) +{ + void __user *restorer = ksig->ka.sa.sa_restorer; + unsigned long ssp; + int err; + + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || + !features_enabled(ARCH_SHSTK_SHSTK)) + return 0; + + if (!restorer) + return -EINVAL; + + ssp = get_user_shstk_addr(); + if (unlikely(!ssp)) + return -EINVAL; + + err = shstk_push_sigframe(&ssp); + if (unlikely(err)) + return err; + + /* Push restorer address */ + ssp -= SS_FRAME_SIZE; + err = write_user_shstk_64((u64 __user *)ssp, (u64)restorer); + if (unlikely(err)) + return -EFAULT; + + fpregs_lock_and_load(); + wrmsrl(MSR_IA32_PL3_SSP, ssp); + fpregs_unlock(); + + return 0; +} + +int restore_signal_shadow_stack(void) +{ + unsigned long ssp; + int err; + + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || + !features_enabled(ARCH_SHSTK_SHSTK)) + return 0; + + ssp = get_user_shstk_addr(); + if (unlikely(!ssp)) + return -EINVAL; + + err = shstk_pop_sigframe(&ssp); + if (unlikely(err)) + return err; + + fpregs_lock_and_load(); + wrmsrl(MSR_IA32_PL3_SSP, ssp); + fpregs_unlock(); + + return 0; +} + void shstk_free(struct task_struct *tsk) { struct thread_shstk *shstk = &tsk->thread.shstk; diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index 004cb30b7419..356253e85ce9 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -40,6 +40,7 @@ #include #include #include +#include static inline int is_ia32_compat_frame(struct ksignal *ksig) { diff --git a/arch/x86/kernel/signal_64.c b/arch/x86/kernel/signal_64.c index 0e808c72bf7e..cacf2ede6217 100644 --- a/arch/x86/kernel/signal_64.c +++ b/arch/x86/kernel/signal_64.c @@ -175,6 +175,9 @@ int x64_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs) frame = get_sigframe(ksig, regs, sizeof(struct rt_sigframe), &fp); uc_flags = frame_uc_flags(regs); + if (setup_signal_shadow_stack(ksig)) + return -EFAULT; + if (!user_access_begin(frame, sizeof(*frame))) return -EFAULT; @@ -260,6 +263,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;