From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA4D4C43460 for ; Tue, 6 Apr 2021 22:49:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A8811613E1 for ; Tue, 6 Apr 2021 22:49:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344142AbhDFWtx (ORCPT ); Tue, 6 Apr 2021 18:49:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:56884 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234634AbhDFWtw (ORCPT ); Tue, 6 Apr 2021 18:49:52 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9A9B8613E7 for ; Tue, 6 Apr 2021 22:49:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1617749383; bh=a1o900AcqYwmW0fqkduPTeR3emc4C6Rs1gEIEqdiYaE=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=KEGDvjznudoHSkkT4y8V8E5/qd3x4roetFh+RpuJqloQCxb5ul1FSIFFwdyhhkCVE CW+F27jDoneZukSbicGCRN0vGYvja89yI0gmXYVVfuw1C0Nr1UXKNB5y8Vl78J6HXr YVwxAmbmm/iDQRLgUSYRZ6JfQoPE8yzmWh+bF1mVeJMCO2LkQKYupqhwy/3q3oHsWh 2Q2DIi5sxyP4cK22nuF0G4d/q3HAxigzoqqsaHEGuTYL5a9sYWneu4cyHAHjroNHJB 7Uie18iRLZqlQprOAHclOsKzv3IxX7iNngvfGKiu/X/4R8II/8UNOZd1jg+V8wzCer Mv1NB0hkTDMYQ== Received: by mail-ed1-f53.google.com with SMTP id k8so11036217edn.6 for ; Tue, 06 Apr 2021 15:49:43 -0700 (PDT) X-Gm-Message-State: AOAM531Dma0tTbT5vPlcGeI7IvyBTqIMz59GNqrb5QojYiDSdetSoqr8 yjFWIzS7pk0Ol8LapiO1VKA/GFnXPZ2huAY6kXpacA== X-Google-Smtp-Source: ABdhPJwKOmYqoxCioac2TWNaDNZOT88xkRzZYjrRVTEBe+20+kIF/hfmpbTf57P7Ofjo4GQDMBsu3KVolwUAhM7TefM= X-Received: by 2002:a50:fa92:: with SMTP id w18mr790243edr.172.1617749382023; Tue, 06 Apr 2021 15:49:42 -0700 (PDT) MIME-Version: 1.0 References: <20210401221104.31584-1-yu-cheng.yu@intel.com> <20210401221104.31584-25-yu-cheng.yu@intel.com> In-Reply-To: <20210401221104.31584-25-yu-cheng.yu@intel.com> From: Andy Lutomirski Date: Tue, 6 Apr 2021 15:49:30 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v24 24/30] x86/cet/shstk: Introduce shadow stack token setup/verify routines To: Yu-cheng Yu Cc: X86 ML , "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , LKML , "open list:DOCUMENTATION" , Linux-MM , linux-arch , Linux API , 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" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-doc@vger.kernel.org On Thu, Apr 1, 2021 at 3:12 PM Yu-cheng Yu wrote: > > A shadow stack restore token marks a restore point of the shadow stack, and > the address in a token must point directly above the token, which is within > the same shadow stack. This is distinctively different from other pointers > on the shadow stack, since those pointers point to executable code area. > > The restore token can be used as an extra protection for signal handling. > To deliver a signal, create a shadow stack restore token and put the token > and the signal restorer address on the shadow stack. In sigreturn, verify > the token and restore from it the shadow stack pointer. > > Introduce token setup and verify routines. Also introduce WRUSS, which is > a kernel-mode instruction but writes directly to user shadow stack. It is > used to construct user signal stack as described above. > > Signed-off-by: Yu-cheng Yu > Cc: Kees Cook > --- > arch/x86/include/asm/cet.h | 9 ++ > arch/x86/include/asm/special_insns.h | 32 +++++++ > arch/x86/kernel/shstk.c | 126 +++++++++++++++++++++++++++ > 3 files changed, 167 insertions(+) > > diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h > index 8b83ded577cc..ef6155213b7e 100644 > --- a/arch/x86/include/asm/cet.h > +++ b/arch/x86/include/asm/cet.h > @@ -20,6 +20,10 @@ int shstk_setup_thread(struct task_struct *p, unsigned long clone_flags, > unsigned long stack_size); > void shstk_free(struct task_struct *p); > void shstk_disable(void); > +int shstk_setup_rstor_token(bool ia32, unsigned long rstor, > + unsigned long *token_addr, unsigned long *new_ssp); > +int shstk_check_rstor_token(bool ia32, unsigned long token_addr, > + unsigned long *new_ssp); > #else > static inline int shstk_setup(void) { return 0; } > static inline int shstk_setup_thread(struct task_struct *p, > @@ -27,6 +31,11 @@ static inline int shstk_setup_thread(struct task_struct *p, > unsigned long stack_size) { return 0; } > static inline void shstk_free(struct task_struct *p) {} > static inline void shstk_disable(void) {} > +static inline int shstk_setup_rstor_token(bool ia32, unsigned long rstor, > + unsigned long *token_addr, > + unsigned long *new_ssp) { return 0; } > +static inline int shstk_check_rstor_token(bool ia32, unsigned long token_addr, > + unsigned long *new_ssp) { return 0; } > #endif > > #endif /* __ASSEMBLY__ */ > diff --git a/arch/x86/include/asm/special_insns.h b/arch/x86/include/asm/special_insns.h > index 1d3cbaef4bb7..c41c371f6c7d 100644 > --- a/arch/x86/include/asm/special_insns.h > +++ b/arch/x86/include/asm/special_insns.h > @@ -234,6 +234,38 @@ static inline void clwb(volatile void *__p) > : [pax] "a" (p)); > } > > +#ifdef CONFIG_X86_SHADOW_STACK > +#if defined(CONFIG_IA32_EMULATION) || defined(CONFIG_X86_X32) > +static inline int write_user_shstk_32(unsigned long addr, unsigned int val) u32 __user *addr? > +{ > + asm_volatile_goto("1: wrussd %1, (%0)\n" > + _ASM_EXTABLE(1b, %l[fail]) > + :: "r" (addr), "r" (val) > + :: fail); > + return 0; > +fail: > + return -EPERM; -EFAULT? > +} > +#else > +static inline int write_user_shstk_32(unsigned long addr, unsigned int val) > +{ > + WARN_ONCE(1, "%s used but not supported.\n", __func__); > + return -EFAULT; > +} > +#endif > + > +static inline int write_user_shstk_64(unsigned long addr, unsigned long val) u64 __user *addr, perhaps? > +{ > + asm_volatile_goto("1: wrussq %1, (%0)\n" > + _ASM_EXTABLE(1b, %l[fail]) > + :: "r" (addr), "r" (val) Can you use the modern [addr] "r" (addr) syntax?