Skip to contentSkip to conversation

KVM: x86: Fix missing EFER validity checks

PATCH Applied Yosry Ahmed 4 participants 13 messages Jul 13 – Jul 31

Subsystems (5)
Stable branch x86 KVM Kselftest +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover KVM: x86: Fix missing EFER validity checks

v3 of the EFER missing validity checks on KVM_SET_SREGS* fix, split out a larger series of EFER fixes.

v2 -> v3:
- Split out KVM_SET_SREG* fix and selftest.

v2: https://lore.kernel.org/kvm/[email protected]/

Yosry Ahmed (2):
KVM: x86: Check EFER validity on KVM_SET_SREGS*
KVM: selftests: Extend set_sregs test to cover EFER

 arch/x86/kvm/regs.c                           |  3 +-
 .../selftests/kvm/include/x86/processor.h     |  2 +
 .../selftests/kvm/x86/set_sregs_test.c        | 83 ++++++++++++++-----
 3 files changed, 66 insertions(+), 22 deletions(-)

base-commit: 3aec122bdcaf6f8c4ecfc7f6adf6773015ced676

Signature · 1 line
2.55.0.141.g00534a21ce-goog
#1 lore raw

PATCH v3 1/2 KVM: x86: Check EFER validity on KVM_SET_SREGS*

When handling userspace SREGS writes, check the validity of EFER (i.e. allowed bits) before writing the new value of EFER through the per-vendor set_efer callbacks. This prevents userspace from writing bogus values (e.g. EFER.SVME=1 with nested=0).

Note: on KVM_SET_MSRS, KVM only checks EFER validity in terms of KVM caps, not guest caps, so it is possible to set EFER bits that are supported by KVM but not by the guest CPUID. Potentially allowing userspace to set msrs before CPUID.

However, for KVM_SET_SREGS*, check the validity of the set bits against both KVM and guest caps. This is consistent with other validity checks (e.g. for CR4) that check validity against guest caps, which already imposes the need to set CPUID before SREGS.

Change-Id I45701ec440e4fdd8f086eb70db0c0845fb0ed509
---
 arch/x86/kvm/regs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/regs.c +2 -1
index bd8147798cc3e..8f66438989e47 100644
@@ -564,7 +564,8 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
 	}
 
 	return kvm_is_valid_cr4(vcpu, sregs->cr4) &&
-	       kvm_is_valid_cr0(vcpu, sregs->cr0);
+	       kvm_is_valid_cr0(vcpu, sregs->cr0) &&
+	       kvm_valid_efer(vcpu, sregs->efer);
 }
 
 static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,
Signature · 1 line
2.55.0.141.g00534a21ce-goog
#11 lore raw

PATCH v3 2/2 KVM: selftests: Extend set_sregs test to cover EFER

In reply to Yosry Ahmed

Extend the set_sregs test to cover various bits in EFER. Update TEST_INVALID_CR_BIT() to operate on EFER as well as CRx (and rename it accordingly). Add test cases to check that EFER bits are disallowed without the relevant CPUID enablement.

Assisted-by Gemini:unknown-version
Change-Id I588e7259c6257f40640127a52ce808a0f5afa016
---
 .../selftests/kvm/include/x86/processor.h     |  2 +
 .../selftests/kvm/x86/set_sregs_test.c        | 83 ++++++++++++++-----
 2 files changed, 64 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +2 -0
