Blame SOURCES/0007-Try-to-convince-covscan-that-sysfs_read_file-doesn-t.patch

529d1b
From 5e2174acaf1a51ead0a079776229e0df89c7fd81 Mon Sep 17 00:00:00 2001
529d1b
From: Peter Jones <pjones@redhat.com>
529d1b
Date: Wed, 13 Jun 2018 09:15:17 -0400
529d1b
Subject: [PATCH 07/17] Try to convince covscan that sysfs_read_file() doesn't
529d1b
 leak on error.
529d1b
529d1b
Basically, covscan gets confused about some of our return paths and
529d1b
doesn't  think the error condition correlates with not having allocated
529d1b
(or having freed) the ram we're using to pass the file data back.
529d1b
529d1b
Signed-off-by: Peter Jones <pjones@redhat.com>
529d1b
---
529d1b
 src/linux.h |  5 +++++
529d1b
 src/util.h  | 38 ++++++++++++++++++++------------------
529d1b
 2 files changed, 25 insertions(+), 18 deletions(-)
529d1b
529d1b
diff --git a/src/linux.h b/src/linux.h
529d1b
index 2f9eb0fe66f..39826224a53 100644
529d1b
--- a/src/linux.h
529d1b
+++ b/src/linux.h
529d1b
@@ -173,6 +173,11 @@ extern ssize_t HIDDEN make_mac_path(uint8_t *buf, ssize_t size,
529d1b
                         free(buf_);                                     \
529d1b
                         *(buf) = (__typeof__(*(buf)))buf2_;             \
529d1b
                         errno = error_;                                 \
529d1b
+                } else if (buf_) {                                      \
529d1b
+                        /* covscan is _sure_ we leak buf_ if bufsize_ */\
529d1b
+                        /* is <= 0, which is wrong, but appease it.   */\
529d1b
+                        free(buf_);                                     \
529d1b
+                        buf_ = NULL;                                    \
529d1b
                 }                                                       \
529d1b
                 bufsize_;                                               \
529d1b
         })
529d1b
diff --git a/src/util.h b/src/util.h
529d1b
index cc5f669e6ec..ef85a4c277e 100644
529d1b
--- a/src/util.h
529d1b
+++ b/src/util.h
529d1b
@@ -149,22 +149,24 @@
529d1b
 #endif
529d1b
 
529d1b
 static inline int UNUSED
529d1b
-read_file(int fd, uint8_t **buf, size_t *bufsize)
529d1b
+read_file(int fd, uint8_t **result, size_t *bufsize)
529d1b
 {
529d1b
         uint8_t *p;
529d1b
         size_t size = 4096;
529d1b
         size_t filesize = 0;
529d1b
         ssize_t s = 0;
529d1b
+        uint8_t *buf, *newbuf;
529d1b
 
529d1b
-        uint8_t *newbuf;
529d1b
         if (!(newbuf = calloc(size, sizeof (uint8_t)))) {
529d1b
                 efi_error("could not allocate memory");
529d1b
+                *result = buf = NULL;
529d1b
+                *bufsize = 0;
529d1b
                 return -1;
529d1b
         }
529d1b
-        *buf = newbuf;
529d1b
+        buf = newbuf;
529d1b
 
529d1b
         do {
529d1b
-                p = *buf + filesize;
529d1b
+                p = buf + filesize;
529d1b
                 /* size - filesize shouldn't exceed SSIZE_MAX because we're
529d1b
                  * only allocating 4096 bytes at a time and we're checking that
529d1b
                  * before doing so. */
529d1b
@@ -179,8 +181,8 @@ read_file(int fd, uint8_t **buf, size_t *bufsize)
529d1b
                         continue;
529d1b
                 } else if (s < 0) {
529d1b
                         int saved_errno = errno;
529d1b
-                        free(*buf);
529d1b
-                        *buf = NULL;
529d1b
+                        free(buf);
529d1b
+                        *result = buf = NULL;
529d1b
                         *bufsize = 0;
529d1b
                         errno = saved_errno;
529d1b
                         efi_error("could not read from file");
529d1b
@@ -194,38 +196,38 @@ read_file(int fd, uint8_t **buf, size_t *bufsize)
529d1b
                         /* See if we're going to overrun and return an error
529d1b
                          * instead. */
529d1b
                         if (size > (size_t)-1 - 4096) {
529d1b
-                                free(*buf);
529d1b
-                                *buf = NULL;
529d1b
+                                free(buf);
529d1b
+                                *result = buf = NULL;
529d1b
                                 *bufsize = 0;
529d1b
                                 errno = ENOMEM;
529d1b
                                 efi_error("could not read from file");
529d1b
                                 return -1;
529d1b
                         }
529d1b
-                        newbuf = realloc(*buf, size + 4096);
529d1b
+                        newbuf = realloc(buf, size + 4096);
529d1b
                         if (newbuf == NULL) {
529d1b
                                 int saved_errno = errno;
529d1b
-                                free(*buf);
529d1b
-                                *buf = NULL;
529d1b
+                                free(buf);
529d1b
+                                *result = buf = NULL;
529d1b
                                 *bufsize = 0;
529d1b
                                 errno = saved_errno;
529d1b
                                 efi_error("could not allocate memory");
529d1b
                                 return -1;
529d1b
                         }
529d1b
-                        *buf = newbuf;
529d1b
-                        memset(*buf + size, '\0', 4096);
529d1b
+                        buf = newbuf;
529d1b
+                        memset(buf + size, '\0', 4096);
529d1b
                         size += 4096;
529d1b
                 }
529d1b
         } while (1);
529d1b
 
529d1b
-        newbuf = realloc(*buf, filesize+1);
529d1b
+        newbuf = realloc(buf, filesize+1);
529d1b
         if (!newbuf) {
529d1b
-                free(*buf);
529d1b
-                *buf = NULL;
529d1b
+                free(buf);
529d1b
+                *result = buf = NULL;
529d1b
                 efi_error("could not allocate memory");
529d1b
                 return -1;
529d1b
         }
529d1b
         newbuf[filesize] = '\0';
529d1b
-        *buf = newbuf;
529d1b
+        *result = newbuf;
529d1b
         *bufsize = filesize+1;
529d1b
         return 0;
529d1b
 }
529d1b
@@ -329,7 +331,7 @@ get_file(uint8_t **result, const char * const fmt, ...)
529d1b
         close(fd);
529d1b
         errno = error;
529d1b
 
529d1b
-        if (rc < 0) {
529d1b
+        if (rc < 0 || bufsize < 1) {
529d1b
                 efi_error("could not read file \"%s\"", path);
529d1b
                 return -1;
529d1b
         }
529d1b
-- 
529d1b
2.17.1
529d1b