Blob Blame History Raw
From 8a557289017d7549bebf00164214bf377761dabc Mon Sep 17 00:00:00 2001
Message-Id: <8a557289017d7549bebf00164214bf377761dabc@dist-git>
From: "Daniel P. Berrange" <berrange@redhat.com>
Date: Tue, 18 Feb 2014 15:45:37 -0700
Subject: [PATCH] Add virFileMakeParentPath helper function

https://bugzilla.redhat.com/show_bug.cgi?id=1045643
prereq of CVE-2013-6456

Add a helper function which takes a file path and ensures
that all directory components leading up to the file exist.
IOW, it strips the filename part of the path and passes
the result to virFileMakePath.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
(cherry picked from commit c321bfc5c37c603af349dacf531bb03c91b0755e)
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virfile.c       | 29 +++++++++++++++++++++++++++++
 src/util/virfile.h       |  1 +
 3 files changed, 31 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 852affa..e5f80e1 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1393,6 +1393,7 @@ virFileIsMountPoint;
 virFileLinkPointsTo;
 virFileLock;
 virFileLoopDeviceAssociate;
+virFileMakeParentPath;
 virFileMakePath;
 virFileMakePathWithMode;
 virFileMatchesNameSuffix;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 0d4a6be..defcc3f 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -2262,6 +2262,35 @@ cleanup:
     return ret;
 }
 
+
+int
+virFileMakeParentPath(const char *path)
+{
+    char *p;
+    char *tmp;
+    int ret = -1;
+
+    VIR_DEBUG("path=%s", path);
+
+    if (VIR_STRDUP(tmp, path) < 0) {
+        errno = ENOMEM;
+        return -1;
+    }
+
+    if ((p = strrchr(tmp, '/')) == NULL) {
+        errno = EINVAL;
+        goto cleanup;
+    }
+    *p = '\0';
+
+    ret = virFileMakePathHelper(tmp, 0777);
+
+ cleanup:
+    VIR_FREE(tmp);
+    return ret;
+}
+
+
 /* Build up a fully qualified path for a config file to be
  * associated with a persistent guest or network */
 char *
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 0d20cdb..20baf6f 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -198,6 +198,7 @@ int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid,
 int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK;
 int virFileMakePathWithMode(const char *path,
                             mode_t mode) ATTRIBUTE_RETURN_CHECK;
+int virFileMakeParentPath(const char *path) ATTRIBUTE_RETURN_CHECK;
 
 char *virFileBuildPath(const char *dir,
                        const char *name,
-- 
1.9.0