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 29AC5C4321E for ; Sat, 3 Dec 2022 02:44:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BE1006B0073; Fri, 2 Dec 2022 21:44:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B90CA6B0074; Fri, 2 Dec 2022 21:44:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A32616B0078; Fri, 2 Dec 2022 21:44:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9120B6B0073 for ; Fri, 2 Dec 2022 21:44:24 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6337916069D for ; Sat, 3 Dec 2022 02:44:24 +0000 (UTC) X-FDA: 80199451248.19.FF91DB7 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf15.hostedemail.com (Postfix) with ESMTP id F34ABA000B for ; Sat, 3 Dec 2022 02:44:23 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=JYaR+zgp; spf=pass (imf15.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.44 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670035464; a=rsa-sha256; cv=none; b=oFbHcRnNw+IE59WQPVinhhv7mx1NXeegUKYOj6tB4N7ImFwApyHqhIvg8vsYzE3Xb3oyni JoGhzeF3yLvUEtKpoLXiFdA5HippYrH4QeV8nutd4mCrpWYMRBy1lDrowAoAOTSCDFRWdQ nNCqSuNsw97X2gyyg+9LKWM7cbGsuoY= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=JYaR+zgp; spf=pass (imf15.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.44 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670035464; 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=4dlKif1ZvnqP6fqB1QWIg1KA4Ojy8aY4ojSm5QzsaqQ=; b=LeVp8WwYdhPHFiXjUR/1fcG3EP48NuX0mYL9/emQePB70AaEDRgUGIluhZvDJwKvSE2Y4o IK+uUOcSphQxoe39ppWtzLr68EvNGazrKH0IbnFUOg22lb1XhL2htrpsxAK8f9JDoVjYVV hRtWLR9IqcWhg+dr9eO1TiKjxPOJSF0= Received: by mail-pj1-f44.google.com with SMTP id t17so6540742pjo.3 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=BN6RZKKpf3ehj12Ywb+bLz26Rxod92yIt/wKIxbSSgBZsEEl1HeuEhH8vcJny7IUcG 5N2hYddn8dXg4qwF1MRcRUjthfSxSTrpCBKJ/jD5uTvLUcw58inN8/H59xf4QTSLFgK6 5LW7M/HNpmBaxFFdNwmRmxK4XOIcswmO9hg387koETipp7yVo8Z0qQIyimLucm6WYHXE KZT2vczf4bI5Sw1/rL+i0TgP4xxLRo7x4IXCt7+lsb6D0XhEco5pEyxvGR+ZIebOjJ3k nWFFvHooi8YYZAAR+oGuVntDqnSekxapb4n+XsHIcI5BXhjPGklr8xIP2PDwEo+jmDAX aAxw== X-Gm-Message-State: ANoB5pk9kmft7tC9e+W6h4CIO/8Fi2U0caoVZMGqpeTMrszs7CjoFjfN J/nmllcFbDieeZnSAT6r7lzFhg== 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> X-Rspam-User: X-Spamd-Result: default: False [2.48 / 9.00]; SORBS_IRL_BL(3.00)[209.85.216.44:from]; BAYES_HAM(-2.62)[90.16%]; SUSPICIOUS_RECIPS(1.50)[]; MID_RHS_NOT_FQDN(0.50)[]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; RCVD_NO_TLS_LAST(0.10)[]; DKIM_TRACE(0.00)[chromium.org:+]; RCPT_COUNT_TWELVE(0.00)[39]; FROM_EQ_ENVFROM(0.00)[]; DMARC_POLICY_ALLOW(0.00)[chromium.org,none]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; R_DKIM_ALLOW(0.00)[chromium.org:s=google]; ARC_NA(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17]; TO_DN_SOME(0.00)[]; TAGGED_RCPT(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspamd-Queue-Id: F34ABA000B X-Rspamd-Server: rspam01 X-Stat-Signature: 9zhpi53sia677bsp79dnq117jp8xrcm9 X-HE-Tag: 1670035463-477670 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 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