From patchwork Tue Nov 15 23:09:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 20628 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2987340wru; Tue, 15 Nov 2022 15:14:39 -0800 (PST) X-Google-Smtp-Source: AA0mqf6Jnldw54i+3LsF4oCo6zrT4ujNf7VTdhGb8FfAVZ+mlInX631aC9pwedLgHW8k+KLFAhOV X-Received: by 2002:a62:6446:0:b0:565:c122:b63 with SMTP id y67-20020a626446000000b00565c1220b63mr19884115pfb.49.1668554079091; Tue, 15 Nov 2022 15:14:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668554079; cv=none; d=google.com; s=arc-20160816; b=Z969+jG/w8G2ZSqKqJ0Bs9SX5K/MzDz+RosDRRs6X8VkAEehMpnRhbzTCsgFNOrdnG rn3VLErx3AxpShXDJ4nGYBy3ldWFCI+6iVZLH5QzdHQ4h+Z93voqHP20+KkSWi/NDBWC 2+i0m6jjjizilIxrrlMk09y7db8FTbpZd8/4nzlE9mqLruaSpYaxLfnds+AxQixwXLw5 SOfmUPEfKr6V2rvEfBims5khR/mXIpliBervV6b4CglXcuc8bTDt+KVSimU9OshWUlvr xNr8KKWnGSTxGvjwDj37diSUKMw4pUelLsb2TwxCr8bl0tNjDnSsS40gq5rDu9BW9OET 63Og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qhCq8AfDMkagyPimKyHUX7gwQAIYuE0429CZlbx4GKo=; b=v3d+uADAxDDRfkubMFqfaX33oXeOL5N1PU+KHsJgwlrRvJbs1t7n3wbukYG9HLR33X f6f8ztnew10agpfygQOWiGajOnMojnUPl91ZjEyfD/sKdNHwkk9bXPUXk7Fs5ubpWoER Pz3yjGPAxcdqffSX83HSL8riKama8E9Dx7/WKhB045NXWGezZXe7bR0eAD6kD9Q8wRS+ 2AYg31IusvSkVdMYSPCjikTWNpPdS9QAAtXVcJG7j7SoLRGTrNmEPYF9RVrVsAYhQxr7 D2d2B9h2FzbT03n3GB4l/iAMVgnxrJH+kOOgKpnHXj/EL/BMUzEucr8S1BM6u2qtDuKZ NWTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=GimrCofV; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z3-20020a170903018300b0017f641cac01si15740315plg.27.2022.11.15.15.14.24; Tue, 15 Nov 2022 15:14:39 -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=@kylehuey.com header.s=google header.b=GimrCofV; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231811AbiKOXMD (ORCPT + 99 others); Tue, 15 Nov 2022 18:12:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231931AbiKOXLe (ORCPT ); Tue, 15 Nov 2022 18:11:34 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BBBF2E694 for ; Tue, 15 Nov 2022 15:11:33 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id q1-20020a17090a750100b002139ec1e999so625044pjk.1 for ; Tue, 15 Nov 2022 15:11:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qhCq8AfDMkagyPimKyHUX7gwQAIYuE0429CZlbx4GKo=; b=GimrCofVDchBbipFskt1iUCOXnsWmIdeeUhJXRau6K5YrKcWWLcbm4F6m/Rt+KUJAw +WRhPd+7BzDfOsxy0qrFpcfJzeV+BcJpYPqUUHzjI40fCqFnDZ/60Sn/OqkCFCwvJ1Pa 8fbLcULMD5kLsZxJQx+HNteTzYyOKSAqUvU6rdjNsW8sgOgMMV75IDivsBzAHyMGZTSN 1LCWAgm2a9FTx2BX9r0sq0SdTky6C/z5V1IyDlILeX5grbmZ82gCRccKNehMA4xWGVLe jDkvaMW/D3aT8UGl4tk5nqXwC7c4CNpe93sbiL70+q70UZ6IwOrMNi/O9mCpoQygC8oy X3Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qhCq8AfDMkagyPimKyHUX7gwQAIYuE0429CZlbx4GKo=; b=g/3ABBWyelJTMsG3LPzuM+88jCMqTe+OOA6MfkUDJjND50AAAjMK2+hU9iDeWCZxpT xjYo3xuK0KS6trs2ANfH9hKNJ9cc5LdZNcg6nbZ8KSpbvVyxVLVNS7spV0fSFiKBZCw3 t0LSiqpE7jRhsr2DwyQArCD7rLaNthnx73KwMjjwTZCEfdLVNf731ro3jIHDE5+IN4DR nBd1MmKsovx0xcIooM8NncKJkCRw7lFmu66Vm60lr0q50DA5+V7NoOnXyGCnMTFOjvoH OkM4jNfKyL6fGKq4fcK2BzT28CKzStuBvvGZ3SWJs85idEt7dD+ETvgf8aHtff4coU0/ JCgQ== X-Gm-Message-State: ANoB5pnSkMrLetZtd3n8NnFNj2ohfaS+vlyGsC1KWkErpb/97L5wL8hX bq4F3IerJ/o53IXrGSpj/DaKgA== X-Received: by 2002:a17:902:e483:b0:188:b44b:598 with SMTP id i3-20020a170902e48300b00188b44b0598mr6272266ple.54.1668553892785; Tue, 15 Nov 2022 15:11:32 -0800 (PST) Received: from minbar.home.kylehuey.com (c-71-198-251-229.hsd1.ca.comcast.net. [71.198.251.229]) by smtp.gmail.com with ESMTPSA id f15-20020a62380f000000b0056c360af4e3sm9308372pfa.9.2022.11.15.15.11.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 15:11:32 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Dave Hansen Cc: Linus Torvalds , Thomas Gleixner , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Robert O'Callahan , David Manouchehri , Kyle Huey Subject: [PATCH v7 1/6] x86/fpu: Take task_struct* in copy_sigframe_from_user_to_xstate() Date: Tue, 15 Nov 2022 15:09:27 -0800 Message-Id: <20221115230932.7126-2-khuey@kylehuey.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221115230932.7126-1-khuey@kylehuey.com> References: <20221115230932.7126-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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?1749605761690955638?= X-GMAIL-MSGID: =?utf-8?q?1749605761690955638?= This will allow copy_sigframe_from_user_to_xstate() to grab the address of thread_struct's pkru value in a later patch. Signed-off-by: Kyle Huey --- arch/x86/kernel/fpu/signal.c | 2 +- arch/x86/kernel/fpu/xstate.c | 4 ++-- arch/x86/kernel/fpu/xstate.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c index 91d4b6de58ab..558076dbde5b 100644 --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -396,7 +396,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx, fpregs = &fpu->fpstate->regs; if (use_xsave() && !fx_only) { - if (copy_sigframe_from_user_to_xstate(fpu->fpstate, buf_fx)) + if (copy_sigframe_from_user_to_xstate(tsk, buf_fx)) return false; } else { if (__copy_from_user(&fpregs->fxsave, buf_fx, diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 59e543b95a3c..32ba5d95628d 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1278,10 +1278,10 @@ int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf) * XSAVE[S] format and copy to the target thread. This is called from the * sigreturn() and rt_sigreturn() system calls. */ -int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, +int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf) { - return copy_uabi_to_xstate(fpstate, NULL, ubuf); + return copy_uabi_to_xstate(tsk->thread.fpu.fpstate, NULL, ubuf); } static bool validate_independent_components(u64 mask) diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h index 5ad47031383b..f08ee2722e74 100644 --- a/arch/x86/kernel/fpu/xstate.h +++ b/arch/x86/kernel/fpu/xstate.h @@ -47,7 +47,7 @@ extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate, extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk, enum xstate_copy_mode mode); extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf); -extern int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, const void __user *ubuf); +extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf); extern void fpu__init_cpu_xstate(void); From patchwork Tue Nov 15 23:09:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 20631 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2987437wru; Tue, 15 Nov 2022 15:14:55 -0800 (PST) X-Google-Smtp-Source: AA0mqf5xNiQ6xdyGxmUXuM35IuQTtJnxS34A1qIqybvNmp6DwvvhOwDyuATIdFdIKr0lSxyco4Y0 X-Received: by 2002:a62:e310:0:b0:53e:81ab:9419 with SMTP id g16-20020a62e310000000b0053e81ab9419mr20519575pfh.15.1668554095085; Tue, 15 Nov 2022 15:14:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668554095; cv=none; d=google.com; s=arc-20160816; b=XyBsPcqCP4HATE1BQIwngHZY3gpdIO+Cs5IwJ3Zahxxzr9KMekUqWAJz0fXCagYYv1 49XidibhjO30kg0XcPDLa1tSawFw/H+UbUfgtJRNUqnue1Y62ZpPtEQh3ucZtaVOex1e kG5mMMMRNKQkMDCYAVJgB06F+DbjoqkDiTlY1tazZBBWM1M+N0ZEe8DKzH43lDbXRFjf E96ddp6TH1H46g28tLTzJ86bJJahNQsomazigkcab4CnuV/IHf/GtpUOy8tXnmYVXekZ n9EvetLdK01uvmwX20A6/RuCkZMB5138Bcc2WtPq+vRnvqP6U9PayUAMIj/zY6RxiAwJ CW7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VkXZyYO2hGQH+BId88DD3kMOt/JKYNASeU3kq3/BxNg=; b=FUyu90OMB4hL9kD7x19wrusheTfncRX8los0ctwBM7moaM6WsH254NBl7iXLwFkzxN Gai6CUhWDX1ezmjkK5xOsjes7itj7+gxVCogukrTNwYmAVam2d/uFrZBV7aaf1VvVmvu zUxmtQFwTVoDAPXlPPt3T8Y6Mmyp2mrAYtuPRjYElCq/jRmxrxkd4DL2N8LxwLdLVUHy yjwr+vub3ePTlyLIMCWuMB7+mKcOJlthoBMsxO0U0p6Jq9vZZ7SD1uIVCtCaVh/+IeL1 BEEzrO5DnEfY8De8F+9GYFKB9VfohRYdixWDctc5NjUFZV+aSkwIC/OMcUxpZ5Y0/MjW c7ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=PJSFalW9; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s14-20020a170902b18e00b00179f9521d08si12488232plr.149.2022.11.15.15.14.41; Tue, 15 Nov 2022 15:14:55 -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=@kylehuey.com header.s=google header.b=PJSFalW9; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229643AbiKOXMK (ORCPT + 99 others); Tue, 15 Nov 2022 18:12:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238289AbiKOXLf (ORCPT ); Tue, 15 Nov 2022 18:11:35 -0500 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9EEC2E6AA for ; Tue, 15 Nov 2022 15:11:34 -0800 (PST) Received: by mail-pj1-x1033.google.com with SMTP id v3-20020a17090ac90300b00218441ac0f6so2486386pjt.0 for ; Tue, 15 Nov 2022 15:11:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VkXZyYO2hGQH+BId88DD3kMOt/JKYNASeU3kq3/BxNg=; b=PJSFalW97JocjRPsweyy+GvKZWv53ItjFSUoPx5y9DdQ6EZ/fzc+SZpCngVuSd9ea1 LlVOTnFpygwuMc3lztlKy+xmDdb+Jp/+xaSiCp9boUPxnjP7mObm84gEd526bQlKMdKW tIgzIfdG2ycjjmG23/XuePNtsMRIGoQMQNfwDlCWtaSjDiGY1UowCVM8bqRYoONG2bbc I+d6SnAqmdBy6cFZWgDfGsC9zzqcv8d4iGLP/cJX/FPyfHJrLqafXYRt827bXtjHLxHW lHyI34dIT86OTlpWY2FinxvlEhvZLL0UIdSVo1hAKM3l1YPJV1d/rsASSEGIkcIKAwnZ RwBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VkXZyYO2hGQH+BId88DD3kMOt/JKYNASeU3kq3/BxNg=; b=oLha6ugiHeE5sTAh3yaresYul0Pa6eL9J463lZ5wktOgCgz04YRRS59t3iyJBYY+hb MYwql6sZ1C8hgaabh6bX/Xx5Y4DtaNkvFuTM1RofB98lBoWkuGSxq3SUVuiYyDVACtTq J83oscPz/1VIyVnrswu5SJ+HEZ9vMySMr63B7jmExKQe35YYxQ0Z0g278cjwJ40Ek3Rw rThxCa9e31oohWdx1nGcX2OosFuxVGgWkM7TjiBsRkVWnt/PHRjSBWnadd/zS4xNOF1n UVd0D4DZmCfN7zi0D5D17s31N8tUVdD3jgmYbx0iqrRFcS4NHXdTxWDJL2whYiJPt3w+ Ueug== X-Gm-Message-State: ANoB5pkwW8moaPyvHyppVmP5eSkgS012KrD5JF/o+2i9xIQnPVc2BHP/ acS2yHcF5BKqQDzPQoTvqbBjPw== X-Received: by 2002:a17:903:228e:b0:177:faf5:58c5 with SMTP id b14-20020a170903228e00b00177faf558c5mr6230654plh.166.1668553894421; Tue, 15 Nov 2022 15:11:34 -0800 (PST) Received: from minbar.home.kylehuey.com (c-71-198-251-229.hsd1.ca.comcast.net. [71.198.251.229]) by smtp.gmail.com with ESMTPSA id f15-20020a62380f000000b0056c360af4e3sm9308372pfa.9.2022.11.15.15.11.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 15:11:34 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Dave Hansen Cc: Linus Torvalds , Thomas Gleixner , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Robert O'Callahan , David Manouchehri , Kyle Huey Subject: [PATCH v7 2/6] x86/fpu: Add a pkru argument to copy_uabi_from_kernel_to_xstate(). Date: Tue, 15 Nov 2022 15:09:28 -0800 Message-Id: <20221115230932.7126-3-khuey@kylehuey.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221115230932.7126-1-khuey@kylehuey.com> References: <20221115230932.7126-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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?1749605778321331062?= X-GMAIL-MSGID: =?utf-8?q?1749605778321331062?= Both KVM (through KVM_SET_XSTATE) and ptrace (through PTRACE_SETREGSET with NT_X86_XSTATE) ultimately call copy_uabi_from_kernel_to_xstate(), but the canonical locations for the current PKRU value for KVM guests and processes in a ptrace stop are different (in the kvm_vcpu_arch and the thread_state structs respectively). In preparation for eventually handling PKRU in copy_uabi_to_xstate, pass in a pointer to the PKRU location. Signed-off-by: Kyle Huey --- arch/x86/kernel/fpu/core.c | 2 +- arch/x86/kernel/fpu/regset.c | 2 +- arch/x86/kernel/fpu/xstate.c | 2 +- arch/x86/kernel/fpu/xstate.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 3b28c5b25e12..550157686323 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -406,7 +406,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); + ret = copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru); if (ret) return ret; diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c index 75ffaef8c299..6d056b68f4ed 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -167,7 +167,7 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset, } fpu_force_restore(fpu); - ret = copy_uabi_from_kernel_to_xstate(fpu->fpstate, kbuf ?: tmpbuf); + ret = copy_uabi_from_kernel_to_xstate(fpu->fpstate, kbuf ?: tmpbuf, &target->thread.pkru); out: vfree(tmpbuf); diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 32ba5d95628d..a4d24ae66796 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1268,7 +1268,7 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, * Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S] * format and copy to the target thread. Used by ptrace and KVM. */ -int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf) +int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru) { return copy_uabi_to_xstate(fpstate, kbuf, NULL); } diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h index f08ee2722e74..a4ecb04d8d64 100644 --- a/arch/x86/kernel/fpu/xstate.h +++ b/arch/x86/kernel/fpu/xstate.h @@ -46,7 +46,7 @@ extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate, u32 pkru_val, enum xstate_copy_mode copy_mode); extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk, enum xstate_copy_mode mode); -extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf); +extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru); extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf); From patchwork Tue Nov 15 23:09:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 20629 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2987378wru; Tue, 15 Nov 2022 15:14:45 -0800 (PST) X-Google-Smtp-Source: AA0mqf5wSPfbfoSWWNCdl3lPQlB6WFxNMKmFQNjyR9kJp2dEUBQmUhg1M62dSU6e+hcavVbv2p/6 X-Received: by 2002:a17:90a:d595:b0:213:f8a9:356c with SMTP id v21-20020a17090ad59500b00213f8a9356cmr628079pju.224.1668554085479; Tue, 15 Nov 2022 15:14:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668554085; cv=none; d=google.com; s=arc-20160816; b=uSG42aoWGEc2W2RuuCrpyx8crHcZGaC3i4xSVnhMvrDkSo9jg/aAakefdHH45O7fKV FKyz8QLx2GBiynFJE6nQIu9MHEXjNtubono8xJxpV388Ek4edTD3U3L0fkQNe1nnzFk1 GdG7k7Z9c2CbxYb8iGb3Wm0pU/fy4H5bZCMMRQ+hOQ2rs18fmFjU8uBf9hGCaedMKwI6 a2uWTlzd1AuebZ+gUMvBaJrPhoAgov+LC1Jb75Ub+n3LX/Etp/TbPeNN3DZTXI62SBcp ch4dCxusIwxOYjQd9OMlmLRWTjI2jbf6+RKqqNVkfo5Y0rV8EMqI5+nL9pgrUKF0QO5y oAgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IkcqOwsa0mNg6ehrFGqMru8FrDdXrPVN26VUHl2O8Uk=; b=TbjKBvR10iDG4ZT26kxZz2ouktPrcswWIvRThPxBuNGT7TFyJm4vPunTra5vavBcCO vDhhm6sCsHMtr+xkoOd36xRGggKpNvgv7BDQbdUAkCKfG1/BxyqAIP4DFQYSCK5k/U4H H/WmfY1etHpGk0movoErGvJFNiWbasBVA6YrppczR5ZvdOeWVMFVKiuwa7e3emEbIKgD d/jkRcDVSkeO8M+QKJZP0KUvjEyRL1+NuCyp+X6FMjwm9Ew8I0zQ7R3jZu4WbJtJaKwP HMkcpsxjxuS3b0KTNevuDjbQSawCniB8VtknsEefnAIib2stPKx7rnc5UymmAqgSz1Z+ q6pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=COkajg1x; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z17-20020a170903019100b00183243c7a2dsi15369353plg.406.2022.11.15.15.14.31; Tue, 15 Nov 2022 15:14:45 -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=@kylehuey.com header.s=google header.b=COkajg1x; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231578AbiKOXMQ (ORCPT + 99 others); Tue, 15 Nov 2022 18:12:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238530AbiKOXLh (ORCPT ); Tue, 15 Nov 2022 18:11:37 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 640422B611 for ; Tue, 15 Nov 2022 15:11:36 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id q1-20020a17090a750100b002139ec1e999so625219pjk.1 for ; Tue, 15 Nov 2022 15:11:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IkcqOwsa0mNg6ehrFGqMru8FrDdXrPVN26VUHl2O8Uk=; b=COkajg1xfIWppQNgvV1yPMUIBGJNDsN7XOZhgcmUBNkuQT5h7POGLw7aCrSgOJf1TM fi7NPk3zvATxjZv2bNJcrHavWcRc2UgJAIP6OCemRQn3b/U6IWlcE7pn+5h1em+rNTul dzS9rv2/1ApGIHuka4h6ZnT7Kkg/TNMxfv+hcGk4Brs4h4vfyIqc/JprwgljCNFZg+/x 3nAyLM4Y+r/cslbUQ7jeW+L9I4aGcWyK0cnm1r5cbcnQtlYkw/v6LwqzmcrfBRb9dOQH Vos5+xpdw5OowrlXFlUM6umMxnMY9+Y3UBnE2eqKNCAOLOgVeg0qKEGNs0Bv4+m4ydV6 QivA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IkcqOwsa0mNg6ehrFGqMru8FrDdXrPVN26VUHl2O8Uk=; b=pfPyQG0aM63uE+qNHBQ29hewMivwjK9bBfuw438S/P4KjmmDSP39mtIUoiwhU3LWjn wDYA04ClkOQypfxGznCahEPzmUgdpx78MrfENUJGUXNXwBaijpMXe5MlQqINbn+bDBUA IZ15dlRr9iBTba3Eqmbz0VfRC3LPmFhXUePUZSw+uyW+QuvbC1Bc3CEF8wGfvpHWtTFX IIwq6TIf3BfNjp8W6H911UQLHTRuo2Izl0hQLNnlxxrNu45B0VGA4DDSbn1cbdG1Uvti IaMlO1Vk+TmV9F748HI4kdwswXBd3jeWvUiRZBf3WMkejOXWdccNheMHbB4+jFqUaPIu 12Ag== X-Gm-Message-State: ANoB5plkur5kBOjL47YjRvukJJuiK5rpM5FPIzi0KfqjCvZHqIoml7rA +/d5C4PzIDqxBv29fqMJruCYAw== X-Received: by 2002:a17:902:8c8d:b0:17f:73d6:4375 with SMTP id t13-20020a1709028c8d00b0017f73d64375mr6208515plo.24.1668553896035; Tue, 15 Nov 2022 15:11:36 -0800 (PST) Received: from minbar.home.kylehuey.com (c-71-198-251-229.hsd1.ca.comcast.net. [71.198.251.229]) by smtp.gmail.com with ESMTPSA id f15-20020a62380f000000b0056c360af4e3sm9308372pfa.9.2022.11.15.15.11.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 15:11:35 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Dave Hansen Cc: Linus Torvalds , Thomas Gleixner , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Robert O'Callahan , David Manouchehri , Kyle Huey Subject: [PATCH v7 3/6] x86/fpu: Add a pkru argument to copy_uabi_to_xstate() Date: Tue, 15 Nov 2022 15:09:29 -0800 Message-Id: <20221115230932.7126-4-khuey@kylehuey.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221115230932.7126-1-khuey@kylehuey.com> References: <20221115230932.7126-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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?1749605769132999202?= X-GMAIL-MSGID: =?utf-8?q?1749605769132999202?= In preparation for moving PKRU handling code out of fpu_copy_uabi_to_guest_fpstate() and into copy_uabi_to_xstate(), add an argument that copy_uabi_from_kernel_to_xstate() can use to pass the canonical location of the PKRU value. For copy_sigframe_from_user_to_xstate() the kernel will actually restore the PKRU value from the fpstate, but pass in the thread_struct's pkru location anyways for consistency. Signed-off-by: Kyle Huey --- arch/x86/kernel/fpu/xstate.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index a4d24ae66796..3a6ced76e932 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1200,8 +1200,18 @@ static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size, } +/** + * copy_uabi_to_xstate - Copy a UABI format buffer to the kernel xstate + * @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 + * + * Converts from the UABI format into the kernel internal hardware + * dependent format. + */ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, - const void __user *ubuf) + const void __user *ubuf, u32 *pkru) { struct xregs_state *xsave = &fpstate->regs.xsave; unsigned int offset, size; @@ -1270,7 +1280,7 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, */ int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru) { - return copy_uabi_to_xstate(fpstate, kbuf, NULL); + return copy_uabi_to_xstate(fpstate, kbuf, NULL, pkru); } /* @@ -1281,7 +1291,7 @@ int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf) { - return copy_uabi_to_xstate(tsk->thread.fpu.fpstate, NULL, ubuf); + return copy_uabi_to_xstate(tsk->thread.fpu.fpstate, NULL, ubuf, &tsk->thread.pkru); } static bool validate_independent_components(u64 mask) From patchwork Tue Nov 15 23:09:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 20634 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2987847wru; Tue, 15 Nov 2022 15:15:49 -0800 (PST) X-Google-Smtp-Source: AA0mqf67Ngc3Gutwn0v7+OMFja9ZK2NkKkWGIfxTAGD9cTN1+kxIdeBsFI3FX68oYosqpoyhXFJJ X-Received: by 2002:a17:903:24b:b0:186:8a1e:9b46 with SMTP id j11-20020a170903024b00b001868a1e9b46mr6128186plh.80.1668554148967; Tue, 15 Nov 2022 15:15:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668554148; cv=none; d=google.com; s=arc-20160816; b=SioD+fbQADT6MaMA0XQdX3dgvUpr6Zicr6KfjXhz9+ehYiRORbwLrqzVL1GYq3k0sg K06qFyAhrj9U9JXTIVDX0E1dcjvXCNftZW7YuzCLTD2MSxqsAFKqrklRWUfIqRHCTJ4C d7F0hfTe6I5LZfSlFyysn+JrJNDs5t4jzq5S/JK6TrHdW3EfHl/MuqL3l3g7D7TZZhU6 Xzi46w/JpJlW+0Bo7272E9FHHMZ24h6Z2vgkfsJGD2g5SIH7ZIpiI7CFNV86KAdVORih /8UjU7twWoM/NG+y58546ArSKHgjdKnQgOvkR6EaCvLx9Om0UuirIDaGokMzXYAdlZ0N AtGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JUnl5mVywhHwsFoEEbFn7p+05kDcFKVp21uUWgQ1eAI=; b=MEtTCv3XGtT6wcS7ac/A7YFEF7keadoaA0kqYZAdrGNhfH9RMvJGU0twgr2fl/ptAU USwagTX8HSeUSgCuEx5J26N5/oUBT48YGH1Nzl3kwJLw+bdZD05yZmSyT9IjK7//Xcxj 8RF3otRM1o1C1rhRIs2ipJgy3DWpGfS74Nf38R2c+gFLzJXkRf/M9c1feuYqih9RNBXN 4FymofVGIf+axE1/3a2T/0q6h7SHUL1Ppmfv1mRkavo8RoP6h3aCJ6sXuu9kW9UYpBgp tJ5vEEaHD34NwSTOtxrcGCwl/hZ3IfUg+3W+DYSMZ8cvFeuJudcS2TDWhZOSdF3WFgSZ rTAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=DsjDjVjE; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i22-20020aa78d96000000b0052ee88f59c4si12680196pfr.372.2022.11.15.15.15.35; Tue, 15 Nov 2022 15:15: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=@kylehuey.com header.s=google header.b=DsjDjVjE; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232067AbiKOXMG (ORCPT + 99 others); Tue, 15 Nov 2022 18:12:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232473AbiKOXLj (ORCPT ); Tue, 15 Nov 2022 18:11:39 -0500 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D6132D74F for ; Tue, 15 Nov 2022 15:11:38 -0800 (PST) Received: by mail-pg1-x535.google.com with SMTP id v3so14985348pgh.4 for ; Tue, 15 Nov 2022 15:11:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JUnl5mVywhHwsFoEEbFn7p+05kDcFKVp21uUWgQ1eAI=; b=DsjDjVjE5eNrFy11hX+hSy394Ka7KCuWodECy1nbg4uc4FpRLFMxTr7G36Ovt+sQHk PJv0QI4UTeM0xRiJvJWeBsIm5Q1VBiC189XuLKUxCFH5vPD5+GxmyGT8AR8kM22M8GrL DCvOCfX0BrGFAKtgXxQL6ZUikSpBFMguHt956oDc3b6N7lspDsJfAG/p6bg6xbm3QlBH hoPVV1PE6f8ffwIlgLBK/5a56Bivs/clznujDp0ZxHLWrtfEJFItl/cOgwoynuqc00pq 3JeGMobmsgjOK8KhTh9+nH4EvwzFFsQiXb8Q/k0aI/lQC0M0h4gRgm9f8mFiOt5R1YBv ukLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JUnl5mVywhHwsFoEEbFn7p+05kDcFKVp21uUWgQ1eAI=; b=hYVhK3wK4t0ZqppZceDjsta2+WbEmednjN9Runi1ryRy81LdSiGrJWPSfBrn2HbvB/ fP7Mbfs8KNC31XJtm30zFn2C00JDPeIfq/Xi0xfjO9TD8vIBvwjmUCzth3YzoidbuKew RYYbUt+sWa5yQ3q3IaxzXc17OyrgnVaSRlLvQhMF7DgPeEGn8lJNdwa6+utYRp5Rqbk3 jMVKlc33n+BcBYuF0zH29dJ/XWEhM0dcvtVIxFEzj5upvvy+ZRxvfCt3R57w6RTeiLzj +mZuP+LyKKffnvuFx8mBk/EzzRRfzETcWVMWWRlifaK+00wONh+roH3QJpA/2I5/SHd/ YZQw== X-Gm-Message-State: ANoB5pk4pjSbgG0z3YRlKD/LAhR6192arc56b1ktlEgaGCvP1e8+h3re Rhcxvbzgxk5iAEJ/0Zm95ndI+Wou7IT9cxBj X-Received: by 2002:a63:1a24:0:b0:470:60a5:2f70 with SMTP id a36-20020a631a24000000b0047060a52f70mr18313359pga.99.1668553897780; Tue, 15 Nov 2022 15:11:37 -0800 (PST) Received: from minbar.home.kylehuey.com (c-71-198-251-229.hsd1.ca.comcast.net. [71.198.251.229]) by smtp.gmail.com with ESMTPSA id f15-20020a62380f000000b0056c360af4e3sm9308372pfa.9.2022.11.15.15.11.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 15:11:37 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Dave Hansen Cc: Linus Torvalds , Thomas Gleixner , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Robert O'Callahan , David Manouchehri , Kyle Huey , Borislav Petkov , stable@vger.kernel.org Subject: [PATCH v7 4/6] x86/fpu: Allow PKRU to be (once again) written by ptrace. Date: Tue, 15 Nov 2022 15:09:30 -0800 Message-Id: <20221115230932.7126-5-khuey@kylehuey.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221115230932.7126-1-khuey@kylehuey.com> References: <20221115230932.7126-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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?1749605835671435435?= X-GMAIL-MSGID: =?utf-8?q?1749605835671435435?= 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. Fixes: e84ba47e313d ("x86/fpu: Hook up PKRU into ptrace()") Signed-off-by: Kyle Huey Cc: Dave Hansen Cc: Thomas Gleixner Cc: Borislav Petkov Cc: stable@vger.kernel.org # 5.14+ --- 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 550157686323..46b935bc87c8 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 3a6ced76e932..bebc30c29ed3 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': From patchwork Tue Nov 15 23:09:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 20632 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2987446wru; Tue, 15 Nov 2022 15:14:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf5pJ07P945AU6UEjA/4QxLktJ1Cjvur+kIVbe2S6/qUrKoF0O3CN8MRgBl8fHinhrYIFXQu X-Received: by 2002:a17:90b:3a89:b0:205:ff5b:d27a with SMTP id om9-20020a17090b3a8900b00205ff5bd27amr582770pjb.225.1668554097949; Tue, 15 Nov 2022 15:14:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668554097; cv=none; d=google.com; s=arc-20160816; b=PoyztBb8aa1sS7Dr+fXudB5pJmrqXOMMsrYzjjaMipfSHeNTLaeOcHoprlgiKQ0h+n M8BTET5cb7WGAU162gDHPfmbF68pUPRqDa0mwp6pL0dezlXqE3OUV/HICvTKwAA+rKfm uD2htpZKeiTqRO3vhlqdZKm1zEiaL6tvnEbcUYeXMJHDHhkW9/1ZXX8i9SAihwOJYFWS RmaWKA22DQQytylk6k0ZxYiJmgpA6B6Moi6xjo0fWGHN5+yzhlaa6rMXcEzi+agYfeSa xJxfcnqxtCoS9xV3WdI4J0mGjnnHV2GkeM+RmKVuBsdRfjQW5UkNrpEcNajVLCold63G W9uA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1qpXWSCDrMjTrrN7DSUZrVNnGlngy6qbcHa+BWaBxfw=; b=X3iR/3PqwEcOXF7h75Jk/7SOwCX+NRolhG/S36QDZPGMa+dbFkuFR7AUchCZgZQRiM ImHd8cFdq6EnCSMi5rHQ+ZRLaNbl4eUu6KdXjAqI44i4ABFP/9rjQqZYaqD6REDcoyIP MkMR8rcmomtcDxBkLuZ4cpxRCk1SmVTsXNMzocHDaHx1hgmOj6DSibHXHeKsSjwgp7mP GoWsglx3DDABJMCfOWNPfTu/bPzpXjmvpLc71257Wl2qCGEOaebrSNah32p/5Vuj60vW 7pZBPwHZW0Ksltu5eiChmc8JG6Bk64qnJmcufBqsfjvH/Rlt6oGXTFjmwinCP9BgksYe Ye/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=by3OpFNk; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 5-20020a630e45000000b0046f9f4f8e08si12832343pgo.156.2022.11.15.15.14.44; Tue, 15 Nov 2022 15:14:57 -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=@kylehuey.com header.s=google header.b=by3OpFNk; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231431AbiKOXMU (ORCPT + 99 others); Tue, 15 Nov 2022 18:12:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238586AbiKOXLk (ORCPT ); Tue, 15 Nov 2022 18:11:40 -0500 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24B112E6A6 for ; Tue, 15 Nov 2022 15:11:40 -0800 (PST) Received: by mail-pj1-x102a.google.com with SMTP id b11so14922112pjp.2 for ; Tue, 15 Nov 2022 15:11:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1qpXWSCDrMjTrrN7DSUZrVNnGlngy6qbcHa+BWaBxfw=; b=by3OpFNkQCvqiF7Z+Ei1ZUihu4ifOPLgyDRqCGJ9mDwSmsppwq5B3L7OSbVs6FSzZq uhL+U8YozGaxewngILbOAgMLv44xfey9tO53DDpqYVyjzvahWzl8o30YKcgVXdT8kNm+ yVYA2KpHsHz+kaX5LE4vA2BwqI/ZXosIYX/iaFNgbaNHzVkjrTPfHjGOFLGbgOY2TEO8 UoZlZ+woOIAoxIsgvz0lPHGOU7pjF/pHTS8lE2M3ixMF41UBdWT/uVG4VyrikJK0hbZp ANMTkWGeITdPtIEs873AmNugHQc01OnW4Tepj+jV9QwKdUq+iq1VRDxQMMnKRRXkkUfl w74A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1qpXWSCDrMjTrrN7DSUZrVNnGlngy6qbcHa+BWaBxfw=; b=VWFwpud+eRny5n9t9kS26HOLtp4o8dwvPtyMPPMLH3E1od4WznXrjrlMoehNkpwQNF Ncj8dlcets1CR0nKMSo74YvkVJmHF/mYmmsK3sFcAgbMSrQOTXJq/uoRXjqGVGmj2SqS 8SGkqn+y9GSk/E41eHGuaa5XdaSA6QfmOYKinV1cmy5fSykrPy7Y5w5RQk80eOITRoOx HXCnqJM2gil8MeFzXCGHk2inanHBlfGWpESDrQNeoSFdvZI8RVyP+xuiLggnxM1TL7Ch ytVbsEo2S9Q4zlx8BjqCEe3x++OduIWqAxxnfUxELcZS2UcwJot9+nYP6TzVRdXmFOph pGWw== X-Gm-Message-State: ANoB5pkgh6hj/Iwb+R00ILocerj/wLOju5vPVQtWM1EK9J0VDItpgf94 fLUHk3+ipANDBG65ARTaw6NR3Q== X-Received: by 2002:a17:902:8649:b0:187:3921:2b2d with SMTP id y9-20020a170902864900b0018739212b2dmr6440688plt.13.1668553899569; Tue, 15 Nov 2022 15:11:39 -0800 (PST) Received: from minbar.home.kylehuey.com (c-71-198-251-229.hsd1.ca.comcast.net. [71.198.251.229]) by smtp.gmail.com with ESMTPSA id f15-20020a62380f000000b0056c360af4e3sm9308372pfa.9.2022.11.15.15.11.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 15:11:39 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Dave Hansen Cc: Linus Torvalds , Thomas Gleixner , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Robert O'Callahan , David Manouchehri , Kyle Huey , Borislav Petkov , stable@vger.kernel.org Subject: [PATCH v7 5/6] x86/fpu: Emulate XRSTOR's behavior if the xfeatures PKRU bit is not set Date: Tue, 15 Nov 2022 15:09:31 -0800 Message-Id: <20221115230932.7126-6-khuey@kylehuey.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221115230932.7126-1-khuey@kylehuey.com> References: <20221115230932.7126-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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?1749605782033895245?= X-GMAIL-MSGID: =?utf-8?q?1749605782033895245?= The hardware XRSTOR instruction resets the PKRU register to its hardware init value (namely 0) if the PKRU bit is not set in the xfeatures mask. Emulating that here restores the pre-5.14 behavior for PTRACE_SET_REGSET with NT_X86_XSTATE, and makes sigreturn (which still uses XRSTOR) and ptrace behave identically. KVM has never used XRSTOR and never had this behavior, so KVM opts-out of this emulation by passing a NULL pkru pointer to copy_uabi_to_xstate(). Fixes: e84ba47e313d ("x86/fpu: Hook up PKRU into ptrace()") Signed-off-by: Kyle Huey Cc: Dave Hansen Cc: Thomas Gleixner Cc: Borislav Petkov Cc: stable@vger.kernel.org # 5.14+ --- arch/x86/kernel/fpu/core.c | 8 ++++++++ arch/x86/kernel/fpu/xstate.c | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 46b935bc87c8..8d0f6019c21d 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -404,6 +404,14 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, if (ustate->xsave.header.xfeatures & ~xcr0) return -EINVAL; + /* + * Nullify @vpkru to preserve its current value if PKRU's bit isn't set + * in the header. KVM's odd ABI is to leave PKRU untouched in this + * case (all other components are eventually re-initialized). + */ + if (!(ustate->xsave.header.xfeatures & XFEATURE_MASK_PKRU)) + vpkru = NULL; + return copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru); } EXPORT_SYMBOL_GPL(fpu_copy_uabi_to_guest_fpstate); diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index bebc30c29ed3..193c6e95daa8 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1219,8 +1219,14 @@ static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size, * 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. + * The kernel will restore it manually, so the XRSTOR behavior that resets + * the PKRU register to the hardware init value (0) if the corresponding + * xfeatures bit is not set is emulated here. * 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. + * pkru field. A pointer to that is passed in @pkru. KVM hasn't used + * XRSTOR and hasn't had the PKRU resetting behavior described above. To + * preserve that KVM behavior, it passes NULL for @pkru if the xfeatures + * bit is not set. */ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, const void __user *ubuf, u32 *pkru) @@ -1277,6 +1283,13 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, xpkru = __raw_xsave_addr(xsave, XFEATURE_PKRU); *pkru = xpkru->pkru; + } else { + /* + * KVM may pass NULL here to indicate that it does not need + * PKRU updated. + */ + if (pkru) + *pkru = 0; } /* From patchwork Tue Nov 15 23:09:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 20635 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2988003wru; Tue, 15 Nov 2022 15:16:10 -0800 (PST) X-Google-Smtp-Source: AA0mqf6I+lnQmCm4/nkIwcCwlW3ozDDkpTmweVv4xcROmmyfVhJ1Jc+m0gII3fJElYYh+sDT22fp X-Received: by 2002:a17:902:8649:b0:187:3921:2b2d with SMTP id y9-20020a170902864900b0018739212b2dmr6457216plt.13.1668554169818; Tue, 15 Nov 2022 15:16:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668554169; cv=none; d=google.com; s=arc-20160816; b=EPxrQJwNkoYfys2u/SSQWQLw8P4C6DmeFJXe7FtpjOPNEDUszHtCUZNxu8MVQjlNIL 8CopjvDGenW+dzDFf1gaMBAcArr829rGq//BmckI7xSHAB+vT1ZYkHB4hIg4XaH1Ml5G +v11w9dPm8khl/XHy3xgwN+jtXEVSASRNosdm/yqgAK5GI8ImCF+IOPKyg0rSr6aSElh E8TnZwS4aRhSwsrjuYvPMZYotSZLKJCOBRD+Mn45SIkuEVMpLLPdsqLOeQvGmbdNSf/t S0HlK/wFIlIyblHfSg8Tv2m61axEV15q9q5QZj5FkcJxx1TuNm1/VSAAd+H6AM9BK5BE w1tA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kKbS/wfCljHaJs+yu72j7uPbynmA+GaQ8f/h+/BgRKY=; b=fjujNZk2kilYAFxFDiX689j+0FDRy9qsnRvoTrviLD/dms321Td1z7dEpFbl6T1dcS JoVsY6qj5ehOM9qugwu0l45V15RQecRnRbdDC8GycuVyN8pcp+fYaYWfWjX25MRXQ9rf OX2Q1U2cbKl2TFtQbF6j824HjIRrsljAwXmP5G+mOy/ACCCzSgfHbmAMEFXCEbi5E2Aw s0JhojP28sF76NF7/QsdxwMYbvjnXpIaSBEFH/XZVignHT7WiKLD7zj871V1W/N7deqL 3TgvnC7AJSWYI2Tkh97Lolz2p+POfP3LMAbxjH2N0B6LBb1dFETwhcazFH+LrLzsYUWD RPnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=bZ3HnJm2; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x15-20020a63cc0f000000b0047090ef7ef9si12700472pgf.779.2022.11.15.15.15.56; Tue, 15 Nov 2022 15:16:09 -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=@kylehuey.com header.s=google header.b=bZ3HnJm2; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238595AbiKOXM0 (ORCPT + 99 others); Tue, 15 Nov 2022 18:12:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238570AbiKOXLm (ORCPT ); Tue, 15 Nov 2022 18:11:42 -0500 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD18E2DA97 for ; Tue, 15 Nov 2022 15:11:41 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id o7so14928194pjj.1 for ; Tue, 15 Nov 2022 15:11:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kKbS/wfCljHaJs+yu72j7uPbynmA+GaQ8f/h+/BgRKY=; b=bZ3HnJm24MjM7JcrDyTgpeH8OkI1L3C7EdNdqnF1eW9Y7D1gI4VdHaJilFToAT8KMx PYpoNinjDQG8SGBPelhz1UeCskoSCwsbt0tFlD/yoRnBth+9dyBRKMRsSQULkvkkpDVQ pX7DgPeMF9hc71wHUtcEalQqWSc7SnNPsmZS4gdwz/5+b1JyjE3pLhVNpv/gmXHdtGOt xq/WzMVrd1jJogO/U+dShKXlgHy9j5A4a+XApFdHgzjrFWJgTTP4eh3g2W/YvWRZloBa M1MM0PiCQANScP4JnyUkUGnyZVRYVkX048SeqFRoJXKdeLg0LWSipYpxRcFU83CJyK2I ME/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kKbS/wfCljHaJs+yu72j7uPbynmA+GaQ8f/h+/BgRKY=; b=wML2D0N/ma5vrKYE3+v1lyNisxdPp1jSrsDp4L/Vzho9zqZiBzQq8X1IJJjTqwW7ml VT8c6EAwA3BldotZhxBkZfw9VfK6L8Mao//7JNsb6wWFnWKKXZI1N9gPZ41pmG0FETMW roC3ai+KVTI43qGZF4/Euv0xzyJTDjW97A3hNWJKAv0ssrr/klLFYaVInhKRrLy3+XH1 6GD6XRrduNCw6vmhgUb3goKRgJ972g+U+dBeJy7FpRjIsJmmx5ewFSBMfIRg9aWsUaGO JdN3Us2h4eoCDSOvrgbrs2ng/fcFcN4l4mxGJcX8b4uO1TfEeci34L9mQ8AtjKjr6wpa tKhQ== X-Gm-Message-State: ANoB5plx7PuLFY3qJ7kNJ3iSur7kP2JVAh8G0NkNROcyT/IhTN3BwLdc Q7i4Zb/kqGV8lxJPgxIqQ2WE1g== X-Received: by 2002:a17:902:b694:b0:17f:7d9a:4952 with SMTP id c20-20020a170902b69400b0017f7d9a4952mr6190603pls.117.1668553901195; Tue, 15 Nov 2022 15:11:41 -0800 (PST) Received: from minbar.home.kylehuey.com (c-71-198-251-229.hsd1.ca.comcast.net. [71.198.251.229]) by smtp.gmail.com with ESMTPSA id f15-20020a62380f000000b0056c360af4e3sm9308372pfa.9.2022.11.15.15.11.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 15:11:40 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Dave Hansen Cc: Linus Torvalds , Thomas Gleixner , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Robert O'Callahan , David Manouchehri , Kyle Huey Subject: [PATCH v7 6/6] selftests/vm/pkeys: Add a regression test for setting PKRU through ptrace Date: Tue, 15 Nov 2022 15:09:32 -0800 Message-Id: <20221115230932.7126-7-khuey@kylehuey.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221115230932.7126-1-khuey@kylehuey.com> References: <20221115230932.7126-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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?1749605857091261849?= X-GMAIL-MSGID: =?utf-8?q?1749605857091261849?= This tests PTRACE_SETREGSET with NT_X86_XSTATE modifying PKRU directly and removing the PKRU bit from XSTATE_BV. Signed-off-by: Kyle Huey --- tools/testing/selftests/vm/pkey-x86.h | 12 ++ tools/testing/selftests/vm/protection_keys.c | 131 ++++++++++++++++++- 2 files changed, 141 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/pkey-x86.h b/tools/testing/selftests/vm/pkey-x86.h index b078ce9c6d2a..72c14cd3ddc7 100644 --- a/tools/testing/selftests/vm/pkey-x86.h +++ b/tools/testing/selftests/vm/pkey-x86.h @@ -104,6 +104,18 @@ static inline int cpu_has_pkeys(void) return 1; } +static inline int cpu_max_xsave_size(void) +{ + unsigned long XSTATE_CPUID = 0xd; + unsigned int eax; + unsigned int ebx; + unsigned int ecx; + unsigned int edx; + + __cpuid_count(XSTATE_CPUID, 0, eax, ebx, ecx, edx); + return ecx; +} + static inline u32 pkey_bit_position(int pkey) { return pkey * PKEY_BITS_PER_PKEY; diff --git a/tools/testing/selftests/vm/protection_keys.c b/tools/testing/selftests/vm/protection_keys.c index 291bc1e07842..95f403a0c46d 100644 --- a/tools/testing/selftests/vm/protection_keys.c +++ b/tools/testing/selftests/vm/protection_keys.c @@ -18,12 +18,13 @@ * do a plain mprotect() to a mprotect_pkey() area and make sure the pkey sticks * * Compile like this: - * gcc -o protection_keys -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm - * gcc -m32 -o protection_keys_32 -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm + * gcc -mxsave -o protection_keys -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm + * gcc -mxsave -m32 -o protection_keys_32 -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm */ #define _GNU_SOURCE #define __SANE_USERSPACE_TYPES__ #include +#include #include #include #include @@ -1550,6 +1551,129 @@ void test_implicit_mprotect_exec_only_memory(int *ptr, u16 pkey) do_not_expect_pkey_fault("plain read on recently PROT_EXEC area"); } +#if defined(__i386__) || defined(__x86_64__) +void test_ptrace_modifies_pkru(int *ptr, u16 pkey) +{ + u32 new_pkru; + pid_t child; + int status, ret; + int pkey_offset = pkey_reg_xstate_offset(); + size_t xsave_size = cpu_max_xsave_size(); + void *xsave; + u32 *pkey_register; + u64 *xstate_bv; + struct iovec iov; + + new_pkru = ~read_pkey_reg(); + /* Don't make PROT_EXEC mappings inaccessible */ + new_pkru &= ~3; + + child = fork(); + pkey_assert(child >= 0); + dprintf3("[%d] fork() ret: %d\n", getpid(), child); + if (!child) { + ptrace(PTRACE_TRACEME, 0, 0, 0); + /* Stop and allow the tracer to modify PKRU directly */ + raise(SIGSTOP); + + /* + * need __read_pkey_reg() version so we do not do shadow_pkey_reg + * checking + */ + if (__read_pkey_reg() != new_pkru) + exit(1); + + /* Stop and allow the tracer to clear XSTATE_BV for PKRU */ + raise(SIGSTOP); + + if (__read_pkey_reg() != 0) + exit(1); + + /* Stop and allow the tracer to examine PKRU */ + raise(SIGSTOP); + + exit(0); + } + + pkey_assert(child == waitpid(child, &status, 0)); + dprintf3("[%d] waitpid(%d) status: %x\n", getpid(), child, status); + pkey_assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP); + + xsave = (void *)malloc(xsave_size); + pkey_assert(xsave > 0); + + /* Modify the PKRU register directly */ + iov.iov_base = xsave; + iov.iov_len = xsave_size; + ret = ptrace(PTRACE_GETREGSET, child, (void *)NT_X86_XSTATE, &iov); + pkey_assert(ret == 0); + + pkey_register = (u32 *)(xsave + pkey_offset); + pkey_assert(*pkey_register == read_pkey_reg()); + + *pkey_register = new_pkru; + + ret = ptrace(PTRACE_SETREGSET, child, (void *)NT_X86_XSTATE, &iov); + pkey_assert(ret == 0); + + /* Test that the modification is visible in ptrace before any execution */ + memset(xsave, 0xCC, xsave_size); + ret = ptrace(PTRACE_GETREGSET, child, (void *)NT_X86_XSTATE, &iov); + pkey_assert(ret == 0); + pkey_assert(*pkey_register == new_pkru); + + /* Execute the tracee */ + ret = ptrace(PTRACE_CONT, child, 0, 0); + pkey_assert(ret == 0); + + /* Test that the tracee saw the PKRU value change */ + pkey_assert(child == waitpid(child, &status, 0)); + dprintf3("[%d] waitpid(%d) status: %x\n", getpid(), child, status); + pkey_assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP); + + /* Test that the modification is visible in ptrace after execution */ + memset(xsave, 0xCC, xsave_size); + ret = ptrace(PTRACE_GETREGSET, child, (void *)NT_X86_XSTATE, &iov); + pkey_assert(ret == 0); + pkey_assert(*pkey_register == new_pkru); + + /* Clear the PKRU bit from XSTATE_BV */ + xstate_bv = (u64 *)(xsave + 512); + *xstate_bv &= ~(1 << 9); + + ret = ptrace(PTRACE_SETREGSET, child, (void *)NT_X86_XSTATE, &iov); + pkey_assert(ret == 0); + + /* Test that the modification is visible in ptrace before any execution */ + memset(xsave, 0xCC, xsave_size); + ret = ptrace(PTRACE_GETREGSET, child, (void *)NT_X86_XSTATE, &iov); + pkey_assert(ret == 0); + pkey_assert(*pkey_register == 0); + + ret = ptrace(PTRACE_CONT, child, 0, 0); + pkey_assert(ret == 0); + + /* Test that the tracee saw the PKRU value go to 0 */ + pkey_assert(child == waitpid(child, &status, 0)); + dprintf3("[%d] waitpid(%d) status: %x\n", getpid(), child, status); + pkey_assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP); + + /* Test that the modification is visible in ptrace after execution */ + memset(xsave, 0xCC, xsave_size); + ret = ptrace(PTRACE_GETREGSET, child, (void *)NT_X86_XSTATE, &iov); + pkey_assert(ret == 0); + pkey_assert(*pkey_register == 0); + + ret = ptrace(PTRACE_CONT, child, 0, 0); + pkey_assert(ret == 0); + pkey_assert(child == waitpid(child, &status, 0)); + dprintf3("[%d] waitpid(%d) status: %x\n", getpid(), child, status); + pkey_assert(WIFEXITED(status)); + pkey_assert(WEXITSTATUS(status) == 0); + free(xsave); +} +#endif + void test_mprotect_pkey_on_unsupported_cpu(int *ptr, u16 pkey) { int size = PAGE_SIZE; @@ -1585,6 +1709,9 @@ void (*pkey_tests[])(int *ptr, u16 pkey) = { test_pkey_syscalls_bad_args, test_pkey_alloc_exhaust, test_pkey_alloc_free_attach_pkey0, +#if defined(__i386__) || defined(__x86_64__) + test_ptrace_modifies_pkru, +#endif }; void run_tests_once(void)