Blame SOURCES/unzip-zipbomb-part2.patch

9ae17e
From 47b3ceae397d21bf822bc2ac73052a4b1daf8e1c Mon Sep 17 00:00:00 2001
9ae17e
From: Mark Adler <madler@alumni.caltech.edu>
9ae17e
Date: Tue, 11 Jun 2019 22:01:18 -0700
9ae17e
Subject: [PATCH] Detect and reject a zip bomb using overlapped entries.
9ae17e
9ae17e
This detects an invalid zip file that has at least one entry that
9ae17e
overlaps with another entry or with the central directory to the
9ae17e
end of the file. A Fifield zip bomb uses overlapped local entries
9ae17e
to vastly increase the potential inflation ratio. Such an invalid
9ae17e
zip file is rejected.
9ae17e
9ae17e
See https://www.bamsoftware.com/hacks/zipbomb/ for David Fifield's
9ae17e
analysis, construction, and examples of such zip bombs.
9ae17e
9ae17e
The detection maintains a list of covered spans of the zip files
9ae17e
so far, where the central directory to the end of the file and any
9ae17e
bytes preceding the first entry at zip file offset zero are
9ae17e
considered covered initially. Then as each entry is decompressed
9ae17e
or tested, it is considered covered. When a new entry is about to
9ae17e
be processed, its initial offset is checked to see if it is
9ae17e
contained by a covered span. If so, the zip file is rejected as
9ae17e
invalid.
9ae17e
9ae17e
This commit depends on a preceding commit: "Fix bug in
9ae17e
undefer_input() that misplaced the input state."
9ae17e
---
9ae17e
 extract.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
9ae17e
 globals.c |   1 +
9ae17e
 globals.h |   3 +
9ae17e
 process.c |  11 ++++
9ae17e
 unzip.h   |   1 +
9ae17e
 5 files changed, 205 insertions(+), 1 deletion(-)
9ae17e
9ae17e
diff --git a/extract.c b/extract.c
9ae17e
index 1acd769..0973a33 100644
9ae17e
--- a/extract.c
9ae17e
+++ b/extract.c
9ae17e
@@ -319,6 +319,125 @@ static ZCONST char Far UnsupportedExtraField[] =
9ae17e
   "\nerror:  unsupported extra-field compression type (%u)--skipping\n";
9ae17e
 static ZCONST char Far BadExtraFieldCRC[] =
9ae17e
   "error [%s]:  bad extra-field CRC %08lx (should be %08lx)\n";
