linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Chang S. Bae" <chang.seok.bae@intel.com>
To: linux-kernel@vger.kernel.org
Cc: x86@kernel.org, tglx@linutronix.de, mingo@redhat.com,
	bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com,
	yuan.yao@intel.com, chang.seok.bae@intel.com
Subject: [PATCH v2] x86/fpu: Fix copy_xstate_to_uabi() to copy init states correctly
Date: Fri, 21 Oct 2022 11:58:44 -0700	[thread overview]
Message-ID: <20221021185844.13472-1-chang.seok.bae@intel.com> (raw)
In-Reply-To: <20221018221349.4196-2-chang.seok.bae@intel.com>

When an extended state component is not present in fpstate, but in init
state, the function copies from init_fpstate via copy_feature().

But, dynamic states are not present in init_fpstate because of all-zeros
init states. Then retrieving them from init_fpstate will explode like this:

 BUG: kernel NULL pointer dereference, address: 0000000000000000
 ...
 RIP: 0010:memcpy_erms+0x6/0x10
  ? __copy_xstate_to_uabi_buf+0x381/0x870
  fpu_copy_guest_fpstate_to_uabi+0x28/0x80
  kvm_arch_vcpu_ioctl+0x14c/0x1460 [kvm]
  ? __this_cpu_preempt_check+0x13/0x20
  ? vmx_vcpu_put+0x2e/0x260 [kvm_intel]
  kvm_vcpu_ioctl+0xea/0x6b0 [kvm]
  ? kvm_vcpu_ioctl+0xea/0x6b0 [kvm]
  ? __fget_light+0xd4/0x130
  __x64_sys_ioctl+0xe3/0x910
  ? debug_smp_processor_id+0x17/0x20
  ? fpregs_assert_state_consistent+0x27/0x50
  do_syscall_64+0x3f/0x90
  entry_SYSCALL_64_after_hwframe+0x63/0xcd

Adjust the 'mask' to zero out the userspace buffer for the features that
are not available both from fpstate and from init_fpstate.

The dynamic features depend on the compacted XSAVE format. Ensure it is
enabled before reading XCOMP_BV in init_fpstate.

Fixes: 2308ee57d93d ("x86/fpu/amx: Enable the AMX feature in 64-bit mode")
Reported-by: Yuan Yao <yuan.yao@intel.com>
Suggested-by: Dave Hansen <dave.hansen@intel.com>
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Tested-by: Yuan Yao <yuan.yao@intel.com>
Cc: x86@kernel.org
Cc: linux-kernel@vger.kernel.org
Link: https://lore.kernel.org/lkml/BYAPR11MB3717EDEF2351C958F2C86EED95259@BYAPR11MB3717.namprd11.prod.outlook.com/
---
Change from v1:
* Adjust the 'mask' instead of the loop iteration code (Dave Hansen).

The context of this along with the init_fpstate fix was described in the v1
cover letter:
  https://lore.kernel.org/lkml/20221018221349.4196-1-chang.seok.bae@intel.com/
---
 arch/x86/kernel/fpu/xstate.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index e77cabfa802f..59e543b95a3c 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -1125,6 +1125,15 @@ void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
 	 */
 	mask = fpstate->user_xfeatures;
 
+	/*
+	 * Dynamic features are not present in init_fpstate. When they are
+	 * in an all zeros init state, remove those from 'mask' to zero
+	 * those features in the user buffer instead of retrieving them
+	 * from init_fpstate.
+	 */
+	if (fpu_state_size_dynamic())
+		mask &= (header.xfeatures | xinit->header.xcomp_bv);
+
 	for_each_extended_xfeature(i, mask) {
 		/*
 		 * If there was a feature or alignment gap, zero the space
-- 
2.17.1


  parent reply	other threads:[~2022-10-21 19:09 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-24 19:12 [PATCH 0/3] x86/fpu: Improve the init_fpstate setup code Chang S. Bae
2022-08-24 19:12 ` [PATCH 1/3] x86/fpu: Configure init_fpstate attributes orderly Chang S. Bae
2022-10-03 21:18   ` Neel Natu
2022-10-17 13:47   ` [tip: x86/urgent] " tip-bot2 for Chang S. Bae
2022-08-24 19:12 ` [PATCH 2/3] x86/fpu: Fix the init_fpstate size check with the actual size Chang S. Bae
2022-10-17 13:47   ` [tip: x86/urgent] " tip-bot2 for Chang S. Bae
2022-08-24 19:12 ` [PATCH 3/3] x86/fpu: Exclude dynamic states from init_fpstate Chang S. Bae
2022-10-17 13:47   ` [tip: x86/urgent] " tip-bot2 for Chang S. Bae
2022-10-18 22:13 ` [PATCH 0/1] x86/fpu: Follow up on the init_fpstate fix Chang S. Bae
2022-10-18 22:13   ` [PATCH 1/1] x86/fpu: Fix copy_xstate_to_uabi() to copy init states correctly Chang S. Bae
2022-10-20 16:57     ` Dave Hansen
2022-10-20 18:52       ` Chang S. Bae
2022-10-21 18:58     ` Chang S. Bae [this message]
2022-10-21 22:25       ` [tip: x86/urgent] " tip-bot2 for Chang S. Bae
2022-10-22  6:23         ` Ingo Molnar
2022-10-22 13:02           ` Chang S. Bae
2023-02-27 21:05   ` [PATCH 0/2] x86/fpu/xstate: Follow up on the init_fpstate fallout again Chang S. Bae
2023-02-27 21:05     ` [PATCH 1/2] x86/fpu/xstate: Prevent false-positive warning in __copy_xstate_uabi_buf() Chang S. Bae
2023-02-27 21:05     ` [PATCH 2/2] selftests/x86/amx: Add a ptrace test Chang S. Bae

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221021185844.13472-1-chang.seok.bae@intel.com \
    --to=chang.seok.bae@intel.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=yuan.yao@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).