Blame SOURCES/unzip-zipbomb-part2.patch

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