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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF8FEC678D5 for ; Wed, 8 Mar 2023 15:26:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B4AA6B0072; Wed, 8 Mar 2023 10:26:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 26345280001; Wed, 8 Mar 2023 10:26:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 102AC6B0075; Wed, 8 Mar 2023 10:26:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id EECAF6B0072 for ; Wed, 8 Mar 2023 10:26:20 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id AA49414108C for ; Wed, 8 Mar 2023 15:26:20 +0000 (UTC) X-FDA: 80546107320.13.129B406 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by imf13.hostedemail.com (Postfix) with ESMTP id 4A37F20020 for ; Wed, 8 Mar 2023 15:26:13 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=temperror ("DNS error when getting key") header.d=alien8.de header.s=dkim header.b=dtU8MD7M; spf=temperror (imf13.hostedemail.com: error in processing during lookup of bp@alien8.de: DNS error) smtp.mailfrom=bp@alien8.de; dmarc=temperror reason="server fail" header.from=alien8.de (policy=temperror) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678289179; a=rsa-sha256; cv=none; b=oGWSDkij7CQeW3QnaHL8T/wnrOdfGKRzXcJZMgb4m0IIhLSeBwmw6ZyeDK0j7loUbEEImD +Z3TvkKM5rjwMfM/4n2GTfpAQZ8QwOy5tSpRbC1fPv/rVKjtw8qMHz4qxxh9O6WORc4JMh lPxTxn4H6dLjKJ4cBEIJKSqshpAq6XQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=temperror ("DNS error when getting key") header.d=alien8.de header.s=dkim header.b=dtU8MD7M; spf=temperror (imf13.hostedemail.com: error in processing during lookup of bp@alien8.de: DNS error) smtp.mailfrom=bp@alien8.de; dmarc=temperror reason="server fail" header.from=alien8.de (policy=temperror) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678289179; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0YZFEJ4cAs1XpubhR+bVW6Ra4NdOXmwmbEoV5/5yyRc=; b=AMzuG8oktW3l8tGzKtHtN9JyHW0Vdr46GutznNsherVr72tUABNtCM/RPG6EpwHZ5kzizo uIVmlbLnEu8UvheMeLPqlW9Z7OjdufD7tbIqGflZ9Ze/p4IsxDyBSB2fp7TT3DgIwZ1s0D ukh72Q5NTt3kQqXCYnlu2C6SXcETEvA= Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id E4F2B1EC053F; Wed, 8 Mar 2023 16:26:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1678289165; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references; bh=0YZFEJ4cAs1XpubhR+bVW6Ra4NdOXmwmbEoV5/5yyRc=; b=dtU8MD7MnP6eQPuPrVOrLw3IOhDbONhvPWew+BaeOWqCec5Wv2fzyDEfHf4wjHzLBlYNYH Z9maXvegSwDqzaejWBASPjzFR36tHjxJJ6jB9g4zq8roVzifvs06B6rFJJjnkgcS3kZlvd zE6MuI7N30FA56Zls5hIliZDyjtUSm4= Date: Wed, 8 Mar 2023 16:26:00 +0100 From: Borislav Petkov To: Rick Edgecombe 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 , 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, Yu-cheng Yu Subject: Re: [PATCH v7 30/41] x86/shstk: Handle thread shadow stack Message-ID: References: <20230227222957.24501-1-rick.p.edgecombe@intel.com> <20230227222957.24501-31-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20230227222957.24501-31-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Queue-Id: 4A37F20020 X-Rspamd-Server: rspam01 X-Stat-Signature: ode7jeqrup4cui5msn6jsrkmaom7kwsd X-HE-Tag: 1678289173-597052 X-HE-Meta: U2FsdGVkX1+UjTEfLvg5HE86BYiV4eVxYKyvrqD9InH6zfxmnrVVNWBdDOU61aDtP7gPxDXJ2z+p0aaYctej7ld4jPIlGiP5RDz8jaAYFqlbTWFL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MyvBQiYqFzCVgEeqkv8Z6tIMYtnA2ntIGhjsVNHOMcPB2OEUoPJ7ej6r29GI+xgcX19XrCoDePNf5E7Kv9YmK/Am+ozhIm90g+0= 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: On Mon, Feb 27, 2023 at 02:29:46PM -0800, Rick Edgecombe wrote: > From: Yu-cheng Yu > > When a process is duplicated, but the child shares the address space with > the parent, there is potential for the threads sharing a single stack to > cause conflicts for each other. In the normal non-cet case this is handled "non-CET" > in two ways. > > With regular CLONE_VM a new stack is provided by userspace such that the > parent and child have different stacks. > > For vfork, the parent is suspended until the child exits. So as long as > the child doesn't return from the vfork()/CLONE_VFORK calling function and > sticks to a limited set of operations, the parent and child can share the > same stack. > > For shadow stack, these scenarios present similar sharing problems. For the > CLONE_VM case, the child and the parent must have separate shadow stacks. > Instead of changing clone to take a shadow stack, have the kernel just > allocate one and switch to it. > > Use stack_size passed from clone3() syscall for thread shadow stack size. A > compat-mode thread shadow stack size is further reduced to 1/4. This > allows more threads to run in a 32-bit address space. The clone() does not > pass stack_size, which was added to clone3(). In that case, use > RLIMIT_STACK size and cap to 4 GB. > > For shadow stack enabled vfork(), the parent and child can share the same > shadow stack, like they can share a normal stack. Since the parent is > suspended until the child terminates, the child will not interfere with > the parent while executing as long as it doesn't return from the vfork() > and overwrite up the shadow stack. The child can safely overwrite down > the shadow stack, as the parent can just overwrite this later. So CET does > not add any additional limitations for vfork(). > > Userspace implementing posix vfork() can actually prevent the child from "POSIX" ... > diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c > index f851558b673f..bc3de4aeb661 100644 > --- a/arch/x86/kernel/fpu/core.c > +++ b/arch/x86/kernel/fpu/core.c > @@ -552,8 +552,41 @@ static inline void fpu_inherit_perms(struct fpu *dst_fpu) > } > } > > +#ifdef CONFIG_X86_USER_SHADOW_STACK > +static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp) > +{ > + struct cet_user_state *xstate; > + > + /* If ssp update is not needed. */ > + if (!ssp) > + return 0; > + > + xstate = get_xsave_addr(&dst->thread.fpu.fpstate->regs.xsave, > + XFEATURE_CET_USER); > + > + /* > + * If there is a non-zero ssp, then 'dst' must be configured with a shadow > + * stack and the fpu state should be up to date since it was just copied > + * from the parent in fpu_clone(). So there must be a valid non-init CET > + * state location in the buffer. > + */ > + if (WARN_ON_ONCE(!xstate)) > + return 1; > + > + xstate->user_ssp = (u64)ssp; > + > + return 0; > +} > +#else > +static int update_fpu_shstk(struct task_struct *dst, unsigned long shstk_addr) ^^^^^^^^^^^ ssp, like above. Better yet: static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp) { #ifdef CONFIG_X86_USER_SHADOW_STACK ... #endif return 0; } and less ifdeffery. > +{ > + return 0; > +} > +#endif > + > /* Clone current's FPU state on fork */ > -int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal) > +int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal, > + unsigned long ssp) > { > struct fpu *src_fpu = ¤t->thread.fpu; > struct fpu *dst_fpu = &dst->thread.fpu; > @@ -613,6 +646,12 @@ int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal) > if (use_xsave()) > dst_fpu->fpstate->regs.xsave.header.xfeatures &= ~XFEATURE_MASK_PASID; > > + /* > + * Update shadow stack pointer, in case it changed during clone. > + */ > + if (update_fpu_shstk(dst, ssp)) > + return 1; > + > trace_x86_fpu_copy_src(src_fpu); > trace_x86_fpu_copy_dst(dst_fpu); > > diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c > index b650cde3f64d..bf703f53fa49 100644 > --- a/arch/x86/kernel/process.c > +++ b/arch/x86/kernel/process.c > @@ -48,6 +48,7 @@ > #include > #include > #include > +#include > > #include "process.h" > > @@ -119,6 +120,7 @@ void exit_thread(struct task_struct *tsk) > > free_vm86(t); > > + shstk_free(tsk); > fpu__drop(fpu); > } > > @@ -140,6 +142,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) > struct inactive_task_frame *frame; > struct fork_frame *fork_frame; > struct pt_regs *childregs; > + unsigned long shstk_addr = 0; > int ret = 0; > > childregs = task_pt_regs(p); > @@ -174,7 +177,13 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) > frame->flags = X86_EFLAGS_FIXED; > #endif > > - fpu_clone(p, clone_flags, args->fn); > + /* Allocate a new shadow stack for pthread if needed */ > + ret = shstk_alloc_thread_stack(p, clone_flags, args->stack_size, > + &shstk_addr); That function will return 0 even if shstk_addr hasn't been written in it and you will continue merrily and call fpu_clone(..., shstk_addr=0); why don't you return the shadow stack address or negative on error instead of adding an I/O parameter which is pretty much always nasty to deal with. > + if (ret) > + return ret; > + > + fpu_clone(p, clone_flags, args->fn, shstk_addr); > > /* Kernel thread ? */ > if (unlikely(p->flags & PF_KTHREAD)) { ... -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette