File libvirt-util-new-function-virFileLength.patch of Package libvirt.7069

From b14cc816610b4389cf79c5a74ddef228ad4a4afc Mon Sep 17 00:00:00 2001
Message-Id: <b14cc816610b4389cf79c5a74ddef228ad4a4afc@dist-git>
From: Laine Stump <laine@laine.org>
Date: Thu, 17 Nov 2016 12:18:27 -0500
Subject: [PATCH] util: new function virFileLength()

This new function just calls fstat() (if provided with a valid fd) or
stat() (if fd is -1) and returns st_size (or -1 if there is an
error). We may decide we want this function to be more complex, and
handle things like block devices - this is a placeholder (that works)
for any more complicated function.

(cherry picked from commit e026563f0136a04b177e3627cec3afab233da690)

CVE-2017-5715

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virfile.c       | 34 ++++++++++++++++++++++++++++++++++
 src/util/virfile.h       |  1 +
 3 files changed, 36 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 554b24f287..96a948f8ce 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1547,6 +1547,7 @@ virFileIsLink;
 virFileIsMountPoint;
 virFileIsSharedFS;
 virFileIsSharedFSType;
+virFileLength;
 virFileLinkPointsTo;
 virFileLock;
 virFileLoopDeviceAssociate;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 71e542bc39..f348f24f2a 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1754,6 +1754,40 @@ virFileActivateDirOverride(const char *argv0)
     }
 }
 
+
+/**
+ * virFileLength:
+ * @path: full path of the file
+ * @fd: open file descriptor for file (or -1 to use @path)
+ *
+ * If fd >= 0, return the length of the open file indicated by @fd.
+ * If fd < 0 (i.e. -1) return the length of the file indicated by
+ * @path.
+ *
+ * Returns the length, or -1 if the file doesn't
+ * exist or its info was inaccessible. No error is logged.
+ */
+off_t
+virFileLength(const char *path, int fd)
+{
+    struct stat s;
+
+    if (fd >= 0) {
+        if (fstat(fd, &s) < 0)
+            return -1;
+    } else {
+        if (stat(path, &s) < 0)
+            return -1;
+    }
+
+    if (!S_ISREG(s.st_mode))
+       return -1;
+
+    return s.st_size;
+
+}
+
+
 bool
 virFileIsDir(const char *path)
 {
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 79d6bb20b0..c25f9204ec 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -181,6 +181,7 @@ char *virFileFindResourceFull(const char *filename,
 void virFileActivateDirOverride(const char *argv0)
     ATTRIBUTE_NONNULL(1);
 
+off_t virFileLength(const char *path, int fd) ATTRIBUTE_NONNULL(1);
 bool virFileIsDir (const char *file) ATTRIBUTE_NONNULL(1);
 bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1);
 bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1);
-- 
2.15.1

openSUSE Build Service is sponsored by