net: Fix a potential segfault
PATCH v2 1/1 net: Fix a potential segfault
If user forgets to provide any backend types for '-netdev' in qemu CLI, It triggers seg fault.
e.g.
Expected:
$ qemu -netdev id=net0 qemu-system-x86_64: Parameter 'type' is missing
Actual:
$ qemu -netdev id=net0 Segmentation fault (core dumped)
Signed-off-by Lin Ma <[email protected]>
--- net/net.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
net/net.c +6 -3
@@ -1093,9 +1093,12 @@ static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)int ret =-1 ;Visitor *v =opts_visitor_new (opts); -if (is_netdev &&is_help_option (qemu_opt_get (opts,"type" ))) { -show_netdevs (); -exit (0 ); +if (is_netdev) { +const char *type =qemu_opt_get (opts,"type" ); +if (type &&is_help_option (type)) { +show_netdevs (); +exit (0 ); + } }else {/* Parse convenience option format ip6-net=fec0::0[/64] */ const char *ip6_net =qemu_opt_get (opts,"ipv6-net" );
Signature · 1 line
2.16.2
Quoted 5 lines · Lin Ma
If user forgets to provide any backend types for '-netdev' in qemu CLI, It triggers seg fault.
e.g.
Quoted 7 lines
Expected:
$ qemu -netdev id=net0 qemu-system-x86_64: Parameter 'type' is missingActual:
$ qemu -netdev id=net0 Segmentation fault (core dumped)
Ok, thanks for adding the description!
Quoted 16 lines
Signed-off-by Lin Ma <[email protected]>--- net/net.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)net/net.c +0
-2@@ -1093,9 +1093,12 @@ static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)int ret =-1 ;Visitor *v =opts_visitor_new (opts); -if (is_netdev &&is_help_option (qemu_opt_get (opts,"type" ))) { -show_netdevs ();
net/net.c static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp) · line 1104
Quoted 10 lines
net/net.c +6
-1-exit (0 ); +if (is_netdev) { +const char *type =qemu_opt_get (opts,"type" ); +if (type &&is_help_option (type)) { +show_netdevs (); +exit (0 ); + } }else {/* Parse convenience option format ip6-net=fec0::0[/64] */ const char *ip6_net =qemu_opt_get (opts,"ipv6-net" );
I think you've got to do it in a slightly different way:
const char *type = qemu_opt_get(opts, "type");
if (is_netdev && type && is_help_option(type)) {
show_netdevs();
exit(0);
} else ...otherwise the "else" branch is not entered anymore in case it is a non-help netdev option.
Thomas