From mboxrd@z Thu Jan 1 00:00:00 1970 From: Yu-cheng Yu Subject: Re: [RFC PATCH v9 25/27] x86/cet/shstk: Handle thread Shadow Stack Date: Wed, 25 Mar 2020 14:51:13 -0700 Message-ID: References: <20200205181935.3712-1-yu-cheng.yu@intel.com> <20200205181935.3712-26-yu-cheng.yu@intel.com> <202002251324.5D515260@keescook> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Return-path: Received: from mga14.intel.com ([192.55.52.115]:65091 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726081AbgCYVvO (ORCPT ); Wed, 25 Mar 2020 17:51:14 -0400 In-Reply-To: <202002251324.5D515260@keescook> Sender: linux-arch-owner@vger.kernel.org List-ID: To: Kees Cook Cc: 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 , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel On Tue, 2020-02-25 at 13:29 -0800, Kees Cook wrote: > On Wed, Feb 05, 2020 at 10:19:33AM -0800, Yu-cheng Yu wrote: > > [...] > > A 64-bit SHSTK has a fixed size of RLIMIT_STACK. A compat-mode thread SHSTK > > has a fixed size of 1/4 RLIMIT_STACK. This allows more threads to share a > > 32-bit address space. > > I am not understanding this part. :) Entries are sizeof(unsigned long), > yes? A 1/2 RLIMIT_STACK would cover 32-bit, but 1/4 is less, so why does > that provide for more threads? Each thread has a separate shadow stack. If each shadow stack is smaller, the address space can accommodate more shadow stack allocations. > >[...] > > diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c > > index cba5c7656aab..5b45abda80a1 100644 > > --- a/arch/x86/kernel/cet.c > > +++ b/arch/x86/kernel/cet.c > > @@ -170,6 +170,47 @@ int cet_setup_shstk(void) > > return 0; > > } > > > > +int cet_setup_thread_shstk(struct task_struct *tsk) > > +{ > > + unsigned long addr, size; > > + struct cet_user_state *state; > > + struct cet_status *cet = &tsk->thread.cet; > > + > > + if (!cet->shstk_enabled) > > + return 0; > > + > > + state = get_xsave_addr(&tsk->thread.fpu.state.xsave, > > + XFEATURE_CET_USER); > > + > > + if (!state) > > + return -EINVAL; > > + > > + size = rlimit(RLIMIT_STACK); > > Is SHSTK incompatible with RLIM_INFINITY stack rlimits? I will change it to: size = min(rlimit(RLIMIT_STACK), 4 GB); > > > + > > + /* > > + * Compat-mode pthreads share a limited address space. > > + * If each function call takes an average of four slots > > + * stack space, we need 1/4 of stack size for shadow stack. > > + */ > > + if (in_compat_syscall()) > > + size /= 4; > > + > > + addr = alloc_shstk(size); > > I assume it'd fail here, but I worry about Stack Clash style attacks. > I'd like to see test cases that make sure the SHSTK gap is working > correctly. I will work on some tests. Thanks, Yu-cheng From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com ([192.55.52.115]:65091 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726081AbgCYVvO (ORCPT ); Wed, 25 Mar 2020 17:51:14 -0400 Message-ID: Subject: Re: [RFC PATCH v9 25/27] x86/cet/shstk: Handle thread Shadow Stack From: Yu-cheng Yu Date: Wed, 25 Mar 2020 14:51:13 -0700 In-Reply-To: <202002251324.5D515260@keescook> References: <20200205181935.3712-1-yu-cheng.yu@intel.com> <20200205181935.3712-26-yu-cheng.yu@intel.com> <202002251324.5D515260@keescook> Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-arch-owner@vger.kernel.org List-ID: To: Kees Cook Cc: 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 , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , x86-patch-review@intel.com Message-ID: <20200325215113.uN7dxckDnkHAd2pKf7362VkQr2BLJfvSLgIV8UKeB7I@z> On Tue, 2020-02-25 at 13:29 -0800, Kees Cook wrote: > On Wed, Feb 05, 2020 at 10:19:33AM -0800, Yu-cheng Yu wrote: > > [...] > > A 64-bit SHSTK has a fixed size of RLIMIT_STACK. A compat-mode thread SHSTK > > has a fixed size of 1/4 RLIMIT_STACK. This allows more threads to share a > > 32-bit address space. > > I am not understanding this part. :) Entries are sizeof(unsigned long), > yes? A 1/2 RLIMIT_STACK would cover 32-bit, but 1/4 is less, so why does > that provide for more threads? Each thread has a separate shadow stack. If each shadow stack is smaller, the address space can accommodate more shadow stack allocations. > >[...] > > diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c > > index cba5c7656aab..5b45abda80a1 100644 > > --- a/arch/x86/kernel/cet.c > > +++ b/arch/x86/kernel/cet.c > > @@ -170,6 +170,47 @@ int cet_setup_shstk(void) > > return 0; > > } > > > > +int cet_setup_thread_shstk(struct task_struct *tsk) > > +{ > > + unsigned long addr, size; > > + struct cet_user_state *state; > > + struct cet_status *cet = &tsk->thread.cet; > > + > > + if (!cet->shstk_enabled) > > + return 0; > > + > > + state = get_xsave_addr(&tsk->thread.fpu.state.xsave, > > + XFEATURE_CET_USER); > > + > > + if (!state) > > + return -EINVAL; > > + > > + size = rlimit(RLIMIT_STACK); > > Is SHSTK incompatible with RLIM_INFINITY stack rlimits? I will change it to: size = min(rlimit(RLIMIT_STACK), 4 GB); > > > + > > + /* > > + * Compat-mode pthreads share a limited address space. > > + * If each function call takes an average of four slots > > + * stack space, we need 1/4 of stack size for shadow stack. > > + */ > > + if (in_compat_syscall()) > > + size /= 4; > > + > > + addr = alloc_shstk(size); > > I assume it'd fail here, but I worry about Stack Clash style attacks. > I'd like to see test cases that make sure the SHSTK gap is working > correctly. I will work on some tests. Thanks, Yu-cheng