opkg

statically linked package installer
git clone anongit@rnpnr.xyz:opkg.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

0023-Detect-and-reject-a-zip-bomb-using-overlapped-entrie.patch (13171B)


      1 From 6cdb7b372b4d46e06a982c6f3494c086d2418c7b Mon Sep 17 00:00:00 2001
      2 From: Mark Adler <madler@alumni.caltech.edu>
      3 Date: Fri, 31 Jan 2020 22:05:59 -0800
      4 Subject: [PATCH] Detect and reject a zip bomb using overlapped entries.
      5 
      6     Detect and reject a zip bomb using overlapped entries.
      7 
      8     This detects an invalid zip file that has at least one entry that
      9     overlaps with another entry or with the central directory to the
     10     end of the file. A Fifield zip bomb uses overlapped local entries
     11     to vastly increase the potential inflation ratio. Such an invalid
     12     zip file is rejected.
     13 
     14     See https://www.bamsoftware.com/hacks/zipbomb/ for David Fifield's
     15     analysis, construction, and examples of such zip bombs.
     16 
     17     The detection maintains a list of covered spans of the zip files
     18     so far, where the central directory to the end of the file and any
     19     bytes preceding the first entry at zip file offset zero are
     20     considered covered initially. Then as each entry is decompressed
     21     or tested, it is considered covered. When a new entry is about to
     22     be processed, its initial offset is checked to see if it is
     23     contained by a covered span. If so, the zip file is rejected as
     24     invalid.
     25 
     26     This commit depends on a preceding commit: "Fix bug in
     27     undefer_input() that misplaced the input state."
     28 ---
     29  extract.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
     30  globals.c |   1 +
     31  globals.h |   3 +
     32  process.c |  11 ++++
     33  unzip.h   |   1 +
     34  5 files changed, 205 insertions(+), 1 deletion(-)
     35 
     36 diff --git a/extract.c b/extract.c
     37 index 549a5eb..1f078d1 100644
     38 --- a/extract.c
     39 +++ b/extract.c
     40 @@ -321,6 +321,125 @@ static ZCONST char Far UnsupportedExtraField[] =
     41    "\nerror:  unsupported extra-field compression type (%u)--skipping\n";
     42  static ZCONST char Far BadExtraFieldCRC[] =
     43    "error [%s]:  bad extra-field CRC %08lx (should be %08lx)\n";
     44 +static ZCONST char Far NotEnoughMemCover[] =
     45 +  "error: not enough memory for bomb detection\n";
     46 +static ZCONST char Far OverlappedComponents[] =
     47 +  "error: invalid zip file with overlapped components (possible zip bomb)\n";
     48 +
     49 +
     50 +
     51 +
     52 +
     53 +/* A growable list of spans. */
     54 +typedef zoff_t bound_t;
     55 +typedef struct {
     56 +    bound_t beg;        /* start of the span */
     57 +    bound_t end;        /* one past the end of the span */
     58 +} span_t;
     59 +typedef struct {
     60 +    span_t *span;       /* allocated, distinct, and sorted list of spans */
     61 +    size_t num;         /* number of spans in the list */
     62 +    size_t max;         /* allocated number of spans (num <= max) */
     63 +} cover_t;
     64 +
     65 +/*
     66 + * Return the index of the first span in cover whose beg is greater than val.
     67 + * If there is no such span, then cover->num is returned.
     68 + */
     69 +static size_t cover_find(cover, val)
     70 +    cover_t *cover;
     71 +    bound_t val;
     72 +{
     73 +    size_t lo = 0, hi = cover->num;
     74 +    while (lo < hi) {
     75 +        size_t mid = (lo + hi) >> 1;
     76 +        if (val < cover->span[mid].beg)
     77 +            hi = mid;
     78 +        else
     79 +            lo = mid + 1;
     80 +    }
     81 +    return hi;
     82 +}
     83 +
     84 +/* Return true if val lies within any one of the spans in cover. */
     85 +static int cover_within(cover, val)
     86 +    cover_t *cover;
     87 +    bound_t val;
     88 +{
     89 +    size_t pos = cover_find(cover, val);
     90 +    return pos > 0 && val < cover->span[pos - 1].end;
     91 +}
     92 +
     93 +/*
     94 + * Add a new span to the list, but only if the new span does not overlap any
     95 + * spans already in the list. The new span covers the values beg..end-1. beg
     96 + * must be less than end.
     97 + *
     98 + * Keep the list sorted and merge adjacent spans. Grow the allocated space for
     99 + * the list as needed. On success, 0 is returned. If the new span overlaps any
    100 + * existing spans, then 1 is returned and the new span is not added to the
    101 + * list. If the new span is invalid because beg is greater than or equal to
    102 + * end, then -1 is returned. If the list needs to be grown but the memory
    103 + * allocation fails, then -2 is returned.
    104 + */
    105 +static int cover_add(cover, beg, end)
    106 +    cover_t *cover;
    107 +    bound_t beg;
    108 +    bound_t end;
    109 +{
    110 +    size_t pos;
    111 +    int prec, foll;
    112 +
    113 +    if (beg >= end)
    114 +    /* The new span is invalid. */
    115 +        return -1;
    116 +
    117 +    /* Find where the new span should go, and make sure that it does not
    118 +       overlap with any existing spans. */
    119 +    pos = cover_find(cover, beg);
    120 +    if ((pos > 0 && beg < cover->span[pos - 1].end) ||
    121 +        (pos < cover->num && end > cover->span[pos].beg))
    122 +        return 1;
    123 +
    124 +    /* Check for adjacencies. */
    125 +    prec = pos > 0 && beg == cover->span[pos - 1].end;
    126 +    foll = pos < cover->num && end == cover->span[pos].beg;
    127 +    if (prec && foll) {
    128 +        /* The new span connects the preceding and following spans. Merge the
    129 +           following span into the preceding span, and delete the following
    130 +           span. */
    131 +        cover->span[pos - 1].end = cover->span[pos].end;
    132 +        cover->num--;
    133 +        memmove(cover->span + pos, cover->span + pos + 1,
    134 +                (cover->num - pos) * sizeof(span_t));
    135 +    }
    136 +    else if (prec)
    137 +        /* The new span is adjacent only to the preceding span. Extend the end
    138 +           of the preceding span. */
    139 +        cover->span[pos - 1].end = end;
    140 +    else if (foll)
    141 +        /* The new span is adjacent only to the following span. Extend the
    142 +           beginning of the following span. */
    143 +        cover->span[pos].beg = beg;
    144 +    else {
    145 +        /* The new span has gaps between both the preceding and the following
    146 +           spans. Assure that there is room and insert the span.  */
    147 +        if (cover->num == cover->max) {
    148 +            size_t max = cover->max == 0 ? 16 : cover->max << 1;
    149 +            span_t *span = realloc(cover->span, max * sizeof(span_t));
    150 +            if (span == NULL)
    151 +                return -2;
    152 +            cover->span = span;
    153 +            cover->max = max;
    154 +        }
    155 +        memmove(cover->span + pos + 1, cover->span + pos,
    156 +                (cover->num - pos) * sizeof(span_t));
    157 +        cover->num++;
    158 +        cover->span[pos].beg = beg;
    159 +        cover->span[pos].end = end;
    160 +    }
    161 +    return 0;
    162 +}
    163  
    164  
    165  
    166 @@ -376,6 +495,29 @@ int extract_or_test_files(__G)    /* return PK-type error code */
    167      }
    168  #endif /* !SFX || SFX_EXDIR */
    169  
    170 +    /* One more: initialize cover structure for bomb detection. Start with a
    171 +       span that covers the central directory though the end of the file. */
    172 +    if (G.cover == NULL) {
    173 +        G.cover = malloc(sizeof(cover_t));
    174 +        if (G.cover == NULL) {
    175 +            Info(slide, 0x401, ((char *)slide,
    176 +              LoadFarString(NotEnoughMemCover)));
    177 +            return PK_MEM;
    178 +        }
    179 +        ((cover_t *)G.cover)->span = NULL;
    180 +        ((cover_t *)G.cover)->max = 0;
    181 +    }
    182 +    ((cover_t *)G.cover)->num = 0;
    183 +    if ((G.extra_bytes != 0 &&
    184 +         cover_add((cover_t *)G.cover, 0, G.extra_bytes) != 0) ||
    185 +        cover_add((cover_t *)G.cover,
    186 +                  G.extra_bytes + G.ecrec.offset_start_central_directory,
    187 +                  G.ziplen) != 0) {
    188 +        Info(slide, 0x401, ((char *)slide,
    189 +          LoadFarString(NotEnoughMemCover)));
    190 +        return PK_MEM;
    191 +    }
    192 +
    193  /*---------------------------------------------------------------------------
    194      The basic idea of this function is as follows.  Since the central di-
    195      rectory lies at the end of the zipfile and the member files lie at the
    196 @@ -593,7 +735,8 @@ int extract_or_test_files(__G)    /* return PK-type error code */
    197              if (error > error_in_archive)
    198                  error_in_archive = error;
    199              /* ...and keep going (unless disk full or user break) */
    200 -            if (G.disk_full > 1 || error_in_archive == IZ_CTRLC) {
    201 +            if (G.disk_full > 1 || error_in_archive == IZ_CTRLC ||
    202 +                error == PK_BOMB) {
    203                  /* clear reached_end to signal premature stop ... */
    204                  reached_end = FALSE;
    205                  /* ... and cancel scanning the central directory */
    206 @@ -1062,6 +1205,11 @@ static int extract_or_test_entrylist(__G__ numchunk,
    207  
    208          /* seek_zipf(__G__ pInfo->offset);  */
    209          request = G.pInfo->offset + G.extra_bytes;
    210 +        if (cover_within((cover_t *)G.cover, request)) {
    211 +            Info(slide, 0x401, ((char *)slide,
    212 +              LoadFarString(OverlappedComponents)));
    213 +            return PK_BOMB;
    214 +        }
    215          inbuf_offset = request % INBUFSIZ;
    216          bufstart = request - inbuf_offset;
    217  
    218 @@ -1602,6 +1750,18 @@ reprompt:
    219              return IZ_CTRLC;        /* cancel operation by user request */
    220          }
    221  #endif
    222 +        error = cover_add((cover_t *)G.cover, request,
    223 +                          G.cur_zipfile_bufstart + (G.inptr - G.inbuf));
    224 +        if (error < 0) {
    225 +            Info(slide, 0x401, ((char *)slide,
    226 +              LoadFarString(NotEnoughMemCover)));
    227 +            return PK_MEM;
    228 +        }
    229 +        if (error != 0) {
    230 +            Info(slide, 0x401, ((char *)slide,
    231 +              LoadFarString(OverlappedComponents)));
    232 +            return PK_BOMB;
    233 +        }
    234  #ifdef MACOS  /* MacOS is no preemptive OS, thus call event-handling by hand */
    235          UserStop();
    236  #endif
    237 @@ -2003,6 +2163,34 @@ static int extract_or_test_member(__G)    /* return PK-type error code */
    238      }
    239  
    240      undefer_input(__G);
    241 +
    242 +    if ((G.lrec.general_purpose_bit_flag & 8) != 0) {
    243 +        /* skip over data descriptor (harder than it sounds, due to signature
    244 +         * ambiguity)
    245 +         */
    246 +#       define SIG 0x08074b50
    247 +#       define LOW 0xffffffff
    248 +        uch buf[12];
    249 +        unsigned shy = 12 - readbuf((char *)buf, 12);
    250 +        ulg crc = shy ? 0 : makelong(buf);
    251 +        ulg clen = shy ? 0 : makelong(buf + 4);
    252 +        ulg ulen = shy ? 0 : makelong(buf + 8); /* or high clen if ZIP64 */
    253 +        if (crc == SIG &&                       /* if not SIG, no signature */
    254 +            (G.lrec.crc32 != SIG ||             /* if not SIG, have signature */
    255 +             (clen == SIG &&                    /* if not SIG, no signature */
    256 +              ((G.lrec.csize & LOW) != SIG ||   /* if not SIG, have signature */
    257 +               (ulen == SIG &&                  /* if not SIG, no signature */
    258 +                (G.zip64 ? G.lrec.csize >> 32 : G.lrec.ucsize) != SIG
    259 +                                                /* if not SIG, have signature */
    260 +                )))))
    261 +                   /* skip four more bytes to account for signature */
    262 +                   shy += 4 - readbuf((char *)buf, 4);
    263 +        if (G.zip64)
    264 +            shy += 8 - readbuf((char *)buf, 8); /* skip eight more for ZIP64 */
    265 +        if (shy)
    266 +            error = PK_ERR;
    267 +    }
    268 +
    269      return error;
    270  
    271  } /* end function extract_or_test_member() */
    272 diff --git a/globals.c b/globals.c
    273 index fa8cca5..1e0f608 100644
    274 --- a/globals.c
    275 +++ b/globals.c
    276 @@ -181,6 +181,7 @@ Uz_Globs *globalsCtor()
    277  # if (!defined(NO_TIMESTAMPS))
    278      uO.D_flag=1;    /* default to '-D', no restoration of dir timestamps */
    279  # endif
    280 +    G.cover = NULL;     /* not allocated yet */
    281  #endif
    282  
    283      uO.lflag=(-1);
    284 diff --git a/globals.h b/globals.h
    285 index 11b7215..2bdcdeb 100644
    286 --- a/globals.h
    287 +++ b/globals.h
    288 @@ -260,12 +260,15 @@ typedef struct Globals {
    289      ecdir_rec       ecrec;         /* used in unzip.c, extract.c */
    290      z_stat   statbuf;              /* used by main, mapname, check_for_newer */
    291  
    292 +    int zip64;                     /* true if Zip64 info in extra field */
    293 +
    294      int      mem_mode;
    295      uch      *outbufptr;           /* extract.c static */
    296      ulg      outsize;              /* extract.c static */
    297      int      reported_backslash;   /* extract.c static */
    298      int      disk_full;
    299      int      newfile;
    300 +    void     **cover;              /* used in extract.c for bomb detection */
    301  
    302      int      didCRlast;            /* fileio static */
    303      ulg      numlines;             /* fileio static: number of lines printed */
    304 diff --git a/process.c b/process.c
    305 index e4f2405..e4e7aee 100644
    306 --- a/process.c
    307 +++ b/process.c
    308 @@ -637,6 +637,13 @@ void free_G_buffers(__G)     /* releases all memory allocated in global vars */
    309      }
    310  #endif
    311  
    312 +    /* Free the cover span list and the cover structure. */
    313 +    if (G.cover != NULL) {
    314 +        free(*(G.cover));
    315 +        free(G.cover);
    316 +        G.cover = NULL;
    317 +    }
    318 +
    319  } /* end function free_G_buffers() */
    320  
    321  
    322 @@ -1913,6 +1920,8 @@ int getZip64Data(__G__ ef_buf, ef_len)
    323  #define Z64FLGS 0xffff
    324  #define Z64FLGL 0xffffffff
    325  
    326 +    G.zip64 = FALSE;
    327 +
    328      if (ef_len == 0 || ef_buf == NULL)
    329          return PK_COOL;
    330  
    331 @@ -2084,6 +2093,8 @@ int getUnicodeData(__G__ ef_buf, ef_len)
    332                      (ZCONST char *)(offset + ef_buf), ULen);
    333              G.unipath_filename[ULen] = '\0';
    334            }
    335 +
    336 +          G.zip64 = TRUE;
    337          }
    338  
    339          /* Skip this extra field block */
    340 diff --git a/unzip.h b/unzip.h
    341 index 5b2a326..ed24a5b 100644
    342 --- a/unzip.h
    343 +++ b/unzip.h
    344 @@ -645,6 +645,7 @@ typedef struct _Uzp_cdir_Rec {
    345  #define PK_NOZIP           9   /* zipfile not found */
    346  #define PK_PARAM          10   /* bad or illegal parameters specified */
    347  #define PK_FIND           11   /* no files found */
    348 +#define PK_BOMB           12   /* likely zip bomb */
    349  #define PK_DISK           50   /* disk full */
    350  #define PK_EOF            51   /* unexpected EOF */
    351  
    352 -- 
    353 2.25.0
    354