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 --- 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 770a2c3bd445..80b19d47840b 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3211,6 +3211,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 > PAGE_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) @@ -3218,7 +3223,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); @@ -3294,7 +3299,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); @@ -3327,7 +3332,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); @@ -3457,7 +3462,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) { @@ -3495,7 +3500,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.rc1.315.gc6ed9934b7-goog