index 7d3a27bc0d842..b161174ece453 100644
@@ -208,6 +208,7 @@ struct kvm_x86_cpu_feature {
 #define	X86_FEATURE_PERFCTR_NB		KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 24)
 #define	X86_FEATURE_PERFCTR_LLC		KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 28)
 #define	X86_FEATURE_NX			KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 20)
+#define	X86_FEATURE_FXSR_OPT		KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 25)
 #define	X86_FEATURE_GBPAGES		KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 26)
 #define	X86_FEATURE_RDTSCP		KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 27)
 #define	X86_FEATURE_LM			KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 29)
@@ -226,6 +227,7 @@ struct kvm_x86_cpu_feature {
 #define X86_FEATURE_SEV			KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1)
 #define X86_FEATURE_SEV_ES		KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3)
 #define X86_FEATURE_SEV_SNP		KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 4)
+#define	X86_FEATURE_AUTOIBRS		KVM_X86_CPU_FEATURE(0x80000021, 0, EAX, 8)
 #define	X86_FEATURE_GP_ON_USER_CPUID	KVM_X86_CPU_FEATURE(0x80000021, 0, EAX, 17)
 #define	X86_FEATURE_PERFMON_V2		KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 0)
 #define	X86_FEATURE_LBR_PMC_FREEZE	KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 2)
tools/testing/selftests/kvm/x86/set_sregs_test.c +62 -21

132 lines not on this page · raw message

Signature · 1 line
2.55.0.141.g00534a21ce-goog
#2 lore raw

Re: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

In reply to Yosry Ahmed
Quoted 11 lines · Yosry Ahmed

When handling userspace SREGS writes, check the validity of EFER (i.e. allowed bits) before writing the new value of EFER through the per-vendor set_efer callbacks. This prevents userspace from writing bogus values (e.g. EFER.SVME=1 with nested=0).

Note: on KVM_SET_MSRS, KVM only checks EFER validity in terms of KVM caps, not guest caps, so it is possible to set EFER bits that are supported by KVM but not by the guest CPUID. Potentially allowing userspace to set msrs before CPUID.

Quoted 7 lines

However, for KVM_SET_SREGS*, check the validity of the set bits against both KVM and guest caps. This is consistent with other validity checks (e.g. for CR4) that check validity against guest caps, which already imposes the need to set CPUID before SREGS.

Cc: [email protected]
Change-Id: I45701ec440e4fdd8f086eb70db0c0845fb0ed509

Forgot to drop these, sorry :/

#5 lore raw

Re: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

In reply to Yosry Ahmed
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the return value of kvm_x86_call(set_efer) is ignored in __set_sregs_common(), leading to a silent failure when nested state allocation fails. --

--- Patch [1]: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS* --- commit 6edd9e84461af3c58ac965b84f89b7c4985ddeb3
Author: Yosry Ahmed <[email protected]>

KVM: x86: Check EFER validity on KVM_SET_SREGS*

This commit adds checks to validate EFER against both KVM and guest caps during KVM_SET_SREGS* writes, preventing userspace from writing bogus values through the per-vendor set_efer callbacks.

Quoted 4 lines
arch/x86/kvm/regs.c +0 -0
index bd8147798cc3e..8f66438989e47 100644

[ ... ]

Quoted 12 lines
arch/x86/kvm/regs.c +2 -1
@@ -564,7 +564,8 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
 	}
 
 	return kvm_is_valid_cr4(vcpu, sregs->cr4) &&
-	       kvm_is_valid_cr0(vcpu, sregs->cr0);
+	       kvm_is_valid_cr0(vcpu, sregs->cr0) &&
+	       kvm_valid_efer(vcpu, sregs->efer);
 }
 
 static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,

int *mmu_reset_needed, bool update_pdptrs)
{

[ ... ]

arch/x86/kvm/regs.c

Quoted 6 lines
arch/x86/kvm/regs.c +0 -0
 	vcpu->arch.cr3 = sregs->cr3;
 	kvm_register_mark_dirty(vcpu, VCPU_REG_CR3);
 	kvm_x86_call(post_set_cr3)(vcpu, sregs->cr3);
 
 	*mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
 	kvm_x86_call(set_efer)(vcpu, sregs->efer);

[Severity: High]
This isn't a bug introduced by this patch, but should the return value of kvm_x86_call() be checked here?

If a userspace process calls the KVM_SET_SREGS ioctl with EFER.SVME set, kvm_x86_call(set_efer) could call into svm_set_efer(), which attempts to allocate nested state via svm_allocate_nested().

If the system is under memory pressure, this allocation can fail and return -ENOMEM. Because the return value is ignored in __set_sregs_common(), doesn't this lead to a silent failure where KVM fails to set the guest's EFER register but still returns success to userspace?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#6 lore raw

Re: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

Quoted 50 lines · On Mon, Jul 13, 2026 at 11:14 AM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the return value of kvm_x86_call(set_efer) is ignored in __set_sregs_common(), leading to a silent failure when nested state allocation fails. --

--- Patch [1]: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS* --- commit 6edd9e84461af3c58ac965b84f89b7c4985ddeb3
Author: Yosry Ahmed <[email protected]>

KVM: x86: Check EFER validity on KVM_SET_SREGS*

This commit adds checks to validate EFER against both KVM and guest caps during KVM_SET_SREGS* writes, preventing userspace from writing bogus values through the per-vendor set_efer callbacks.

Quoted 4 lines
arch/x86/kvm/regs.c +0 -0
index bd8147798cc3e..8f66438989e47 100644

[ ... ]

Quoted 12 lines
arch/x86/kvm/regs.c +2 -1
@@ -564,7 +564,8 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
      }
 
      return kvm_is_valid_cr4(vcpu, sregs->cr4) &&
