kvm tool: Don't close not yet opened files and SIGSEV fix
PATCH 1/1 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]$
--- tools/kvm/builtin-run.c | 4 ++++ tools/kvm/kvm.c | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-)
tools/kvm/builtin-run.c +4 -0
@@ -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;
tools/kvm/kvm.c +8 -3
@@ -123,10 +123,12 @@ static int kvm__check_extensions(structstatic 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_dekvm_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);
Quoted 23 lines · Cyrill Gorcunov
In case if there error happened in
kvm__initand 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 <[email protected]>---
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
===================================================================
tools/kvm/builtin-run.c static int kvm_cmd_run_init(int argc, co · line 1000
Quoted 4 lines
tools/kvm/builtin-run.c +1
-0@@ -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.
Quoted 3 lines
tools/kvm/builtin-run.c +3
-0+ r =PTR_ERR (kvm); + goto fail; + }
Quoted 26 lines · Pekka Enberg
Quoted 25 lines · Cyrill Gorcunov
In case if there error happened in
kvm__initand 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 <[email protected]>---
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
===================================================================tools/kvm/builtin-run.c +0
-0@@ -997,6 +997,10 @@ static int kvm_cmd_run_init(int argc, co }
Quoted 5 lines
tools/kvm/builtin-run.c static int kvm_cmd_run_init(int argc, co · line 1000
Quoted 2 lines
tools/kvm/builtin-run.c +1
-0kvm =kvm__init (dev, hugetlbfs_path, ram_size, guest_name); +if (IS_ERR_OR_NULL (kvm)) {
PTR_ERRis 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
Quoted 24 lines · Cyrill Gorcunov
Index:
linux-2.6.git/tools/kvm/kvm.c
===================================================================tools/kvm/kvm.c +4
-1@@ -123,10 +123,12 @@ static int kvm__check_extensions(structstatic 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_dekvm_ipc__start (kvm__create_socket (kvm));kvm_ipc__register_handler (KVM_IPC_PID , kvm__pid);return kvm; +cleanup:
tools/kvm/kvm.c struct kvm *kvm__init(const char *kvm_de · line 404
Quoted 6 lines
tools/kvm/kvm.c +4
-2-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.
Quoted 4 lines
free(kvm); return ERR_PTR(ret);
Quoted 19 lines · Pekka Enberg
Quoted 18 lines · Cyrill Gorcunov
Index:
linux-2.6.git/tools/kvm/kvm.c
===================================================================tools/kvm/kvm.c +3
-1@@ -123,10 +123,12 @@ static int kvm__check_extensions(structstatic 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; }
Quoted 15 lines
tools/kvm/kvm.c struct kvm *kvm__init(const char *kvm_de · line 404
Quoted 12 lines
tools/kvm/kvm.c +1
-0@@ -394,9 +396,12 @@ struct kvm *kvm__init(const char *kvm_dekvm_ipc__start (kvm__create_socket (kvm));kvm_ipc__register_handler (KVM_IPC_PID , kvm__pid);return kvm; +cleanup:
tools/kvm/kvm.c +4
-2-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
Quoted 5 lines · Cyrill Gorcunov
Strictly speaking,
kvm__initneed 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]$
--- tools/kvm/builtin-run.c | 4 ++++ tools/kvm/kvm.c | 41 +++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 20 deletions(-)
tools/kvm/builtin-run.c +4 -0
@@ -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;
tools/kvm/kvm.c +21 -20
88 lines not on this page · raw message