unzip-zipbomb-part2.patch 13 KB

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