public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix
@ 2012-02-04 16:32 Cyrill Gorcunov
  2012-02-04 17:38 ` Pekka Enberg
  2012-02-04 17:54 ` Pekka Enberg
  0 siblings, 2 replies; 6+ messages in thread
From: Cyrill Gorcunov @ 2012-02-04 16:32 UTC (permalink / raw)
  To: Pekka Enberg, Sasha Levin, Asias He, Ingo Molnar, KVM-ML

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@gmail.com>
---
 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);

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix
  2012-02-04 16:32 [PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix Cyrill Gorcunov
@ 2012-02-04 17:38 ` Pekka Enberg
  2012-02-04 17:48   ` Cyrill Gorcunov
  2012-02-04 17:54 ` Pekka Enberg
  1 sibling, 1 reply; 6+ messages in thread
From: Pekka Enberg @ 2012-02-04 17:38 UTC (permalink / raw)
  To: Cyrill Gorcunov; +Cc: Sasha Levin, Asias He, Ingo Molnar, KVM-ML

On Sat, 4 Feb 2012, Cyrill Gorcunov wrote:
> 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@gmail.com>
> ---
> 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)) {

PTR_ERR is still not going to work if kvm is NULL.

> +		r = PTR_ERR(kvm);
> +		goto fail;
> +	}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix
  2012-02-04 17:38 ` Pekka Enberg
@ 2012-02-04 17:48   ` Cyrill Gorcunov
  0 siblings, 0 replies; 6+ messages in thread
From: Cyrill Gorcunov @ 2012-02-04 17:48 UTC (permalink / raw)
  To: Pekka Enberg; +Cc: Sasha Levin, Asias He, Ingo Molnar, KVM-ML

On Sat, Feb 04, 2012 at 07:38:41PM +0200, Pekka Enberg wrote:
> On Sat, 4 Feb 2012, Cyrill Gorcunov wrote:
> >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@gmail.com>
> >---
> >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)) {
> 
> PTR_ERR is still not going to work if kvm is NULL.
> 

It should be IS_ERR rather (kvm__init never returns with NULL),
but it's a candidate for another patch where both kvm__init
and kvm__new should test for IS_ERR only.

Pekka, could you please s/IS_ERR_OR_NULL/IS_ERR/ in this patch,
and that's all. Hm?

	Cyrill

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix
  2012-02-04 16:32 [PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix Cyrill Gorcunov
  2012-02-04 17:38 ` Pekka Enberg
@ 2012-02-04 17:54 ` Pekka Enberg
  2012-02-04 18:02   ` Cyrill Gorcunov
  1 sibling, 1 reply; 6+ messages in thread
From: Pekka Enberg @ 2012-02-04 17:54 UTC (permalink / raw)
  To: Cyrill Gorcunov; +Cc: Sasha Levin, Asias He, Ingo Molnar, KVM-ML

On Sat, 4 Feb 2012, Cyrill Gorcunov wrote:
> 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);

No, please don't do this! Use specific error handling labels instead.

> 	free(kvm);
>
> 	return ERR_PTR(ret);
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix
  2012-02-04 17:54 ` Pekka Enberg
@ 2012-02-04 18:02   ` Cyrill Gorcunov
  2012-02-05 20:19     ` Cyrill Gorcunov
  0 siblings, 1 reply; 6+ messages in thread
From: Cyrill Gorcunov @ 2012-02-04 18:02 UTC (permalink / raw)
  To: Pekka Enberg; +Cc: Sasha Levin, Asias He, Ingo Molnar, KVM-ML

On Sat, Feb 04, 2012 at 07:54:38PM +0200, Pekka Enberg wrote:
> On Sat, 4 Feb 2012, Cyrill Gorcunov wrote:
> >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);
> 
> No, please don't do this! Use specific error handling labels instead.
> 

Strictly speaking, kvm__init need more serious rewrite together with
kvm__arch_init/kvm_ipc__start/kvm_ipc__register_handler ret. vals tests,
i'll do this a bit late.

	Cyrill

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix
  2012-02-04 18:02   ` Cyrill Gorcunov
