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 D4444C47089 for ; Sat, 3 Dec 2022 02:44:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235061AbiLCCoZ (ORCPT ); Fri, 2 Dec 2022 21:44:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234850AbiLCCoY (ORCPT ); Fri, 2 Dec 2022 21:44:24 -0500 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D3D4D2F7 for ; Fri, 2 Dec 2022 18:44:23 -0800 (PST) Received: by mail-pl1-x633.google.com with SMTP id b21so6254785plc.9 for ; Fri, 02 Dec 2022 18:44:23 -0800 (PST) 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:message-id:reply-to; bh=4dlKif1ZvnqP6fqB1QWIg1KA4Ojy8aY4ojSm5QzsaqQ=; b=JYaR+zgpAk73txGe1j+x7Gd0Q9OfSP4rlxVQK/OupY+gNmcJR/9STitKgFIuNdWvIq wRPfTZ2RrIqeDWV19i5hwCyXUczsjcZCfpCdaLhdh31xbEzQoo5bKan/pBTBqFZmLTiY t7MHgfxNSYr+jiGRZbORKDLpvhHxAm3qdLS1U= 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 :message-id:reply-to; bh=4dlKif1ZvnqP6fqB1QWIg1KA4Ojy8aY4ojSm5QzsaqQ=; b=OCPVgECIQ6OSYwGaRRl3uU3/DLyav440oD4RBUFze7fgbJubokHk1GhNrZHCM8QZxz 8EhDiAtNK9zvhBiVZOBruzc2AUv0zE/dqI6BSPZDFYGZ3SPELQIVO8cGeOTgQ/0UsmQQ TrXGodnCL/tPCqV2s/X6aXbQLA2pkKcY1j88YWBNz4cGRHtf4pPy34sWw168Wi9i7H9P 1Fx/FHlSNFZKgFMM+a+AZRfkol4JxZzd6WnI1DMCqG4UB+Bq6Ja/JUqmF4OuKvXNe3Ni lRdk3y2XyrGTpU2uj8xZnsm9ZcUnEgp9wRaBHefeiCgs91UryzcQmBCaSbxGsSVj7g5I iuyg== X-Gm-Message-State: ANoB5pkYnxju1DYK3EGztNKQ6gRJBqq+mOXwDdMODF9zwsalkHNSYbiH wPqXzwnuH8Y2tQAdvR7QEAsqTA== X-Google-Smtp-Source: AA0mqf4tSqXeGiuc5Pf6/1RmNBUy/t0YdoCLyWnsfRj5Qv75KbKNER0Hx6ZISuteeyLDCXFEDpLbjA== X-Received: by 2002:a17:902:7885:b0:189:1366:fba7 with SMTP id q5-20020a170902788500b001891366fba7mr56854862pll.45.1670035462913; Fri, 02 Dec 2022 18:44:22 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id y23-20020a17090264d700b00187197c499asm6262673pli.164.2022.12.02.18.44.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Dec 2022 18:44:22 -0800 (PST) Date: Fri, 2 Dec 2022 18:44:21 -0800 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 , 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, Yu-cheng Yu Subject: Re: [PATCH v4 27/39] x86/shstk: Handle thread shadow stack Message-ID: <202212021844.D61875795@keescook> References: <20221203003606.6838-1-rick.p.edgecombe@intel.com> <20221203003606.6838-28-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221203003606.6838-28-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-api@vger.kernel.org On Fri, Dec 02, 2022 at 04:35:54PM -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 > 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 > returning from the vfork() calling function, using CET. Glibc does this > by adjusting the shadow stack pointer in the child, so that the child > receives a #CP if it tries to return from vfork() calling function. > > Free the shadow stack on thread exit by doing it in mm_release(). Skip > this when exiting a vfork() child since the stack is shared in the > parent. > > During this operation, the shadow stack pointer of the new thread needs > to be updated to point to the newly allocated shadow stack. Since the > ability to do this is confined to the FPU subsystem, change > fpu_clone() to take the new shadow stack pointer, and update it > internally inside the FPU subsystem. This part was suggested by Thomas > Gleixner. > > Tested-by: Pengfei Xu > Tested-by: John Allen > Suggested-by: Thomas Gleixner > Signed-off-by: Yu-cheng Yu Reviewed-by: Kees Cook -- Kees Cook