Commit 506cfba9 authored by Al Viro's avatar Al Viro Committed by Paolo Bonzini

KVM: don't use anon_inode_getfd() before possible failures

Once anon_inode_getfd() has succeeded, it's impossible to undo
in a clean way and no, sys_close() is not usable in such cases.
Use anon_inode_getfile() and get_unused_fd_flags() to get struct file
and descriptor and do *not* install the file into the descriptor table
until after the last possible failure exit.
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 7964218c
...@@ -3050,6 +3050,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type) ...@@ -3050,6 +3050,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
{ {
int r; int r;
struct kvm *kvm; struct kvm *kvm;
struct file *file;
kvm = kvm_create_vm(type); kvm = kvm_create_vm(type);
if (IS_ERR(kvm)) if (IS_ERR(kvm))
...@@ -3061,17 +3062,25 @@ static int kvm_dev_ioctl_create_vm(unsigned long type) ...@@ -3061,17 +3062,25 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
return r; return r;
} }
#endif #endif
r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR | O_CLOEXEC); r = get_unused_fd_flags(O_CLOEXEC);
if (r < 0) { if (r < 0) {
kvm_put_kvm(kvm); kvm_put_kvm(kvm);
return r; return r;
} }
file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
if (IS_ERR(file)) {
put_unused_fd(r);
kvm_put_kvm(kvm);
return PTR_ERR(file);
}
if (kvm_create_vm_debugfs(kvm, r) < 0) { if (kvm_create_vm_debugfs(kvm, r) < 0) {
kvm_put_kvm(kvm); put_unused_fd(r);
fput(file);
return -ENOMEM; return -ENOMEM;
} }
fd_install(r, file);
return r; return r;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment