Skip to contentSkip to conversation

KVM: selftests: Compare the entire XSAVE buffer in the SEV smoke test

PATCH In review Gokul K 2 participants 2 messages Aug 7 – Aug 10

Subsystems (3)
lore
Share & tools
lore

Gokul K

author
#0 lore raw

PATCH 1/1 KVM: selftests: Compare the entire XSAVE buffer in the SEV smoke test

compare_xsave() stops at index 4094, so the last byte of the XSAVE state is never compared. Both buffers handed to it are 4096 bytes: the host copy is a struct kvm_xsave, whose region[] member is 1024 u32s, and the guest copy is the PAGE_SIZE shared page that guest_code_xsave() runs XSAVE into. A VMSA synchronization bug that corrupted only that final byte would go unreported and the test would still pass.

Bound the loop with sizeof(struct kvm_xsave) rather than an open-coded length, and make the index unsigned so it does not mix signedness with sizeof.

Fixes 8c53183dbaa2 ("selftests: kvm: add test for transferring FPU state into VMSA")
---
 tools/testing/selftests/kvm/x86/sev_smoke_test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/sev_smoke_test.c +3 -2
index 6b2cbe2a90b7..c7af27954356 100644
@@ -90,9 +90,10 @@ asm("guest_code_xsave:\n"
 
 static void compare_xsave(u8 *from_host, u8 *from_guest)
 {
-	int i;
+	unsigned int i;
 	bool bad = false;
-	for (i = 0; i < 4095; i++) {
+
+	for (i = 0; i < sizeof(struct kvm_xsave); i++) {
 		if (from_host[i] != from_guest[i]) {
 			printf("mismatch at %u | %02hhx %02hhx\n",
 			       i, from_host[i], from_guest[i]);
Signature · 1 line
2.54.0
#1 lore raw
Quoted 22 lines · Gokul K

compare_xsave() stops at index 4094, so the last byte of the XSAVE state is never compared. Both buffers handed to it are 4096 bytes: the host copy is a struct kvm_xsave, whose region[] member is 1024 u32s, and the guest copy is the PAGE_SIZE shared page that guest_code_xsave() runs XSAVE into. A VMSA synchronization bug that corrupted only that final byte would go unreported and the test would still pass.

Bound the loop with sizeof(struct kvm_xsave) rather than an open-coded length, and make the index unsigned so it does not mix signedness with sizeof.

Fixes 8c53183dbaa2 ("selftests: kvm: add test for transferring FPU state into VMSA")
---
 tools/testing/selftests/kvm/x86/sev_smoke_test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/sev_smoke_test.c +0 -0
index 6b2cbe2a90b7..c7af27954356 100644

tools/testing/selftests/kvm/x86/sev_smoke_test.c asm("guest_code_xsave:\n" · line 95

Quoted 7 lines
tools/testing/selftests/kvm/x86/sev_smoke_test.c +1 -2
@@ -90,9 +90,10 @@ asm("guest_code_xsave:\n"
 
 static void compare_xsave(u8 *from_host, u8 *from_guest)
 {
-	int i;
+	unsigned int i;
 	bool bad = false;
-	for (i = 0; i < 4095; i++) {

The fix looks right, but this is so bizarrely wrong that I can't help but wonder if it was somehow intentional.

Paolo?

Quoted 9 lines
tools/testing/selftests/kvm/x86/sev_smoke_test.c +2 -0
+
+	for (i = 0; i < sizeof(struct kvm_xsave); i++) {
 		if (from_host[i] != from_guest[i]) {
 			printf("mismatch at %u | %02hhx %02hhx\n",
 			       i, from_host[i], from_guest[i]);
Signature · 1 line
2.54.0