[PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In case if there error happened in kvm__init and we have
no files opened -- we should not try to close them.

Also once kvm failed to init the caller should not try
to dereference a pointer obtained, otherwise we might get
SIGSEV

 | [cyrill@moon kvm]$ ./lkvm run ...
 |  Error: '/dev/kvm' not found. Please make sure your kernel has CONFIG_KVM enabled and that the KVM modules are loaded.
 | Segmentation fault (core dumped)
 | [cyrill@moon kvm]$ 

Signed-off-by: Cyrill Gorcunov <gorcunov@xxxxxxxxx>
---
 tools/kvm/builtin-run.c |    4 ++++
 tools/kvm/kvm.c         |   11 ++++++++---
 2 files changed, 12 insertions(+), 3 deletions(-)

Index: linux-2.6.git/tools/kvm/builtin-run.c
===================================================================
--- linux-2.6.git.orig/tools/kvm/builtin-run.c
+++ linux-2.6.git/tools/kvm/builtin-run.c
@@ -997,6 +997,10 @@ static int kvm_cmd_run_init(int argc, co
 	}
 
 	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
+	if (IS_ERR_OR_NULL(kvm)) {
+		r = PTR_ERR(kvm);
+		goto fail;
+	}
 
 	kvm->single_step = single_step;
 
Index: linux-2.6.git/tools/kvm/kvm.c
===================================================================
--- linux-2.6.git.orig/tools/kvm/kvm.c
+++ linux-2.6.git/tools/kvm/kvm.c
@@ -123,10 +123,12 @@ static int kvm__check_extensions(struct
 static struct kvm *kvm__new(void)
 {
 	struct kvm *kvm = calloc(1, sizeof(*kvm));
-
 	if (!kvm)
 		return ERR_PTR(-ENOMEM);
 
+	kvm->sys_fd = -1;
+	kvm->vm_fd = -1;
+
 	return kvm;
 }
 
@@ -394,9 +396,12 @@ struct kvm *kvm__init(const char *kvm_de
 	kvm_ipc__start(kvm__create_socket(kvm));
 	kvm_ipc__register_handler(KVM_IPC_PID, kvm__pid);
 	return kvm;
+
 cleanup:
-	close(kvm->vm_fd);
-	close(kvm->sys_fd);
+	if (kvm->vm_fd >= 0)
+		close(kvm->vm_fd);
+	if (kvm->sys_fd >= 0)
+		close(kvm->sys_fd);
 	free(kvm);
 
 	return ERR_PTR(ret);
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux