From patchwork Thu Jan 19 21:23:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108817 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 9E529C46467 for ; Thu, 19 Jan 2023 21:24:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43F06280010; Thu, 19 Jan 2023 16:24:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 354C3280001; Thu, 19 Jan 2023 16:24:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 180B6280010; Thu, 19 Jan 2023 16:24:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E06FB280001 for ; Thu, 19 Jan 2023 16:24:29 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A8925140988 for ; Thu, 19 Jan 2023 21:24:29 +0000 (UTC) X-FDA: 80372827458.14.2A94BA7 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf27.hostedemail.com (Postfix) with ESMTP id A64BB40017 for ; Thu, 19 Jan 2023 21:24:27 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hB6vCRKw; spf=pass (imf27.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=1674163468; 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=cjMBHlgASRiVZwcEv7s62E9H2RxSF9urW8wYPqkrv6A=; b=KiXoOIZBty+5jeIuvILbsd76Z8S1cMfJeXdxXcsKg4o1TGeyx8Ess9z472jheG2x/8J567 YqD+eL1wCWw7W/sm7Mg8/8O9YMMOg3Sr3BKd8U82KXM+gkJvA3NaYniJJme0KKzgA4qsbL 6J0jeqj1Ls8fIptycnuJ4Ik7gZwscdk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hB6vCRKw; spf=pass (imf27.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=1674163468; a=rsa-sha256; cv=none; b=vEopv744AZv73rDTYLEOWholKGuRCy2Qtn0NMYSlBTMJP+C1drChd8gNWol8WV1ZxD7O0u p8eC/TZEf2yYUqUAtNMzcJhl89WpoCQAoSMfRQTAFcGrVrJaincdDdstCWs+es++RoXFfi +pvrm7a5GWfQmDxs4Y0hb8zAKRp4Di0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163467; x=1705699467; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Q/roafiLg5FWGNhLb00ov8wc44ATIJKREdVQC5GYlS4=; b=hB6vCRKwHo0DaWQQU0IWD04AZrGw0MWUD2JOGBWY6+mSgEB/3e4Zxi14 fa8TchgY7D/aB0YeR9TFE7ArusPocGQl1RubLl5fSivvG8ClKGEmJUjM8 XzVg/0oCleoZgTi4OFMclI4jcBdiAJ0Gix2S+DxTKJkrSiow+a5BVOZZn eUk5nxjJtvFcV66JjclE58wzy26j7X2TqSPPwOmWiCSayIQfY2iaVR4Gu SjKzPSazjSeu8V4wm/r6f6JzD310DyJneJq+2r4NKkalP0kCkB8M/r2uo BSy71FedVs/1hsluTfhKJ9DWI+zSE1+d3kMQxvdPQHx8dOsmbqKWnHFOY A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323120105" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323120105" 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:27 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139201" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139201" 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:25 -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 Subject: [PATCH v5 39/39] x86/shstk: Add ARCH_SHSTK_STATUS Date: Thu, 19 Jan 2023 13:23:17 -0800 Message-Id: <20230119212317.8324-40-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: A64BB40017 X-Stat-Signature: 31ahfb7bkui35ezh645si359q1iq73qb X-Rspam-User: X-HE-Tag: 1674163467-457299 X-HE-Meta: U2FsdGVkX18aGIO4acF21Mdp7hgPiIauKNBEm1hjs3iK6hjOp4TW1RppuHKeV08ta03tCmXjl7jWymv/gAHnQJtW0eDvL29LrVks+ir9Shroa8+Nq3Oorl9VYWmtByopF6kNDgsoX1dRbOExEWaqgkXoRGnvsM41F+/im87EfxIgDNEBA6b5hGtaoM+9pIjpqRsi8TxSBMvWXNLOCsM3+UzJf07uLkZECpTIJ8zJN1BMsj/yF6kRSCs6pFP5EY28gi4UkLnNEsPlYHaIHyQh7R37OiIghlyvjJSK1T0a6glxgiVxsB5Gr12ARpqqBIQ1nefFaQr+pA6ytshrmyh5Aw7WdhZGqT2SYZNxFPwnZ6dE6pZAb/wmrYaQjMMhwubO4u3F2V35l+yNTurddbhrlLHVJ+frHR655CELfIpH2TJqo7ItNfQurQDnQKRCtR5ryJ022qELsuf8T4f7m3A/Bk3h+eG+kon4T2hqz+MczhoZJBWLEs1WCEjhz784KIpCy0UEBID5xjpyL2rHJwRzmVVrPqH0Lrc40MwqnTm6QYl1BMl0yDVS2vFPoX7+OPHLo2VNyb4Q8T1k9OnplpL8D2taWpBxfcMpcuXBr866abQitUaRSePbZVBexQ7kqg5+Q1OUvEGSfVwTpVG1F3XgU5dUhsN0O2lsPxyL4HA8UJaJpADG+6lVxhqbPk5B4b8gHAJXgOSaiA4KhNMN2MGZAusRBjpVd2o64qgwnMeW3YG7O/sP87tTBTfDdZTgB6/OtE7avugFWq0CsGyKF85wJFl8n0A4FTbnfVKkICpfivbGZICakzJfrSxRrqhsy4ULqUN36gpJSJ92TKRq1+VDh3oiLyYIXfO/C51XcAzDWQRnVcHGs05g/ycDuKMFByI/w8+tUdFheAwDbx8LzPqJpRmHU/0Hb4Lm2SaxL5XixnXHTfazlpne5bcp2FzpuwMLK7OJmSl7w4PuMycedab xfTS1RXh 3ScSod6nt4s39xPJV/O0Xfx+YPDwhGH4l/tUOrq5mMXVf1RC3hXm26Dfdx1PpzOvSBsaCIXgX2S28A5LPyexg2T2OfT5rFLWQq2CszkRHR+3dxYlgea8eX2yVQ8PtolLCgl1a8sdqYxrWbWAhoWYWM0H3fyCS2bwqf7MdLfiO3E2XUgpk+G5bH4HjZxyC5HYfvNkVcDbsDVg6OpwspHPGzs98B8GFWdeZsBbApeKFABkTXohiNfmVZZQtrouJyEo6tltUJvdLJLBkouoyqAhEx0coSpA65mcwPYeFH+E1+ffB0M/T/LonEpJGb34KqcHvke+G 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: CRIU and GDB need to get the current shadow stack and WRSS enablement status. This information is already available via /proc/pid/status, but this is inconvenient for CRIU because it involves parsing the text output in an area of the code where this is difficult. Provide a status arch_prctl(), ARCH_SHSTK_STATUS for retrieving the status. Have arg2 be a userspace address, and make the new arch_prctl simply copy the features out to userspace. Tested-by: Pengfei Xu Suggested-by: Mike Rapoport Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v5: - Fix typo in commit log v4: - New patch Documentation/x86/shstk.rst | 6 ++++++ arch/x86/include/asm/shstk.h | 4 ++-- arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/process_64.c | 1 + arch/x86/kernel/shstk.c | 8 +++++++- 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Documentation/x86/shstk.rst b/Documentation/x86/shstk.rst index e8ed5fc0f7ae..7f4af798794e 100644 --- a/Documentation/x86/shstk.rst +++ b/Documentation/x86/shstk.rst @@ -77,6 +77,11 @@ 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. +arch_prctl(ARCH_SHSTK_STATUS, unsigned long addr) + Copy the currently enabled features to the address passed in addr. The + features are described using the bits passed into the others in + 'features'. + The return values are as follows. On success, return 0. On error, errno can be:: @@ -84,6 +89,7 @@ be:: -ENOTSUPP if the feature is not supported by the hardware or kernel. -EINVAL arguments (non existing feature, etc) + -EFAULT if could not copy information back to userspace The feature's bits supported are:: diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index 746c040f7cb6..73de995f55ca 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -14,7 +14,7 @@ struct thread_shstk { u64 size; }; -long shstk_prctl(struct task_struct *task, int option, unsigned long features); +long shstk_prctl(struct task_struct *task, int option, unsigned long arg2); void reset_thread_features(void); int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, unsigned long stack_size, @@ -24,7 +24,7 @@ int setup_signal_shadow_stack(struct ksignal *ksig); int restore_signal_shadow_stack(void); #else static inline long shstk_prctl(struct task_struct *task, int option, - unsigned long features) { return -EINVAL; } + unsigned long arg2) { return -EINVAL; } static inline void reset_thread_features(void) {} static inline int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 200efbbe5809..1b85bc876c2d 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -26,6 +26,7 @@ #define ARCH_SHSTK_DISABLE 0x5002 #define ARCH_SHSTK_LOCK 0x5003 #define ARCH_SHSTK_UNLOCK 0x5004 +#define ARCH_SHSTK_STATUS 0x5005 /* 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 d368854fa9c4..dde43caf196e 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -836,6 +836,7 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) case ARCH_SHSTK_DISABLE: case ARCH_SHSTK_LOCK: case ARCH_SHSTK_UNLOCK: + case ARCH_SHSTK_STATUS: return shstk_prctl(task, option, arg2); default: ret = -EINVAL; diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index a639119a21c5..3b1433bd63c7 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -445,8 +445,14 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsi return alloc_shstk(addr, aligned_size, size, set_tok); } -long shstk_prctl(struct task_struct *task, int option, unsigned long features) +long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) { + unsigned long features = arg2; + + if (option == ARCH_SHSTK_STATUS) { + return put_user(task->thread.features, (unsigned long __user *)arg2); + } + if (option == ARCH_SHSTK_LOCK) { task->thread.features_locked |= features; return 0;