Skip to content

Commit f0736cf

Browse files
Alex Williamsonmatosatti
authored andcommitted
KVM: Restrict non-existing slot state transitions
The API documentation states: When changing an existing slot, it may be moved in the guest physical memory space, or its flags may be modified. An "existing slot" requires a non-zero npages (memory_size). The only transition we should therefore allow for a non-existing slot should be to create the slot, which includes setting a non-zero memory_size. We currently allow calls to modify non-existing slots, which is pointless, confusing, and possibly wrong. With this we know that the invalidation path of __kvm_set_memory_region is always for a delete or move and never for adding a zero size slot. Reviewed-by: Gleb Natapov <gleb@redhat.com> Signed-off-by: Alex Williamson <alex.williamson@redhat.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
1 parent f3200d0 commit f0736cf

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎virt/kvm/kvm_main.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,10 +758,15 @@ int __kvm_set_memory_region(struct kvm *kvm,
758758
new.npages = npages;
759759
new.flags = mem->flags;
760760

761-
/* Disallow changing a memory slot's size. */
761+
/*
762+
* Disallow changing a memory slot's size or changing anything about
763+
* zero sized slots that doesn't involve making them non-zero.
764+
*/
762765
r = -EINVAL;
763766
if (npages && old.npages && npages != old.npages)
764767
goto out_free;
768+
if (!npages && !old.npages)
769+
goto out_free;
765770

766771
/* Check for overlaps */
767772
r = -EEXIST;
@@ -780,7 +785,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
780785
r = -ENOMEM;
781786

782787
/* Allocate if a slot is being created */
783-
if (npages && !old.npages) {
788+
if (!old.npages) {
784789
new.user_alloc = user_alloc;
785790
new.userspace_addr = mem->userspace_addr;
786791

0 commit comments

Comments
 (0)