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

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