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 are 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.Reviewed-by: James Houghton <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> --- virt/kvm/kvm_main.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 2eda12a52bf4..c6af11cea296 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3300,6 +3300,12 @@ static int next_segment(unsigned long len, int offset) return len; } +static bool kvm_is_guest_access_ok(struct kvm *kvm, unsigned long offset, + unsigned long len, unsigned long 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) @@ -3307,7 +3313,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); @@ -3383,7 +3389,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); @@ -3416,7 +3422,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); @@ -3546,7 +3552,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) { @@ -3584,7 +3590,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.385.gac671c819a-goog
