KVM: selftests: Fix the bounds of the SEV debug dst buffer check
PATCH 1/1 KVM: selftests: Fix the bounds of the SEV debug dst buffer check
validate_dst() is passed a starting offset and a byte count, but bounds its loop with "i < nr_bytes", comparing the offset against the count rather than against offset + nr_bytes. Only calls that pass offset 0 check the region they were asked to check. Every other call checks too few bytes, and any call whose offset is >= the count returns without checking anything at all.
Of the 1990 validate_dst() calls a full run makes per VM type, 740 (37%) check zero bytes, only the 29 zero-offset calls are correct, and 62% of the intended bytes are checked overall. The unaligned offsets the sweep deliberately picks to exercise partial chunks are precisely the ones that end up checking nothing.
The bug is latent today: validate_buffers() runs first and compares all of src[] against dst[], so a genuine {en,de}crypt corruption is still caught. That is presumably why this went unnoticed.
Bound the loop with offset + nr_bytes, and rename the parameter to offset so it cannot be mistaken for a length again. The caller already guarantees offset + nr_bytes <= BUFFER_SIZE, so the corrected loop stays in bounds.
---
Compile-tested only: this host has no SEV (kvm_amd sev=N, no /dev/sev), so the test SKIPs here rather than running.
The figures above come from replaying the exact offset/size sweep of __test_sev_dbg() and test_sev_dbg() outside the kernel against both the current and the corrected loop. Injecting a single-byte corruption at every position the check is supposed to cover, the current loop detects 62% of them and the corrected loop detects all of them.
tools/testing/selftests/kvm/x86/sev_dbg_test.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/sev_dbg_test.c +4 -2
@@ -14,9 +14,11 @@ static u8 *data;static u8 src[BUFFER_SIZE ]__aligned(PAGE_SIZE) ;static u8 dst[BUFFER_SIZE ]__aligned(PAGE_SIZE) ; -static void validate_dst (int i,int nr_bytes,u8 pattern) +static void validate_dst (int offset,int nr_bytes,u8 pattern) { -for ( ; i < nr_bytes; i++) +int i; + +for (i = offset; i < offset + nr_bytes; i++)TEST_ASSERT (dst[i] == pattern,"Expected 0x%x at byte %u, got 0x%x" , pattern, i, dst[i]);
Signature · 1 line
2.54.0
Gentle ping.
This one is independent of the other two selftests patches I have in flight, so it can be applied or dropped on its own.
Happy to respin if anything needs changing.
Sean Christopherson
maintainerQuoted 4 lines · Gokul K
Gentle ping.
This one is independent of the other two selftests patches I have in flight, so it can be applied or dropped on its own.
This is on my list of things to grab for 7.4. We're in the middle of a merge window so I'm not applying anything at the moment (I start after rc2).
Sean Christopherson
maintainerQuoted 8 lines · Gokul K
validate_dst()is passed a starting offset and a byte count, but bounds its loop with "i <nr_bytes", comparing the offset against the count rather than against offset +nr_bytes. Only calls that pass offset 0 check the region they were asked to check. Every other call checks too few bytes, and any call whose offset is >= the count returns without checking anything at all.[...]
Applied to kvm-x86 selftests, thanks!
[1/1] KVM: selftests: Fix the bounds of the SEV debug dst buffer check
https://github.com/kvm-x86/linux/commit/622a09f6deb5