-            kvm_is_valid_cr0(vcpu, sregs->cr0);
+            kvm_is_valid_cr0(vcpu, sregs->cr0) &&
+            kvm_valid_efer(vcpu, sregs->efer);
 }
 
 static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,

int *mmu_reset_needed, bool update_pdptrs)
{

[ ... ]

Quoted 6 lines
      vcpu->arch.cr3 = sregs->cr3;
      kvm_register_mark_dirty(vcpu, VCPU_REG_CR3);
      kvm_x86_call(post_set_cr3)(vcpu, sregs->cr3);

      *mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
      kvm_x86_call(set_efer)(vcpu, sregs->efer);

[Severity: High]
This isn't a bug introduced by this patch, but should the return value of kvm_x86_call() be checked here?

Quoted 8 lines

If a userspace process calls the KVM_SET_SREGS ioctl with EFER.SVME set, kvm_x86_call(set_efer) could call into svm_set_efer(), which attempts to allocate nested state via svm_allocate_nested().

If the system is under memory pressure, this allocation can fail and return -ENOMEM. Because the return value is ignored in __set_sregs_common(), doesn't this lead to a silent failure where KVM fails to set the guest's EFER register but still returns success to userspace?

This seems like a real issue, and I think it's not very easy to fix because it seems like KVM_SET_SREGS is currently atomic, we either do all the updates or we fail. So we can't just fail in the middle of __set_sregs_common() if set_efer() fails, we have to either undo the previous changes or re-order the writes to do EFER first. Re-ordering seems like it should be fine, I don't immediately see svm_set_efer() or vmx_set_efer() depending on any of the previous updates, but I didn't look too closely.

Anyway, this is completely orthogonal to this patch AFAICT.

#3 lore raw

Re: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

In reply to Yosry Ahmed
Quoted 17 lines · Yosry Ahmed
Quoted 16 lines · Yosry Ahmed

When handling userspace SREGS writes, check the validity of EFER (i.e. allowed bits) before writing the new value of EFER through the per-vendor set_efer callbacks. This prevents userspace from writing bogus values (e.g. EFER.SVME=1 with nested=0).

Note: on KVM_SET_MSRS, KVM only checks EFER validity in terms of KVM caps, not guest caps, so it is possible to set EFER bits that are supported by KVM but not by the guest CPUID. Potentially allowing userspace to set msrs before CPUID.

However, for KVM_SET_SREGS*, check the validity of the set bits against both KVM and guest caps. This is consistent with other validity checks (e.g. for CR4) that check validity against guest caps, which already imposes the need to set CPUID before SREGS.

Quoted 4 lines
Quoted 2 lines

Cc: [email protected]
Change-Id: I45701ec440e4fdd8f086eb70db0c0845fb0ed509

Forgot to drop these, sorry :/

By "these", I assume you mean the Change-Id here and in the next patch? But keep the Cc: stable@?

#4 lore raw

Re: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

Quoted 19 lines · Sean Christopherson
Quoted 17 lines · Yosry Ahmed
Quoted 16 lines · Yosry Ahmed

When handling userspace SREGS writes, check the validity of EFER (i.e. allowed bits) before writing the new value of EFER through the per-vendor set_efer callbacks. This prevents userspace from writing bogus values (e.g. EFER.SVME=1 with nested=0).

Note: on KVM_SET_MSRS, KVM only checks EFER validity in terms of KVM caps, not guest caps, so it is possible to set EFER bits that are supported by KVM but not by the guest CPUID. Potentially allowing userspace to set msrs before CPUID.

However, for KVM_SET_SREGS*, check the validity of the set bits against both KVM and guest caps. This is consistent with other validity checks (e.g. for CR4) that check validity against guest caps, which already imposes the need to set CPUID before SREGS.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Cc: [email protected]
Change-Id: I45701ec440e4fdd8f086eb70db0c0845fb0ed509

Forgot to drop these, sorry :/

By "these", I assume you mean the Change-Id here and in the next patch? But keep the Cc: stable@?

Yes, just the Change-Id.

#7 lore raw

Re: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

In reply to Yosry Ahmed
Quoted 11 lines · Yosry Ahmed

When handling userspace SREGS writes, check the validity of EFER (i.e. allowed bits) before writing the new value of EFER through the per-vendor set_efer callbacks. This prevents userspace from writing bogus values (e.g. EFER.SVME=1 with nested=0).

Note: on KVM_SET_MSRS, KVM only checks EFER validity in terms of KVM caps, not guest caps, so it is possible to set EFER bits that are supported by KVM but not by the guest CPUID. Potentially allowing userspace to set msrs before CPUID.

Quoted 4 lines

However, for KVM_SET_SREGS*, check the validity of the set bits against both KVM and guest caps. This is consistent with other validity checks (e.g. for CR4) that check validity against guest caps, which already imposes the need to set CPUID before SREGS.

Where is the requirement to set CPUID before SREGS documented, aside from this commit message?

#8 lore raw

Re: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

Quoted 13 lines · Jim Mattson
Quoted 11 lines · Yosry Ahmed

When handling userspace SREGS writes, check the validity of EFER (i.e. allowed bits) before writing the new value of EFER through the per-vendor set_efer callbacks. This prevents userspace from writing bogus values (e.g. EFER.SVME=1 with nested=0).

Note: on KVM_SET_MSRS, KVM only checks EFER validity in terms of KVM caps, not guest caps, so it is possible to set EFER bits that are supported by KVM but not by the guest CPUID. Potentially allowing userspace to set msrs before CPUID.

Quoted 7 lines
Quoted 4 lines

However, for KVM_SET_SREGS*, check the validity of the set bits against both KVM and guest caps. This is consistent with other validity checks (e.g. for CR4) that check validity against guest caps, which already imposes the need to set CPUID before SREGS.

Where is the requirement to set CPUID before SREGS documented, aside from this commit message?

I don't think so, and also coming back to this again I think the commit message is wrong. The whole basis for doing validity checks against guest CPUID (other than the convenience of using kvm_valid_efer()) is cr4_guest_rsvd_bits, which is initialized based on both KVM caps and guest CPUID.

However, cr4_guest_rsvd_bits seems to be initialized *after* CPUID is set, so it only checks CR4 against CPUID if userspace already set CPUID. It doesn't impose a restriction to set CPUID before SREGS, but this patch is.

So I think this may be too restrictive. We should probably only check against KVM caps, which was the whole motivation of this patch to begin with (disallowing EFER.SVME if nested=0). Maybe we should just drop the Cc:stable as it won't apply to any of the stable trees any way, and do this on top of the kvm_caps.supported_efer_bits changes?

arch/x86/kvm/regs.c +0 -0
index 8f66438989e47..412a37ca86ecc 100644
@@ -565,7 +565,7 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu

*vcpu, struct kvm_sregs *sregs)

        return kvm_is_valid_cr4(vcpu, sregs->cr4) &&
               kvm_is_valid_cr0(vcpu, sregs->cr0) &&

