Blob Blame History Raw
From c0e6c7fa7cd8fb4bb4ee26552c7ab35352c47ed0 Mon Sep 17 00:00:00 2001
Message-Id: <c0e6c7fa7cd8fb4bb4ee26552c7ab35352c47ed0@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Tue, 24 Feb 2015 10:32:21 +0100
Subject: [PATCH] util: storage: Fix parsing of nbd:// URI without path

https://bugzilla.redhat.com/show_bug.cgi?id=1195156

If a storage file would be backed with a NBD device without path
(nbd://localhost) libvirt would crash when parsing the backing path for
the disk as the URI structure's path element is NULL in such case but
the NBD parser would access it shamelessly.

(cherry picked from commit fdb80ed4f6563928b9942a0d1450e0c725aa6c06)

    Conflicts: tests/virstoragetest.c - test case numbering refactor was
                                        not backported and thus I
                                        manually numbered this test as
                                        12.5

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/util/virstoragefile.c |  3 ++-
 tests/virstoragetest.c    | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 580ad20..3ab20a4 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -2150,7 +2150,8 @@ virStorageSourceParseBackingURI(virStorageSourcePtr src,
     /* XXX We currently don't support auth, so don't bother parsing it */
 
     /* possibly skip the leading slash */
-    if (VIR_STRDUP(src->path,
+    if (uri->path &&
+        VIR_STRDUP(src->path,
                    *uri->path == '/' ? uri->path + 1 : uri->path) < 0)
         goto cleanup;
 
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
index 2601edc..fc37489 100644
--- a/tests/virstoragetest.c
+++ b/tests/virstoragetest.c
@@ -870,6 +870,20 @@ mymain(void)
                (&qcow2, &nbd2), EXP_PASS,
                (&qcow2, &nbd2), ALLOW_PROBE | EXP_PASS);
 
+    /* Rewrite qcow2 to use an nbd: protocol without path as backend */
+    virCommandFree(cmd);
+    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
+                               "-F", "raw", "-b", "nbd://example.org",
+                               "qcow2", NULL);
+    if (virCommandRun(cmd, NULL) < 0)
+        ret = -1;
+    qcow2.expBackingStoreRaw = "nbd://example.org";
+
+    nbd2.path = NULL;
+    TEST_CHAIN(12.5, absqcow2, VIR_STORAGE_FILE_QCOW2,
+               (&qcow2, &nbd2), EXP_PASS,
+               (&qcow2, &nbd2), ALLOW_PROBE | EXP_PASS);
+
     /* qed file */
     testFileData qed = {
         .expBackingStoreRaw = absraw,
-- 
2.3.0