Blob Blame History Raw
From da15d67c5cf0b6c0c1974d8ca251dbeec3d6a964 Mon Sep 17 00:00:00 2001
Message-Id: <da15d67c5cf0b6c0c1974d8ca251dbeec3d6a964@dist-git>
From: Paolo Bonzini <pbonzini@redhat.com>
Date: Tue, 12 Dec 2017 16:23:40 +0100
Subject: [PATCH] util: add virFileReadHeaderQuiet wrapper around
 virFileReadHeaderFD

CVE-2017-5715

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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 62ebb8180e..0c972d1861 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1646,6 +1646,7 @@ virFileReadAll;
 virFileReadAllQuiet;
 virFileReadBufQuiet;
 virFileReadHeaderFD;
+virFileReadHeaderQuiet;
 virFileReadLimFD;
 virFileReadLink;
 virFileReadValueBitmap;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index a91c2c3495..51d1f5ce66 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1356,6 +1356,25 @@ virFileReadHeaderFD(int fd, int maxlen, char **buf)
 }
 
 
+int
+virFileReadHeaderQuiet(const char *path,
+                       int maxlen,
+                       char **buf)
+{
+    int fd;
+    int len;
+
+    fd = open(path, O_RDONLY);
+    if (fd < 0)
+        return -1;
+
+    len = virFileReadHeaderFD(fd, maxlen, buf);
+    VIR_FORCE_CLOSE(fd);
+
+    return len;
+}
+
+
 /* A wrapper around saferead_lim that maps a failure due to
    exceeding the maximum size limitation to EOVERFLOW.  */
 int
diff --git a/src/util/virfile.h b/src/util/virfile.h
index ba1c57c06a..38cc23ecaa 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -129,6 +129,8 @@ int virFileDeleteTree(const char *dir);
 
 int virFileReadHeaderFD(int fd, int maxlen, char **buf)
     ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(3);
+int virFileReadHeaderQuiet(const char *path, int maxlen, char **buf)
+    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
 int virFileReadLimFD(int fd, int maxlen, char **buf)
     ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(3);
 int virFileReadAll(const char *path, int maxlen, char **buf)
-- 
2.15.1