From patchwork Mon Feb 27 22:29:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13154271 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 B4E26CA9EA0 for ; Mon, 27 Feb 2023 22:32:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2C6D36B00AE; Mon, 27 Feb 2023 17:32:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 229DB6B00AF; Mon, 27 Feb 2023 17:32:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C9066B00B0; Mon, 27 Feb 2023 17:32:05 -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 E6DFC6B00AE for ; Mon, 27 Feb 2023 17:32:04 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C5AEC1C5AD0 for ; Mon, 27 Feb 2023 22:32:04 +0000 (UTC) X-FDA: 80514520968.01.250CD6F Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf24.hostedemail.com (Postfix) with ESMTP id BCC9B180009 for ; Mon, 27 Feb 2023 22:32:02 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=eyOA9yPc; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 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=1677537123; 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=njwT8/nseUUwtQUAH3WeQoFGEFftjwWBmHlQjdHCBPs=; b=qZTg7n1DWczbnn611gO7ghDP694czpBf+YeBGvVZsAqYhkuw+yiYi84a0SLzGjgjUQXvYB lJzUUoddSz/bKH12e2IZ2N+q3sBk+FTOIuWQuSuNrUci/Vt+WHshrvEJ34k8ErwqkhaI8E TuChGJrCvKGueGSvIQg5v8JELzfvd2o= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=eyOA9yPc; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf24.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677537123; a=rsa-sha256; cv=none; b=amp4hdtb2wSPTyqph14IAxFSiM9fHZYdtSdex00/zFUl5njWUR4z/59HSgoYHM2fRLHehf uteLHxcEC1FynGNxcYs6t4pDCP62Gx1eQiJSPbvk8Ojmo0V7W4HLkYOUckE63S2fExAKwl 7ATWOfCrNzbr0nqPFXxVsztBTuoYI3I= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537122; x=1709073122; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=ND8Cme6dUKUNgrJvq4rzFWKNWp2gSll9XobN53PYr6I=; b=eyOA9yPcLqW8nbUF4vQARdTUev+INKPKdzAidptpl04iC4YXbfDIfvld vCcCz4FAssyIbzUSQtcfROezZn2h9FC98Luj5EN+Uz2E6s5B7oRZEIIV2 xRhKUPVu+9UCl+RF4cV06+7a04WBDKRo1cRzMWwB8qWenMps940kuhnIl iW9mvZUEeQWM1VaWIvptFghs2k39YEGQV5sfT/Osoz1q4zrRHdcyGMS+L bsSHE87B9b+Sa0UB5psiqVvRpUoTKwsHP255R8p2xCRHe8tCFBezNR3QA xYPrxVjcTzj2xOCbyUSAysgAhUINDSDXv7ZgONwDw0qpJgaPfOKxc+7nY A==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657983" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657983" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:37 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024819" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024819" Received: from leonqu-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.72.19]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:36 -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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Mike Rapoport Subject: [PATCH v7 40/41] x86/shstk: Add ARCH_SHSTK_UNLOCK Date: Mon, 27 Feb 2023 14:29:56 -0800 Message-Id: <20230227222957.24501-41-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230227222957.24501-1-rick.p.edgecombe@intel.com> References: <20230227222957.24501-1-rick.p.edgecombe@intel.com> X-Rspamd-Queue-Id: BCC9B180009 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: fpnu1sir7hn193k76d5acjkegpkzgqxw X-HE-Tag: 1677537122-608467 X-HE-Meta: U2FsdGVkX18NodDSIlpZ8jk1ByaZ1vNwxzWzAZx0OsdRWFKrG6khNrXnbsQefV/cHy9tmCsKoO5T7SnPwAM7bqTMG7eU8Ym8+s5uqf+n1itrSHfEjvH1xQKTfSiSWsXH0tO8UD727EJr2mQQ4Lj6tsUn/FOD9JWtIlOs+ZEA19eLVLS1ZIJcFltRdEsz5j9yuiSC+astwqKOH0hTnEDOaVF2muDyjqg57eENz8SOGzwZLyPFDJrC9oQk0ufgKbSPI4VbXjjJJ7OxjiS+wBzV4SgWEhyCCXOWqI8V0CthLx7DS1Uvv0I3vGkMxYXBwiXj2fZX2jcxLJ6l+xEnc6MXwDPeAvUtIMNh2kraww2uN9jRBytCTUzMQZ5+tSpywWU4w/upEgmhBL0xFtGjAzfsaBIR6boCO8ErzLRK5yWLpvUngB1TIYTaMaLLrr1wtnenA4z94tiu0Ji+SMAFBrd/wS5H1PUPNQ9VfePieGSSGL/eKlungf4mH/YjBnJIoqG6Ns1CxKXX0sgJzOnjdpPtXbzQlcZwqaW85rJLWxZ03Nrpw4kBnljE0d/EWsmd8XRuNhYRsbIbIcrySCPJwIaVKLPzGhV4pU9sFppjHygECWO8tUrTBD1tg24B7YWoRlSmwdMu45Ai0HqqLCIXfrAtITGcGW+AD7vRzlS9XPeQvLbv+K/PRkfzI8/PTEDZ0b5p5L2sKsl3EZ1eCJgw5pbkJdz45TER2gRmjjlN8w0OK0MGU+y51S0UvDfxyDwTtJ3Lj1kREy/YW/mcOS7a9uWBhuNxOQxF+Fhn0XUyb6EL/m6qWhZkBqFzTCA7J2I+PpIiPkZDVE73+hEy2fXzdrjc944wVINc1iWRGOCwUwchgspvRxDrS/UhqkeGhV4HTuusH/TPmxuJPc1Xypp2zoehJJnoGp5Dzd6ReISNDkTQXH9u45WhSJc/z77Te6amCmWlYyQBoGecIQVHS7trpXt TmAKlFJJ Pv/nzQ3B5aKmRPQK/de2kqs6pbsxLydEmWr0DFiR2aOusgRS8lMIVYrZl1yPESzRR4duefIv/P3fbszdDVY/kNiJhPW/gS00uDwT7sHjeBIC3hq1RjALAs5m5+YYq+enkFtb12y2MUAPVZTqa3/jyDE/XZ9u3bxYh5uNTFyqsaNd4ImvetfwfnbKKh45jqkNiExlijpPW9jH8yph0sSaB9NqApTZXfR4FIPMmybk0voFFRzEfNnIzFSBeFOrZGxsmbK+CxuEhfcYq7JuXAGLMBeS/XGs7kUxONRGvj9sE/C3tZ4p2f0D/1+KzZFtGIt7iGa0wESYGeUg3HG9nZ9GlGvpEPa48P0kudIB9SpEoVEsEisq3cqi/E1UBKuojrURigVcg24jgj6uF+GjjDZzMDtvt1APejEXKItuy 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. Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Acked-by: Mike Rapoport (IBM) Reviewed-by: Kees Cook 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 2faf9b45ac72..3197ff824809 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -451,9 +451,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)