@ 2012-02-05 20:19     ` Cyrill Gorcunov
  0 siblings, 0 replies; 6+ messages in thread
From: Cyrill Gorcunov @ 2012-02-05 20:19 UTC (permalink / raw)
  To: Pekka Enberg, Sasha Levin, Asias He, Ingo Molnar, KVM-ML

On Sat, Feb 04, 2012 at 10:02:19PM +0400, Cyrill Gorcunov wrote:
> 
> Strictly speaking, kvm__init need more serious rewrite together with
> kvm__arch_init/kvm_ipc__start/kvm_ipc__register_handler ret. vals tests,
> i'll do this a bit late.
> 

Sorry for delay, was busy. Anyway, here is a quickfix for sigsev and close()s.

	Cyrill
---
Subject: kvm tool: Don't close not yet opened files and SIGSEV fix

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@gmail.com>
---
 tools/kvm/builtin-run.c |    4 ++++
 tools/kvm/kvm.c         |   41 +++++++++++++++++++++--------------------
 2 files changed, 25 insertions(+), 20 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(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;
 }
 
@@ -341,47 +343,42 @@ struct kvm *kvm__init(const char *kvm_de
 	}
 
 	kvm = kvm__new();
-	if (IS_ERR_OR_NULL(kvm))
+	if (IS_ERR(kvm))
 		return kvm;
 
 	kvm->sys_fd = open(kvm_dev, O_RDWR);
 	if (kvm->sys_fd < 0) {
-		if (errno == ENOENT) {
+		if (errno == ENOENT)
 			pr_err("'%s' not found. Please make sure your kernel has CONFIG_KVM "
-				"enabled and that the KVM modules are loaded.", kvm_dev);
-			ret = -errno;
-			goto cleanup;
-		}
-		if (errno == ENODEV) {
-			die("'%s' KVM driver not available.\n  # (If the KVM "
-				"module is loaded then 'dmesg' may offer further clues "
-				"about the failure.)", kvm_dev);
-			ret = -errno;
-			goto cleanup;
-		}
+			       "enabled and that the KVM modules are loaded.", kvm_dev);
+		else if (errno == ENODEV)
+			pr_err("'%s' KVM driver not available.\n  # (If the KVM "
+			       "module is loaded then 'dmesg' may offer further clues "
+			       "about the failure.)", kvm_dev);
+		else
+			pr_err("Could not open %s: ", kvm_dev);
 
-		pr_err("Could not open %s: ", kvm_dev);
 		ret = -errno;
-		goto cleanup;
+		goto err_free;
 	}
 
 	ret = ioctl(kvm->sys_fd, KVM_GET_API_VERSION, 0);
 	if (ret != KVM_API_VERSION) {
 		pr_err("KVM_API_VERSION ioctl");
 		ret = -errno;
-		goto cleanup;
+		goto err_sys_fd;
 	}
 
 	kvm->vm_fd = ioctl(kvm->sys_fd, KVM_CREATE_VM, 0);
 	if (kvm->vm_fd < 0) {
 		ret = kvm->vm_fd;
-		goto cleanup;
+		goto err_sys_fd;
 	}
 
 	kvm->name = strdup(name);
 	if (!kvm->name) {
 		ret = -ENOMEM;
-		goto cleanup;
+		goto err;
 	}
 
 	if (kvm__check_extensions(kvm)) {
@@ -393,10 +390,14 @@ 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:
+
+err:
 	close(kvm->vm_fd);
+err_sys_fd:
 	close(kvm->sys_fd);
+err_free:
 	free(kvm);
 
 	return ERR_PTR(ret);

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-02-05 20:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-04 16:32 [PATCH] kvm tool: Don't close not yet opened files and SIGSEV fix Cyrill Gorcunov
2012-02-04 17:38 ` Pekka Enberg
2012-02-04 17:48   ` Cyrill Gorcunov
2012-02-04 17:54 ` Pekka Enberg
2012-02-04 18:02   ` Cyrill Gorcunov
2012-02-05 20:19     ` Cyrill Gorcunov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox