From patchwork Sun Mar 19 00:15:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13180160 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 241ADC74A5B for ; Sun, 19 Mar 2023 00:16:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A375928001E; Sat, 18 Mar 2023 20:16:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 94B99280001; Sat, 18 Mar 2023 20:16:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7750F28001E; Sat, 18 Mar 2023 20:16:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 53E49280001 for ; Sat, 18 Mar 2023 20:16:51 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3570DA0BFA for ; Sun, 19 Mar 2023 00:16:51 +0000 (UTC) X-FDA: 80583732222.21.57647D8 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by imf11.hostedemail.com (Postfix) with ESMTP id 2164140007 for ; Sun, 19 Mar 2023 00:16:48 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="A/CALrw0"; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679185009; 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=DMqkzk73IxHG1I4Q11s/ouCJSTSEx1hUtj8KnGYTYws=; b=l/aclEeg+DZPTgCP5E7XM2PLfm8QNAWXfFhriMs4AnQnGotzE7zdlSHrlbACPv+O9jTziD 9HfUAZiTBwtxOawbWFyjHZc6IjOILVB4omPgzB1oznlIX6hsP3fmS4J/XnH1dnXh9R2e0M oY9ylcZCsZOdhZUoxdbUtZhuPkwgL4w= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="A/CALrw0"; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679185009; a=rsa-sha256; cv=none; b=t8Oo0x7/47758zlFSD13r4goo/9IA7a3kV4HpOIxfVxTd2Nuf/a2IUxBlwsJJur+7QrI+R snP15YxKEnxEjtMtCNa6Onjv4ZqtWWnrW8l5ObZsykD7LA9iNL52lRF053IlNmUfXtpe5O lqcQNygKTBwdf46BvzNZ1n4JerPqFGs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679185009; x=1710721009; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=44qPir7RLXKA27y57yzk3u7KQrjWXmALrqhQJT9eSm4=; b=A/CALrw0pNbAk1xXqLxc6m/QcDPRjqpBhxNGm0XCxVQOjPNZ7XpEq3EP gFgp0NJYHXgNSjyCaw385981I/EapVB7Ao0btYx2ZfSt12bj1oGBvQ07s zRSqF9eo4HDu/31Wdh2cGtNF6ZB8a3GPlpqYd9bGT4Un9tgQSHMqzWGKf +HykXtVllIqY4QPKEMVaBkQI0DUkQfidYDDs+Mg7Y7Ck2GewS9XLBuS0n G6Jp+iNH/9Xx0qY5M7gym9y7ZMBfcCJqKM0IEyDAmDqdp9VvT+4/ha0xA AeieL42kyxOF3IJTfC12MOrmCd3ql9sFJzSC8LzZqM87OcDoNdDhJYURT g==; X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="338491455" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="338491455" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="749672951" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="749672951" Received: from bmahatwo-mobl1.gar.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.135.34.5]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:46 -0700 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, szabolcs.nagy@arm.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v8 32/40] x86/shstk: Handle signals for shadow stack Date: Sat, 18 Mar 2023 17:15:27 -0700 Message-Id: <20230319001535.23210-33-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230319001535.23210-1-rick.p.edgecombe@intel.com> References: <20230319001535.23210-1-rick.p.edgecombe@intel.com> X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 2164140007 X-Rspam-User: X-Stat-Signature: zhn6iyj6edcqth89zdx3jnhbtx89ck33 X-HE-Tag: 1679185008-807195 X-HE-Meta: U2FsdGVkX19pVvGfguQlEXouqvCD0mek00NYOay2QPs1lvYjAIXOVDiB7NB8WwODhYdn4fDMz+mJ8lwyQ5j1zMbpcW6jxLB2yWtLrST//yFMe9N1ppZTcN4dV1O3KZ1pKocub5fYQ0KKyvdVK9Sa81V/t7P7Fa7mq8DML94aKtNqmVaOX6WJTDedTe3nSZfJ5j85WQz94/3XW50bU6nFY/UHxGZSRiY/ev5M9+YM+BlrU0EhtpkTwBZO47vDCDXr5mJNvmJZuyLG6qZMFENqSYQ3F/qheRB+Y9pBo28tQLcstT0Y5TJgSOi4HLPJ4kYHKm7G8CD30+5eVqWRY0aHt6h549srrr9KONJx931KpRtB5W/XLxcocPEec7po0RUbnEjWENQ9NeBVjrVDVDz0Uj2ae/z64kcwELZ2RqQp1FetAFMlSx8lY9QHJ60giwcmwIZ+OadRmrecduq6nxSDuTNCnqICIztK9MkzZ5p3s37trwVtCNhsbQY4EfXF1wxKFyu7rWXY4qSqsfJTBQDgT4XaUi5pwXOj0GEFYRcojr/XwOuCjmFhRUsfBtD5lV+TT9Qu16ylhW+8JhZrsW2ulEe+DdUXr+0FGSP3F3lG9sE+UgxCj1+vobqM5BHCR9mhZNvXJdKGgRPVDXegyPUwIf/vNBTz6MPhEIvZTymrTMmD3NchYdnxIjlvjxi9SSDRHzs3UwJSs0Jx3TbSZ4bMDY0l4pQYcbIXC4h1oCFt4Ri8aAXHcjOXOocCzTlLmSFzk50ONTBcOKVs29D3w/JSnPSApca2hFOMoEhRmdLZN7Sl8FEbZJczRMItKez7wpvf+xYQan4hGYiVnIIbD607HEPbntQgZBCOLuWPowipEEyYKywcCmImHIXKXwyETVBhfkkOMY83ARLxClDjRJ8sKA7SdD+LH3bzJvr86X3xzFFE0wW6iEBikjXDFRTPC1X/FPNyDYS4klbvL3bJQtI BxjSxA1m qx3vJfqARWVyj+tZDcAGW/WMRpUg7m5/vMdUK6oRYA3cnHdlHTQOIqdt+QNeZ+YN+/byCYlrsDVkpZYQyLeVrrBc7MEDuOuSmXXYQX9ZTpGIB7HTRlycuquXTrfE4AsZKyucgfpwRX7bdtiFA7fuohqZu/w6R+G1pR2a3k/puuXeb8PfXp9CnbOJlU26D1aevYMx4kvZk5+OOXA3u8KcCA4SacvT7KRDM4OoKOVwBnn4ZD5LJX1ViqhBPFitas0X593SweOxSR5fVDJAF35JfUcCVFOYwPaq4/Uybf4VLoIH06pTngw7bspsIDDlNXGq5g9Dnoc9p4Is2XDMunMuWD/cZb+STATcaWleY 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: When a signal is handled, the context is pushed to the stack before handling it. For shadow stacks, since the shadow stack only tracks 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 visible to userspace 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 the shadow stack data format defined earlier can be used. 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. Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook --- v8: - Update commit log verbiage (Boris) - Remove duplicate alignment check (Boris) 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. --- arch/x86/include/asm/shstk.h | 5 ++ arch/x86/kernel/shstk.c | 95 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/signal.c | 1 + arch/x86/kernel/signal_64.c | 6 +++ 4 files changed, 107 insertions(+) diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index d4a5c7b10cb5..ecb23a8ca47d 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 { @@ -18,6 +19,8 @@ void reset_thread_features(void); unsigned long shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, unsigned long stack_size); 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; } @@ -26,6 +29,8 @@ static inline unsigned long shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, unsigned long stack_size) { 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 e22928c63ffc..f02e8ea4f1b5 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -232,6 +232,101 @@ 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(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;