Blame SOURCES/procps-ng-3.3.10-CVE-2018-1124.patch

430826
From 9d2ec74b76d220f5343e548fcb7058d723293a22 Mon Sep 17 00:00:00 2001
430826
From: Qualys Security Advisory <qsa@qualys.com>
430826
Date: Thu, 1 Jan 1970 00:00:00 +0000
430826
Subject: [PATCH 1/3] proc/alloc.*: Use size_t, not unsigned int.
430826
430826
Otherwise this can truncate sizes on 64-bit platforms, and is one of the
430826
reasons the integer overflows in file2strvec() are exploitable at all.
430826
Also: catch potential integer overflow in xstrdup() (should never
430826
happen, but better safe than sorry), and use memcpy() instead of
430826
strcpy() (faster).
430826
430826
Warnings:
430826
430826
- in glibc, realloc(ptr, 0) is equivalent to free(ptr), but not here,
430826
  because of the ++size;
430826
430826
- here, xstrdup() can return NULL (if str is NULL), which goes against
430826
  the idea of the xalloc wrappers.
430826
430826
We were tempted to call exit() or xerrx() in those cases, but decided
430826
against it, because it might break things in unexpected places; TODO?
430826
---
430826
 proc/alloc.c | 20 ++++++++++++--------
430826
 proc/alloc.h |  4 ++--
430826
 2 files changed, 14 insertions(+), 10 deletions(-)