- kvm_valid_efer(vcpu, sregs->efer);
+ !(sregs->efer & ~kvm_caps.supported_efer_bits);
}

static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,

---

(quoted diff) +0 -0

b/tools/testing/selftests/kvm/x86/set_sregs_test.c index 603226ffe4374..c07ccf0bbe85a 100644

tools/testing/selftests/kvm/x86/set_sregs_test.c +0 -1
@@ -171,7 +171,6 @@ int main(int argc, char *argv[])
         */
        vm = vm_create_barebones();
        vcpu = __vm_vcpu_add(vm, 0);
-       test_efer_bits(vcpu, KVM_ALWAYS_ALLOWED_EFER);
        test_cr_bits(vcpu, KVM_ALWAYS_ALLOWED_CR4);
        kvm_vm_free(vm);
 

---

Sean, WDYT?

If we will change this, would you rather do it in place, a followup fix, or a new version?

#9 lore raw

Re: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

Quoted 72 lines · Yosry Ahmed
Quoted 13 lines · Jim Mattson
Quoted 11 lines · Yosry Ahmed

When handling userspace SREGS writes, check the validity of EFER (i.e. allowed bits) before writing the new value of EFER through the per-vendor set_efer callbacks. This prevents userspace from writing bogus values (e.g. EFER.SVME=1 with nested=0).