9ae17e
+static ZCONST char Far NotEnoughMemCover[] =
9ae17e
+  "error: not enough memory for bomb detection\n";
9ae17e
+static ZCONST char Far OverlappedComponents[] =
9ae17e
+  "error: invalid zip file with overlapped components (possible zip bomb)\n";
9ae17e
+
9ae17e
+
9ae17e
+
9ae17e
+
9ae17e
+
9ae17e
+/* A growable list of spans. */
9ae17e
+typedef zoff_t bound_t;
9ae17e
+typedef struct {
9ae17e
+    bound_t beg;        /* start of the span */
9ae17e
+    bound_t end;        /* one past the end of the span */
9ae17e
+} span_t;
9ae17e
+typedef struct {
9ae17e
+    span_t *span;       /* allocated, distinct, and sorted list of spans */
9ae17e
+    size_t num;         /* number of spans in the list */
9ae17e
+    size_t max;         /* allocated number of spans (num <= max) */
9ae17e
+} cover_t;
9ae17e
+
9ae17e
+/*
9ae17e
+ * Return the index of the first span in cover whose beg is greater than val.
9ae17e
+ * If there is no such span, then cover->num is returned.
9ae17e
+ */
9ae17e
+static size_t cover_find(cover, val)
9ae17e
+    cover_t *cover;
9ae17e
+    bound_t val;
9ae17e
+{
9ae17e
+    size_t lo = 0, hi = cover->num;
9ae17e
+    while (lo < hi) {
9ae17e
+        size_t mid = (lo + hi) >> 1;
9ae17e
+        if (val < cover->span[mid].beg)
9ae17e
+            hi = mid;
9ae17e
+        else
9ae17e
+            lo = mid + 1;
9ae17e
+    }
9ae17e
+    return hi;
9ae17e
+}
9ae17e
+
9ae17e
+/* Return true if val lies within any one of the spans in cover. */
9ae17e
+static int cover_within(cover, val)
9ae17e
+    cover_t *cover;
9ae17e
+    bound_t val;
9ae17e
+{
9ae17e
+    size_t pos = cover_find(cover, val);
9ae17e
+    return pos > 0 && val < cover->span[pos - 1].end;
9ae17e
+}
9ae17e
+
9ae17e
+/*
9ae17e
+ * Add a new span to the list, but only if the new span does not overlap any
9ae17e
+ * spans already in the list. The new span covers the values beg..end-1. beg
9ae17e
+ * must be less than end.
9ae17e
+ *
9ae17e
+ * Keep the list sorted and merge adjacent spans. Grow the allocated space for
9ae17e
+ * the list as needed. On success, 0 is returned. If the new span overlaps any
9ae17e
+ * existing spans, then 1 is returned and the new span is not added to the
9ae17e
+ * list. If the new span is invalid because beg is greater than or equal to
9ae17e
+ * end, then -1 is returned. If the list needs to be grown but the memory
9ae17e
+ * allocation fails, then -2 is returned.
9ae17e
+ */
9ae17e
+static int cover_add(cover, beg, end)
9ae17e
+    cover_t *cover;
9ae17e
+    bound_t beg;
9ae17e
+    bound_t end;
9ae17e
+{
9ae17e
+    size_t pos;
9ae17e
+    int prec, foll;
9ae17e
+
9ae17e
+    if (beg >= end)
9ae17e
+    /* The new span is invalid. */
9ae17e
+        return -1;
9ae17e
+
9ae17e
+    /* Find where the new span should go, and make sure that it does not
9ae17e
+       overlap with any existing spans. */
9ae17e
+    pos = cover_find(cover, beg);
9ae17e
+    if ((pos > 0 && beg < cover->span[pos - 1].end) ||
9ae17e
+        (pos < cover->num && end > cover->span[pos].beg))
9ae17e
+        return 1;
9ae17e
+
9ae17e
+    /* Check for adjacencies. */
9ae17e
+    prec = pos > 0 && beg == cover->span[pos - 1].end;
9ae17e
+    foll = pos < cover->num && end == cover->span[pos].beg;
9ae17e
+    if (prec && foll) {
9ae17e
+        /* The new span connects the preceding and following spans. Merge the
9ae17e
+           following span into the preceding span, and delete the following
9ae17e
+           span. */
9ae17e
+        cover->span[pos - 1].end = cover->span[pos].end;
9ae17e
+        cover->num--;
9ae17e
+        memmove(cover->span + pos, cover->span + pos + 1,
9ae17e
+                (cover->num - pos) * sizeof(span_t));
9ae17e
+    }
9ae17e
+    else if (prec)
9ae17e
+        /* The new span is adjacent only to the preceding span. Extend the end
9ae17e
+           of the preceding span. */
9ae17e
+        cover->span[pos - 1].end = end;
9ae17e
+    else if (foll)
9ae17e
+        /* The new span is adjacent only to the following span. Extend the
9ae17e
+           beginning of the following span. */
9ae17e
+        cover->span[pos].beg = beg;
9ae17e
+    else {
9ae17e
+        /* The new span has gaps between both the preceding and the following
9ae17e
+           spans. Assure that there is room and insert the span.  */
9ae17e
+        if (cover->num == cover->max) {
9ae17e
+            size_t max = cover->max == 0 ? 16 : cover->max << 1;
9ae17e
+            span_t *span = realloc(cover->span, max * sizeof(span_t));
9ae17e
+            if (span == NULL)
9ae17e
+                return -2;
9ae17e
+            cover->span = span;
9ae17e
+            cover->max = max;
9ae17e
+        }
9ae17e
+        memmove(cover->span + pos + 1, cover->span + pos,
9ae17e
+                (cover->num - pos) * sizeof(span_t));
9ae17e
+        cover->num++;
9ae17e
+        cover->span[pos].beg = beg;
9ae17e
+        cover->span[pos].end = end;
9ae17e
+    }
9ae17e
+    return 0;
9ae17e
+}
9ae17e
 
9ae17e
 
9ae17e
 
9ae17e
@@ -374,6 +493,29 @@ int extract_or_test_files(__G)    /* return PK-type error code */
9ae17e
     }
9ae17e
 #endif /* !SFX || SFX_EXDIR */
9ae17e
 
9ae17e
+    /* One more: initialize cover structure for bomb detection. Start with a
9ae17e
+       span that covers the central directory though the end of the file. */
9ae17e
+    if (G.cover == NULL) {
9ae17e
+        G.cover = malloc(sizeof(cover_t));
9ae17e
+        if (G.cover == NULL) {
9ae17e
+            Info(slide, 0x401, ((char *)slide,
9ae17e
+              LoadFarString(NotEnoughMemCover)));
9ae17e
+            return PK_MEM;
9ae17e
+        }
9ae17e
+        ((cover_t *)G.cover)->span = NULL;
9ae17e
+        ((cover_t *)G.cover)->max = 0;
9ae17e
+    }
9ae17e
+    ((cover_t *)G.cover)->num = 0;
9ae17e
+    if ((G.extra_bytes != 0 &&
9ae17e
+         cover_add((cover_t *)G.cover, 0, G.extra_bytes) != 0) ||
9ae17e
+        cover_add((cover_t *)G.cover,
9ae17e
+                  G.extra_bytes + G.ecrec.offset_start_central_directory,
9ae17e
+                  G.ziplen) != 0) {
9ae17e
+        Info(slide, 0x401, ((char *)slide,
9ae17e
+          LoadFarString(NotEnoughMemCover)));
9ae17e
+        return PK_MEM;
9ae17e
+    }
9ae17e
+
9ae17e
 /*---------------------------------------------------------------------------
9ae17e
     The basic idea of this function is as follows.  Since the central di-
9ae17e
     rectory lies at the end of the zipfile and the member files lie at the
9ae17e
@@ -591,7 +733,8 @@ int extract_or_test_files(__G)    /* return PK-type error code */
9ae17e
             if (error > error_in_archive)
