Skip to content

Commit edde99c

Browse files
mstsirkinmatosatti
authored andcommitted
KVM: Write protect memory after slot swap
I have observed the following bug trigger: 1. userspace calls GET_DIRTY_LOG 2. kvm_mmu_slot_remove_write_access is called and makes a page ro 3. page fault happens and makes the page writeable fault is logged in the bitmap appropriately 4. kvm_vm_ioctl_get_dirty_log swaps slot pointers a lot of time passes 5. guest writes into the page 6. userspace calls GET_DIRTY_LOG At point (5), bitmap is clean and page is writeable, thus, guest modification of memory is not logged and GET_DIRTY_LOG returns an empty bitmap. The rule is that all pages are either dirty in the current bitmap, or write-protected, which is violated here. It seems that just moving kvm_mmu_slot_remove_write_access down to after the slot pointer swap should fix this bug. KVM-Stable-Tag. Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent ff8b16d commit edde99c

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

‎arch/x86/kvm/x86.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,10 +3169,6 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
31693169
struct kvm_memslots *slots, *old_slots;
31703170
unsigned long *dirty_bitmap;
31713171

3172-
spin_lock(&kvm->mmu_lock);
3173-
kvm_mmu_slot_remove_write_access(kvm, log->slot);
3174-
spin_unlock(&kvm->mmu_lock);
3175-
31763172
r = -ENOMEM;
31773173
dirty_bitmap = vmalloc(n);
31783174
if (!dirty_bitmap)
@@ -3194,6 +3190,10 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
31943190
dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
31953191
kfree(old_slots);
31963192

3193+
spin_lock(&kvm->mmu_lock);
3194+
kvm_mmu_slot_remove_write_access(kvm, log->slot);
3195+
spin_unlock(&kvm->mmu_lock);
3196+
31973197
r = -EFAULT;
31983198
if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n)) {
31993199
vfree(dirty_bitmap);

0 commit comments

Comments
 (0)