Note: on KVM_SET_MSRS, KVM only checks EFER validity in terms of KVM caps, not guest caps, so it is possible to set EFER bits that are supported by KVM but not by the guest CPUID. Potentially allowing userspace to set msrs before CPUID.

Quoted 7 lines
Quoted 4 lines

However, for KVM_SET_SREGS*, check the validity of the set bits against both KVM and guest caps. This is consistent with other validity checks (e.g. for CR4) that check validity against guest caps, which already imposes the need to set CPUID before SREGS.

Where is the requirement to set CPUID before SREGS documented, aside from this commit message?

I don't think so, and also coming back to this again I think the commit message is wrong. The whole basis for doing validity checks against guest CPUID (other than the convenience of using kvm_valid_efer()) is cr4_guest_rsvd_bits, which is initialized based on both KVM caps and guest CPUID.

However, cr4_guest_rsvd_bits seems to be initialized *after* CPUID is set, so it only checks CR4 against CPUID if userspace already set CPUID. It doesn't impose a restriction to set CPUID before SREGS, but this patch is.

So I think this may be too restrictive. We should probably only check against KVM caps, which was the whole motivation of this patch to begin with (disallowing EFER.SVME if nested=0). Maybe we should just drop the Cc:stable as it won't apply to any of the stable trees any way, and do this on top of the kvm_caps.supported_efer_bits changes?

arch/x86/kvm/regs.c +1 -1
index 8f66438989e47..412a37ca86ecc 100644
@@ -565,7 +565,7 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu
 *vcpu, struct kvm_sregs *sregs)
 
        return kvm_is_valid_cr4(vcpu, sregs->cr4) &&
               kvm_is_valid_cr0(vcpu, sregs->cr0) &&
-              kvm_valid_efer(vcpu, sregs->efer);
+              !(sregs->efer & ~kvm_caps.supported_efer_bits);
 }
 

static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,

---

tools/testing/selftests/kvm/x86/set_sregs_test.c +0 -2
index 603226ffe4374..c07ccf0bbe85a 100644
@@ -171,7 +171,6 @@ int main(int argc, char *argv[])
         */
        vm = vm_create_barebones();
        vcpu = __vm_vcpu_add(vm, 0);
-       test_efer_bits(vcpu, KVM_ALWAYS_ALLOWED_EFER);
        test_cr_bits(vcpu, KVM_ALWAYS_ALLOWED_CR4);
        kvm_vm_free(vm);
 
---
Quoted 4 lines

Sean, WDYT?

If we will change this, would you rather do it in place, a followup fix, or a new version?

I take this back, I think I was right the first time. kvm_vcpu_after_set_cpuid() is called on vCPU creation (confusing?), and looking closely at set_sregs_test seems like it specifically verifies that CR4 bits guarded by CPUID bits cannot be set before CPUID is set.

I think the main difference here is probably that CR4 bits that are guarded by CPUID are more "advanced" than EFER bits?

#10 lore raw

