fix segfaults caused by accessing CPU in empty machine
cover fix segfaults caused by accessing CPU in empty machine
From: Ziyue Yang <[email protected]>
Many QEMU monitor commands, like "info lapic", "info tlb" and so on use mon_get_cpu or related wrappers to access CPU info without checking whether the CPU exists.
This patch series fix the "info lapic" case, and is the base of the incoming patch series aiming to eliminate segfaults caused by other QEMU commands trying to access CPU that doesn't exist.
Ziyue Yang (2):
monitor.c: make mon_get_cpu return NULL when there is no CPU target/i386/monitor.c: check return value of mon_get_cpu before using
it
monitor.c | 10 +++++++--- target/i386/monitor.c | 7 +++++-- 2 files changed, 12 insertions(+), 5 deletions(-)
--
2.11.0
PATCH 1/2 monitor.c: make mon_get_cpu return NULL when there is no CPU
From: Ziyue Yang <[email protected]>
Currently mon_get_cpu always dereferences first_cpu without checking whether it's a valid pointer. This commit adds check before dereferencing, and reports "No CPU" info if there isn't any CPU then returns NULL.
--- monitor.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
monitor.c +7 -3
@@ -1026,6 +1026,10 @@ int monitor_set_cpu(int cpu_index)CPUState *mon_get_cpu (void ) {if (!cur_mon->mon_cpu) { +if (!first_cpu) { +monitor_printf (cur_mon,"No CPU available on this machine\n" ); +return NULL ; + }monitor_set_cpu (first_cpu->cpu_index); }cpu_synchronize_state (cur_mon->mon_cpu);
@@ -2495,11 +2499,11 @@ static int default_fmt_size = 4;static int is_valid_option (const char *c,const char *typestr) {char option[3 ]; - + option[0 ] ='-' ; option[1 ] = *c; option[2 ] ='\0' ; - + typestr =strstr (typestr, option);return (typestr !=NULL ); }
@@ -2864,7 +2868,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, p++;if (c != *p) {if (!is_valid_option (p, typestr)) { - +monitor_printf (mon,"%s: unsupported option -%c\n" , cmd->name, *p); goto fail;
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 1/2] monitor.c: make mon_get_cpu return NULL when there is no CPU
Quoted 7 lines · Ziyue Yang
From: Ziyue Yang <[email protected]>
Currently
mon_get_cpualways dereferencesfirst_cpuwithout checking whether it's a valid pointer. This commit adds check before dereferencing, and reports "No CPU" info if there isn't any CPU then returns NULL.Signed-off-by Ziyue Yang <[email protected]>
Quoted 43 lines
--- monitor.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)monitor.c +7
-3@@ -1026,6 +1026,10 @@ int monitor_set_cpu(int cpu_index)CPUState *mon_get_cpu (void ) {if (!cur_mon->mon_cpu) { +if (!first_cpu) { +monitor_printf (cur_mon,"No CPU available on this machine\n" ); +return NULL ; + }monitor_set_cpu (first_cpu->cpu_index); }cpu_synchronize_state (cur_mon->mon_cpu);@@ -2495,11 +2499,11 @@ static int default_fmt_size = 4;static int is_valid_option (const char *c,const char *typestr) {char option[3 ]; - + option[0 ] ='-' ; option[1 ] = *c; option[2 ] ='\0' ; - + typestr =strstr (typestr, option);return (typestr !=NULL ); }@@ -2864,7 +2868,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, p++;if (c != *p) {if (!is_valid_option (p, typestr)) { - +monitor_printf (mon,"%s: unsupported option -%c\n" , cmd->name, *p); goto fail;
Re: [Qemu-devel] [PATCH 1/2] monitor.c: make mon_get_cpu return NULL when there is no CPU
Quoted 48 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Ziyue Yang
From: Ziyue Yang <[email protected]>
Currently
mon_get_cpualways dereferencesfirst_cpuwithout checking whether it's a valid pointer. This commit adds check before dereferencing,
and reports "No CPU" info if there isn't any CPU then returns NULL.Signed-off-by Ziyue Yang <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 36 lines
--- monitor.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)monitor.c +4
-0@@ -1026,6 +1026,10 @@ int monitor_set_cpu(int cpu_index)CPUState *mon_get_cpu (void ) {if (!cur_mon->mon_cpu) { +if (!first_cpu) { +monitor_printf (cur_mon,"No CPU available on this machine\n"); +return NULL ; + }monitor_set_cpu (first_cpu->cpu_index); }cpu_synchronize_state(cur_mon->mon_cpu);monitor.c +2
-2@@ -2495,11 +2499,11 @@ static int default_fmt_size = 4;static int is_valid_option (const char *c,const char *typestr) {char option[3 ]; - + option[0 ] ='-' ; option[1 ] = *c; option[2 ] ='\0' ; - + typestr =strstr (typestr, option);return (typestr !=NULL ); }@@ -2864,7 +2868,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, p++;
Quoted 8 lines
Quoted 8 lines
monitor.c +1
-1if (c != *p) {if (!is_valid_option (p, typestr)) { - + monitor_printf(mon, "%s: unsupported option -%c\n", cmd->name, *p);goto fail;
Your patch contains some unnecessary white space changes, please try to avoid that! (or send a separate "beautification" patch to fix these).
Thomas
Re: [Qemu-devel] [PATCH 1/2] monitor.c: make mon_get_cpu return NULL when there is no CPU
Sorry for forgetting the "no irrelevant change" rule...won't do that next time!
2017-02-20 14:09 GMT+08:00 Thomas Huth <[email protected]>:
Quoted 63 lines · Philippe Mathieu-Daudé
Quoted 48 lines · Ziyue Yang
Quoted 8 lines
From: Ziyue Yang <[email protected]>
Currently
mon_get_cpualways dereferencesfirst_cpuwithout checking whether it's a valid pointer. This commit adds check before dereferencing,
and reports "No CPU" info if there isn't any CPU then returns NULL.Signed-off-by Ziyue Yang <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 36 lines
--- monitor.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)monitor.c +4
-0@@ -1026,6 +1026,10 @@ int monitor_set_cpu(int cpu_index)CPUState *mon_get_cpu (void ) {if (!cur_mon->mon_cpu) { +if (!first_cpu) { +monitor_printf (cur_mon,"No CPU available on this machine\n"); +return NULL ; + }monitor_set_cpu (first_cpu->cpu_index); }cpu_synchronize_state(cur_mon->mon_cpu);monitor.c +2
-2@@ -2495,11 +2499,11 @@ static int default_fmt_size = 4;static int is_valid_option (const char *c,const char *typestr) {char option[3 ]; - + option[0 ] ='-' ; option[1 ] = *c; option[2 ] ='\0' ; - + typestr =strstr (typestr, option);return (typestr !=NULL ); }@@ -2864,7 +2868,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, p++;Quoted 8 lines
Quoted 8 lines
monitor.c +1
-1if (c != *p) {if (!is_valid_option (p, typestr)) { - + monitor_printf(mon, "%s: unsupported option -%c\n", cmd->name, *p);goto fail;Your patch contains some unnecessary white space changes, please try to avoid that! (or send a separate "beautification" patch to fix these).
Thomas
PATCH 2/2 target/i386/monitor.c: check return value of mon_get_cpu before using it
In reply to Ziyue YangFrom: Ziyue Yang <[email protected]>
This patch eliminates the segfault caused by accessing CPU that doesn't exist in hmp command "info lapic", which can be reproduced by
$ qemu-system-x86_64 -nographic -M none -serial none -monitor stdio
and then type "info lapic" into qemu monitor.
--- target/i386/monitor.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
target/i386/monitor.c +5 -2
@@ -624,8 +624,11 @@ const MonitorDef *target_monitor_defs(void)void hmp_info_local_apic (Monitor *mon,const QDict *qdict) { -x86_cpu_dump_local_apic_state (mon_get_cpu (), (FILE *)mon, monitor_fprintf, -CPU_DUMP_FPU ); +CPUState *cs =mon_get_cpu (); +if (cs) { +x86_cpu_dump_local_apic_state (cs, (FILE *)mon, monitor_fprintf, +CPU_DUMP_FPU ); + } }void hmp_info_io_apic (Monitor *mon,const QDict *qdict)
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 2/2] target/i386/monitor.c: check return value of mon_get_cpu before using it
Quoted 10 lines · Ziyue Yang
From: Ziyue Yang <[email protected]>
This patch eliminates the segfault caused by accessing CPU that doesn't exist in hmp command "info lapic", which can be reproduced by
$ qemu-system-x86_64 -nographic -M none -serial none -monitor stdioand then type "info lapic" into qemu monitor.
Signed-off-by Ziyue Yang <[email protected]>
Quoted 23 lines
--- target/i386/monitor.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)target/i386/monitor.c +5
-2@@ -624,8 +624,11 @@ const MonitorDef *target_monitor_defs(void)void hmp_info_local_apic (Monitor *mon,const QDict *qdict) { -x86_cpu_dump_local_apic_state (mon_get_cpu (), (FILE *)mon, monitor_fprintf, -CPU_DUMP_FPU ); +CPUState *cs =mon_get_cpu (); +if (cs) { +x86_cpu_dump_local_apic_state (cs, (FILE *)mon, monitor_fprintf, +CPU_DUMP_FPU ); + } }void hmp_info_io_apic (Monitor *mon,const QDict *qdict)
Quoted 8 lines · Ziyue Yang
From: Ziyue Yang <[email protected]>
Many QEMU monitor commands, like "info lapic", "info tlb" and so on use
mon_get_cpuor related wrappers to access CPU info without checking whether the CPU exists.
This patch series fix the "info lapic" case, and is the base of the incoming patch series aiming to eliminate segfaults caused by other QEMU commands trying to access CPU that doesn't exist.
Hi,
FYI, I've posted a patch for all of these monitor commands that crash without CPU already last month:
https://lists.gnu.org/archive/html/qemu-devel/2017-01/msg02602.html
Thomas
Quoted 10 lines · * Thomas Huth ([email protected]) wrote:
Quoted 8 lines · Ziyue Yang
From: Ziyue Yang <[email protected]>
Many QEMU monitor commands, like "info lapic", "info tlb" and so on use
mon_get_cpuor related wrappers to access CPU info without checking whether the CPU exists.
This patch series fix the "info lapic" case, and is the base of the incoming patch series aiming to eliminate segfaults caused by other QEMU commands trying to access CPU that doesn't exist.
Quoted 6 lines
Hi,
FYI, I've posted a patch for all of these monitor commands that crash without CPU already last month:
https://lists.gnu.org/archive/html/qemu-devel/2017-01/msg02602.html
I've just sent that in my HMP pull:
https://lists.nongnu.org/archive/html/qemu-devel/2017-02/msg04939.html
Ziyue Yang: Perhaps it's best to compare with the things in that pull and check to see if you have any items that were not in that pull.
Dave
Quoted 2 lines
Thomas
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK