alexk / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
00810b
commit 8f4b3c3cab8922a2022b9e47c71f1ecf906077ef
00810b
Author: Demi Marie Obenour <athena@invisiblethingslab.com>
00810b
Date:   Mon Feb 8 16:05:01 2021 -0500
00810b
00810b
    hdrblobInit() needs bounds checks too
00810b
    
00810b
    Users can pass untrusted data to hdrblobInit() and it must be robust
00810b
    against this.
00810b
00810b
diff --git a/lib/header.c b/lib/header.c
00810b
index ea39e679f..ebba9c2b0 100644
00810b
--- a/lib/header.c
00810b
+++ b/lib/header.c
00810b
@@ -11,6 +11,7 @@
00810b
 #include "system.h"
00810b
 #include <netdb.h>
00810b
 #include <errno.h>
00810b
+#include <inttypes.h>
00810b
 #include <rpm/rpmtypes.h>
00810b
 #include <rpm/rpmstring.h>
00810b
 #include "lib/header_internal.h"
00810b
@@ -1912,6 +1913,25 @@ hdrblob hdrblobFree(hdrblob blob)
00810b
     return NULL;
00810b
 }
00810b
 
00810b
+static rpmRC hdrblobVerifyLengths(rpmTagVal regionTag, uint32_t il, uint32_t dl,
00810b
+				  char **emsg) {
00810b
+    uint32_t il_max = HEADER_TAGS_MAX;
00810b
+    uint32_t dl_max = HEADER_DATA_MAX;
00810b
+    if (regionTag == RPMTAG_HEADERSIGNATURES) {
00810b
+	il_max = 32;
00810b
+	dl_max = 64 * 1024 * 1024;
00810b
+    }
00810b
+    if (hdrchkRange(il_max, il)) {
00810b
+	rasprintf(emsg, _("hdr tags: BAD, no. of tags(%" PRIu32 ") out of range"), il);
00810b
+	return RPMRC_FAIL;
00810b
+    }
00810b
+    if (hdrchkRange(dl_max, dl)) {
00810b
+	rasprintf(emsg, _("hdr data: BAD, no. of bytes(%" PRIu32 ") out of range"), dl);
00810b
+	return RPMRC_FAIL;
00810b
+    }
00810b
+    return RPMRC_OK;
00810b
+}
00810b
+
00810b
 rpmRC hdrblobRead(FD_t fd, int magic, int exact_size, rpmTagVal regionTag, hdrblob blob, char **emsg)
00810b
 {
00810b
     int32_t block[4];
00810b
@@ -1924,13 +1944,6 @@ rpmRC hdrblobRead(FD_t fd, int magic, int exact_size, rpmTagVal regionTag, hdrbl
00810b
     size_t nb;
00810b
     rpmRC rc = RPMRC_FAIL;		/* assume failure */
00810b
     int xx;
00810b
-    int32_t il_max = HEADER_TAGS_MAX;
00810b
-    int32_t dl_max = HEADER_DATA_MAX;
00810b
-
00810b
-    if (regionTag == RPMTAG_HEADERSIGNATURES) {
00810b
-	il_max = 32;
00810b
-	dl_max = 64 * 1024 * 1024;
00810b
-    }
00810b
 
00810b
     memset(block, 0, sizeof(block));
00810b
     if ((xx = Freadall(fd, bs, blen)) != blen) {
00810b
@@ -1943,15 +1956,9 @@ rpmRC hdrblobRead(FD_t fd, int magic, int exact_size, rpmTagVal regionTag, hdrbl
00810b
 	goto exit;
00810b
     }
00810b
     il = ntohl(block[2]);
00810b
-    if (hdrchkRange(il_max, il)) {
00810b
-	rasprintf(emsg, _("hdr tags: BAD, no. of tags(%d) out of range"), il);
00810b
-	goto exit;
00810b
-    }
00810b
     dl = ntohl(block[3]);
00810b
-    if (hdrchkRange(dl_max, dl)) {
00810b
-	rasprintf(emsg, _("hdr data: BAD, no. of bytes(%d) out of range"), dl);
00810b
+    if (hdrblobVerifyLengths(regionTag, il, dl, emsg))
00810b
 	goto exit;
00810b
-    }
00810b
 
00810b
     nb = (il * sizeof(struct entryInfo_s)) + dl;
00810b
     uc = sizeof(il) + sizeof(dl) + nb;
00810b
@@ -1995,11 +2002,18 @@ rpmRC hdrblobInit(const void *uh, size_t uc,
00810b
 		struct hdrblob_s *blob, char **emsg)
00810b
 {
00810b
     rpmRC rc = RPMRC_FAIL;
00810b
-
00810b
     memset(blob, 0, sizeof(*blob));
00810b
+    if (uc && uc < 8) {
00810b
+	rasprintf(emsg, _("hdr length: BAD"));
00810b
+	goto exit;
00810b
+    }
00810b
+
00810b
     blob->ei = (int32_t *) uh; /* discards const */
00810b
-    blob->il = ntohl(blob->ei[0]);
00810b
-    blob->dl = ntohl(blob->ei[1]);
00810b
+    blob->il = ntohl((uint32_t)(blob->ei[0]));
00810b
+    blob->dl = ntohl((uint32_t)(blob->ei[1]));
00810b
+    if (hdrblobVerifyLengths(regionTag, blob->il, blob->dl, emsg) != RPMRC_OK)
00810b
+	goto exit;
00810b
+
00810b
     blob->pe = (entryInfo) &(blob->ei[2]);
00810b
     blob->pvlen = sizeof(blob->il) + sizeof(blob->dl) +
00810b
 		  (blob->il * sizeof(*blob->pe)) + blob->dl;