From patchwork Thu Aug 1 06:51:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aruna Ramakrishna X-Patchwork-Id: 13749891 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 9F11FC3DA64 for ; Thu, 1 Aug 2024 06:51:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DA5A6B00B8; Thu, 1 Aug 2024 02:51:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE8446B00BC; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98C2B6B00B4; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) 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 522586B00B8 for ; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0EE8EA08C8 for ; Thu, 1 Aug 2024 06:51:42 +0000 (UTC) X-FDA: 82402756044.16.62CC860 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf29.hostedemail.com (Postfix) with ESMTP id 82CBF12001E for ; Thu, 1 Aug 2024 06:51:38 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=jtbvTQ37; spf=pass (imf29.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722495070; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MKU+WG2gzC94jUgb0tLE7u9uI0Bei7s8TSL3t2RlLeo=; b=rh7x755VAunvKB7HS92Qq9nZVj9KIcyCuLNcsjQ0IiDD3imqexHUQG2f653vHM4Z1zNbq4 9Duup38BJbvaMlPf/gdrIj5Zsxh4V1YoGdYMLQkBoXTiBnPnCamtmOkBWXJHVvowh52va/ 2vsnY1CKTrn2/GlFera8LymnOzynNLI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=jtbvTQ37; spf=pass (imf29.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722495070; a=rsa-sha256; cv=none; b=KLbFa5v0LU2ZEZFYajq0oiOQKRsM7NytxE4n9j4D+Y0E3vMs0lWn03+80iaEuG18wPmCO3 1hWapSvDuOJvEZschtZtyNpmEUs/R8PCcKKjOLZ1f6bIOAA8quxOUo7dZ4TPOQ6y06KChs DsodLthixZAw43+etB4wV4/CrIGsEIY= Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 46VLGvI6005722; Thu, 1 Aug 2024 06:51:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=M KU+WG2gzC94jUgb0tLE7u9uI0Bei7s8TSL3t2RlLeo=; b=jtbvTQ37PLeXqm+XI royK+S37eMmisilBy5ySnlj5trGZKLAqaEDvbGjDwfD4RNhP3woyjxFoXNzr5xrf bG6eqhaevkj4RvOQGAPJ+A+pf+MWtkyycmxvT8hKgqrNAHvdJk6z1BSCPenwyYJS vuKJpwoW/TOD1LUWAN5pJMmFjFKEqsQ18535S0Fwb3tiS1JcRV7EpVrt1q+miSOT GFUsnlixK4LF16pY76/rg8kdExusNjvSRfo42ukQbyolrFpRC8cgQnGJZuya7ACf 3A250GG8tLt9Azsej+pMLd4cOOYNBkCnUooWtK6qoMbKvtHBQBEOXvFfX3M2w/UT 4qBtg== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 40mqp214n8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:19 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 47152vFv030993; Thu, 1 Aug 2024 06:51:18 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 40nehveh7m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:18 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 4716pHNE033596; Thu, 1 Aug 2024 06:51:17 GMT Received: from aruramak-dev.osdevelopmeniad.oraclevcn.com (aruramak-dev.allregionaliads.osdevelopmeniad.oraclevcn.com [100.100.253.155]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 40nehveh6x-2; Thu, 01 Aug 2024 06:51:17 +0000 From: Aruna Ramakrishna To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@linux.intel.com, tglx@linutronix.de, mingo@kernel.org, linux-mm@kvack.org, keith.lucas@oracle.com, jeffxu@chromium.org, rick.p.edgecombe@intel.com, jorgelo@chromium.org, keescook@chromium.org, sroettger@google.com, jannh@google.com, aruna.ramakrishna@oracle.com Subject: [PATCH v7 1/5] x86/pkeys: Add PKRU as a parameter in signal handling functions Date: Thu, 1 Aug 2024 06:51:12 +0000 Message-Id: <20240801065116.2088582-2-aruna.ramakrishna@oracle.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> References: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-01_04,2024-07-31_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 suspectscore=0 bulkscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2408010038 X-Proofpoint-ORIG-GUID: X4z3E578c0JDELBUOm_lQRLKs0ZM6qEx X-Proofpoint-GUID: X4z3E578c0JDELBUOm_lQRLKs0ZM6qEx X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 82CBF12001E X-Stat-Signature: n3pijjuxg1ktrsn73actn4jk4xiie3dh X-HE-Tag: 1722495098-437984 X-HE-Meta: U2FsdGVkX1+hnAN+a+Yx0BCQA4C72FJt3YLPyYNdQaTgJ40v0+KyqF0lkWNSqm64z5loK3P3xwE0YGZEKv2iFqdTj1lqyeOMMi5jm4fx45u23lZVwNZySUFvWeZcC2fJ9IzWBwu72/Eepiky2TttATv3akmG0+0Q7qh66+P4lpkitvEu63GuIrli97QEL0yOwzFvnJAcoBwRRhVJbVHkgKT4U0sgM2JX6Lr7+3gO7CuJqaP5T1fb9D2Ck5auB9tn5s9c1dKNSgHGVVya1bzxsXBLTCxTWFkZVrptlZrY27UHue19BycmqgFP1j6vLJ/ij5kHLZ68Isl+S2jjFVkSmEG2Ie0Uszwp1YHLwlMu5yWCC4B3Xaj3qnEJj9c2YL2H/Vj7kD/J1MVW52JepHuu0iqvHKHCLfoS5TqZq2exmdutSfVycuKi6aWURZ3wBuGFpu7vRRAbV561MFLoVSm7xJKQcmGk4/c4uPkxQ4Z6EpwKyK6X9Vnvgao4xP7AbmMjB5Q3WBqH9yRDfwo/P1OaDgUjP9acBFAVzG/xzUTptiMHUhdpCgEj/A2w7NUcYV2D2JBzn7p7ZVZCQYi/9CmcyAUSXB5F/nC/09j56hPAT/Y2BAlmAoeMj4c3GD46cV6lSWmHNa3K80Xfmh60bQCd2kuQjc5tEeMuy27PKR2LQfmei8c2rRyGZWNQHNaD3NWN4FrcxBFeirgB4xPCWFKWf1ZCUblR7AM1guXcKiTYh7YMUvchAsKKTJhtyfj6DEvok8j2qEwve44LXtfE8v2ZRY+PTwQwEvy1UAvZTLhlhgRrpx7rNIDSdVram4ccRC8fAtfYA5DO0h2cJyM/9d7vYmcE7T0fFlGO9PCWwOYdIugIBFxeNBlkDjKfDJmP7vr+V5DNXU7oR3z6gQrbs5SxlvMszsVDLF0rA/D6V37LqwdsOubCT/Vblf8FgtPXmj6Nz9pJJVbgFGWoKTH9r68 4SIFpFFi TH2B8d87SOVJKHsAVFBYq5SjoES/FWzN4dtBSwdnGBEftJXZjl8LCr+D8t2PCSoZjuUF5jGePz/F3IQJ2FbBk7qh8UBaUp3IqSb3Za6JmC7DnHg4cfSGlIxCyaRhthNbSfSkCyUs0dqquohLgM3cQZqsGLPQ5jsWF3RTHShGvZlbh/ZDvv0JVtUP3mbzxXFt+btpFZtsJb8sEa4gwD9ieZZANCaCK/D8oxN4d 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: List-Subscribe: List-Unsubscribe: Problem description: Let's assume there's a multithreaded application that runs untrusted user code. Each thread has its stack/code protected by a non-zero pkey, and the PKRU register is set up such that only that particular non-zero pkey is enabled. Each thread also sets up an alternate signal stack to handle signals, which is protected by pkey zero. The pkeys man page documents that the PKRU will be reset to init_pkru when the signal handler is invoked, which means that pkey zero access will be enabled. But this reset happens after the kernel attempts to push fpu state to the alternate stack, which is not (yet) accessible by the kernel, which leads to a new SIGSEGV being sent to the application, terminating it. Enabling both the non-zero pkey (for the thread) and pkey zero in userspace will not work for this use case. We cannot have the alt stack writeable by all - the rationale here is that the code running in that thread (using a non-zero pkey) is untrusted and should not have access to the alternate signal stack (that uses pkey zero), to prevent the return address of a function from being changed. The expectation is that kernel should be able to set up the alternate signal stack and deliver the signal to the application even if pkey zero is explicitly disabled by the application. The signal handler accessibility should not be dictated by whatever PKRU value the thread sets up. Solution: The PKRU register is managed by XSAVE, which means the sigframe contents must match the register contents - which is not the case here. We want the sigframe to contain the user-defined PKRU value (so that it is restored correctly from sigcontext) but the actual register must be reset to init_pkru so that the alt stack is accessible and the signal can be delivered to the application. It seems that the proper fix here would be to remove PKRU from the XSAVE framework and manage it separately, which is quite complicated. As a workaround, do this: orig_pkru = rdpkru(); wrpkru(orig_pkru & init_pkru_value); xsave_to_user_sigframe(); put_user(pkru_sigframe_addr, orig_pkru) This change is split over multiple patches. In preparation for writing PKRU to sigframe in a later patch, pass in PKRU as an additional parameter down the chain from get_sigframe(): get_sigframe() copy_fpstate_to_sigframe() copy_fpregs_to_sigframe() There are no functional changes in this patch. Signed-off-by: Aruna Ramakrishna --- arch/x86/include/asm/fpu/signal.h | 2 +- arch/x86/kernel/fpu/signal.c | 6 +++--- arch/x86/kernel/signal.c | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/fpu/signal.h b/arch/x86/include/asm/fpu/signal.h index 611fa41711af..eccc75bc9c4f 100644 --- a/arch/x86/include/asm/fpu/signal.h +++ b/arch/x86/include/asm/fpu/signal.h @@ -29,7 +29,7 @@ fpu__alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long fpu__get_fpstate_size(void); -extern bool copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size); +extern bool copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size, u32 pkru); extern void fpu__clear_user_states(struct fpu *fpu); extern bool fpu__restore_sig(void __user *buf, int ia32_frame); diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c index 247f2225aa9f..2b3b9e140dd4 100644 --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -156,7 +156,7 @@ static inline bool save_xstate_epilog(void __user *buf, int ia32_frame, return !err; } -static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf) +static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf, u32 pkru) { if (use_xsave()) return xsave_to_user_sigframe(buf); @@ -185,7 +185,7 @@ static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf) * For [f]xsave state, update the SW reserved fields in the [f]xsave frame * indicating the absence/presence of the extended state to the user. */ -bool copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) +bool copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size, u32 pkru) { struct task_struct *tsk = current; struct fpstate *fpstate = tsk->thread.fpu.fpstate; @@ -228,7 +228,7 @@ bool copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) fpregs_restore_userregs(); pagefault_disable(); - ret = copy_fpregs_to_sigframe(buf_fx); + ret = copy_fpregs_to_sigframe(buf_fx, pkru); pagefault_enable(); fpregs_unlock(); diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index 31b6f5dddfc2..1f1e8e0ac5a3 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -84,6 +84,7 @@ get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size, unsigned long math_size = 0; unsigned long sp = regs->sp; unsigned long buf_fx = 0; + u32 pkru = read_pkru(); /* redzone */ if (!ia32_frame) @@ -139,7 +140,7 @@ get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size, } /* save i387 and extended state */ - if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size)) + if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size, pkru)) return (void __user *)-1L; return (void __user *)sp; From patchwork Thu Aug 1 06:51:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aruna Ramakrishna X-Patchwork-Id: 13749893 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 B4577C3DA64 for ; Thu, 1 Aug 2024 06:51:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C022F6B00BC; Thu, 1 Aug 2024 02:51:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B8AF26B00BD; Thu, 1 Aug 2024 02:51:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F59E6B00C1; Thu, 1 Aug 2024 02:51:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5DD176B00BC for ; Thu, 1 Aug 2024 02:51:43 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0E3F112092B for ; Thu, 1 Aug 2024 06:51:43 +0000 (UTC) X-FDA: 82402756086.28.12D3018 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf13.hostedemail.com (Postfix) with ESMTP id 0768920005 for ; Thu, 1 Aug 2024 06:51:40 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=a+LA9C0O; spf=pass (imf13.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722495096; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=G5mMnDpkYZkQxM2Urrt/xYeOk6eG4fznj0JjVcnQXi0=; b=DvGJqVA6F85qrSeS/nK1GXxIhD/atgjsJgrsbnaRnvIdV0a5dQF2Q9KOgA3BlUxt8shGVw bFr9nH0cNXUZ/qzY2dKLuqRi/L8EeAypUblL4QS0qd4x/aAtDroOOlJU8xxp9yS5v552Xi Ivo381JXIz2t/Xy4QuMDSTcAYn1W6xc= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=a+LA9C0O; spf=pass (imf13.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722495096; a=rsa-sha256; cv=none; b=MeQLC1NK9Axj5redSqOucuSVR0LWyuqUP07upECIX4AthXGRlrUHjM15RqLi5bi5Yav90d NabkqUn4d2IOTtemEBw9mgLEecOiISSRmvOCzvAGSY64uVp1L55iSRm9e7p/sh7+y0uXul dikOvLZa60jh+E/jR1vs4kejS3ZUWt0= Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 46VLHDsl028382; Thu, 1 Aug 2024 06:51:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=G 5mMnDpkYZkQxM2Urrt/xYeOk6eG4fznj0JjVcnQXi0=; b=a+LA9C0OzpoxyhodA zNIjrivRSPsRUxXGWQHrCV1zHsDyYQOVq5u8JMuhIK/i2+vYgsgiIhu8osEdO5Xg GXq5+A5Nzoo+bOTtwgkHdW4ytwls4WRC/235TyFuJxv78fzoRTTn3HRuIv6XJZUg +bfiIUOeFQoPjAjpD9jattvyDvq0OhSWlbk2aaKlU8NezXdzELTMvqcb0vzJl73J DVp7bAz1QQ9+Ilii/6IDxYcit99d4DBayucq+XGyLZThTP9BArE8FNCijUrDCft6 lgI6io136XS3BwgBvkulF/J6P4kU+qaErd1SHl/vE6TUSR0XFkCGLvy1Pqh48uZ+ wQqJg== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 40mqacs21x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:19 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 4714uHW4030801; Thu, 1 Aug 2024 06:51:19 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 40nehveh7r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:18 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 4716pHNG033596; Thu, 1 Aug 2024 06:51:18 GMT Received: from aruramak-dev.osdevelopmeniad.oraclevcn.com (aruramak-dev.allregionaliads.osdevelopmeniad.oraclevcn.com [100.100.253.155]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 40nehveh6x-3; Thu, 01 Aug 2024 06:51:18 +0000 From: Aruna Ramakrishna To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@linux.intel.com, tglx@linutronix.de, mingo@kernel.org, linux-mm@kvack.org, keith.lucas@oracle.com, jeffxu@chromium.org, rick.p.edgecombe@intel.com, jorgelo@chromium.org, keescook@chromium.org, sroettger@google.com, jannh@google.com, aruna.ramakrishna@oracle.com Subject: [PATCH v7 2/5] x86/pkeys: Add helper functions to update PKRU on the sigframe Date: Thu, 1 Aug 2024 06:51:13 +0000 Message-Id: <20240801065116.2088582-3-aruna.ramakrishna@oracle.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> References: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-01_04,2024-07-31_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 suspectscore=0 bulkscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2408010038 X-Proofpoint-ORIG-GUID: LKVdYd-c38gWL3NEsFAaQtglYu2fHkGe X-Proofpoint-GUID: LKVdYd-c38gWL3NEsFAaQtglYu2fHkGe X-Rspam-User: X-Stat-Signature: cecap1g4ho5tehiea4igekfpozs1tnr7 X-Rspamd-Queue-Id: 0768920005 X-Rspamd-Server: rspam11 X-HE-Tag: 1722495100-57159 X-HE-Meta: U2FsdGVkX19kuKxof1wKXey0vwj4e8bdCoR8idA0QAzpT0ytxbRtgIiJFdGBCwxRzpMOtDvU4JG4f5xtRQ+vlh8j4DktnpsIK/oss5Kn7bhnuZvMXnyYF1NJEVAxv6hu6a9+wsoUOJfE9q8qDgsNvSL80sArfBO8+ZASrrjVnTZJXFzVsdW2sqKfZOYiDZqCO+XheI2QjawLtY8df+L4xpx+NoHGU+WAd/gBGfITFmaTNmFnYtQaHKSoFhtqjtLFGlBcwQWhwlPEFNE/eGhskh8bwZqFKnl+hz+XULGj0ByDe2FVCSNvHLnBwlyOfO9ZAHW0HeweV6ukMftpLxcFuxTp5FP2zDGFVBl4Kz3rRGwSu4rRZwz/VG/Yqf7Bxxq08dFbFi1R7ZWrOQFYe/Eo5VFWoQTqMyxV7XqoOr5M5wh1ARThbq5o7/RWYLQgZzgezILqboPkYyqX9/sn3QkE1zx43Y0VvDCpGmy3dZmcBLuDiTIyIo4Sn/EB18S/FAr1pRr77Bdx67oTJcTb8vWuxPdnjOgy49M8Ic+tV7nEjVJ5gSbcJo6t7AM1Afh58papk2iwslDjrNX843oNXVRcREN2/SJHpXqnGFgj/vXvm8vYvgn8BrjyDAPLkVoDgQ0SPEavSvOt2fdpaAB9TVIL6CWRdDQzgh+teBveLMTreNQMT+i/+YQbFCuXIYGoa1+3F4NyRSmRlJxJtTZoL8lWasR2bpLzo+gnr9u94g9iH2xMasBakcjmhQ+l/BG7DEeVkgfWZn1G3lQQlNE7xRQyDQDidRinU8AYFTMdzle9nybXHY7k4sCjh7wKKfANJouOWiQ/eloRNK1MGWFkCHCK+AYb0iNfdEyermGaMj3AIMCVrtNgGRcS1qA73fthBS9C4p+ea5TiovqQCPi9ee+y5S9e5dIVn/C/RHJEx7X4T9haVK8FWElteWbd/THLt9Cq5pue1uGpioNCp5F8OVx rXBusnev uF7N27MoA/8X+IcZDtKsOgb90wNM1Dz2cga1X7++iHGZVe/XCRDgcOKKvDAKxG8CeGR+oxAvY8FyrH3zBoXTC/5eDzqBhC8i/7D/kz0p+CbU8RK8HYT6wi29AEyD0la2SllrveaZDg3/ZSHHXMq1b3rOAW8hcK24Ls5TNFBfwoMGMg+7VBL2F8RLLSxeFJf5Fd6vJrrEBIPdH4BJejaoXu7VnIL19WDgpqYma 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: List-Subscribe: List-Unsubscribe: In the case where a user thread sets up an alternate signal stack protected by the default pkey (i.e. pkey 0), while the thread's stack is protected by a non-zero pkey, both these pkeys have to be enabled in the PKRU register for the signal to be delivered to the application correctly. However, the PKRU value restored after handling the signal must not enable this extra pkey (i.e. pkey 0), so the PKRU value on the on the sigframe should be overwritten with the user-defined value. Add helper functions that will update PKRU value on the sigframe after XSAVE. These functions will be called in a later patch; this patch does not change any behavior as yet. Note that sig_prepare_pkru() makes no assumption about what pkey could be used to protect the altstack (i.e. it may not be part of init_pkru), and so enables all pkeys. Signed-off-by: Aruna Ramakrishna --- arch/x86/kernel/fpu/signal.c | 10 ++++++++++ arch/x86/kernel/fpu/xstate.c | 13 +++++++++++++ arch/x86/kernel/fpu/xstate.h | 2 ++ arch/x86/kernel/signal.c | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c index 2b3b9e140dd4..931c5469d7f3 100644 --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -63,6 +63,16 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *fxbuf, return true; } +/* + * Update the value of PKRU register that was already pushed onto the signal frame. + */ +static inline int update_pkru_in_sigframe(struct xregs_state __user *buf, u32 pkru) +{ + if (unlikely(!cpu_feature_enabled(X86_FEATURE_OSPKE))) + return 0; + return __put_user(pkru, (unsigned int __user *)get_xsave_addr_user(buf, XFEATURE_PKRU)); +} + /* * Signal frame handlers. */ diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index c5a026fee5e0..fa7628bb541b 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -993,6 +993,19 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr) } EXPORT_SYMBOL_GPL(get_xsave_addr); +/* + * Given an xstate feature nr, calculate where in the xsave buffer the state is. + * The xsave buffer should be in standard format, not compacted (e.g. user mode + * signal frames). + */ +void __user *get_xsave_addr_user(struct xregs_state __user *xsave, int xfeature_nr) +{ + if (WARN_ON_ONCE(!xfeature_enabled(xfeature_nr))) + return NULL; + + return (void __user *)xsave + xstate_offsets[xfeature_nr]; +} + #ifdef CONFIG_ARCH_HAS_PKEYS /* diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h index 2ee0b9c53dcc..d04383793676 100644 --- a/arch/x86/kernel/fpu/xstate.h +++ b/arch/x86/kernel/fpu/xstate.h @@ -54,6 +54,8 @@ extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void extern void fpu__init_cpu_xstate(void); extern void fpu__init_system_xstate(unsigned int legacy_size); +extern void __user *get_xsave_addr_user(struct xregs_state *xsave, int xfeature_nr); + static inline u64 xfeatures_mask_supervisor(void) { return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED; diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index 1f1e8e0ac5a3..9dc77ad03a0e 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -60,6 +60,24 @@ static inline int is_x32_frame(struct ksignal *ksig) ksig->ka.sa.sa_flags & SA_X32_ABI; } +/* + * Enable all pkeys temporarily, so as to ensure that both the current + * execution stack as well as the alternate signal stack are writeable. + * The application can use any of the available pkeys to protect the + * alternate signal stack, and we don't know which one it is, so enable + * all. The PKRU register will be reset to init_pkru later in the flow, + * in fpu__clear_user_states(), and it is the application's responsibility + * to enable the appropriate pkey as the first step in the signal handler + * so that the handler does not segfault. + */ +static inline u32 sig_prepare_pkru(void) +{ + u32 orig_pkru = read_pkru(); + + write_pkru(0); + return orig_pkru; +} + /* * Set up a signal frame. */ From patchwork Thu Aug 1 06:51:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aruna Ramakrishna X-Patchwork-Id: 13749889 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 D857DC52D6F for ; Thu, 1 Aug 2024 06:51:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D01C46B00B4; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B67BD6B00B8; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A5EE6B00BC; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2B02A6B00B4 for ; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D23B3120934 for ; Thu, 1 Aug 2024 06:51:41 +0000 (UTC) X-FDA: 82402756002.01.B81D868 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf24.hostedemail.com (Postfix) with ESMTP id D4288180009 for ; Thu, 1 Aug 2024 06:51:39 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=Cu3GCaXO; spf=pass (imf24.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722495072; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HYlGse7LIrCjPVKcie6J2ImrmiUAqWkLFVxRi2crvAA=; b=XQswAvpxiu5kNrzUDXhoGx9bmXBn1goUt+wi/F4i8fSRE+Q8OPcU7zNDfUJveoJvUyiOam sRV014HsLL5jg+6bbNAhwCFNy1WUyUQ4KUMi48HXIGu5Ci7rnOytfmFOy3nR9RhBo2pDxq X3Cd6HOBhwG1fk5buy+6FtTNSsW9ChM= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=Cu3GCaXO; spf=pass (imf24.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722495072; a=rsa-sha256; cv=none; b=PrFDyqp+SBYSW1JxbZEgO3ndYMB7LFSOxRe7MtirmhCaag3MC5iGmoXseKoU2JfkuuUsiz s4rMJQMnCiZZNGeBHKntM1hspa0gW0uYiTv19BBrdojdvn+X6UzvxMkue4oyphM8sMTQ33 3J36QK3m530PGO7YQUQon3bBjNrYAy8= Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 46VLH9AZ010366; Thu, 1 Aug 2024 06:51:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=H YlGse7LIrCjPVKcie6J2ImrmiUAqWkLFVxRi2crvAA=; b=Cu3GCaXOr+jr4CGRr Q32iY+oGtHHWAVN/7GrkCEMwW4S+KwzMrt81aCWAu/z72zQ0SLw3tpu6c+2JPfoS 8kM5SpC/xI1mHO2Y3q2I8UXMCq8LJYT/9KCewWEWZTs7zJnBK46VjtN/GzCjtnTm NzNXjM3kKG/RUZfquBkUKQLbsvxK04AKdQzBsltnK5eBBuPjMismlXcw9ZSZSjEq RRk0UtJMDdSRGf6zImGY/+ReluAX/REqVhvvK4aNTKCUeJ+6oJNxprEyX1+qQvTq LyS2xKf/DAacn3nopvczz0MSwZVex/lgNYJBTYmMEAqo3MNIZxvmb4/vjhyktKBL 91fkw== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 40mrgs92em-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:20 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 4716KPrY030847; Thu, 1 Aug 2024 06:51:19 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 40nehveh7x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:19 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 4716pHNI033596; Thu, 1 Aug 2024 06:51:18 GMT Received: from aruramak-dev.osdevelopmeniad.oraclevcn.com (aruramak-dev.allregionaliads.osdevelopmeniad.oraclevcn.com [100.100.253.155]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 40nehveh6x-4; Thu, 01 Aug 2024 06:51:18 +0000 From: Aruna Ramakrishna To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@linux.intel.com, tglx@linutronix.de, mingo@kernel.org, linux-mm@kvack.org, keith.lucas@oracle.com, jeffxu@chromium.org, rick.p.edgecombe@intel.com, jorgelo@chromium.org, keescook@chromium.org, sroettger@google.com, jannh@google.com, aruna.ramakrishna@oracle.com Subject: [PATCH v7 3/5] x86/pkeys: Update PKRU to enable all pkeys before XSAVE Date: Thu, 1 Aug 2024 06:51:14 +0000 Message-Id: <20240801065116.2088582-4-aruna.ramakrishna@oracle.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> References: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-01_04,2024-07-31_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 spamscore=0 mlxlogscore=937 suspectscore=0 bulkscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2408010038 X-Proofpoint-GUID: y7pqVZIqBkB7gXaqmCtwyn3vc3G3FIM4 X-Proofpoint-ORIG-GUID: y7pqVZIqBkB7gXaqmCtwyn3vc3G3FIM4 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: D4288180009 X-Stat-Signature: 5ad6eoxsasxji9wgcz6d1gcjpxoxod3o X-HE-Tag: 1722495099-243014 X-HE-Meta: U2FsdGVkX19P+3WnQ9ebkO/PBwLw5Nqaz2fjCfmBj2fcmFbDj+RDU8OXb2yTarsuH6q7EQBbT7VA1HgXjL6zBeKxi5I8viRxg2Qslstni6odcGci65k6/Niou1/ruzZnO2qa3CTqJKc2V4YCp2cWcJrQS+X0aibAaa+MkELxtm0bMVarpS6X0Hzg+t6BLTSqKlaE5ZWoyQVCklRCgyOJnsHIPpJ8enwM3crA1umHMkUK5L8I6fxSxwqQZ1LvSHGkk464EbnoEPyf1mLhkh9J+wMmBcf8JPXOHUdNICx4hbTOQhdPkdsP3vP9wPdLvbQBXLo1t7bvertOU0m+1jXVx+NU6TlTyzzVIpqijCPADXpgEdyISRivLDV5NOBQnlX7Xx+XZNtvg0q7UhoNz9fg+gHOoJblY85/egj+1f0Mih81R8UQPRjOGeqBNhoYah2nOCog3hVAI+AWCT0lYBOZvzmdfphykxgbWDaXUkIfBkX9NO4/yVVH9UalwcG69Pvecf+h4dwrF41bhETWsX8+Yh7cc5Y9u6xxWWEIQkvIc7YD+1nAHBEjaTM3TmP7SiZ5EmAcGYehJdzCRdY2EOvGDYHiTT+27DWyAL757lTfjECLP/1GCXszjNiJI1tQxohiJQl65DvQax2kbdLpJsTD59YVql6f6PrZRHPaLCter/ELGnSCoG8AiF1TsUJzyhVfki5Y6mBn3NmAkaWS4mJP4o7U8NbSlJls0AgHDVICbz8VNmXEVJkYHLTxs6DK4zqkN8t0+m5BBpj+zmvLqcEqfSlMFUkYbe1A96gGHphjbmQuAG9o/K9SWuMDvV3qabvWJFbN0yMzBSht/kf05FECVTSbmF9nzWZTd4oZxKB4uKO1WKX6l4b1UWiPn+wWu5dKHSJfg3aIjviVq0wsqPhfGBXXhRq9q68lMbrhRavO4Rs0rmgnY9r7y4+k6+MyDO9VcSctgLoZ0hceqn1B2LA 0k/wIsOV JjwF7RWSfkWQ4vyNeFn3ld8aSuZSbYPTaDqclr/5T3U5frQCibCufiVwzBHaoiWIn8+LQK7dQoKlHJyMYmD9YS7PAA98ZNKc10kVZ3IaTytElpSXn9ZoaVgxQ6LQ+bglH/sS6npKhud+vo+U30BPdUOPRD2fXE4lrVOiQbj27cVOzr5LbSbR8cb/X5iLIIObkjU36W4PvtGIsASQLZfLUuQbBqRpCtRqZ5dP3 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: List-Subscribe: List-Unsubscribe: If the alternate signal stack is protected by a different pkey than the current execution stack, copying xsave data to the sigaltstack will fail if its pkey is not enabled. We do not know which pkey was used by the application for the sigaltstack, so enable all pkeys before xsave so that the signal handler accessibility is not dictated by the PKRU value that the thread sets up. But this updated PKRU value is also pushed onto the sigframe, so overwrite that with the original, user-defined PKRU value so that the value restored from sigcontext does not have the extra pkey enabled. Signed-off-by: Aruna Ramakrishna --- arch/x86/kernel/fpu/signal.c | 11 +++++++++-- arch/x86/kernel/signal.c | 12 ++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c index 931c5469d7f3..1065ab995305 100644 --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -168,8 +168,15 @@ static inline bool save_xstate_epilog(void __user *buf, int ia32_frame, static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf, u32 pkru) { - if (use_xsave()) - return xsave_to_user_sigframe(buf); + int err = 0; + + if (use_xsave()) { + err = xsave_to_user_sigframe(buf); + if (!err) + err = update_pkru_in_sigframe(buf, pkru); + return err; + } + if (use_fxsr()) return fxsave_to_user_sigframe((struct fxregs_state __user *) buf); else diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index 9dc77ad03a0e..5f441039b572 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -102,7 +102,7 @@ get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size, unsigned long math_size = 0; unsigned long sp = regs->sp; unsigned long buf_fx = 0; - u32 pkru = read_pkru(); + u32 pkru; /* redzone */ if (!ia32_frame) @@ -157,9 +157,17 @@ get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size, return (void __user *)-1L; } + /* Update PKRU to enable access to the alternate signal stack. */ + pkru = sig_prepare_pkru(); /* save i387 and extended state */ - if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size, pkru)) + if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size, pkru)) { + /* + * Restore PKRU to the original, user-defined value; disable + * extra pkeys enabled for the alternate signal stack, if any. + */ + write_pkru(pkru); return (void __user *)-1L; + } return (void __user *)sp; } From patchwork Thu Aug 1 06:51:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aruna Ramakrishna X-Patchwork-Id: 13749892 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 19A36C49EA1 for ; Thu, 1 Aug 2024 06:51:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D0A56B00BF; Thu, 1 Aug 2024 02:51:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 60A8D6B00BD; Thu, 1 Aug 2024 02:51:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 421136B00BF; Thu, 1 Aug 2024 02:51:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E520F6B00B7 for ; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A52BF12058F for ; Thu, 1 Aug 2024 06:51:42 +0000 (UTC) X-FDA: 82402756044.16.FA2B480 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf12.hostedemail.com (Postfix) with ESMTP id B161640019 for ; Thu, 1 Aug 2024 06:51:40 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=QmuxIMEs; dmarc=pass (policy=reject) header.from=oracle.com; spf=pass (imf12.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722495054; a=rsa-sha256; cv=none; b=Q3TI9SsynMaRpAo4fjequoA8wgNPR6Y1HWl8ivDm85hi4Ng1zKBofyZ3VTfj4Uk8S0eA96 U55sasxpmMKq2CSLXWKnpO0CCFTDyIZ2ETRAcM5T465asI70GZ95ojRfnnRi0s5tkahyEY jFCfyrCLBOT4uw/X6LrKN3w+xL2uhZ0= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=QmuxIMEs; dmarc=pass (policy=reject) header.from=oracle.com; spf=pass (imf12.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722495054; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=TiPmGpJvBPhD8zSOmWsEhmg/C+5nGFb/MfYfG1gbkVA=; b=xQ2qJGh7jpj8ZhnyU23UEZu3+OrMoQMU9f2qMs/crdEbPPpCZ47CC5+1yp2jVVVKP7j5zs 9QshWsgSudrlrCnEcPXqgn+TofPVeIZzzVtXeqnqlrMAW7oBgcr2Dzg/z3AyGwKYEYSHwB EgEK1NUyzvn9WXscFyVUs0B5da6j1MQ= Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 46VLGvI7005722; Thu, 1 Aug 2024 06:51:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=T iPmGpJvBPhD8zSOmWsEhmg/C+5nGFb/MfYfG1gbkVA=; b=QmuxIMEsmOylZZHcp 3tB7pf/SfuPM5WipqYh3HAoXRqtrnnXPp//shK3e/NIeFTAiweK9nlSdeT5Guj2T R003eLscY/xiMw9Oe+l4jvZHxzkjVIbd4P2cpxbhKF1Yyk53r6iSLRYEgZiHQNCD vRW0eu3c757IPbM5JyJ+wcHEo1LkBT4ozT+Q6/TZmYCzUa8TzgNnVhiAxjSQw6LV pnWnexgkhnvucZNENRymoDrH4hpMz5666W26UY00gVUBC+EvBbER4n+iuOg3ksLG o7jJ0H7Ambgda4XobKsSmCzeGJ3xWRCwxd8Y6XqWoDgfLYsdFwOiM7mWXKi92jct //lgg== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 40mqp214nb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:20 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 4716KoVS030932; Thu, 1 Aug 2024 06:51:19 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 40nehveh80-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:19 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 4716pHNK033596; Thu, 1 Aug 2024 06:51:19 GMT Received: from aruramak-dev.osdevelopmeniad.oraclevcn.com (aruramak-dev.allregionaliads.osdevelopmeniad.oraclevcn.com [100.100.253.155]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 40nehveh6x-5; Thu, 01 Aug 2024 06:51:19 +0000 From: Aruna Ramakrishna To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@linux.intel.com, tglx@linutronix.de, mingo@kernel.org, linux-mm@kvack.org, keith.lucas@oracle.com, jeffxu@chromium.org, rick.p.edgecombe@intel.com, jorgelo@chromium.org, keescook@chromium.org, sroettger@google.com, jannh@google.com, aruna.ramakrishna@oracle.com Subject: [PATCH v7 4/5] x86/pkeys: Restore altstack before sigcontext Date: Thu, 1 Aug 2024 06:51:15 +0000 Message-Id: <20240801065116.2088582-5-aruna.ramakrishna@oracle.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> References: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-01_04,2024-07-31_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 spamscore=0 mlxlogscore=853 suspectscore=0 bulkscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2408010038 X-Proofpoint-ORIG-GUID: z5XWOE0O2Xt7mvn-1DCV-HJyBg0uYceS X-Proofpoint-GUID: z5XWOE0O2Xt7mvn-1DCV-HJyBg0uYceS X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B161640019 X-Stat-Signature: mmek1ap9foqzq1snsuu13q65q4wg9uqt X-Rspam-User: X-HE-Tag: 1722495100-599869 X-HE-Meta: U2FsdGVkX1/GRmi8Va/n2smTLkYz/abmBVzJi1qqlzDw8hHRtTf+yIfr1SrP8rwPDuseIHswApKnNn3jfRxqtqaYRT6eU5dEdLAgE9e2SAD76kin/PGiPsyrAOIAlEmvUnCuRTtp1G5+j+2lkjheQL7CqnWrs32j8UClTJHhQx+h4LCXRg3h6fGdoeK3RnOf8KSCl0cFD0J87o2DVz3+FwQLwWBjerkqSLjJuI0Es8YjEogSdzyAn8AkzP6eBbHe+BlMJ3t9mZcXZnrH8inxMVU0BcLw3pOwIMkqyYab4DTvQLGwOUSuWmtiZ2SsMMvRvsXIUSo8RS3i4kkGEr/+kKC4Fkei1dM/5uPAK7mHcBeYffGpGn6HENoD9QTAdXzIONDxUobUVqhS6HccgynNrQmxsv2JZVONjXurMT7h83LNHEHH4CgBfF7MpEmzVYDNNGogexMon+HwBDJdXEDOuY4nggltWpg0Og86qNKNWNLrfAHECR+l9/VvlwuyWeQtA/+6m+xInpm3EQHc1B+wf+V5KSnuGOTNOH/2W3kHrgNEcFeZG2du39e/FB2QN1eMQWBS4yIKvqYR29WKVfwx9z+9XeAIpZd1+cWptkdjIOoFrSbzNxaovpjun0NAoXru6GusAc35jdRyN8666L66QwEWDKfrEbbySEfwtDRe1vX6ncJrZaslr4f31FYgpc+Bw+9l7U5mVIYiznWWwPcVD+rce/kDLmoxDts5F30R9cKu5ZPynW7284lAAi3CFDO82vVMnZbDYSMC+UCxuAsNdx4bIsr8Hlz9RuIfJriaGPLaXCWJQn7KSzTCZAnfluZqFr4mHMl1XwYCUwevXhrDczmQWx6CvpXskGyHPb0WzCr3Ux2HrvP4QUziMssnLw6iFLZNH2yFeMskxJcCi+79CDRvaXNq+0nuza3iCAzDUEJ811nW6RoHw1OerAWhXZHImm06oQee+N6SUwOcKrB PiVlR6HF nB9OBIMZWxjUEO3GJylWxgoWB14hVudotYR6kaZ8n9zYUT+yT8eL/BVyhdUwyPGAErRgeBeqUBGdAdojKmi/3XVo5kwLfjs/xUkPkbKsF85GDes0dbI/cLoXtcH8v6tv6waFQz9z6NHNdBgtRDYPlsj2Z9glYcFFaSRGnqN2NCfOBn3Q7wAiOxSK+GocKWifaeqUN9+CLvkC/RlcObBWxvPIA/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: List-Subscribe: List-Unsubscribe: A process can disable access to the alternate signal stack and still expect signals to be delivered correctly. get_sigframe() updates the PKRU value to enable access to the altstack, and makes sure that the value on the sigframe is the user-defined PKRU value so that it is correctly restored. However, in sigreturn(), restore_altstack() needs read access to the altstack. But the PKRU has already been restored from the sigframe (in restore_sigcontext()) which will disable access to the altstack, resulting in a SIGSEGV. Fix this by restoring altstack before restoring PKRU. Signed-off-by: Aruna Ramakrishna --- arch/x86/kernel/signal_64.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/signal_64.c b/arch/x86/kernel/signal_64.c index 8a94053c5444..ee9453891901 100644 --- a/arch/x86/kernel/signal_64.c +++ b/arch/x86/kernel/signal_64.c @@ -260,13 +260,13 @@ SYSCALL_DEFINE0(rt_sigreturn) set_current_blocked(&set); - if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags)) + if (restore_altstack(&frame->uc.uc_stack)) goto badframe; - if (restore_signal_shadow_stack()) + if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags)) goto badframe; - if (restore_altstack(&frame->uc.uc_stack)) + if (restore_signal_shadow_stack()) goto badframe; return regs->ax; From patchwork Thu Aug 1 06:51:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aruna Ramakrishna X-Patchwork-Id: 13749890 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 B78CAC52D6D for ; Thu, 1 Aug 2024 06:51:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D7F06B00B1; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 805A66B00B7; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59A5A6B00B1; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) 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 2C5546B00B7 for ; Thu, 1 Aug 2024 02:51:42 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D4B28C0886 for ; Thu, 1 Aug 2024 06:51:41 +0000 (UTC) X-FDA: 82402756002.28.74EEBCE Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf26.hostedemail.com (Postfix) with ESMTP id D22C714002C for ; Thu, 1 Aug 2024 06:51:39 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="Gco/XGbW"; spf=pass (imf26.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722495024; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IjUdlPOb1E7Q+iz0SVeqCKwYttFwvKwhUlSnaNmML3o=; b=lyL5MOnGHGPNRGkTnbHzqXot5s90owsAVTM447YPdxK9QGmDjgZfJKAjx2VfiRaDtR3v08 Po4EoAcOHgHUvdkTIbr6DHAxrbkcEhMBIWxpbCbznEFTuvpgrEXEWNmDaSKKU797fz0vtP OS/nbR5P2embqmbBB0ikV5CAPuGC0ps= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="Gco/XGbW"; spf=pass (imf26.hostedemail.com: domain of aruna.ramakrishna@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=aruna.ramakrishna@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722495024; a=rsa-sha256; cv=none; b=mnVi6tbuKUxCAvKBOBI0H48fQJubVGJKOqx4ewDdy9BfKXvlKFux6PBoqvBn0dPrRtNY+N 0WKuMqwsoTwPrwRWjke87eQg6BJQ8d5sLA8gMAk/zByKpeNTdvZcLTs1jkvAJvyDVdVk+O Kf5V4d/2eryCT+3cxTzZ4rLEEL6yIoQ= Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 46VLHBdA028335; Thu, 1 Aug 2024 06:51:21 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=I jUdlPOb1E7Q+iz0SVeqCKwYttFwvKwhUlSnaNmML3o=; b=Gco/XGbWH7B8ewHVc JeyrBnpd5BG38BbGh47LUfD48c/I9KIcwpVaJfDs6KEKXhxcSRxSvmtgv3kQn+E7 BORTgsFtLJuap7Db/PYalUe0LBbAlK7B4NfhTbyrJD9xz9iEA/KjP/1yTF1ZduV/ BfoivlWSQbG/689FzFBH7d86L6joST7bIXpRDjtL3WcvUxiWMV3GEXCAnr9wzAw1 L82+j1YKyZlXFQsdHLmUA1CUrzmQAeSoxEXACdUM4zc701ydyHk2YUMMYOS+QgBi GMM4Dw6GFuDicnU2pmGGK4UR5EstQtYmlrRUPvJVtgWiJiUUYWL43ZvC2180+/Ir figRA== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 40mqacs21y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:21 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 47152vFx030993; Thu, 1 Aug 2024 06:51:20 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 40nehveh85-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 01 Aug 2024 06:51:20 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 4716pHNM033596; Thu, 1 Aug 2024 06:51:19 GMT Received: from aruramak-dev.osdevelopmeniad.oraclevcn.com (aruramak-dev.allregionaliads.osdevelopmeniad.oraclevcn.com [100.100.253.155]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 40nehveh6x-6; Thu, 01 Aug 2024 06:51:19 +0000 From: Aruna Ramakrishna To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@linux.intel.com, tglx@linutronix.de, mingo@kernel.org, linux-mm@kvack.org, keith.lucas@oracle.com, jeffxu@chromium.org, rick.p.edgecombe@intel.com, jorgelo@chromium.org, keescook@chromium.org, sroettger@google.com, jannh@google.com, aruna.ramakrishna@oracle.com Subject: [PATCH v7 5/5] selftests/mm: Add new testcases for pkeys Date: Thu, 1 Aug 2024 06:51:16 +0000 Message-Id: <20240801065116.2088582-6-aruna.ramakrishna@oracle.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> References: <20240801065116.2088582-1-aruna.ramakrishna@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-01_04,2024-07-31_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 suspectscore=0 bulkscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2408010038 X-Proofpoint-ORIG-GUID: htciDwK-qd_GyG0-8HizUIytNuwGMWVm X-Proofpoint-GUID: htciDwK-qd_GyG0-8HizUIytNuwGMWVm X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D22C714002C X-Stat-Signature: bqp46pcyo7zri4wdjpuxifgtrjk3wsw4 X-Rspam-User: X-HE-Tag: 1722495099-296589 X-HE-Meta: U2FsdGVkX18/oZN/zW+JuTap2BkGlw1kXrSi7KEIJtJrTgdrC5vDXrZWi5h7mP2lBhHxnYLLOYrSqxPY8lDIBeciypC8yvjcQ3ty340m1jJG1HsW1uG0DuryK2Q7pZPLmlOiRSdZW6KYJ6wJdLKMq9DngzVUC6N9FkWiUPukjfluJ3dJDw6TxeO9m9OwH+hB1nKsGnjgNgn13wZllArswHO086r6hujTOpgcabIgttHVZ0Vdl9wuLMbjIZYAWWg7chY6viubWWdbq3bsUUYq/xCsjvhmIV3A9ok+/CwNq/n+hkbzcVNnXWUhWa9X5yAV+XNXdaWiszZxRCTIjBYGT2wdavGl6eNLfv71yNuPKqKooJ8QuogrDTt1KGILBGQY6nbMsL7QAMF4azML0Ij3F91lggkEG2Oa8od+qY6Dk4JWw/hMaCBMyCqcikKLSOlwz2Sm5tJp178Dr/eTjgHVIlNe39yFa+TFG32b3ye9B8qMYE55PHVr8K4fxi9V9nbtDcSEWsZekI5qSwDrif0E/c+GVF4gm+1HUbylSEIWRAzQmwQiKd1KWwu4u+dranN7oO/HplLtmQi3oQgc/naWuMbXbYOFhstW4HJFcCHc2bpNsJAKmZjDE2ansmiOLh1ZzblwbOg5oyJU1QPozcJoTmwIW2PbJDkUHRcB2/Okg1uAd7greg8n8JD9IWGe2V+rk3ZVAsUYcG06DV3tOl1jZHaOV7JZZA9JDF6XUkXcULBozFWUTurbGJTU00vmJQVs/66QGzDZMcv/HehcfnNsFTcIvlYHSzWxHaipEjL6ThwaSzr2K/MnE3PQFAfzCvV2mXvKh+C63ay6ujYh7uuusNmLSsAQ+QRpLIHcgw7XDr3kNyeSV9UW9cmYSRoWZKSijMwbLgEP4le0T4aifyjhBd0VFa68V0PSnb049yh8sG1xjXlKEKo7AwBhAD9IRQJm6imzbdRptyFJVJl/48U JucTQp+C VFaCuBDn2RPUXYt8ydn1gX8d3nzin/80SUyiwjtMKRYM6lmKeRs6banv5kXk3vqWu4HmqGI8yXwfvVfPD2xZelQXGHN2hHVj16jyt3yux7cqsr66hvTIa7FpqsYFDvlPaCQaHh4aLSl9cGd9VBo6slNsg/mSc7UJ3OwpghHObZLcGBzFsFKawJXTRt5Mndh+Q/2+ATrATMsLzBEYV6/A1E+pm6emrm7apRz8/ 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: List-Subscribe: List-Unsubscribe: From: Keith Lucas Add a few new tests to exercise the signal handler flow, especially with pkey 0 disabled. There are 5 new tests added: - test_sigsegv_handler_with_pkey0_disabled - test_sigsegv_handler_cannot_access_stack - test_sigsegv_handler_with_different_pkey_for_stack - test_pkru_preserved_after_sigusr1 - test_pkru_sigreturn [ Aruna: Adapted to upstream ] Signed-off-by: Keith Lucas Signed-off-by: Aruna Ramakrishna --- tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/pkey-helpers.h | 11 +- .../selftests/mm/pkey_sighandler_tests.c | 479 ++++++++++++++++++ tools/testing/selftests/mm/protection_keys.c | 10 - 4 files changed, 491 insertions(+), 11 deletions(-) create mode 100644 tools/testing/selftests/mm/pkey_sighandler_tests.c diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 901e0d07765b..0123a3a0bb17 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -88,6 +88,7 @@ CAN_BUILD_X86_64 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_64bit_pr CAN_BUILD_WITH_NOPIE := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_program.c -no-pie) VMTARGETS := protection_keys +VMTARGETS := pkey_sighandler_tests BINARIES_32 := $(VMTARGETS:%=%_32) BINARIES_64 := $(VMTARGETS:%=%_64) @@ -106,6 +107,7 @@ else ifneq (,$(findstring $(ARCH),powerpc)) TEST_GEN_FILES += protection_keys +TEST_GEN_FILES += pkey_sighandler_tests endif endif diff --git a/tools/testing/selftests/mm/pkey-helpers.h b/tools/testing/selftests/mm/pkey-helpers.h index 1af3156a9db8..2b1189c27167 100644 --- a/tools/testing/selftests/mm/pkey-helpers.h +++ b/tools/testing/selftests/mm/pkey-helpers.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "../kselftest.h" @@ -79,7 +80,15 @@ extern void abort_hooks(void); } \ } while (0) -__attribute__((noinline)) int read_ptr(int *ptr); +noinline int read_ptr(int *ptr) +{ + /* + * Keep GCC from optimizing this away somehow + */ + barrier(); + return *ptr; +} + void expected_pkey_fault(int pkey); int sys_pkey_alloc(unsigned long flags, unsigned long init_val); int sys_pkey_free(unsigned long pkey); diff --git a/tools/testing/selftests/mm/pkey_sighandler_tests.c b/tools/testing/selftests/mm/pkey_sighandler_tests.c new file mode 100644 index 000000000000..c43030c7056d --- /dev/null +++ b/tools/testing/selftests/mm/pkey_sighandler_tests.c @@ -0,0 +1,479 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Tests Memory Protection Keys (see Documentation/core-api/protection-keys.rst) + * + * The testcases in this file exercise various flows related to signal handling, + * using an alternate signal stack, with the default pkey (pkey 0) disabled. + * + * Compile with: + * gcc -mxsave -o pkey_sighandler_tests -O2 -g -std=gnu99 -pthread -Wall pkey_sighandler_tests.c -I../../../../tools/include -lrt -ldl -lm + * gcc -mxsave -m32 -o pkey_sighandler_tests -O2 -g -std=gnu99 -pthread -Wall pkey_sighandler_tests.c -I../../../../tools/include -lrt -ldl -lm + */ +#define _GNU_SOURCE +#define __SANE_USERSPACE_TYPES__ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pkey-helpers.h" + +#define STACK_SIZE PTHREAD_STACK_MIN + +void expected_pkey_fault(int pkey) {} + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t cond = PTHREAD_COND_INITIALIZER; +siginfo_t siginfo = {0}; + +/* + * We need to use inline assembly instead of glibc's syscall because glibc's + * syscall will attempt to access the PLT in order to call a library function + * which is protected by MPK 0 which we don't have access to. + */ +static inline __always_inline +long syscall_raw(long n, long a1, long a2, long a3, long a4, long a5, long a6) +{ + unsigned long ret; +#ifdef __x86_64__ + register long r10 asm("r10") = a4; + register long r8 asm("r8") = a5; + register long r9 asm("r9") = a6; + asm volatile ("syscall" + : "=a"(ret) + : "a"(n), "D"(a1), "S"(a2), "d"(a3), "r"(r10), "r"(r8), "r"(r9) + : "rcx", "r11", "memory"); +#elif defined __i386__ + asm volatile ("int $0x80" + : "=a"(ret) + : "a"(n), "b"(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5) + : "memory"); +#endif + return ret; +} + +static void sigsegv_handler(int signo, siginfo_t *info, void *ucontext) +{ + pthread_mutex_lock(&mutex); + + memcpy(&siginfo, info, sizeof(siginfo_t)); + + pthread_cond_signal(&cond); + pthread_mutex_unlock(&mutex); + + syscall_raw(SYS_exit, 0, 0, 0, 0, 0, 0); +} + +static void sigusr1_handler(int signo, siginfo_t *info, void *ucontext) +{ + pthread_mutex_lock(&mutex); + + memcpy(&siginfo, info, sizeof(siginfo_t)); + + pthread_cond_signal(&cond); + pthread_mutex_unlock(&mutex); +} + +static void sigusr2_handler(int signo, siginfo_t *info, void *ucontext) +{ + /* + * pkru should be the init_pkru value which enabled MPK 0 so + * we can use library functions. + */ + printf("%s invoked.\n", __func__); +} + +static void raise_sigusr2(void) +{ + pid_t tid = 0; + + tid = syscall_raw(SYS_gettid, 0, 0, 0, 0, 0, 0); + + syscall_raw(SYS_tkill, tid, SIGUSR2, 0, 0, 0, 0); + + /* + * We should return from the signal handler here and be able to + * return to the interrupted thread. + */ +} + +static void *thread_segv_with_pkey0_disabled(void *ptr) +{ + /* Disable MPK 0 (and all others too) */ + __write_pkey_reg(0x55555555); + + /* Segfault (with SEGV_MAPERR) */ + *(int *) (0x1) = 1; + return NULL; +} + +static void *thread_segv_pkuerr_stack(void *ptr) +{ + /* Disable MPK 0 (and all others too) */ + __write_pkey_reg(0x55555555); + + /* After we disable MPK 0, we can't access the stack to return */ + return NULL; +} + +static void *thread_segv_maperr_ptr(void *ptr) +{ + stack_t *stack = ptr; + int *bad = (int *)1; + + /* + * Setup alternate signal stack, which should be pkey_mprotect()ed by + * MPK 0. The thread's stack cannot be used for signals because it is + * not accessible by the default init_pkru value of 0x55555554. + */ + syscall_raw(SYS_sigaltstack, (long)stack, 0, 0, 0, 0, 0); + + /* Disable MPK 0. Only MPK 1 is enabled. */ + __write_pkey_reg(0x55555551); + + /* Segfault */ + *bad = 1; + syscall_raw(SYS_exit, 0, 0, 0, 0, 0, 0); + return NULL; +} + +/* + * Verify that the sigsegv handler is invoked when pkey 0 is disabled. + * Note that the new thread stack and the alternate signal stack is + * protected by MPK 0. + */ +static void test_sigsegv_handler_with_pkey0_disabled(void) +{ + struct sigaction sa; + pthread_attr_t attr; + pthread_t thr; + + sa.sa_flags = SA_SIGINFO; + + sa.sa_sigaction = sigsegv_handler; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGSEGV, &sa, NULL) == -1) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + + memset(&siginfo, 0, sizeof(siginfo)); + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + pthread_create(&thr, &attr, thread_segv_with_pkey0_disabled, NULL); + + pthread_mutex_lock(&mutex); + while (siginfo.si_signo == 0) + pthread_cond_wait(&cond, &mutex); + pthread_mutex_unlock(&mutex); + + ksft_test_result(siginfo.si_signo == SIGSEGV && + siginfo.si_code == SEGV_MAPERR && + siginfo.si_addr == (void *)1, + "%s\n", __func__); +} + +/* + * Verify that the sigsegv handler is invoked when pkey 0 is disabled. + * Note that the new thread stack and the alternate signal stack is + * protected by MPK 0, which renders them inaccessible when MPK 0 + * is disabled. So just the return from the thread should cause a + * segfault with SEGV_PKUERR. + */ +static void test_sigsegv_handler_cannot_access_stack(void) +{ + struct sigaction sa; + pthread_attr_t attr; + pthread_t thr; + + sa.sa_flags = SA_SIGINFO; + + sa.sa_sigaction = sigsegv_handler; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGSEGV, &sa, NULL) == -1) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + + memset(&siginfo, 0, sizeof(siginfo)); + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + pthread_create(&thr, &attr, thread_segv_pkuerr_stack, NULL); + + pthread_mutex_lock(&mutex); + while (siginfo.si_signo == 0) + pthread_cond_wait(&cond, &mutex); + pthread_mutex_unlock(&mutex); + + ksft_test_result(siginfo.si_signo == SIGSEGV && + siginfo.si_code == SEGV_PKUERR, + "%s\n", __func__); +} + +/* + * Verify that the sigsegv handler that uses an alternate signal stack + * is correctly invoked for a thread which uses a non-zero MPK to protect + * its own stack, and disables all other MPKs (including 0). + */ +static void test_sigsegv_handler_with_different_pkey_for_stack(void) +{ + struct sigaction sa; + static stack_t sigstack; + void *stack; + int pkey; + int parent_pid = 0; + int child_pid = 0; + + sa.sa_flags = SA_SIGINFO | SA_ONSTACK; + + sa.sa_sigaction = sigsegv_handler; + + sigemptyset(&sa.sa_mask); + if (sigaction(SIGSEGV, &sa, NULL) == -1) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + + stack = mmap(0, STACK_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + assert(stack != MAP_FAILED); + + /* Allow access to MPK 0 and MPK 1 */ + __write_pkey_reg(0x55555550); + + /* Protect the new stack with MPK 1 */ + pkey = pkey_alloc(0, 0); + pkey_mprotect(stack, STACK_SIZE, PROT_READ | PROT_WRITE, pkey); + + /* Set up alternate signal stack that will use the default MPK */ + sigstack.ss_sp = mmap(0, STACK_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + sigstack.ss_flags = 0; + sigstack.ss_size = STACK_SIZE; + + memset(&siginfo, 0, sizeof(siginfo)); + + /* Use clone to avoid newer glibcs using rseq on new threads */ + long ret = syscall_raw(SYS_clone, + CLONE_VM | CLONE_FS | CLONE_FILES | + CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM | + CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | + CLONE_DETACHED, + (long) ((char *)(stack) + STACK_SIZE), + (long) &parent_pid, + (long) &child_pid, 0, 0); + + if (ret < 0) { + errno = -ret; + perror("clone"); + } else if (ret == 0) { + thread_segv_maperr_ptr(&sigstack); + syscall_raw(SYS_exit, 0, 0, 0, 0, 0, 0); + } + + pthread_mutex_lock(&mutex); + while (siginfo.si_signo == 0) + pthread_cond_wait(&cond, &mutex); + pthread_mutex_unlock(&mutex); + + ksft_test_result(siginfo.si_signo == SIGSEGV && + siginfo.si_code == SEGV_MAPERR && + siginfo.si_addr == (void *)1, + "%s\n", __func__); +} + +/* + * Verify that the PKRU value set by the application is correctly + * restored upon return from signal handling. + */ +static void test_pkru_preserved_after_sigusr1(void) +{ + struct sigaction sa; + unsigned long pkru = 0x45454544; + + sa.sa_flags = SA_SIGINFO; + + sa.sa_sigaction = sigusr1_handler; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGUSR1, &sa, NULL) == -1) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + + memset(&siginfo, 0, sizeof(siginfo)); + + __write_pkey_reg(pkru); + + raise(SIGUSR1); + + pthread_mutex_lock(&mutex); + while (siginfo.si_signo == 0) + pthread_cond_wait(&cond, &mutex); + pthread_mutex_unlock(&mutex); + + /* Ensure the pkru value is the same after returning from signal. */ + ksft_test_result(pkru == __read_pkey_reg() && + siginfo.si_signo == SIGUSR1, + "%s\n", __func__); +} + +static noinline void *thread_sigusr2_self(void *ptr) +{ + /* + * A const char array like "Resuming after SIGUSR2" won't be stored on + * the stack and the code could access it via an offset from the program + * counter. This makes sure it's on the function's stack frame. + */ + char str[] = {'R', 'e', 's', 'u', 'm', 'i', 'n', 'g', ' ', + 'a', 'f', 't', 'e', 'r', ' ', + 'S', 'I', 'G', 'U', 'S', 'R', '2', + '.', '.', '.', '\n', '\0'}; + stack_t *stack = ptr; + + /* + * Setup alternate signal stack, which should be pkey_mprotect()ed by + * MPK 0. The thread's stack cannot be used for signals because it is + * not accessible by the default init_pkru value of 0x55555554. + */ + syscall(SYS_sigaltstack, (long)stack, 0, 0, 0, 0, 0); + + /* Disable MPK 0. Only MPK 2 is enabled. */ + __write_pkey_reg(0x55555545); + + raise_sigusr2(); + + /* Do something, to show the thread resumed execution after the signal */ + syscall_raw(SYS_write, 1, (long) str, sizeof(str) - 1, 0, 0, 0); + + /* + * We can't return to test_pkru_sigreturn because it + * will attempt to use a %rbp value which is on the stack + * of the main thread. + */ + syscall_raw(SYS_exit, 0, 0, 0, 0, 0, 0); + return NULL; +} + +/* + * Verify that sigreturn is able to restore altstack even if the thread had + * disabled pkey 0. + */ +static void test_pkru_sigreturn(void) +{ + struct sigaction sa = {0}; + static stack_t sigstack; + void *stack; + int pkey; + int parent_pid = 0; + int child_pid = 0; + + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + + /* + * For this testcase, we do not want to handle SIGSEGV. Reset handler + * to default so that the application can crash if it receives SIGSEGV. + */ + if (sigaction(SIGSEGV, &sa, NULL) == -1) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + + sa.sa_flags = SA_SIGINFO | SA_ONSTACK; + sa.sa_sigaction = sigusr2_handler; + sigemptyset(&sa.sa_mask); + + if (sigaction(SIGUSR2, &sa, NULL) == -1) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + + stack = mmap(0, STACK_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + assert(stack != MAP_FAILED); + + /* + * Allow access to MPK 0 and MPK 2. The child thread (to be created + * later in this flow) will have its stack protected by MPK 2, whereas + * the current thread's stack is protected by the default MPK 0. Hence + * both need to be enabled. + */ + __write_pkey_reg(0x55555544); + + /* Protect the stack with MPK 2 */ + pkey = pkey_alloc(0, 0); + pkey_mprotect(stack, STACK_SIZE, PROT_READ | PROT_WRITE, pkey); + + /* Set up alternate signal stack that will use the default MPK */ + sigstack.ss_sp = mmap(0, STACK_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + sigstack.ss_flags = 0; + sigstack.ss_size = STACK_SIZE; + + /* Use clone to avoid newer glibcs using rseq on new threads */ + long ret = syscall_raw(SYS_clone, + CLONE_VM | CLONE_FS | CLONE_FILES | + CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM | + CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | + CLONE_DETACHED, + (long) ((char *)(stack) + STACK_SIZE), + (long) &parent_pid, + (long) &child_pid, 0, 0); + + if (ret < 0) { + errno = -ret; + perror("clone"); + } else if (ret == 0) { + thread_sigusr2_self(&sigstack); + syscall_raw(SYS_exit, 0, 0, 0, 0, 0, 0); + } + + child_pid = ret; + /* Check that thread exited */ + do { + sched_yield(); + ret = syscall_raw(SYS_tkill, child_pid, 0, 0, 0, 0, 0); + } while (ret != -ESRCH && ret != -EINVAL); + + ksft_test_result_pass("%s\n", __func__); +} + +void (*pkey_tests[])(void) = { + test_sigsegv_handler_with_pkey0_disabled, + test_sigsegv_handler_cannot_access_stack, + test_sigsegv_handler_with_different_pkey_for_stack, + test_pkru_preserved_after_sigusr1, + test_pkru_sigreturn +}; + +int main(int argc, char *argv[]) +{ + int i; + + ksft_print_header(); + ksft_set_plan(ARRAY_SIZE(pkey_tests)); + + for (i = 0; i < ARRAY_SIZE(pkey_tests); i++) + (*pkey_tests[i])(); + + ksft_finished(); + return 0; +} diff --git a/tools/testing/selftests/mm/protection_keys.c b/tools/testing/selftests/mm/protection_keys.c index eaa6d1fc5328..cc6de1644360 100644 --- a/tools/testing/selftests/mm/protection_keys.c +++ b/tools/testing/selftests/mm/protection_keys.c @@ -950,16 +950,6 @@ void close_test_fds(void) nr_test_fds = 0; } -#define barrier() __asm__ __volatile__("": : :"memory") -__attribute__((noinline)) int read_ptr(int *ptr) -{ - /* - * Keep GCC from optimizing this away somehow - */ - barrier(); - return *ptr; -} - void test_pkey_alloc_free_attach_pkey0(int *ptr, u16 pkey) { int i, err;