Blame SOURCES/unzip-zipbomb-switch.patch

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