unzip-zipbomb-part3.patch 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. From 6d351831be705cc26d897db44f878a978f4138fc Mon Sep 17 00:00:00 2001
  2. From: Mark Adler <madler@alumni.caltech.edu>
  3. Date: Thu, 25 Jul 2019 20:43:17 -0700
  4. Subject: [PATCH] Do not raise a zip bomb alert for a misplaced central
  5. directory.
  6. There is a zip-like file in the Firefox distribution, omni.ja,
  7. which is a zip container with the central directory placed at the
  8. start of the file instead of after the local entries as required
  9. by the zip standard. This commit marks the actual location of the
  10. central directory, as well as the end of central directory records,
  11. as disallowed locations. This now permits such containers to not
  12. raise a zip bomb alert, where in fact there are no overlaps.
  13. ---
  14. extract.c | 25 +++++++++++++++++++------
  15. process.c | 6 ++++++
  16. unzpriv.h | 10 ++++++++++
  17. 3 files changed, 35 insertions(+), 6 deletions(-)
  18. diff --git a/extract.c b/extract.c
  19. index 0973a33..1b73cb0 100644
  20. --- a/extract.c
  21. +++ b/extract.c
  22. @@ -493,8 +493,11 @@ int extract_or_test_files(__G) /* return PK-type error code */
  23. }
  24. #endif /* !SFX || SFX_EXDIR */
  25. - /* One more: initialize cover structure for bomb detection. Start with a
  26. - span that covers the central directory though the end of the file. */
  27. + /* One more: initialize cover structure for bomb detection. Start with
  28. + spans that cover any extra bytes at the start, the central directory,
  29. + the end of central directory record (including the Zip64 end of central
  30. + directory locator, if present), and the Zip64 end of central directory
  31. + record, if present. */
  32. if (G.cover == NULL) {
  33. G.cover = malloc(sizeof(cover_t));
  34. if (G.cover == NULL) {
  35. @@ -506,15 +509,25 @@ int extract_or_test_files(__G) /* return PK-type error code */
  36. ((cover_t *)G.cover)->max = 0;
  37. }
  38. ((cover_t *)G.cover)->num = 0;
  39. - if ((G.extra_bytes != 0 &&
  40. - cover_add((cover_t *)G.cover, 0, G.extra_bytes) != 0) ||
  41. - cover_add((cover_t *)G.cover,
  42. + if (cover_add((cover_t *)G.cover,
  43. G.extra_bytes + G.ecrec.offset_start_central_directory,
  44. - G.ziplen) != 0) {
  45. + G.extra_bytes + G.ecrec.offset_start_central_directory +
  46. + G.ecrec.size_central_directory) != 0) {
  47. Info(slide, 0x401, ((char *)slide,
  48. LoadFarString(NotEnoughMemCover)));
  49. return PK_MEM;
  50. }
  51. + if ((G.extra_bytes != 0 &&
  52. + cover_add((cover_t *)G.cover, 0, G.extra_bytes) != 0) ||
  53. + (G.ecrec.have_ecr64 &&
  54. + cover_add((cover_t *)G.cover, G.ecrec.ec64_start,
  55. + G.ecrec.ec64_end) != 0) ||
  56. + cover_add((cover_t *)G.cover, G.ecrec.ec_start,
  57. + G.ecrec.ec_end) != 0) {
  58. + Info(slide, 0x401, ((char *)slide,
  59. + LoadFarString(OverlappedComponents)));
  60. + return PK_BOMB;
  61. + }
  62. /*---------------------------------------------------------------------------
  63. The basic idea of this function is as follows. Since the central di-
  64. diff --git a/process.c b/process.c
  65. index d2e4dc3..d75d405 100644
  66. --- a/process.c
  67. +++ b/process.c
  68. @@ -1408,6 +1408,10 @@ static int find_ecrec64(__G__ searchlen) /* return PK-class error */
  69. /* Now, we are (almost) sure that we have a Zip64 archive. */
  70. G.ecrec.have_ecr64 = 1;
  71. + G.ecrec.ec_start -= ECLOC64_SIZE+4;
  72. + G.ecrec.ec64_start = ecrec64_start_offset;
  73. + G.ecrec.ec64_end = ecrec64_start_offset +
  74. + 12 + makeint64(&byterec[ECREC64_LENGTH]);
  75. /* Update the "end-of-central-dir offset" for later checks. */
  76. G.real_ecrec_offset = ecrec64_start_offset;
  77. @@ -1542,6 +1546,8 @@ static int find_ecrec(__G__ searchlen) /* return PK-class error */
  78. makelong(&byterec[OFFSET_START_CENTRAL_DIRECTORY]);
  79. G.ecrec.zipfile_comment_length =
  80. makeword(&byterec[ZIPFILE_COMMENT_LENGTH]);
  81. + G.ecrec.ec_start = G.real_ecrec_offset;
  82. + G.ecrec.ec_end = G.ecrec.ec_start + 22 + G.ecrec.zipfile_comment_length;
  83. /* Now, we have to read the archive comment, BEFORE the file pointer
  84. is moved away backwards to seek for a Zip64 ECLOC64 structure.
  85. diff --git a/unzpriv.h b/unzpriv.h
  86. index dc9eff5..297b3c7 100644
  87. --- a/unzpriv.h
  88. +++ b/unzpriv.h
  89. @@ -2185,6 +2185,16 @@ typedef struct VMStimbuf {
  90. int have_ecr64; /* valid Zip64 ecdir-record exists */
  91. int is_zip64_archive; /* Zip64 ecdir-record is mandatory */
  92. ush zipfile_comment_length;
  93. + zusz_t ec_start, ec_end; /* offsets of start and end of the
  94. + end of central directory record,
  95. + including if present the Zip64
  96. + end of central directory locator,
  97. + which immediately precedes the
  98. + end of central directory record */
  99. + zusz_t ec64_start, ec64_end; /* if have_ecr64 is true, then these
  100. + are the offsets of the start and
  101. + end of the Zip64 end of central
  102. + directory record */
  103. } ecdir_rec;