logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

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