From: Gokul K <gokul02k@gmail.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>,
kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] KVM: selftests: Fix the bounds of the SEV debug dst buffer check
Date: Sat, 8 Aug 2026 11:49:34 +0530 [thread overview]
Message-ID: <20260808061934.400892-1-gokul02k@gmail.com> (raw)
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.
Fixes: 6edd35e77a42 ("KVM: selftests: Add a test to verify SEV {en,de}crypt debug ioctls")
Signed-off-by: Gokul K <gokul02k@gmail.com>
---
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(-)
diff --git a/tools/testing/selftests/kvm/x86/sev_dbg_test.c b/tools/testing/selftests/kvm/x86/sev_dbg_test.c
index eaa8201b937d..0277e38e3556 100644
--- a/tools/testing/selftests/kvm/x86/sev_dbg_test.c
+++ b/tools/testing/selftests/kvm/x86/sev_dbg_test.c
@@ -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]);
--
2.54.0
reply other threads:[~2026-08-08 6:19 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260808061934.400892-1-gokul02k@gmail.com \
--to=gokul02k@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
/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