Re: [PATCH v3 1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

Quoted 16 lines · Yosry Ahmed
Quoted 15 lines · Yosry Ahmed
Quoted 13 lines · Jim Mattson
Quoted 11 lines · Yosry Ahmed

When handling userspace SREGS writes, check the validity of EFER (i.e. allowed bits) before writing the new value of EFER through the per-vendor set_efer callbacks. This prevents userspace from writing bogus values (e.g. EFER.SVME=1 with nested=0).

Note: on KVM_SET_MSRS, KVM only checks EFER validity in terms of KVM caps, not guest caps, so it is possible to set EFER bits that are supported by KVM but not by the guest CPUID. Potentially allowing userspace to set msrs before CPUID.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

However, for KVM_SET_SREGS*, check the validity of the set bits against both KVM and guest caps. This is consistent with other validity checks (e.g. for CR4) that check validity against guest caps, which already imposes the need to set CPUID before SREGS.

Where is the requirement to set CPUID before SREGS documented, aside from this commit message?

It's not, because it's not a true requirement. And for me, this isn't about whether or not KVM has a documented rule, it's about how likely it is that this change will break userspace. And for that, Yosry's statement is perfect: the risk of breaking userspace is tiny, because unless userspace is getting creative, it already needs to set CPUID before loading SREGS.

Quoted 16 lines
Quoted 16 lines

I don't think so, and also coming back to this again I think the commit message is wrong. The whole basis for doing validity checks against guest CPUID (other than the convenience of using kvm_valid_efer()) is cr4_guest_rsvd_bits, which is initialized based on both KVM caps and guest CPUID.

However, cr4_guest_rsvd_bits seems to be initialized *after* CPUID is set, so it only checks CR4 against CPUID if userspace already set CPUID. It doesn't impose a restriction to set CPUID before SREGS, but this patch is.

So I think this may be too restrictive. We should probably only check against KVM caps, which was the whole motivation of this patch to begin with (disallowing EFER.SVME if nested=0). Maybe we should just drop the Cc:stable as it won't apply to any of the stable trees any way, and do this on top of the kvm_caps.supported_efer_bits changes?

...

Quoted 2 lines

I take this back, I think I was right the first time. kvm_vcpu_after_set_cpuid() is called on vCPU creation (confusing?),

It's confusing/odd until you realize that zeroing CPUID is also "setting" CPUID.

Quoted 2 lines

and looking closely at set_sregs_test seems like it specifically verifies that CR4 bits guarded by CPUID bits cannot be set before CPUID is set.

No, that isn't the goal. There are two goals:

1. Verify userspace can't set CR4 bits that aren't supported according to the virtual CPU model.

2. Verify KVM doesn't try to "help" userspace by populating CPUID with non-zero values, e.g. so that we don't end up with a CPUID version of KVM_X86_QUIRK_STUFF_FEATURE_MSRS.

Combined, they effectively create the "rule" that userspace must set CPUID before setting certain CR4 bits, but that itself is not what the test is trying to validate.

So I 100% agree KVM's documentation is lacking, but what's lacking is a call out that KVM disallows stuffing guest state that would violate the virtual CPU model. I don't want to document a specific ordering of ioctls because then KVM would have to enforce the ordering, e.g. would have to carry code to specifically reject setting SREGS before CPUID, which would be a waste of code.

Quoted 2 lines

I think the main difference here is probably that CR4 bits that are guarded by CPUID are more "advanced" than EFER bits?

Nah, the only "difference" is that it took us longer to notice that KVM wasn't validating EFER. Blame through KVM's history and you'll find the same bugs for at least CR4.

maintainer Applied
#12 lore raw
In reply to Yosry Ahmed
Quoted 9 lines · Yosry Ahmed

v3 of the EFER missing validity checks on KVM_SET_SREGS* fix, split out a larger series of EFER fixes.

v2 -> v3:
- Split out KVM_SET_SREG* fix and selftest.

v2: https://lore.kernel.org/kvm/[email protected]/

[...]

Applied to kvm-x86 misc, thanks!

[1/2] KVM: x86: Check EFER validity on KVM_SET_SREGS*

      https://github.com/kvm-x86/linux/commit/184bd464bdb6
[2/2] KVM: selftests: Extend set_sregs test to cover EFER
      https://github.com/kvm-x86/linux/commit/e43a21553ac8

--
https://github.com/kvm-x86/linux/tree/next