430826
430826
diff --git a/proc/alloc.c b/proc/alloc.c
430826
index 94af47f..1768d73 100644
430826
--- a/proc/alloc.c
430826
+++ b/proc/alloc.c
430826
@@ -37,14 +37,14 @@ static void xdefault_error(const char *restrict fmts, ...) {
430826
 message_fn xalloc_err_handler = xdefault_error;
430826
 
430826
 
430826
-void *xcalloc(unsigned int size) {
430826
+void *xcalloc(size_t size) {
430826
     void * p;
430826
 
430826
     if (size == 0)
430826
         ++size;
430826
     p = calloc(1, size);
430826
     if (!p) {
430826
-        xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
430826
+        xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
430826
         exit(EXIT_FAILURE);
430826
     }
430826
     return p;
430826
@@ -57,20 +57,20 @@ void *xmalloc(size_t size) {
430826
         ++size;
430826
     p = malloc(size);
430826
     if (!p) {
430826
-	xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
430826
+        xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
430826
         exit(EXIT_FAILURE);
430826
     }
430826
     return(p);
430826
 }
430826
 
430826
-void *xrealloc(void *oldp, unsigned int size) {
430826
+void *xrealloc(void *oldp, size_t size) {
430826
     void *p;
430826
 
430826
     if (size == 0)
430826
         ++size;
430826
     p = realloc(oldp, size);
430826
     if (!p) {
430826
-        xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
430826
+        xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
430826
         exit(EXIT_FAILURE);
430826
     }
430826
     return(p);
430826
@@ -80,13 +80,17 @@ char *xstrdup(const char *str) {
430826
     char *p = NULL;
430826
 
430826
     if (str) {
430826
-        unsigned int size = strlen(str) + 1;
430826
+        size_t size = strlen(str) + 1;
430826
+        if (size < 1) {
430826
+            xalloc_err_handler("%s refused to allocate %zu bytes of memory", __func__, size);
430826
+            exit(EXIT_FAILURE);
430826
+        }
430826
         p = malloc(size);
430826
         if (!p) {
430826
-            xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
430826
+            xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
430826
             exit(EXIT_FAILURE);
430826
         }
430826
-        strcpy(p, str);
430826
+        memcpy(p, str, size);
430826
     }
430826
     return(p);
430826
 }
430826
diff --git a/proc/alloc.h b/proc/alloc.h
430826
index 19c91d7..6787a72 100644
430826
--- a/proc/alloc.h
430826
+++ b/proc/alloc.h
430826
@@ -8,9 +8,9 @@ EXTERN_C_BEGIN
430826
  /* change xalloc_err_handler to override the default fprintf(stderr... */
430826
 extern message_fn xalloc_err_handler;
430826
 
430826
-extern void *xcalloc(unsigned int size) MALLOC;
430826
+extern void *xcalloc(size_t size) MALLOC;
430826
 extern void *xmalloc(size_t size) MALLOC;
430826
-extern void *xrealloc(void *oldp, unsigned int size) MALLOC;
430826
+extern void *xrealloc(void *oldp, size_t size) MALLOC;
430826
 extern char *xstrdup(const char *str) MALLOC;
430826
 
430826
 EXTERN_C_END
430826
-- 
430826
2.14.3
430826
430826
430826
From de660b14b80188d9b323c4999d1b91a9456ed687 Mon Sep 17 00:00:00 2001
430826
From: Qualys Security Advisory <qsa@qualys.com>
430826
Date: Thu, 1 Jan 1970 00:00:00 +0000
430826
Subject: [PATCH 2/3] proc/readproc.c: Harden file2str().
430826
430826
1/ Replace sprintf() with snprintf() (and check for truncation).
430826
430826
2/ Prevent an integer overflow of ub->siz. The "tot_read--" is needed to
430826
avoid an off-by-one overflow in "ub->buf[tot_read] = '\0'". It is safe
430826
to decrement tot_read here, because we know that tot_read is equal to
430826
ub->siz (and ub->siz is very large).
430826
430826
We believe that truncation is a better option than failure (implementing
430826
failure instead should be as easy as replacing the "tot_read--" with
430826
"tot_read = 0").
430826
---
430826
 proc/readproc.c | 10 ++++++++--
430826
 1 file changed, 8 insertions(+), 2 deletions(-)
430826
430826
diff --git a/proc/readproc.c b/proc/readproc.c
430826
index 9e3afc9..39235c7 100644
430826
--- a/proc/readproc.c
430826
+++ b/proc/readproc.c
430826
@@ -35,6 +35,7 @@
430826
 #include <signal.h>
430826
 #include <fcntl.h>
430826
 #include <dirent.h>
430826
+#include <limits.h>
430826
 #include <sys/types.h>
430826
 #include <sys/stat.h>
430826
 #ifdef WITH_SYSTEMD
430826
@@ -622,7 +623,7 @@ static void statm2proc(const char* s, proc_t *restrict P) {
430826
 static int file2str(const char *directory, const char *what, struct utlbuf_s *ub) {
430826
  #define buffGRW 1024
430826
     char path[PROCPATHLEN];
430826
-    int fd, num, tot_read = 0;
430826
+    int fd, num, tot_read = 0, len;
430826
 
430826
     /* on first use we preallocate a buffer of minimum size to emulate
430826
        former 'local static' behavior -- even if this read fails, that
430826
@@ -630,11 +631,16 @@ static int file2str(const char *directory, const char *what, struct utlbuf_s *ub
430826
        ( besides, with this xcalloc we will never need to use memcpy ) */
430826
     if (ub->buf) ub->buf[0] = '\0';
430826
     else ub->buf = xcalloc((ub->siz = buffGRW));
430826
-    sprintf(path, "%s/%s", directory, what);
430826
+    len = snprintf(path, sizeof path, "%s/%s", directory, what);
430826
+    if (len <= 0 || (size_t)len >= sizeof path) return -1;
430826
     if (-1 == (fd = open(path, O_RDONLY, 0))) return -1;
430826
     while (0 < (num = read(fd, ub->buf + tot_read, ub->siz - tot_read))) {
430826
         tot_read += num;
430826
         if (tot_read < ub->siz) break;
430826
+        if (ub->siz >= INT_MAX - buffGRW) {
430826
+            tot_read--;
430826
+            break;
430826
+        }
430826
         ub->buf = xrealloc(ub->buf, (ub->siz += buffGRW));
430826
     };
430826
     ub->buf[tot_read] = '\0';
430826
-- 
430826
2.14.3
430826
430826
430826
From 44a4b658f45bc3fbd7170662a52038a7b35c83de Mon Sep 17 00:00:00 2001
430826
From: Qualys Security Advisory <qsa@qualys.com>
430826
Date: Thu, 1 Jan 1970 00:00:00 +0000
430826
Subject: [PATCH 3/3] proc/readproc.c: Fix bugs and overflows in file2strvec().
430826
430826
Note: this is by far the most important and complex patch of the whole
430826
series, please review it carefully; thank you very much!
430826
430826
For this patch, we decided to keep the original function's design and
430826
skeleton, to avoid regressions and behavior changes, while fixing the
430826
various bugs and overflows. And like the "Harden file2str()" patch, this
430826
patch does not fail when about to overflow, but truncates instead: there
430826
is information available about this process, so return it to the caller;
430826
also, we used INT_MAX as a limit, but a lower limit could be used.
430826
430826
The easy changes:
430826
430826
- Replace sprintf() with snprintf() (and check for truncation).
430826
430826
- Replace "if (n == 0 && rbuf == 0)" with "if (n <= 0 && tot <= 0)" and
430826
  do break instead of return: it simplifies the code (only one place to
430826
  handle errors), and also guarantees that in the while loop either n or
430826
  tot is > 0 (or both), even if n is reset to 0 when about to overflow.
430826
430826
- Remove the "if (n < 0)" block in the while loop: it is (and was) dead
430826
  code, since we enter the while loop only if n >= 0.
430826
430826
- Rewrite the missing-null-terminator detection: in the original
430826
  function, if the size of the file is a multiple of 2047, a null-
430826
  terminator is appended even if the file is already null-terminated.
430826
430826
- Replace "if (n <= 0 && !end_of_file)" with "if (n < 0 || tot <= 0)":
430826
  originally, it was equivalent to "if (n < 0)", but we added "tot <= 0"
430826
  to handle the first break of the while loop, and to guarantee that in
430826
  the rest of the function tot is > 0.
430826
430826
- Double-force ("belt and suspenders") the null-termination of rbuf:
430826
  this is (and was) essential to the correctness of the function.
430826
430826
- Replace the final "while" loop with a "for" loop that behaves just
430826
  like the preceding "for" loop: in the original function, this would
430826
  lead to unexpected results (for example, if rbuf is |\0|A|\0|, this
430826
  would return the array {"",NULL} but should return {"","A",NULL}; and
430826
  if rbuf is |A|\0|B| (should never happen because rbuf should be null-
430826
  terminated), this would make room for two pointers in ret, but would
430826
  write three pointers to ret).
430826
430826
The hard changes:
430826
430826
- Prevent the integer overflow of tot in the while loop, but unlike
430826
  file2str(), file2strvec() cannot let tot grow until it almost reaches
430826
  INT_MAX, because it needs more space for the pointers: this is why we
430826
  introduced ARG_LEN, which also guarantees that we can add "align" and
430826
  a few sizeof(char*)s to tot without overflowing.
430826
430826
- Prevent the integer overflow of "tot + c + align": when INT_MAX is
430826
  (almost) reached, we write the maximal safe amount of pointers to ret
430826
  (ARG_LEN guarantees that there is always space for *ret = rbuf and the
430826
  NULL terminator).
430826
---
430826
 proc/readproc.c | 53 ++++++++++++++++++++++++++++++++---------------------
430826
 1 file changed, 32 insertions(+), 21 deletions(-)
430826
430826
diff --git a/proc/readproc.c b/proc/readproc.c
430826
index 39235c7..94ca4e9 100644
430826
--- a/proc/readproc.c
430826
+++ b/proc/readproc.c
430826
@@ -652,11 +652,12 @@ static int file2str(const char *directory, const char *what, struct utlbuf_s *ub
430826
 
430826
 static char** file2strvec(const char* directory, const char* what) {
430826
     char buf[2048];	/* read buf bytes at a time */
430826
-    char *p, *rbuf = 0, *endbuf, **q, **ret;
430826
+    char *p, *rbuf = 0, *endbuf, **q, **ret, *strp;
430826
     int fd, tot = 0, n, c, end_of_file = 0;
430826
     int align;
430826
 
430826
-    sprintf(buf, "%s/%s", directory, what);
430826
+    const int len = snprintf(buf, sizeof buf, "%s/%s", directory, what);
430826
+    if(len <= 0 || (size_t)len >= sizeof buf) return NULL;
430826
     fd = open(buf, O_RDONLY, 0);
430826
     if(fd==-1) return NULL;
430826
 
430826
@@ -664,18 +665,23 @@ static char** file2strvec(const char* directory, const char* what) {
430826
     while ((n = read(fd, buf, sizeof buf - 1)) >= 0) {
430826
 	if (n < (int)(sizeof buf - 1))
430826
 	    end_of_file = 1;
430826
-	if (n == 0 && rbuf == 0) {
430826
-	    close(fd);
430826
-	    return NULL;	/* process died between our open and read */
430826
+	if (n <= 0 && tot <= 0) { /* nothing read now, nothing read before */
430826
+	    break;		/* process died between our open and read */
430826
 	}
430826
-	if (n < 0) {
430826
-	    if (rbuf)
430826
-		free(rbuf);
430826
-	    close(fd);
430826
-	    return NULL;	/* read error */
430826
+	/* ARG_LEN is our guesstimated median length of a command-line argument
430826
+	   or environment variable (the minimum is 1, the maximum is 131072) */
430826
+	#define ARG_LEN 64
430826
+	if (tot >= INT_MAX / (ARG_LEN + (int)sizeof(char*)) * ARG_LEN - n) {
430826
+	    end_of_file = 1; /* integer overflow: null-terminate and break */
430826
+	    n = 0; /* but tot > 0 */
430826
 	}
430826
-	if (end_of_file && (n == 0 || buf[n-1]))/* last read char not null */
430826
+	#undef ARG_LEN
430826
+	if (end_of_file &&
430826
+	    ((n > 0 && buf[n-1] != '\0') ||	/* last read char not null */
430826
+	     (n <= 0 && rbuf[tot-1] != '\0')))	/* last read char not null */
430826
 	    buf[n++] = '\0';			/* so append null-terminator */
430826
+
430826
+	if (n <= 0) break; /* unneeded (end_of_file = 1) but avoid realloc */
430826
 	rbuf = xrealloc(rbuf, tot + n);		/* allocate more memory */
430826
 	memcpy(rbuf + tot, buf, n);		/* copy buffer into it */
430826
 	tot += n;				/* increment total byte ctr */
430826
@@ -683,29 +689,34 @@ static char** file2strvec(const char* directory, const char* what) {
430826
 	    break;
430826
     }
430826
     close(fd);
430826
-    if (n <= 0 && !end_of_file) {
430826
+    if (n < 0 || tot <= 0) {	/* error, or nothing read */
430826
 	if (rbuf) free(rbuf);
430826
 	return NULL;		/* read error */
430826
     }
430826
+    rbuf[tot-1] = '\0'; /* belt and suspenders (the while loop did it, too) */
430826
     endbuf = rbuf + tot;			/* count space for pointers */
430826
     align = (sizeof(char*)-1) - ((tot + sizeof(char*)-1) & (sizeof(char*)-1));
430826
-    for (c = 0, p = rbuf; p < endbuf; p++) {
430826
-	if (!*p || *p == '\n')
430826
+    c = sizeof(char*);				/* one extra for NULL term */
430826
+    for (p = rbuf; p < endbuf; p++) {
430826
+	if (!*p || *p == '\n') {
430826
+	    if (c >= INT_MAX - (tot + (int)sizeof(char*) + align)) break;
430826
 	    c += sizeof(char*);
430826
+	}
430826
 	if (*p == '\n')
430826
 	    *p = 0;
430826
     }
430826
-    c += sizeof(char*);				/* one extra for NULL term */
430826
 
430826
     rbuf = xrealloc(rbuf, tot + c + align);	/* make room for ptrs AT END */
430826
     endbuf = rbuf + tot;			/* addr just past data buf */
430826
     q = ret = (char**) (endbuf+align);		/* ==> free(*ret) to dealloc */
430826
-    *q++ = p = rbuf;				/* point ptrs to the strings */
430826
-    endbuf--;					/* do not traverse final NUL */
430826
-    while (++p < endbuf)
430826
-    	if (!*p)				/* NUL char implies that */
430826
-	    *q++ = p+1;				/* next string -> next char */
430826
-
430826
+    for (strp = p = rbuf; p < endbuf; p++) {
430826
+	if (!*p) {				/* NUL char implies that */
430826
+	    if (c < 2 * (int)sizeof(char*)) break;
430826
+	    c -= sizeof(char*);
430826
+	    *q++ = strp;			/* point ptrs to the strings */
430826
+	    strp = p+1;				/* next string -> next char */
430826
+	}
430826
+    }
430826
     *q = 0;					/* null ptr list terminator */
430826
     return ret;
430826
 }
430826
-- 
430826
2.14.3
430826