From patchwork Sun Mar 19 00:15:34 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: 13180167 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 6E9CDC7618A for ; Sun, 19 Mar 2023 00:17:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E7C64280026; Sat, 18 Mar 2023 20:17:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0642280001; Sat, 18 Mar 2023 20:17:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BE2A8280026; Sat, 18 Mar 2023 20:17:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A6A77280001 for ; Sat, 18 Mar 2023 20:17:02 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 89B42A0759 for ; Sun, 19 Mar 2023 00:17:02 +0000 (UTC) X-FDA: 80583732684.13.422A002 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by imf01.hostedemail.com (Postfix) with ESMTP id 7FD8A4000E for ; Sun, 19 Mar 2023 00:17:00 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SsA4Rvc8; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf01.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679185020; 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=BYztGyXeSgKmXCeXVyD+9/C85SU+OQNRYv4uYO65s38=; b=g+oMM9Nn0gjnDk232/MH8xWNh3W3iIK1t8YGkKrrllDQCsVO71NwMPWHnODbBVIecUzm9B DgWbYVDMonJi7vbCfd35Jb3UE1lhhppyAVsR3to7cLMrB5CR/cN3CoxNjUX0iVgcN4gAbJ X9jbs/+6KmiL3c38q2ZFVRyw9rMIT54= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SsA4Rvc8; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf01.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679185020; a=rsa-sha256; cv=none; b=4vKgB1mx6hm6HsYGtqOt7QgJusLCby+4g6yazcFQuULlAlTVIj1t8DMlMYYPaao9Ay3E/5 E/v5Smg7K5jzQ758nIA6YsA9iDP6B5fPmljDxUlkzwoQGZVbhlZHsyuZvYmRz5ppsGzfqC /XOsuz8MXen2QJyRyTEXAG7STX/rJ88= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679185020; x=1710721020; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=7ZxUjaRiJBiGSF6h5aVdSGV+Yc7zbFIJZDgLv2rDKvs=; b=SsA4Rvc8CVmraXZ39TjzSEDuG7OXqD056TcZlQof9r8Yq+NXLhQdb6Nk N2zuVJDJ87XNamYwJ723lK3bzt6KxuK1G9l/9VWU80lw+fbjCbVN+j7o9 d5OqYI9dCI3BbNbQq4lVPD8RdwYV+wgeylmeFGsXVCWG3cJel0ysL3EcJ yoUVPeaF0urRDt6R/a8MHBGuN4Ar6GdTiPhhNOaKetM4/hai0TZNrcHsu eNru20IaWU7B7U4/GRUPRAp69pDyl/ayHmmCbZj5/rIaH7uV4FyoQAprc RNt9bvdgxbueKE9t7zfc6FHgrPK+zHutGZ/azTC8pxWl24GrNYjXFyiPw w==; X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="338491614" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="338491614" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="749673018" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="749673018" Received: from bmahatwo-mobl1.gar.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.135.34.5]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:58 -0700 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, david@redhat.com, debug@rivosinc.com, szabolcs.nagy@arm.com Cc: rick.p.edgecombe@intel.com, Mike Rapoport Subject: [PATCH v8 39/40] x86/shstk: Add ARCH_SHSTK_UNLOCK Date: Sat, 18 Mar 2023 17:15:34 -0700 Message-Id: <20230319001535.23210-40-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230319001535.23210-1-rick.p.edgecombe@intel.com> References: <20230319001535.23210-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 7FD8A4000E X-Stat-Signature: xokmiwmshpd9adu5ncdb4bm1r7cyofht X-HE-Tag: 1679185020-942599 X-HE-Meta: U2FsdGVkX19OlDHbH1I3cckg1VeYDM5j2tY1MGSuUCdJc3y5TBFGOS1UVaqC91jULX+873s6j9aSi6orTBICgvUFrXiL2t6QhloFoB7+wXzV+mRLpt/b0naEirgxa9hT0L37Du8FYOHlAa2sL3YwnRnb1kq3nzts5BQL49nwLOIXHliXcv34iKWdcqHQWlHTRIc/VeIZpYnnKoYDQ1fHvntzcxfifSBp7kMvgWTmD4sdpZk0zCnls7WK2upOV1MeOux6PuM8YnhoTjnVDQ/TzpqKgDfEZv3TulkBjlqpc8nFEsr4BCsKcy7eH9IPSQxC4E9HkF4ONw7jlqxuPQ7HD2gTQQnCr7BsErElKtpXRI4hGYYzkqIIDp8Zxtaz169C9xjWhwWpCQO8ALXzsOBiR5y7y/UV6ZrUeFA9sQUHF66UhO+AFA38tX32MJwhQsKRDkhhk4Lt4T84w624HLu63EYhNz1t8FbH7xWtSwaz7IU33x0JqDxdNAnTsgKywx0SDXaUdwaKjYqHY7rQC//zzLR3Ns5ZslLgEZXyJsPT3XYOyMMTHn6KJeOrbl99K78wMijkar5qDGUau/LxyonAJBH4bQK343PSVs9a69tLsQsXkHFkjrHvIZ30PXKEi2OSTTBwvmEYjWzVkrcUb6/TrGTpgWYoCBjs1j4dS6yh20GTfBu5RlAvDl5tbJIhNutlHN0u5xL67CaiNZnrKfNJWpr0uS628WovwehRo7vpEYW23nOC+SrNeI5f8ddzkgA31P0DJ4uIyMr/x6RpZwJ69QwZz4Jbn+iVMficg0eymMS+CC7NMOzTkyreSJTuvrwbHYT+g2d1ONCmq1CWR+KBEj1evcfEA9QIje7lLMgk9ifx8UStOtQtbqnG0Mn/Q7PlY49un9Z3dQTlyzTZAK30mZC6suHTsn9wNt0k0ea34bNueF3TtSU4iWG2JExaxUg84kudsujLBMmvu5Hwb/d JS9JAFIb xXY5X9syvZpk4XMlxQX1BpXCfBzIvyPixR2k2R8iMHPYLkJwU+Dkatfcxd6VHkbvaTg9DU1PLsbwh6s2/GNMl1XwY0yk2PS5VzVlQvb4COauT4K137AEn5S/y3qxbwhCitCX0wDlUMpsLBAUCq2lTaU2dTitkhuD1QqOWgY+5bbLzSbWRXojGCQ6eeaB54Uej5Srt0aMwYyaeTt0oxwfDfFHzA2a/s58Ydkr76yIMVWh15kTO8YRjxna4nFKHRus+YXtcB7iySHX3e32QA5fXO7s6LaXCZj1W0zv7PX6t+Og4osPERmTAYNJPWVGI/WBA3qf/CYTcVzXB/hzvDYq4O5eg9fg25FXKeutOWqHAVSso6tU= 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. [Merged into recent API changes, added commit log and docs] Signed-off-by: Mike Rapoport Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook --- v8: - Remove Mike's ack from his own patch (Boris) 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 f09afa504ec0..f3553cc8c758 100644 --- a/Documentation/x86/shstk.rst +++ b/Documentation/x86/shstk.rst @@ -75,6 +75,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 9bbad1763e33..69d4ccaef56f 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 ee89c4206ac9..ad336ab55ace 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -459,9 +459,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)