From nobody Fri May 3 03:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1507653731739638.9507481552828; Tue, 10 Oct 2017 09:42:11 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F33E2267CA; Tue, 10 Oct 2017 16:42:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 21E10679E8; Tue, 10 Oct 2017 16:42:09 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 854CE410B1; Tue, 10 Oct 2017 16:42:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v9AG1TiW003253 for ; Tue, 10 Oct 2017 12:01:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4537977714; Tue, 10 Oct 2017 16:01:29 +0000 (UTC) Received: from inaba.usersys.redhat.com (unknown [10.43.2.219]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 49F7077BFD for ; Tue, 10 Oct 2017 16:01:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com F33E2267CA Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: Andrea Bolognani To: libvir-list@redhat.com Date: Tue, 10 Oct 2017 18:01:17 +0200 Message-Id: <20171010160117.29267-1-abologna@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2] qemu: Don't crash when parsing command line lacking -M X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 10 Oct 2017 16:42:11 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Parse the -M (or -machine) command line option before starting processing in earnest and have a fallback ready in case it's not present, so that while parsing other options we can rely on def->os.machine being initialized. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1379218 Signed-off-by: Andrea Bolognani Reviewed-by: Daniel P. Berrange --- Changes from [v1]: * rework command line parsing instead of adding checks to qemuDomainMachineIs*() functions [v1] https://www.redhat.com/archives/libvir-list/2017-October/msg00406.html src/qemu/qemu_parse_command.c | 174 ++++++++++++-----= ---- .../qemuargv2xml-nomachine-aarch64.args | 11 ++ .../qemuargv2xml-nomachine-aarch64.xml | 39 +++++ .../qemuargv2xml-nomachine-ppc64.args | 11 ++ .../qemuargv2xml-nomachine-ppc64.xml | 49 ++++++ .../qemuargv2xml-nomachine-x86_64.args | 11 ++ .../qemuargv2xml-nomachine-x86_64.xml | 48 ++++++ tests/qemuargv2xmltest.c | 4 + 8 files changed, 270 insertions(+), 77 deletions(-) create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-nomachine-aarch64.a= rgs create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-nomachine-aarch64.x= ml create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-nomachine-ppc64.args create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-nomachine-ppc64.xml create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-nomachine-x86_64.ar= gs create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-nomachine-x86_64.xml diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c index 7c409b03a..aaa4d3703 100644 --- a/src/qemu/qemu_parse_command.c +++ b/src/qemu/qemu_parse_command.c @@ -1884,6 +1884,98 @@ qemuParseCommandLine(virCapsPtr caps, } } =20 + /* Detect machine type before processing any other arguments, + * because they might depend on it */ + for (i =3D 1; progargv[i]; i++) { + const char *arg =3D progargv[i]; + + /* Make sure we have a single - for all options to + simplify next logic */ + if (STRPREFIX(arg, "--")) + arg++; + + if (STREQ(arg, "-M") || + STREQ(arg, "-machine")) { + char *param; + size_t j =3D 0; + + /* -machine [type=3D]name[,prop[=3Dvalue][,...]] + * Set os.machine only if first parameter lacks '=3D' or + * contains explicit type=3D'...' */ + WANT_VALUE(); + if (!(list =3D virStringSplit(val, ",", 0))) + goto error; + param =3D list[0]; + + if (STRPREFIX(param, "type=3D")) + param +=3D strlen("type=3D"); + if (!strchr(param, '=3D')) { + if (VIR_STRDUP(def->os.machine, param) < 0) + goto error; + j++; + } + + /* handle all remaining "-machine" parameters */ + while ((param =3D list[j++])) { + if (STRPREFIX(param, "dump-guest-core=3D")) { + param +=3D strlen("dump-guest-core=3D"); + def->mem.dump_core =3D virTristateSwitchTypeFromString= (param); + if (def->mem.dump_core <=3D 0) + def->mem.dump_core =3D VIR_TRISTATE_SWITCH_ABSENT; + } else if (STRPREFIX(param, "mem-merge=3Doff")) { + def->mem.nosharepages =3D true; + } else if (STRPREFIX(param, "accel=3Dkvm")) { + def->virtType =3D VIR_DOMAIN_VIRT_KVM; + def->features[VIR_DOMAIN_FEATURE_PAE] =3D VIR_TRISTATE= _SWITCH_ON; + } else if (STRPREFIX(param, "aes-key-wrap=3D")) { + if (STREQ(arg, "-M")) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("aes-key-wrap is not supported wi= th " + "this QEMU binary")); + goto error; + } + param +=3D strlen("aes-key-wrap=3D"); + if (!def->keywrap && VIR_ALLOC(def->keywrap) < 0) + goto error; + def->keywrap->aes =3D virTristateSwitchTypeFromString(= param); + if (def->keywrap->aes < 0) + def->keywrap->aes =3D VIR_TRISTATE_SWITCH_ABSENT; + } else if (STRPREFIX(param, "dea-key-wrap=3D")) { + if (STREQ(arg, "-M")) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("dea-key-wrap is not supported wi= th " + "this QEMU binary")); + goto error; + } + param +=3D strlen("dea-key-wrap=3D"); + if (!def->keywrap && VIR_ALLOC(def->keywrap) < 0) + goto error; + def->keywrap->dea =3D virTristateSwitchTypeFromString(= param); + if (def->keywrap->dea < 0) + def->keywrap->dea =3D VIR_TRISTATE_SWITCH_ABSENT; + } + } + virStringListFree(list); + list =3D NULL; + } + } + + /* If no machine type has been found among the arguments, then figure + * out a reasonable value by using capabilities */ + if (!def->os.machine) { + virCapsDomainDataPtr capsdata; + + if (!(capsdata =3D virCapabilitiesDomainDataLookup(caps, def->os.t= ype, + def->os.arch, def->virtType, NULL, NULL))) + goto error; + + if (VIR_STRDUP(def->os.machine, capsdata->machinetype) < 0) { + VIR_FREE(capsdata); + goto error; + } + VIR_FREE(capsdata); + } + /* Now the real processing loop */ for (i =3D 1; progargv[i]; i++) { const char *arg =3D progargv[i]; @@ -2137,69 +2229,6 @@ qemuParseCommandLine(virCapsPtr caps, } if (STREQ(def->name, "")) VIR_FREE(def->name); - } else if (STREQ(arg, "-M") || - STREQ(arg, "-machine")) { - char *param; - size_t j =3D 0; - - /* -machine [type=3D]name[,prop[=3Dvalue][,...]] - * Set os.machine only if first parameter lacks '=3D' or - * contains explicit type=3D'...' */ - WANT_VALUE(); - if (!(list =3D virStringSplit(val, ",", 0))) - goto error; - param =3D list[0]; - - if (STRPREFIX(param, "type=3D")) - param +=3D strlen("type=3D"); - if (!strchr(param, '=3D')) { - if (VIR_STRDUP(def->os.machine, param) < 0) - goto error; - j++; - } - - /* handle all remaining "-machine" parameters */ - while ((param =3D list[j++])) { - if (STRPREFIX(param, "dump-guest-core=3D")) { - param +=3D strlen("dump-guest-core=3D"); - def->mem.dump_core =3D virTristateSwitchTypeFromString= (param); - if (def->mem.dump_core <=3D 0) - def->mem.dump_core =3D VIR_TRISTATE_SWITCH_ABSENT; - } else if (STRPREFIX(param, "mem-merge=3Doff")) { - def->mem.nosharepages =3D true; - } else if (STRPREFIX(param, "accel=3Dkvm")) { - def->virtType =3D VIR_DOMAIN_VIRT_KVM; - def->features[VIR_DOMAIN_FEATURE_PAE] =3D VIR_TRISTATE= _SWITCH_ON; - } else if (STRPREFIX(param, "aes-key-wrap=3D")) { - if (STREQ(arg, "-M")) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("aes-key-wrap is not supported wi= th " - "this QEMU binary")); - goto error; - } - param +=3D strlen("aes-key-wrap=3D"); - if (!def->keywrap && VIR_ALLOC(def->keywrap) < 0) - goto error; - def->keywrap->aes =3D virTristateSwitchTypeFromString(= param); - if (def->keywrap->aes < 0) - def->keywrap->aes =3D VIR_TRISTATE_SWITCH_ABSENT; - } else if (STRPREFIX(param, "dea-key-wrap=3D")) { - if (STREQ(arg, "-M")) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("dea-key-wrap is not supported wi= th " - "this QEMU binary")); - goto error; - } - param +=3D strlen("dea-key-wrap=3D"); - if (!def->keywrap && VIR_ALLOC(def->keywrap) < 0) - goto error; - def->keywrap->dea =3D virTristateSwitchTypeFromString(= param); - if (def->keywrap->dea < 0) - def->keywrap->dea =3D VIR_TRISTATE_SWITCH_ABSENT; - } - } - virStringListFree(list); - list =3D NULL; } else if (STREQ(arg, "-serial")) { WANT_VALUE(); if (STRNEQ(val, "none")) { @@ -2489,6 +2518,11 @@ qemuParseCommandLine(virCapsPtr caps, =20 argRecognized =3D false; } + } else if (STREQ(arg, "-M") || + STREQ(arg, "-machine")) { + // This option has already been processed before entering this + // loop, so we just need to skip its argument and move along + WANT_VALUE(); } else { argRecognized =3D false; } @@ -2571,20 +2605,6 @@ qemuParseCommandLine(virCapsPtr caps, } } =20 - if (!def->os.machine) { - virCapsDomainDataPtr capsdata; - - if (!(capsdata =3D virCapabilitiesDomainDataLookup(caps, def->os.t= ype, - def->os.arch, def->virtType, NULL, NULL))) - goto error; - - if (VIR_STRDUP(def->os.machine, capsdata->machinetype) < 0) { - VIR_FREE(capsdata); - goto error; - } - VIR_FREE(capsdata); - } - if (!nographics && (def->ngraphics =3D=3D 0 || have_sdl)) { virDomainGraphicsDefPtr sdl; const char *display =3D qemuFindEnv(progenv, "DISPLAY"); diff --git a/tests/qemuargv2xmldata/qemuargv2xml-nomachine-aarch64.args b/t= ests/qemuargv2xmldata/qemuargv2xml-nomachine-aarch64.args new file mode 100644 index 000000000..b17c0d0c2 --- /dev/null +++ b/tests/qemuargv2xmldata/qemuargv2xml-nomachine-aarch64.args @@ -0,0 +1,11 @@ +LC_ALL=3DC \ +PATH=3D/bin \ +HOME=3D/home/test \ +USER=3Dtest \ +LOGNAME=3Dtest \ +QEMU_AUDIO_DRV=3Dnone \ +/usr/bin/qemu-system-aarch64 \ +-name QEMUGuest1 \ +-m 512 \ +-hda /dev/HostVG/QEMUGuest1 \ +-cdrom /root/boot.iso diff --git a/tests/qemuargv2xmldata/qemuargv2xml-nomachine-aarch64.xml b/te= sts/qemuargv2xmldata/qemuargv2xml-nomachine-aarch64.xml new file mode 100644 index 000000000..eb8f9db80 --- /dev/null +++ b/tests/qemuargv2xmldata/qemuargv2xml-nomachine-aarch64.xml @@ -0,0 +1,39 @@ + + QEMUGuest1 + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 524288 + 524288 + 1 + + hvm + + + + + + destroy + restart + destroy + + /usr/bin/qemu-system-aarch64 + + + + +
+ + + + + + +
+ + + + + + + diff --git a/tests/qemuargv2xmldata/qemuargv2xml-nomachine-ppc64.args b/tes= ts/qemuargv2xmldata/qemuargv2xml-nomachine-ppc64.args new file mode 100644 index 000000000..ac618775e --- /dev/null +++ b/tests/qemuargv2xmldata/qemuargv2xml-nomachine-ppc64.args @@ -0,0 +1,11 @@ +LC_ALL=3DC \ +PATH=3D/bin \ +HOME=3D/home/test \ +USER=3Dtest \ +LOGNAME=3Dtest \ +QEMU_AUDIO_DRV=3Dnone \ +/usr/bin/qemu-system-ppc64 \ +-name QEMUGuest1 \ +-m 512 \ +-hda /dev/HostVG/QEMUGuest1 \ +-cdrom /root/boot.iso diff --git a/tests/qemuargv2xmldata/qemuargv2xml-nomachine-ppc64.xml b/test= s/qemuargv2xmldata/qemuargv2xml-nomachine-ppc64.xml new file mode 100644 index 000000000..fa41070cb --- /dev/null +++ b/tests/qemuargv2xmldata/qemuargv2xml-nomachine-ppc64.xml @@ -0,0 +1,49 @@ + + QEMUGuest1 + c7a5fdbd-edaf-9455-926a-d65c16db1809 + 524288 + 524288 + 1 + + hvm + + + destroy + restart + destroy + + /usr/bin/qemu-system-ppc64 + + + + +
+ + + + + + +
+ + +
+ + + + + + +
+ + + + +