From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kees Cook Subject: Re: [RFC PATCH 3/5] selftest/x86: Fix sigreturn_64 test. Date: Thu, 21 May 2020 15:48:52 -0700 Message-ID: <202005211547.793B36B@keescook> References: <20200521211720.20236-1-yu-cheng.yu@intel.com> <20200521211720.20236-4-yu-cheng.yu@intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Content-Disposition: inline In-Reply-To: <20200521211720.20236-4-yu-cheng.yu@intel.com> Sender: linux-kernel-owner@vger.kernel.org To: Yu-cheng Yu 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 List-Id: linux-arch.vger.kernel.org On Thu, May 21, 2020 at 02:17:18PM -0700, Yu-cheng Yu wrote: > When shadow stack is enabled, selftests/x86/sigreturn_64 triggers a fault > when doing sigreturn to 32-bit context but the task's shadow stack pointer > is above 32-bit address range. Fix it by: > > - Allocate a small shadow stack below 32-bit address, > - Switch to the new shadow stack, > - Run tests, > - Switch back to the original 64-bit shadow stack. > > Signed-off-by: Yu-cheng Yu > --- > tools/testing/selftests/x86/sigreturn.c | 28 +++++++++++++++++++++++++ > 1 file changed, 28 insertions(+) > > diff --git a/tools/testing/selftests/x86/sigreturn.c b/tools/testing/selftests/x86/sigreturn.c > index 57c4f67f16ef..5bcd74d416ff 100644 > --- a/tools/testing/selftests/x86/sigreturn.c > +++ b/tools/testing/selftests/x86/sigreturn.c > @@ -45,6 +45,14 @@ > #include > #include > #include > +#include > +#include > +#include > + > +#ifdef __x86_64__ > +int arch_prctl(int code, unsigned long *addr); > +#define ARCH_CET_ALLOC_SHSTK 0x3004 > +#endif > > /* Pull in AR_xyz defines. */ > typedef unsigned int u32; > @@ -766,6 +774,20 @@ int main() > int total_nerrs = 0; > unsigned short my_cs, my_ss; > > +#ifdef __x86_64__ > + /* Alloc a shadow stack within 32-bit address range */ > + unsigned long arg, ssp_64, ssp_32; > + ssp_64 = _get_ssp(); > + > + if (ssp_64 != 0) { > + arg = 0x1001; > + arch_prctl(ARCH_CET_ALLOC_SHSTK, &arg); > + ssp_32 = arg + 0x1000 - 8; > + asm volatile("RSTORSSP (%0)\n":: "r" (ssp_32)); > + asm volatile("SAVEPREVSSP"); > + } > +#endif If the headers and code are going to be repeated, I would put that in a shared header so they're not copy/pasted between these two tests. -Kees -- Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Thu, 21 May 2020 15:48:52 -0700 From: Kees Cook Subject: Re: [RFC PATCH 3/5] selftest/x86: Fix sigreturn_64 test. Message-ID: <202005211547.793B36B@keescook> References: <20200521211720.20236-1-yu-cheng.yu@intel.com> <20200521211720.20236-4-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200521211720.20236-4-yu-cheng.yu@intel.com> Sender: owner-linux-mm@kvack.org To: Yu-cheng Yu 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 , Weijiang Yang List-ID: Message-ID: <20200521224852.Pf3jLf4NFLAEYAL3MU2hIlh8NZbCBOs4utWzBWFrtK8@z> On Thu, May 21, 2020 at 02:17:18PM -0700, Yu-cheng Yu wrote: > When shadow stack is enabled, selftests/x86/sigreturn_64 triggers a fault > when doing sigreturn to 32-bit context but the task's shadow stack pointer > is above 32-bit address range. Fix it by: > > - Allocate a small shadow stack below 32-bit address, > - Switch to the new shadow stack, > - Run tests, > - Switch back to the original 64-bit shadow stack. > > Signed-off-by: Yu-cheng Yu > --- > tools/testing/selftests/x86/sigreturn.c | 28 +++++++++++++++++++++++++ > 1 file changed, 28 insertions(+) > > diff --git a/tools/testing/selftests/x86/sigreturn.c b/tools/testing/selftests/x86/sigreturn.c > index 57c4f67f16ef..5bcd74d416ff 100644 > --- a/tools/testing/selftests/x86/sigreturn.c > +++ b/tools/testing/selftests/x86/sigreturn.c > @@ -45,6 +45,14 @@ > #include > #include > #include > +#include > +#include > +#include > + > +#ifdef __x86_64__ > +int arch_prctl(int code, unsigned long *addr); > +#define ARCH_CET_ALLOC_SHSTK 0x3004 > +#endif > > /* Pull in AR_xyz defines. */ > typedef unsigned int u32; > @@ -766,6 +774,20 @@ int main() > int total_nerrs = 0; > unsigned short my_cs, my_ss; > > +#ifdef __x86_64__ > + /* Alloc a shadow stack within 32-bit address range */ > + unsigned long arg, ssp_64, ssp_32; > + ssp_64 = _get_ssp(); > + > + if (ssp_64 != 0) { > + arg = 0x1001; > + arch_prctl(ARCH_CET_ALLOC_SHSTK, &arg); > + ssp_32 = arg + 0x1000 - 8; > + asm volatile("RSTORSSP (%0)\n":: "r" (ssp_32)); > + asm volatile("SAVEPREVSSP"); > + } > +#endif If the headers and code are going to be repeated, I would put that in a shared header so they're not copy/pasted between these two tests. -Kees -- Kees Cook