a5aa2d
From 5b44c818b96193b3e240f38f61985fa2bc780eb7 Mon Sep 17 00:00:00 2001
a5aa2d
From: Jakub Martisko <jamartis@redhat.com>
a5aa2d
Date: Tue, 30 Nov 2021 15:42:17 +0100
a5aa2d
Subject: [PATCH] Add an option to disable the zipbomb detection
a5aa2d
a5aa2d
This can be done by settting a newly introduced environment variable
a5aa2d
UNZIP_DISABLE_ZIPBOMB_DETECTION to {TRUE,True,true}. If the variable is unset, or
a5aa2d
set to any other value the zipbomb detection is left enabled.
a5aa2d
a5aa2d
Example:
a5aa2d
	UNZIP_DISABLE_ZIPBOMB_DETECTION=True unzip ./zbsm.zip -d ./test
a5aa2d
---
a5aa2d
 extract.c | 85 ++++++++++++++++++++++++++++++-------------------------
a5aa2d
 unzip.c   | 15 ++++++++--
a5aa2d
 unzip.h   |  1 +
a5aa2d
 3 files changed, 60 insertions(+), 41 deletions(-)
a5aa2d
a5aa2d
diff --git a/extract.c b/extract.c
a5aa2d
index 878817d..3e58071 100644
a5aa2d
--- a/extract.c
a5aa2d
+++ b/extract.c
a5aa2d
@@ -322,7 +322,8 @@ static ZCONST char Far BadExtraFieldCRC[] =
a5aa2d
 static ZCONST char Far NotEnoughMemCover[] =
a5aa2d
   "error: not enough memory for bomb detection\n";
a5aa2d
 static ZCONST char Far OverlappedComponents[] =
a5aa2d
-  "error: invalid zip file with overlapped components (possible zip bomb)\n";
a5aa2d
+  "error: invalid zip file with overlapped components (possible zip bomb)\n \
a5aa2d
+To unzip the file anyway, rerun the command with UNZIP_DISABLE_ZIPBOMB_DETECTION=TRUE environmnent variable\n";
a5aa2d
 
a5aa2d
 
a5aa2d
 
a5aa2d
@@ -502,35 +503,37 @@ int extract_or_test_files(__G)    /* return PK-type error code */
a5aa2d
        the end of central directory record (including the Zip64 end of central
a5aa2d
        directory locator, if present), and the Zip64 end of central directory
a5aa2d
        record, if present. */
a5aa2d
-    if (G.cover == NULL) {
a5aa2d
+    if (uO.zipbomb == TRUE) {
a5aa2d
+      if (G.cover == NULL) {
a5aa2d
         G.cover = malloc(sizeof(cover_t));
a5aa2d
         if (G.cover == NULL) {
a5aa2d
-            Info(slide, 0x401, ((char *)slide,
a5aa2d
-              LoadFarString(NotEnoughMemCover)));
a5aa2d
-            return PK_MEM;
a5aa2d
+            Info(slide, 0x401, ((char *)slide,
a5aa2d
+              LoadFarString(NotEnoughMemCover)));
a5aa2d
+            return PK_MEM;
a5aa2d
         }
a5aa2d
         ((cover_t *)G.cover)->span = NULL;
a5aa2d
         ((cover_t *)G.cover)->max = 0;
a5aa2d
-    }
a5aa2d
-    ((cover_t *)G.cover)->num = 0;
a5aa2d
-    if (cover_add((cover_t *)G.cover,
a5aa2d
-                  G.extra_bytes + G.ecrec.offset_start_central_directory,
a5aa2d
-                  G.extra_bytes + G.ecrec.offset_start_central_directory +
a5aa2d
-                  G.ecrec.size_central_directory) != 0) {
a5aa2d
+    }
a5aa2d
+    ((cover_t *)G.cover)->num = 0;
a5aa2d
+    if (cover_add((cover_t *)G.cover,
a5aa2d
+                  G.extra_bytes + G.ecrec.offset_start_central_directory,
a5aa2d
+                  G.extra_bytes + G.ecrec.offset_start_central_directory +
a5aa2d
+                  G.ecrec.size_central_directory) != 0) {
a5aa2d
         Info(slide, 0x401, ((char *)slide,
a5aa2d
-          LoadFarString(NotEnoughMemCover)));
a5aa2d
+          LoadFarString(NotEnoughMemCover)));
a5aa2d
         return PK_MEM;
a5aa2d
-    }
a5aa2d
-    if ((G.extra_bytes != 0 &&
a5aa2d
-         cover_add((cover_t *)G.cover, 0, G.extra_bytes) != 0) ||
a5aa2d
-        (G.ecrec.have_ecr64 &&
a5aa2d
-         cover_add((cover_t *)G.cover, G.ecrec.ec64_start,
a5aa2d
-                   G.ecrec.ec64_end) != 0) ||
a5aa2d
-        cover_add((cover_t *)G.cover, G.ecrec.ec_start,
a5aa2d
-                  G.ecrec.ec_end) != 0) {
a5aa2d
+    }
a5aa2d
+    if ((G.extra_bytes != 0 &&
a5aa2d
+         cover_add((cover_t *)G.cover, 0, G.extra_bytes) != 0) ||
a5aa2d
+        (G.ecrec.have_ecr64 &&
a5aa2d
+         cover_add((cover_t *)G.cover, G.ecrec.ec64_start,
a5aa2d
+                   G.ecrec.ec64_end) != 0) ||
a5aa2d
+        cover_add((cover_t *)G.cover, G.ecrec.ec_start,
a5aa2d
+                  G.ecrec.ec_end) != 0) {
a5aa2d
         Info(slide, 0x401, ((char *)slide,
a5aa2d
-          LoadFarString(OverlappedComponents)));
a5aa2d
+          LoadFarString(OverlappedComponents)));
a5aa2d
         return PK_BOMB;
