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+RDU8OXb2yTarsuH6q7EQBb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bj27cVOzr5LbSbR8cb/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; }