From nobody Thu May 2 08:27:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1587729901; cv=none; d=zohomail.com; s=zohoarc; b=KNG+W0B5Wt90ePy1FQ/AJzYQvu7i/MoK+l0+Mcs56BTeJi0MQzHUIqpaKVFUEd3PiTgF56uyQ6hEl1WnKhy9BN6FvcX0ao93hKeajwQ+GjJ1uKkNFt6suALAAeaPJ1RlwxwERprSNwLT4GLsFP8rhGBf0EXt4RYFxIejSfS4B4o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1587729901; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=X0gYMY79vKYw9NX9CYYFZ4tAfrHN/1WbuyKfITJzBmI=; b=N+zE/6oAfgcKNiBGGpl0KieFPg+JRsoZ6kpvERV8djzlk6segkqR76Dvkhh992A4cucDIPIDXqXEFd495p1Q1b/Nck2nbNXyhkccReZkKkhw2/Kmlkoq+UlPg2zQT8ylFQBe+VJJEYZnJEXT9ui3MyONYi9AusMLagsRVtiEbxU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1587729901781842.711326733163; Fri, 24 Apr 2020 05:05:01 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-177-MKknyy4lPMKsHvatF8VOiQ-1; Fri, 24 Apr 2020 08:04:58 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 164F28005A7; Fri, 24 Apr 2020 12:04:52 +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 78BA15D9CC; Fri, 24 Apr 2020 12:04:50 +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 7BDAC3FA44; Fri, 24 Apr 2020 12:04:47 +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 03OC4k83029107 for ; Fri, 24 Apr 2020 08:04:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5F1D660607; Fri, 24 Apr 2020 12:04:46 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.40.208.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C8E46084A; Fri, 24 Apr 2020 12:04:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587729900; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=X0gYMY79vKYw9NX9CYYFZ4tAfrHN/1WbuyKfITJzBmI=; b=N19JqwZ5itY1Rzh/mEszlHylXGOsv5BzVBsqHiWmI1eMzO0D7CYYwuR4mN+Hvxf+gMLOoA gU2hIIjfqBSSSVdqNXJx4KSvd3xXK9vBffFZMLEhOZIa3J5VEXJpSHukR6O24NgkX8/4F1 6KkVhJ1CrmhPPSZegiu3OZwPHJzOtPc= X-MC-Unique: MKknyy4lPMKsHvatF8VOiQ-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH] virStorageSourceParseNBDColonString: Rewrite to match what qemu does Date: Fri, 24 Apr 2020 14:04:40 +0200 Message-Id: <13ecd97e7b78ad3468a900a1e1bad74fc1627b7d.1587729880.git.pkrempa@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Peter Krempa 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Our implementation wasn't quite able to parse everything that qemu does. This patch rewrites the parser to a code that semantically resembles the combination of 'nbd_parse_filename' and 'inet_parse' methods in qemu to be able to parse the strings in an equivalent manner. The only thing that libvirt doesn't do is to check the lenghts of various components in the nbd string in places where qemu uses constant size buffers. The test cases validate that some of the corner cases involving colons are parsed properly. https://bugzilla.redhat.com/show_bug.cgi?id=3D1826652 Signed-off-by: Peter Krempa Reviewed-by: Eric Blake --- src/util/virstoragefile.c | 90 +++++++++++++++++++++++---------------- tests/virstoragetest.c | 16 +++++++ 2 files changed, 70 insertions(+), 36 deletions(-) diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index ffc8bdb344..a2ecdc3928 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -3072,59 +3072,77 @@ static int virStorageSourceParseNBDColonString(const char *nbdstr, virStorageSourcePtr src) { - VIR_AUTOSTRINGLIST backing =3D NULL; - const char *exportname; - - if (!(backing =3D virStringSplit(nbdstr, ":", 0))) - return -1; - - /* we know that backing[0] now equals to "nbd" */ - - if (VIR_ALLOC_N(src->hosts, 1) < 0) - return -1; + g_autofree char *nbd =3D g_strdup(nbdstr); + char *export_name; + char *host_spec; + char *unixpath; + char *port; + src->hosts =3D g_new0(virStorageNetHostDef, 1); src->nhosts =3D 1; - src->hosts->transport =3D VIR_STORAGE_NET_HOST_TRANS_TCP; + + /* We extract the parameters in a similar way qemu does it */ /* format: [] denotes optional sections, uppercase are variable strings * nbd:unix:/PATH/TO/SOCKET[:exportname=3DEXPORTNAME] * nbd:HOSTNAME:PORT[:exportname=3DEXPORTNAME] */ - if (!backing[1]) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("missing remote information in '%s' for protocol = nbd"), - nbdstr); - return -1; - } else if (STREQ(backing[1], "unix")) { - if (!backing[2]) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("missing unix socket path in nbd backing stri= ng %s"), - nbdstr); - return -1; - } - src->hosts->socket =3D g_strdup(backing[2]); + /* first look for ':exportname=3D' and cut it off */ + if ((export_name =3D strstr(nbd, ":exportname=3D"))) { + src->path =3D g_strdup(export_name + strlen(":exportname=3D")); + export_name[0] =3D '\0'; + } + + /* Verify the prefix and contents. Note that we require a + * "host_spec" part to be present. */ + if (!(host_spec =3D STRSKIP(nbd, "nbd:")) || host_spec[0] =3D=3D '\0') + goto malformed; + + if ((unixpath =3D STRSKIP(host_spec, "unix:"))) { src->hosts->transport =3D VIR_STORAGE_NET_HOST_TRANS_UNIX; - } else { - src->hosts->name =3D g_strdup(backing[1]); - if (!backing[2]) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("missing port in nbd string '%s'"), - nbdstr); - return -1; + if (unixpath[0] =3D=3D '\0') + goto malformed; + + src->hosts->socket =3D g_strdup(unixpath); + } else { + src->hosts->transport =3D VIR_STORAGE_NET_HOST_TRANS_TCP; + + if (host_spec[0] =3D=3D ':') { + /* no host given */ + goto malformed; + } else if (host_spec[0] =3D=3D '[') { + host_spec++; + /* IPv6 addr */ + if (!(port =3D strstr(host_spec, "]:"))) + goto malformed; + + port[0] =3D '\0'; + port +=3D 2; + + if (host_spec[0] =3D=3D '\0') + goto malformed; + } else { + if (!(port =3D strchr(host_spec, ':'))) + goto malformed; + + port[0] =3D '\0'; + port++; } - if (virStringParsePort(backing[2], &src->hosts->port) < 0) + if (virStringParsePort(port, &src->hosts->port) < 0) return -1; - } - if ((exportname =3D strstr(nbdstr, "exportname=3D"))) { - exportname +=3D strlen("exportname=3D"); - src->path =3D g_strdup(exportname); + src->hosts->name =3D g_strdup(host_spec); } return 0; + + malformed: + virReportError(VIR_ERR_INTERNAL_ERROR, + _("malformed nbd string '%s'"), nbdstr); + return -1; } diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index 6d2b21c25f..ac1480de4e 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -1261,10 +1261,26 @@ mymain(void) "\n" " \n" "\n"); + TEST_BACKING_PARSE("nbd:[::1]:6000:exportname=3D:test", + "\n" + " \n" + "\n"); + TEST_BACKING_PARSE("nbd:127.0.0.1:6000:exportname=3D:test", + "\n" + " \n" + "\n"); TEST_BACKING_PARSE("nbd:unix:/tmp/sock:exportname=3D/", "\n" " \= n" "\n"); + TEST_BACKING_PARSE("nbd:unix:/tmp/sock:", + "\n" + " = \n" + "\n"); + TEST_BACKING_PARSE("nbd:unix:/tmp/sock::exportname=3D:", + "\n" + " = \n" + "\n"); TEST_BACKING_PARSE("nbd://example.org:1234", "\n" " \n" --=20 2.26.0