9ae17e
                 error_in_archive = error;
9ae17e
             /* ...and keep going (unless disk full or user break) */
9ae17e
-            if (G.disk_full > 1 || error_in_archive == IZ_CTRLC) {
9ae17e
+            if (G.disk_full > 1 || error_in_archive == IZ_CTRLC ||
9ae17e
+                error == PK_BOMB) {
9ae17e
                 /* clear reached_end to signal premature stop ... */
9ae17e
                 reached_end = FALSE;
9ae17e
                 /* ... and cancel scanning the central directory */
9ae17e
@@ -1060,6 +1203,11 @@ static int extract_or_test_entrylist(__G__ numchunk,
9ae17e
 
9ae17e
         /* seek_zipf(__G__ pInfo->offset);  */
9ae17e
         request = G.pInfo->offset + G.extra_bytes;
9ae17e
+        if (cover_within((cover_t *)G.cover, request)) {
9ae17e
+            Info(slide, 0x401, ((char *)slide,
9ae17e
+              LoadFarString(OverlappedComponents)));
9ae17e
+            return PK_BOMB;
9ae17e
+        }
9ae17e
         inbuf_offset = request % INBUFSIZ;
9ae17e
         bufstart = request - inbuf_offset;
9ae17e
 
9ae17e
@@ -1591,6 +1739,18 @@ static int extract_or_test_entrylist(__G__ numchunk,
9ae17e
             return IZ_CTRLC;        /* cancel operation by user request */
9ae17e
         }
9ae17e
 #endif
9ae17e
+        error = cover_add((cover_t *)G.cover, request,
9ae17e
+                          G.cur_zipfile_bufstart + (G.inptr - G.inbuf));
9ae17e
+        if (error < 0) {
9ae17e
+            Info(slide, 0x401, ((char *)slide,
9ae17e
+              LoadFarString(NotEnoughMemCover)));
9ae17e
+            return PK_MEM;
9ae17e
+        }
9ae17e
+        if (error != 0) {
9ae17e
+            Info(slide, 0x401, ((char *)slide,
9ae17e
+              LoadFarString(OverlappedComponents)));
9ae17e
+            return PK_BOMB;
9ae17e
+        }
9ae17e
 #ifdef MACOS  /* MacOS is no preemptive OS, thus call event-handling by hand */
9ae17e
         UserStop();
9ae17e
 #endif
9ae17e
@@ -1992,6 +2152,34 @@ static int extract_or_test_member(__G)    /* return PK-type error code */
9ae17e
     }
9ae17e
 
9ae17e
     undefer_input(__G);
9ae17e
+
9ae17e
+    if ((G.lrec.general_purpose_bit_flag & 8) != 0) {
9ae17e
+        /* skip over data descriptor (harder than it sounds, due to signature
9ae17e
+         * ambiguity)
9ae17e
+         */
9ae17e
+#       define SIG 0x08074b50
9ae17e
+#       define LOW 0xffffffff
9ae17e
+        uch buf[12];
9ae17e
+        unsigned shy = 12 - readbuf((char *)buf, 12);
9ae17e
+        ulg crc = shy ? 0 : makelong(buf);
9ae17e
+        ulg clen = shy ? 0 : makelong(buf + 4);
9ae17e
+        ulg ulen = shy ? 0 : makelong(buf + 8); /* or high clen if ZIP64 */
9ae17e
+        if (crc == SIG &&                       /* if not SIG, no signature */
9ae17e
+            (G.lrec.crc32 != SIG ||             /* if not SIG, have signature */
9ae17e
+             (clen == SIG &&                    /* if not SIG, no signature */
9ae17e
+              ((G.lrec.csize & LOW) != SIG ||   /* if not SIG, have signature */
9ae17e
+               (ulen == SIG &&                  /* if not SIG, no signature */
9ae17e
+                (G.zip64 ? G.lrec.csize >> 32 : G.lrec.ucsize) != SIG
9ae17e
+                                                /* if not SIG, have signature */
9ae17e
+                )))))
9ae17e
+                   /* skip four more bytes to account for signature */
9ae17e
+                   shy += 4 - readbuf((char *)buf, 4);
9ae17e
+        if (G.zip64)
9ae17e
+            shy += 8 - readbuf((char *)buf, 8); /* skip eight more for ZIP64 */
9ae17e
+        if (shy)
9ae17e
+            error = PK_ERR;
9ae17e
+    }
9ae17e
+
9ae17e
     return error;
9ae17e
 
9ae17e
 } /* end function extract_or_test_member() */
9ae17e
diff --git a/globals.c b/globals.c
9ae17e
index fa8cca5..1e0f608 100644
9ae17e
--- a/globals.c
9ae17e
+++ b/globals.c
9ae17e
@@ -181,6 +181,7 @@ Uz_Globs *globalsCtor()
9ae17e
 # if (!defined(NO_TIMESTAMPS))
9ae17e
     uO.D_flag=1;    /* default to '-D', no restoration of dir timestamps */
9ae17e
 # endif
9ae17e
+    G.cover = NULL;     /* not allocated yet */
9ae17e
 #endif
9ae17e
 
9ae17e
     uO.lflag=(-1);
9ae17e
diff --git a/globals.h b/globals.h
9ae17e
index 11b7215..2bdcdeb 100644
9ae17e
--- a/globals.h
9ae17e
+++ b/globals.h
9ae17e
@@ -260,12 +260,15 @@ typedef struct Globals {
9ae17e
     ecdir_rec       ecrec;         /* used in unzip.c, extract.c */
9ae17e
     z_stat   statbuf;              /* used by main, mapname, check_for_newer */
9ae17e
 
9ae17e
+    int zip64;                     /* true if Zip64 info in extra field */
9ae17e
+
9ae17e
     int      mem_mode;
9ae17e
     uch      *outbufptr;           /* extract.c static */
9ae17e
     ulg      outsize;              /* extract.c static */
9ae17e
     int      reported_backslash;   /* extract.c static */
9ae17e
     int      disk_full;
9ae17e
     int      newfile;
9ae17e
+    void     **cover;              /* used in extract.c for bomb detection */
9ae17e
 
9ae17e
     int      didCRlast;            /* fileio static */
9ae17e
     ulg      numlines;             /* fileio static: number of lines printed */
9ae17e
diff --git a/process.c b/process.c
9ae17e
index 1e9a1e1..d2e4dc3 100644
9ae17e
--- a/process.c
9ae17e
+++ b/process.c
9ae17e
@@ -637,6 +637,13 @@ void free_G_buffers(__G)     /* releases all memory allocated in global vars */
9ae17e
     }
9ae17e
 #endif
9ae17e
 
9ae17e
+    /* Free the cover span list and the cover structure. */
9ae17e
+    if (G.cover != NULL) {
9ae17e
+        free(*(G.cover));
9ae17e
+        free(G.cover);
9ae17e
+        G.cover = NULL;
9ae17e
+    }
9ae17e
+
9ae17e
 } /* end function free_G_buffers() */
