Thread (21 messages) 21 messages, 3 authors, 3d ago

Re: [PATCH v3 09/15] KVM: Pre-check uaccesses in KVM's APIs to read/write guest memory

flat view

From: James Houghton <hidden>
Date: 2026-10-28 00:23:38
Also in: kvm, lkml

On Tue, Oct 27, 2026 at 3:12 PM Sean Christopherson [off-list ref] wrote:
Add a check on whether or not doing a user access is ok in KVM's APIs for
accessing guest memory, *before* doing memslot lookup/validation, so that
buggy KVM accesses to guest memory more likely to fail noisily.  Because
"to guest memory [are] more likely"
KVM installs dummy/empty memslots early in VM destruction, bugs in the VM
teardown path will fail on the memslot checks without getting to the WARNs
buried in kvm_copy_{to,from}_user() and friends.

Signed-off-by: Sean Christopherson <seanjc@google.com>
One somewhat minor comment below. Feel free to add:

Reviewed-by: James Houghton <redacted>
quoted hunk ↗ jump to hunk
---
 virt/kvm/kvm_main.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 1058b864d427..d8b914b76f90 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3298,6 +3298,11 @@ static int next_segment(unsigned long len, int offset)
                return len;
 }

+static bool kvm_is_guest_access_ok(struct kvm *kvm, int offset, int len, int size)
Hrm... I think you at least want `len` and `size` to be `unsigned
long`, and you probably want `offset` to be `unsigned int` or
`unsigned long`. The *_cached() functions are the interesting ones in
this case; I've left a note below at the first callsite.
quoted hunk ↗ jump to hunk
+{
+       return kvm_can_do_uaccess(kvm) && !WARN_ON_ONCE(offset + len > size);
+}
+
 /* Copy @len bytes from guest memory at '(@gfn * PAGE_SIZE) + @offset' to @data */
 static int __kvm_read_guest_page(struct kvm *kvm, struct kvm_memory_slot *slot,
                                 gfn_t gfn, void *data, int offset, int len)
@@ -3305,7 +3310,7 @@ static int __kvm_read_guest_page(struct kvm *kvm, struct kvm_memory_slot *slot,
        int r;
        unsigned long addr;

-       if (WARN_ON_ONCE(offset + len > PAGE_SIZE))
+       if (!kvm_is_guest_access_ok(kvm, offset, len, PAGE_SIZE))
                return -EFAULT;

        addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
@@ -3381,7 +3386,7 @@ static int __kvm_read_guest_atomic(struct kvm *kvm, struct kvm_memory_slot *slot
        int r;
        unsigned long addr;

-       if (WARN_ON_ONCE(offset + len > PAGE_SIZE))
+       if (!kvm_is_guest_access_ok(kvm, offset, len, PAGE_SIZE))
                return -EFAULT;

        addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
@@ -3414,7 +3419,7 @@ static int __kvm_write_guest_page(struct kvm *kvm,
        int r;
        unsigned long addr;

-       if (WARN_ON_ONCE(offset + len > PAGE_SIZE))
+       if (!kvm_is_guest_access_ok(kvm, offset, len, PAGE_SIZE))
                return -EFAULT;

        addr = gfn_to_hva_memslot(memslot, gfn);
@@ -3544,7 +3549,7 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
        int r;
        gpa_t gpa = ghc->gpa + offset;

-       if (WARN_ON_ONCE(len + offset > ghc->len))
+       if (!kvm_is_guest_access_ok(kvm, offset, len, ghc->len))
Currently, this truncates `len` and `ghc->len` from `unsigned long` to
`int`, and it will change `offset` from `unsigned int` to `int`.

quoted hunk ↗ jump to hunk
                return -EINVAL;

        if (slots->generation != ghc->generation) {
@@ -3582,7 +3587,7 @@ int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
        int r;
        gpa_t gpa = ghc->gpa + offset;

-       if (WARN_ON_ONCE(len + offset > ghc->len))
+       if (!kvm_is_guest_access_ok(kvm, offset, len, ghc->len))
                return -EINVAL;

        if (slots->generation != ghc->generation) {
--
2.56.0.307.g5fe7bd680c-goog
  
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help