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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29277C433F5 for ; Mon, 3 Oct 2022 20:29:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229988AbiJCU3P (ORCPT ); Mon, 3 Oct 2022 16:29:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229871AbiJCU3J (ORCPT ); Mon, 3 Oct 2022 16:29:09 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4084148CB7 for ; Mon, 3 Oct 2022 13:29:06 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id e129so10607293pgc.9 for ; Mon, 03 Oct 2022 13:29:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=sjiHVMgSJ5/4IaH3w5MgZlrFfbHSvGJ0QZv2CX5Q/Ws=; b=EcvIJqz3OKy+EfBwdQWABPnlwEoIn9TPS4LhPVQBLaYr92fAI04h6e0GN7Hvkhm3PT wSq+TKMvX4ENAjG/nSaud29ZGjfSaaHS6uMaHt3EhLLWZ5oSFWdSsGUVpfYlmUied/Ex 0Ses9zSA3UbzwJ0ZpnEKSo+vR5Hq5Uf2WCakE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=sjiHVMgSJ5/4IaH3w5MgZlrFfbHSvGJ0QZv2CX5Q/Ws=; b=lCEsXAIeZ492gCiq12Ep3ZB59CdmTlZospGPCYI6SMolEEBWeJLlv6tD65kHOB/vql vZBmG34LPqev4L7zoTmJdYsw9v612Q5ZMrBiIX+HTooOncnYrjOGPFVwOtorvQQScPmF DUi4Hf5IObF6VmKQ1gEMYt3ASkVQ29XyJ/Ig0M9q9fXKfhBaFOCn2p0LpqdD9Ni77lMS fHHicdjlGtrHjfmxXDuwMR51tEopmIF4RVaED8H5e5DwBC39+gabjkm2xMo67jQELMdb 5mcmmRN/gVT4Enl3Z3dwQG0TcSFf+oqFKysW9kLe8WAZDO91sFMLgPgQdMtr739rCaNf wlyg== X-Gm-Message-State: ACrzQf0fUpL4zPyzw1V8Mj7yxahC6uYCutngeDVOQhALcmFsQFj24mZb jKmQbroEBYd4g6sU9IZ172eSWQ== X-Google-Smtp-Source: AMsMyM4Gba8ZMDrS0NKETmREdN+X/ll6WDjF1VgZV8Lxe7Gg1+jFfe0zg0hHJQqHTr4zbWkyx69aNw== X-Received: by 2002:a05:6a00:2402:b0:52c:81cf:8df2 with SMTP id z2-20020a056a00240200b0052c81cf8df2mr23916900pfh.40.1664828945594; Mon, 03 Oct 2022 13:29:05 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id s12-20020a170902a50c00b001785a72d285sm7661699plq.48.2022.10.03.13.29.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Oct 2022 13:29:04 -0700 (PDT) Date: Mon, 3 Oct 2022 13:29:03 -0700 From: Kees Cook 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 , 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" , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, Yu-cheng Yu Subject: Re: [PATCH v2 25/39] x86/cet/shstk: Handle thread shadow stack Message-ID: <202210031317.4611D6A1E7@keescook> References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <20220929222936.14584-26-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220929222936.14584-26-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 29, 2022 at 03:29:22PM -0700, Rick Edgecombe wrote: > [...] > +#ifdef CONFIG_X86_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; My brain will work to undo the collision of Shadow Stack Pointer with Stack Smashing Protection. ;) > [...] > diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c > index a0b8d4adb2bf..db4e53f9fdaf 100644 > --- a/arch/x86/kernel/shstk.c > +++ b/arch/x86/kernel/shstk.c > @@ -118,6 +118,46 @@ void reset_thread_shstk(void) > current->thread.features_locked = 0; > } > > +int shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags, > + unsigned long stack_size, unsigned long *shstk_addr) Er, arg 3 is "stack_size". From later: > + ret = shstk_alloc_thread_stack(p, clone_flags, args->flags, &shstk_addr); ^^^^^^^^^^^ clone_flags and args->flags are identical ... this must be accidentally working. I was expecting 0 there. > +{ > + struct thread_shstk *shstk = &tsk->thread.shstk; > + unsigned long addr; > + > + /* > + * If shadow stack is not enabled on the new thread, skip any > + * switch to a new shadow stack. > + */ > + if (!feature_enabled(CET_SHSTK)) > + return 0; > + > + /* > + * clone() does not pass stack_size, which was added to clone3(). > + * Use RLIMIT_STACK and cap to 4 GB. > + */ > + if (!stack_size) > + stack_size = min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G); Again, perhaps the clamp should happen in alloc_shstk()? > + > + /* > + * For CLONE_VM, except vfork, the child needs a separate shadow > + * stack. > + */ > + if ((clone_flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM) > + return 0; > + > + > + stack_size = PAGE_ALIGN(stack_size); Uhm, I think a line went missing here. :P "x86/cet/shstk: Introduce map_shadow_stack syscall" adds the missing: + addr = alloc_shstk(0, stack_size, 0, false); Please add back the original. :) > + if (IS_ERR_VALUE(addr)) > + return PTR_ERR((void *)addr); > + > + shstk->base = addr; > + shstk->size = stack_size; > + > + *shstk_addr = addr + stack_size; > + > + return 0; > +} > + > void shstk_free(struct task_struct *tsk) > { > struct thread_shstk *shstk = &tsk->thread.shstk; > @@ -126,7 +166,13 @@ void shstk_free(struct task_struct *tsk) > !feature_enabled(CET_SHSTK)) > return; > > - if (!tsk->mm) > + /* > + * When fork() with CLONE_VM fails, the child (tsk) already has a > + * shadow stack allocated, and exit_thread() calls this function to > + * free it. In this case the parent (current) and the child share > + * the same mm struct. > + */ > + if (!tsk->mm || tsk->mm != current->mm) > return; > > unmap_shadow_stack(shstk->base, shstk->size); > -- > 2.17.1 > -- Kees Cook