a5aa2d
+      }
a5aa2d
     }
a5aa2d
 
a5aa2d
 /*---------------------------------------------------------------------------
a5aa2d
@@ -1222,10 +1225,12 @@ static int extract_or_test_entrylist(__G__ numchunk,
a5aa2d
 
a5aa2d
         /* seek_zipf(__G__ pInfo->offset);  */
a5aa2d
         request = G.pInfo->offset + G.extra_bytes;
a5aa2d
-        if (cover_within((cover_t *)G.cover, request)) {
a5aa2d
+        if (uO.zipbomb == TRUE) {
a5aa2d
+          if (cover_within((cover_t *)G.cover, request)) {
a5aa2d
             Info(slide, 0x401, ((char *)slide,
a5aa2d
-              LoadFarString(OverlappedComponents)));
a5aa2d
+              LoadFarString(OverlappedComponents)));
a5aa2d
             return PK_BOMB;
a5aa2d
+          }
a5aa2d
         }
a5aa2d
         inbuf_offset = request % INBUFSIZ;
a5aa2d
         bufstart = request - inbuf_offset;
a5aa2d
@@ -1758,17 +1763,19 @@ reprompt:
a5aa2d
             return IZ_CTRLC;        /* cancel operation by user request */
a5aa2d
         }
a5aa2d
 #endif
a5aa2d
-        error = cover_add((cover_t *)G.cover, request,
a5aa2d
-                          G.cur_zipfile_bufstart + (G.inptr - G.inbuf));
a5aa2d
-        if (error < 0) {
a5aa2d
+        if (uO.zipbomb == TRUE) {
a5aa2d
+          error = cover_add((cover_t *)G.cover, request,
a5aa2d
+                            G.cur_zipfile_bufstart + (G.inptr - G.inbuf));
a5aa2d
+          if (error < 0) {
a5aa2d
             Info(slide, 0x401, ((char *)slide,
a5aa2d
-              LoadFarString(NotEnoughMemCover)));
a5aa2d
+                                LoadFarString(NotEnoughMemCover)));
a5aa2d
             return PK_MEM;
a5aa2d
-        }
a5aa2d
-        if (error != 0) {
a5aa2d
+          }
a5aa2d
+          if (error != 0) {
a5aa2d
             Info(slide, 0x401, ((char *)slide,
a5aa2d
-              LoadFarString(OverlappedComponents)));
a5aa2d
+                                LoadFarString(OverlappedComponents)));
a5aa2d
             return PK_BOMB;
a5aa2d
+          }
a5aa2d
         }
a5aa2d
 #ifdef MACOS  /* MacOS is no preemptive OS, thus call event-handling by hand */
a5aa2d
         UserStop();
a5aa2d
@@ -2171,8 +2178,8 @@ static int extract_or_test_member(__G)    /* return PK-type error code */
a5aa2d
     }
a5aa2d
 
a5aa2d
     undefer_input(__G);