9ae17e
 
9ae17e
 
9ae17e
@@ -1890,6 +1897,8 @@ int getZip64Data(__G__ ef_buf, ef_len)
9ae17e
 #define Z64FLGS 0xffff
9ae17e
 #define Z64FLGL 0xffffffff
9ae17e
 
9ae17e
+    G.zip64 = FALSE;
9ae17e
+
9ae17e
     if (ef_len == 0 || ef_buf == NULL)
9ae17e
         return PK_COOL;
9ae17e
 
9ae17e
@@ -1927,6 +1936,8 @@ int getZip64Data(__G__ ef_buf, ef_len)
9ae17e
 #if 0
9ae17e
           break;                /* Expect only one EF_PKSZ64 block. */
9ae17e
 #endif /* 0 */
9ae17e
+
9ae17e
+          G.zip64 = TRUE;
9ae17e
         }
9ae17e
9ae17e
         /* Skip this extra field block. */
9ae17e
diff --git a/unzip.h b/unzip.h
9ae17e
index 5b2a326..ed24a5b 100644
9ae17e
--- a/unzip.h
9ae17e
+++ b/unzip.h
9ae17e
@@ -645,6 +645,7 @@ typedef struct _Uzp_cdir_Rec {
9ae17e
 #define PK_NOZIP           9   /* zipfile not found */
9ae17e
 #define PK_PARAM          10   /* bad or illegal parameters specified */
9ae17e
 #define PK_FIND           11   /* no files found */
9ae17e
+#define PK_BOMB           12   /* likely zip bomb */
9ae17e
 #define PK_DISK           50   /* disk full */
9ae17e
 #define PK_EOF            51   /* unexpected EOF */
9ae17e