Skip to content

Commit 025dde5

Browse files
committed
KVM: Harden guest memory APIs against out-of-bounds accesses
When reading or writing a guest page, WARN and bail if offset+len would result in a read to a different page so that KVM bugs are more likely to be detected, and so that any such bugs are less likely to escalate to an out-of-bounds access. E.g. if userspace isn't using guard pages and the target page is at the end of a memslot. Note, KVM already hardens itself in similar APIs, e.g. in the "cached" variants, it's just the vanilla APIs that are playing with fire. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent ec495f2 commit 025dde5

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

virt/kvm/kvm_main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,6 +3275,9 @@ static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn,
32753275
int r;
32763276
unsigned long addr;
32773277

3278+
if (WARN_ON_ONCE(offset + len > PAGE_SIZE))
3279+
return -EFAULT;
3280+
32783281
addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
32793282
if (kvm_is_error_hva(addr))
32803283
return -EFAULT;
@@ -3348,6 +3351,9 @@ static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
33483351
int r;
33493352
unsigned long addr;
33503353

3354+
if (WARN_ON_ONCE(offset + len > PAGE_SIZE))
3355+
return -EFAULT;
3356+
33513357
addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
33523358
if (kvm_is_error_hva(addr))
33533359
return -EFAULT;
@@ -3378,6 +3384,9 @@ static int __kvm_write_guest_page(struct kvm *kvm,
33783384
int r;
33793385
unsigned long addr;
33803386

3387+
if (WARN_ON_ONCE(offset + len > PAGE_SIZE))
3388+
return -EFAULT;
3389+
33813390
addr = gfn_to_hva_memslot(memslot, gfn);
33823391
if (kvm_is_error_hva(addr))
33833392
return -EFAULT;

0 commit comments

Comments
 (0)