a5aa2d
-
a5aa2d
-    if ((G.lrec.general_purpose_bit_flag & 8) != 0) {
a5aa2d
+    if (uO.zipbomb == TRUE) {
a5aa2d
+      if ((G.lrec.general_purpose_bit_flag & 8) != 0) {
a5aa2d
         /* skip over data descriptor (harder than it sounds, due to signature
a5aa2d
          * ambiguity)
a5aa2d
          */
a5aa2d
@@ -2189,16 +2196,16 @@ static int extract_or_test_member(__G)    /* return PK-type error code */
a5aa2d
               ((G.lrec.csize & LOW) != SIG ||   /* if not SIG, have signature */
a5aa2d
                (ulen == SIG &&                  /* if not SIG, no signature */
a5aa2d
                 (G.pInfo->zip64 ? G.lrec.csize >> 32 : G.lrec.ucsize) != SIG
a5aa2d
-                                                /* if not SIG, have signature */
a5aa2d
+                /* if not SIG, have signature */
a5aa2d
                 )))))
a5aa2d
-                   /* skip four more bytes to account for signature */
a5aa2d
-                   shy += 4 - readbuf((char *)buf, 4);
a5aa2d
+          /* skip four more bytes to account for signature */
a5aa2d
+          shy += 4 - readbuf((char *)buf, 4);
a5aa2d
         if (G.pInfo->zip64)
a5aa2d
-            shy += 8 - readbuf((char *)buf, 8); /* skip eight more for ZIP64 */
a5aa2d
+          shy += 8 - readbuf((char *)buf, 8); /* skip eight more for ZIP64 */
a5aa2d
         if (shy)
a5aa2d
-            error = PK_ERR;
a5aa2d
+          error = PK_ERR;
a5aa2d
+      }
a5aa2d
     }
a5aa2d
-
a5aa2d
     return error;
a5aa2d
 
a5aa2d
 } /* end function extract_or_test_member() */
a5aa2d
diff --git a/unzip.c b/unzip.c
a5aa2d
index 8dbfc95..abb3644 100644
a5aa2d
--- a/unzip.c
a5aa2d
+++ b/unzip.c
a5aa2d
@@ -1329,10 +1329,9 @@ int uz_opts(__G__ pargc, pargv)
a5aa2d
     int *pargc;
a5aa2d
     char ***pargv;
a5aa2d
 {
a5aa2d
-    char **argv, *s;
a5aa2d
+    char **argv, *s, *zipbomb_envar;
a5aa2d
     int argc, c, error=FALSE, negative=0, showhelp=0;
a5aa2d
 
a5aa2d
-
a5aa2d
     argc = *pargc;
a5aa2d
     argv = *pargv;
a5aa2d
 
a5aa2d
@@ -1923,6 +1922,18 @@ opts_done:  /* yes, very ugly...but only used by UnZipSFX with -x xlist */
a5aa2d
     else
a5aa2d
         G.extract_flag = TRUE;
a5aa2d
 
a5aa2d
+    /* Disable the zipbomb detection, this is the only option set only via the shell variables but it should at least not clash with something in the future. */
a5aa2d
+    zipbomb_envar = getenv("UNZIP_DISABLE_ZIPBOMB_DETECTION");
a5aa2d
+    uO.zipbomb = TRUE;
a5aa2d
+    if (zipbomb_envar != NULL) {
a5aa2d
+      /* strcasecmp might be a better approach here but it is POSIX-only */
a5aa2d
+      if ((strcmp ("TRUE", zipbomb_envar) == 0)
a5aa2d
+       || (strcmp ("True", zipbomb_envar) == 0)
a5aa2d
+       || (strcmp ("true",zipbomb_envar) == 0)) {
a5aa2d
+        uO.zipbomb = FALSE;
a5aa2d
+      }
a5aa2d
+    }
a5aa2d
+
a5aa2d
     *pargc = argc;
a5aa2d
     *pargv = argv;
a5aa2d
     return PK_OK;
a5aa2d
diff --git a/unzip.h b/unzip.h
a5aa2d
index ed24a5b..e7665e8 100644
a5aa2d
--- a/unzip.h
a5aa2d
+++ b/unzip.h
a5aa2d
@@ -559,6 +559,7 @@ typedef struct _UzpOpts {
a5aa2d
 #ifdef UNIX
a5aa2d
     int cflxflag;       /* -^: allow control chars in extracted filenames */
a5aa2d
 #endif
a5aa2d
+  int zipbomb;
a5aa2d
 #endif /* !FUNZIP */
a5aa2d
 } UzpOpts;
a5aa2d
 
a5aa2d
-- 
a5aa2d
2.33.0
a5aa2d