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
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 <[email protected]>
---
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)
+{
+ 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))
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