]> bbs.cooldavid.org Git - net-next-2.6.git/commitdiff
KVM: Disallow fork() and similar games when using a VM
authorAvi Kivity <avi@qumranet.com>
Wed, 21 Nov 2007 14:41:05 +0000 (16:41 +0200)
committerAvi Kivity <avi@qumranet.com>
Wed, 30 Jan 2008 15:53:13 +0000 (17:53 +0200)
We don't want the meaning of guest userspace changing under our feet.

Signed-off-by: Avi Kivity <avi@qumranet.com>
drivers/kvm/kvm.h
drivers/kvm/kvm_main.c
kernel/fork.c

index 1fd8158ced894e65ea9e863aff391f83971d7d83..be18620bd65632a46435b67a6131def6c911dd9f 100644 (file)
@@ -305,6 +305,7 @@ struct kvm_vm_stat {
 
 struct kvm {
        struct mutex lock; /* protects everything except vcpus */
+       struct mm_struct *mm; /* userspace tied to this vm */
        int naliases;
        struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
        int nmemslots;
index aec6b67cfebb4e97ff3e0debab928df64d7f3682..0efd759e585fe86aeb6d0310cb4a2bf9560ca9be 100644 (file)
@@ -165,6 +165,8 @@ static struct kvm *kvm_create_vm(void)
        if (IS_ERR(kvm))
                goto out;
 
+       kvm->mm = current->mm;
+       atomic_inc(&kvm->mm->mm_count);
        kvm_io_bus_init(&kvm->pio_bus);
        mutex_init(&kvm->lock);
        kvm_io_bus_init(&kvm->mmio_bus);
@@ -202,12 +204,15 @@ void kvm_free_physmem(struct kvm *kvm)
 
 static void kvm_destroy_vm(struct kvm *kvm)
 {
+       struct mm_struct *mm = kvm->mm;
+
        spin_lock(&kvm_lock);
        list_del(&kvm->vm_list);
        spin_unlock(&kvm_lock);
        kvm_io_bus_destroy(&kvm->pio_bus);
        kvm_io_bus_destroy(&kvm->mmio_bus);
        kvm_arch_destroy_vm(kvm);
+       mmdrop(mm);
 }
 
 static int kvm_vm_release(struct inode *inode, struct file *filp)
@@ -818,6 +823,8 @@ static long kvm_vcpu_ioctl(struct file *filp,
        void __user *argp = (void __user *)arg;
        int r;
 
+       if (vcpu->kvm->mm != current->mm)
+               return -EIO;
        switch (ioctl) {
        case KVM_RUN:
                r = -EINVAL;
@@ -976,6 +983,8 @@ static long kvm_vm_ioctl(struct file *filp,
        void __user *argp = (void __user *)arg;
        int r;
 
+       if (kvm->mm != current->mm)
+               return -EIO;
        switch (ioctl) {
        case KVM_CREATE_VCPU:
                r = kvm_vm_ioctl_create_vcpu(kvm, arg);
index 314f5101d2b0e5f864f22fce25c4255c7305f789..05e0b6f4365bfc5fca5ef1cabd400c3894c013e3 100644 (file)
@@ -393,6 +393,7 @@ void fastcall __mmdrop(struct mm_struct *mm)
        destroy_context(mm);
        free_mm(mm);
 }
+EXPORT_SYMBOL_GPL(__mmdrop);
 
 /*
  * Decrement the use count and release all resources for an mm.