Skip to contentSkip to conversation

KVM: VMX: Add array_index_nospec() to evmcs_field_offset()

PATCH Superseded Amirmohammad Eftekhar 2 participants 2 messages Oct 23 – Oct 24

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: VMX: Add array_index_nospec() to evmcs_field_offset()

From: Amirmohammad Eftekhar <[email protected]>

evmcs_field_offset() bounds-checks the guest-supplied field index against nr_evmcs_1_fields and then indexes vmcs_field_to_evmcs_1[] with it, but, unlike its vmcs12 counterpart get_vmcs12_field_offset(), it does not clamp the index with array_index_nospec() after the check. The field index is derived from a guest-supplied VMCS field encoding on the enlightened VMREAD/VMWRITE path, so the array access can be performed speculatively past the bounds check (Spectre v1), disclosing memory adjacent to vmcs_field_to_evmcs_1[].

Clamp the index with array_index_nospec() immediately after the bounds check, mirroring get_vmcs12_field_offset().

---
 arch/x86/kvm/vmx/hyperv_evmcs.h | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/vmx/hyperv_evmcs.h +2 -0
index bc08fe40590e..052b62b8ed8e 100644
@@ -136,6 +136,8 @@ static __always_inline int evmcs_field_offset(unsigned long field,
 	if (unlikely(index >= nr_evmcs_1_fields))
 		return -ENOENT;
 
+	index = array_index_nospec(index, nr_evmcs_1_fields);
+
 	evmcs_field = &vmcs_field_to_evmcs_1[index];
 
 	/*
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · Amirmohammad Eftekhar

From: Amirmohammad Eftekhar <[email protected]>

evmcs_field_offset() bounds-checks the guest-supplied field index against nr_evmcs_1_fields and then indexes vmcs_field_to_evmcs_1[] with it, but, unlike its vmcs12 counterpart get_vmcs12_field_offset(), it does not clamp the index with array_index_nospec() after the check. The field index is derived from a guest-supplied VMCS field encoding on the enlightened VMREAD/VMWRITE path, so the array access can be performed speculatively past the bounds check (Spectre v1), disclosing memory adjacent to vmcs_field_to_evmcs_1[].

Quoted 5 lines

Clamp the index with array_index_nospec() immediately after the bounds check, mirroring get_vmcs12_field_offset().

---

You already posted this exact patch[*], and at a glance several of the bug reports appear to be duplicates as well. Please stop spamming duplicates. If you have questions about the flow and/or timing of patches, e.g. since we're in a merge window, then Documentation/process/maintainer-kvm-x86.rst is your friend.

[*] https://lore.kernel.org/all/[email protected]