From patchwork Wed Nov 16 23:33:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 21324 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp115701wrr; Wed, 16 Nov 2022 15:34:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf7/yzP0PA5hrwwcx5JJolca9N4uTk0ZNIUmlUAdFWxNZ16GT4b2WRDOhD3lOcu75QSlXskx X-Received: by 2002:a17:906:901:b0:7ae:23c:3cb4 with SMTP id i1-20020a170906090100b007ae023c3cb4mr53931ejd.599.1668641688329; Wed, 16 Nov 2022 15:34:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668641688; cv=none; d=google.com; s=arc-20160816; b=gCOaZlDZLxM8toKHcdZWOi8ELkxwqOn8NFyRuwDihkIbJcCRwpiz2w7+pdmsWbWPNo g886Vt0w3jMGga1CCxMlmGRpxVXmUeI/d2zyuj3SEZSWXMCSdlaDUo1xUYW6RkJTz+7D tNO6xXPLoRBsf5TBvBnCOqHkJAL1RqluTzPxplI58F4Wiyt7tMMIvGagyGpylSeIInq4 wlMoVCkU0blQXb5qil74Ms6nWiMNLvj2e2/aO5XOM1C+3qB8kDQvXb6IsogOUbEbthdg 3k4TdjQkv5M1PAPvSoHZONB8CIh9RUDk5fpwNFLhOdWE8XF/0m359fmEhhXo89ZhAxdR 3oKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=qtth/yszbv/CMc9Mpe5Ald2w4YlHGMP25/ZJ6OztT6w=; b=kgK1TD0G7lCTPJdndpImgVTRKut72wRQXnSIxfgFsIkV/vTnwaLhdEBO8ghYDf4YmG hf/cTpAlmC3lhJlP5emxFEwXza/l0vP3+4PID3G59FJBwCZa0jlWldgbzZZn2GChUuJB 6bUC8sBsX/UEsPZf7K8RMdV5qE/749n6VjBBuIqH1pvP4TKnaF153SIuQqXjl2n46gcZ +P3X08HDtO6zBLJwaGZXw6kCXN79lncyEtkwcOgqyiViw6HqBGrhScKxrT6ZhrjpqXX2 W/kE9xWMxu8uTKCbWSWpbhQonJyS8zrRqGWPYfvgVrSU7R6ecOfzkkobFizg3g5Vb3BE Y5oA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Pvs7v8hd; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=vIIJvFJ0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k9-20020a17090646c900b0078db719e54csi12178795ejs.98.2022.11.16.15.34.25; Wed, 16 Nov 2022 15:34:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Pvs7v8hd; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=vIIJvFJ0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234429AbiKPXeF (ORCPT + 99 others); Wed, 16 Nov 2022 18:34:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234101AbiKPXdz (ORCPT ); Wed, 16 Nov 2022 18:33:55 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05FA612A88; Wed, 16 Nov 2022 15:33:53 -0800 (PST) Date: Wed, 16 Nov 2022 23:33:50 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668641632; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qtth/yszbv/CMc9Mpe5Ald2w4YlHGMP25/ZJ6OztT6w=; b=Pvs7v8hd128krxgUaQkQGGsyMTU3IKBIasdUdbT0+Zalr6WVQjA5ZxE385CYWSuHqQQz67 nKtwVpolPlQfKuwA7u/9ZCfavYfDNlm6FRgY1aCfoTXe+WPEJduDtNzaMXw6soJ55etL0D 8WsRSOIJMbF/ExE933tDwVgxs5htVQBRnsGiYMS9JNMQw2Gi12XCSCN5ZBGgMnF3fJ4D7r MUVF400gqNX/187C+bXx3wFu0Yop8NcsIcIfASUlGhlj3RTKghY8yV7aBUfyo1bKBe1Z7U 5LwHj9VIf6LCLeOyc58Vg9No/j0e/y2el3uizOJ2CgjmoELj3p1quw9poOmjcg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668641632; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qtth/yszbv/CMc9Mpe5Ald2w4YlHGMP25/ZJ6OztT6w=; b=vIIJvFJ0ivVhmOHKeCUSqhyVo0UrX3Ld4sPrnJCGyM/yEGf2hSr/VwpYDro5PjWN9VhMmR lV8HU3p7ZWENTpBg== From: "tip-bot2 for Kyle Huey" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/fpu] x86/fpu: Allow PKRU to be (once again) written by ptrace. Cc: Kyle Huey , Dave Hansen , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <166864163052.4906.15510519158305981625.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749697626887828324?= X-GMAIL-MSGID: =?utf-8?q?1749697626887828324?= The following commit has been merged into the x86/fpu branch of tip: Commit-ID: 4a804c4f8356393d6b5eff7600f07615d7869c13 Gitweb: https://git.kernel.org/tip/4a804c4f8356393d6b5eff7600f07615d7869c13 Author: Kyle Huey AuthorDate: Tue, 15 Nov 2022 15:09:30 -08:00 Committer: Dave Hansen CommitterDate: Wed, 16 Nov 2022 15:03:53 -08:00 x86/fpu: Allow PKRU to be (once again) written by ptrace. Move KVM's PKRU handling code in fpu_copy_uabi_to_guest_fpstate() to copy_uabi_to_xstate() so that it is shared with other APIs that write the XSTATE such as PTRACE_SETREGSET with NT_X86_XSTATE. This restores the pre-5.14 behavior of ptrace. The regression can be seen by running gdb and executing `p $pkru`, `set $pkru = 42`, and `p $pkru`. On affected kernels (5.14+) the write to the PKRU register (which gdb performs through ptrace) is ignored. [ dhansen: removed stable@ tag for now. The ABI was broken for long enough that this is not urgent material. Let's let it stew in tip for a few weeks before it's submitted to stable because there are so many ABIs potentially affected. ] Fixes: e84ba47e313d ("x86/fpu: Hook up PKRU into ptrace()") Signed-off-by: Kyle Huey Signed-off-by: Dave Hansen Link: https://lore.kernel.org/all/20221115230932.7126-5-khuey%40kylehuey.com --- arch/x86/kernel/fpu/core.c | 13 +------------ arch/x86/kernel/fpu/xstate.c | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 5501576..46b935b 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -391,8 +391,6 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, { struct fpstate *kstate = gfpu->fpstate; const union fpregs_state *ustate = buf; - struct pkru_state *xpkru; - int ret; if (!cpu_feature_enabled(X86_FEATURE_XSAVE)) { if (ustate->xsave.header.xfeatures & ~XFEATURE_MASK_FPSSE) @@ -406,16 +404,7 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, if (ustate->xsave.header.xfeatures & ~xcr0) return -EINVAL; - ret = copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru); - if (ret) - return ret; - - /* Retrieve PKRU if not in init state */ - if (kstate->regs.xsave.header.xfeatures & XFEATURE_MASK_PKRU) { - xpkru = get_xsave_addr(&kstate->regs.xsave, XFEATURE_PKRU); - *vpkru = xpkru->pkru; - } - return 0; + return copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru); } EXPORT_SYMBOL_GPL(fpu_copy_uabi_to_guest_fpstate); #endif /* CONFIG_KVM */ diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index d657c8b..a8cf604 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1205,10 +1205,22 @@ static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size, * @fpstate: The fpstate buffer to copy to * @kbuf: The UABI format buffer, if it comes from the kernel * @ubuf: The UABI format buffer, if it comes from userspace - * @pkru: unused + * @pkru: The location to write the PKRU value to * * Converts from the UABI format into the kernel internal hardware * dependent format. + * + * This function ultimately has three different callers with distinct PKRU + * behavior. + * 1. When called from sigreturn the PKRU register will be restored from + * @fpstate via an XRSTOR. Correctly copying the UABI format buffer to + * @fpstate is sufficient to cover this case, but the caller will also + * pass a pointer to the thread_struct's pkru field in @pkru and updating + * it is harmless. + * 2. When called from ptrace the PKRU register will be restored from the + * thread_struct's pkru field. A pointer to that is passed in @pkru. + * 3. When called from KVM the PKRU register will be restored from the vcpu's + * pkru field. A pointer to that is passed in @pkru. */ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, const void __user *ubuf, u32 *pkru) @@ -1260,6 +1272,13 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, } } + if (hdr.xfeatures & XFEATURE_MASK_PKRU) { + struct pkru_state *xpkru; + + xpkru = __raw_xsave_addr(xsave, XFEATURE_PKRU); + *pkru = xpkru->pkru; + } + /* * The state that came in from userspace was user-state only. * Mask all the user states out of 'xfeatures':