From patchwork Thu Jan 19 21:23:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13108816 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 18C5CC678D6 for ; Thu, 19 Jan 2023 21:24:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8AB3A28000F; Thu, 19 Jan 2023 16:24:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 85B65280001; Thu, 19 Jan 2023 16:24:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65F8E28000F; Thu, 19 Jan 2023 16:24:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4D7D0280001 for ; Thu, 19 Jan 2023 16:24:28 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 355E6A033A for ; Thu, 19 Jan 2023 21:24:28 +0000 (UTC) X-FDA: 80372827416.23.122E243 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id 3843F180016 for ; Thu, 19 Jan 2023 21:24:26 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FHI692CH; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674163466; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=JxHOoebEC0ntNY7urTWVeuLnqJvKXHl/N1WxuofeGTg=; b=ZsWZtDzq2mpO0fA75viHKhKOw0Hc7Qm6MBrq74SmN2w7Bez2hOkdYWP+JRzNUnWTA1Z3FF LCyBTELt4G52KrRrg7LZIJFztIarncXJKdTnh3k94yR7cvgb1rxMi4NnBI+bQhU1wiDEhN iPYDR6+fZ8rrRDqH4I+OBcDiB3TrKyY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FHI692CH; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674163466; a=rsa-sha256; cv=none; b=cmagROUQkh7pOE42Jbe+ymfnCY+k2SonFnyMr4pfETJoLkYamup0n78Q6cefiuAnHwK45l cbzmoFe1lZrpY66ruC4E59dlV4w7uYatH2FmJEdtxmyqYVXO1ksda0gld/GCxfVxH0MEFT jbOcS9VEPxns2+lN+Z0l3gmuo8AZGP8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163466; x=1705699466; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=SwMjGEgQ4NIbBGCsK+je+PfgR9ALTBIfAb8eu9YmH38=; b=FHI692CHEeFbm/zc1xCzAjQjtiY/mhwjmySAACfbqKWkU4J76GC0Hycz SRpx2bO5BXpGMq++h9DMEGhb1cA0iI3QFKF9Bwi3QMh/2uPu5eUOkBPsR rz0tayBlWGPOKb82Qux+nf4ZYKYB9Pbk5sxErJOwBpJTCawuNwxhoQSC+ o+Qe4U95ggZ9V1hXYfA1v1A7U+jJPfBZgQrBNkbCspiyavlH0dLSXIP51 DO2YRSkjunRdlvSR18L33ZWTdCf7Hyf4oygvi2NF+JBshpjYLhyr+klSB ZNA2xaJrDvtINxXHPHGrFTaFcSvlk38wjPzMdroM4gM5U/dnAY19YCNni A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323120094" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323120094" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:24:25 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139196" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139196" Received: from hossain3-mobl.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.252.128.187]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:24:23 -0800 From: Rick Edgecombe To: 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 , 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 Cc: rick.p.edgecombe@intel.com, Mike Rapoport Subject: [PATCH v5 38/39] x86/shstk: Add ARCH_SHSTK_UNLOCK Date: Thu, 19 Jan 2023 13:23:16 -0800 Message-Id: <20230119212317.8324-39-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230119212317.8324-1-rick.p.edgecombe@intel.com> References: <20230119212317.8324-1-rick.p.edgecombe@intel.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3843F180016 X-Stat-Signature: pgc49s49u11h3bj316eb36xr4s7y1o15 X-Rspam-User: X-HE-Tag: 1674163466-162747 X-HE-Meta: U2FsdGVkX1+peVQ98X15rdzug4RhM3bXPTZIWkJbo7/9u6+S3D2Iu2aPaNANvZhku6gBNj2QOaNxYLXYSpi1FZgZqgy8ysvJRleWc26kTsYURophHcGmAUAhHo1VCmZ+lR6FThxHYUeEBn6z664rWJU6r+tudLcr8Fp8BFy+4r7MCE1nv0n6ZrroaiE58m3c31Gz2FuNyuh2VBa2+4eNZtRZ/6ij/hJJLc7tngbClYdee8La84keHsA2JYz6NxcNI+I94aitw1DzU5u6VTLPLpsrBllTSi3BjsP/ByS849/QiUHJH+oRI1ix2fE3I8yATvuTZ2ur42ayqgMXq8CS3I87ECa0EUlQOYTyxCtbLkVC4qKr3P8J+9LLx1Nu31HrQgQ4tE57UJca+ozA1FWlxYEf+KCa+8+V0o9Rx7bg1ZY7cbyCviQqH+yZjfs3exsRQ/0jITx0XiUuLa3jYJKz5Jb28iihaebuXqqx8uP2/w5UJ5PEmOp3lDUuo73V2IsRuF8571cGUnCLo//W6vFBxSxXU/kTw+SlDXS5c1VemNIEtNsnqfTXcz/QZzNG02GmRlFX9cqkufd1eYbad1peyEa0PZDp0vfRsWx4WMPn3cXFzvvjYxBWqSZRQjNlG4l/lPSkmuMB6OAXoCHmlfado5118XR4b/4m38gDY6ytGTAGuzWaUCWhtkWt/XqudludU5bL1U9d+cqYlIwMdkfcLZuhezpVZFiFa/xXANis1ZtH5O1J/Yo/0GUQL6DV2qPIs1rdOnEE1JuwGcSL9+cp82+8n08XWAWpFCky6t5UTB5YoH7eC4u6lVYXxq+E33JFA8l9XDUSMXXo0hN85XHIw7Wwy1sZ2QXOFEG0erXuW3Qef65VZxEYgA3EeghV0quJ8pptF0jQnHg8hJWGuJm4II36SCPn8WyTy0/V//lvr+fzzyC35dwetJoBkZWQEHFNGUKwEqYqMv4ZF9Thfcv K1wez+8u kOh/rtz8F+rptPtaIFCTxV5ilUKvnvPt5luErlqfcDJJK3TBmTRS23EnOrL81kx9btC2ijpLMMTy7z0bDQOmn2eZ9g1RbUn+E+p/dIkAt6Q94HS9o9kz/3wBwVp2Fv8Ql+8kN+x/d28yBFJDDPZS74JfIO7DkXBvyMCuvF+W+9JaJx+Lp+WRrkRAo9o7gRL9P01ETBw+totw+lowDFmXYbSsHbVIhlaGaC8KeM+YS6zMT1Vr4R2EWPZxg7LM9AVI0Szex8eN0254nQURzIQWKA+FDjztgvsruKNywbov/qJN66G6v1CuhpL4Mqo943UAKO6gzaWMD44aOo+OyqAFDyiTBVbtcLRO4plxY+dllPYT1n/A= 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: From: Mike Rapoport Userspace loaders may lock features before a CRIU restore operation has the chance to set them to whatever state is required by the process being restored. Allow a way for CRIU to unlock features. Add it as an arch_prctl() like the other shadow stack operations, but restrict it being called by the ptrace arch_pctl() interface. Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Mike Rapoport [Merged into recent API changes, added commit log and docs] Signed-off-by: Rick Edgecombe --- v4: - Add to docs that it is ptrace only. - Remove "CET" references v3: - Depend on CONFIG_CHECKPOINT_RESTORE (Kees) Documentation/x86/shstk.rst | 4 ++++ arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/process_64.c | 1 + arch/x86/kernel/shstk.c | 9 +++++++-- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Documentation/x86/shstk.rst b/Documentation/x86/shstk.rst index f2e6f323cf68..e8ed5fc0f7ae 100644 --- a/Documentation/x86/shstk.rst +++ b/Documentation/x86/shstk.rst @@ -73,6 +73,10 @@ arch_prctl(ARCH_SHSTK_LOCK, unsigned long features) are ignored. The mask is ORed with the existing value. So any feature bits set here cannot be enabled or disabled afterwards. +arch_prctl(ARCH_SHSTK_UNLOCK, unsigned long features) + Unlock features. 'features' is a mask of all features to unlock. All + bits set are processed, unset bits are ignored. Only works via ptrace. + The return values are as follows. On success, return 0. On error, errno can be:: diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index e31495668056..200efbbe5809 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -25,6 +25,7 @@ #define ARCH_SHSTK_ENABLE 0x5001 #define ARCH_SHSTK_DISABLE 0x5002 #define ARCH_SHSTK_LOCK 0x5003 +#define ARCH_SHSTK_UNLOCK 0x5004 /* ARCH_SHSTK_ features bits */ #define ARCH_SHSTK_SHSTK (1ULL << 0) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 71094c8a305f..d368854fa9c4 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -835,6 +835,7 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) case ARCH_SHSTK_ENABLE: case ARCH_SHSTK_DISABLE: case ARCH_SHSTK_LOCK: + case ARCH_SHSTK_UNLOCK: return shstk_prctl(task, option, arg2); default: ret = -EINVAL; diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 07142e6f05f6..a639119a21c5 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -452,9 +452,14 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long features) return 0; } - /* Don't allow via ptrace */ - if (task != current) + /* Only allow via ptrace */ + if (task != current) { + if (option == ARCH_SHSTK_UNLOCK && IS_ENABLED(CONFIG_CHECKPOINT_RESTORE)) { + task->thread.features_locked &= ~features; + return 0; + } return -EINVAL; + } /* Do not allow to change locked features */ if (features & task->thread.features_locked)