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

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