Blame SOURCES/CVE-2018-7727.patch

5cf55c
From 83a2da55922f67e07f22048ac9671a44cc0d35c4 Mon Sep 17 00:00:00 2001
5cf55c
From: Guido Draheim <guidod@gmx.de>
5cf55c
Date: Wed, 14 Mar 2018 07:50:44 +0100
5cf55c
Subject: [PATCH] ensure disk_close to avoid mem-leak #40
5cf55c
5cf55c
---
5cf55c
 bins/unzzipcat-mem.c | 57 +++++++++++++++++++++++++++-------------------------
5cf55c
 bins/unzzipdir-mem.c | 10 ++++-----
5cf55c
 2 files changed, 34 insertions(+), 33 deletions(-)
5cf55c
5cf55c
diff --git a/bins/unzzipcat-mem.c b/bins/unzzipcat-mem.c
5cf55c
index d13029c..9bc966b 100644
5cf55c
--- a/bins/unzzipcat-mem.c
5cf55c
+++ b/bins/unzzipcat-mem.c
5cf55c
@@ -89,7 +89,7 @@ static FILE* create_fopen(char* name, char* mode, int subdirs)
5cf55c
 
5cf55c
 static int unzzip_cat (int argc, char ** argv, int extract)
5cf55c
 {
5cf55c
-    int done;
5cf55c
+    int done = 0;
5cf55c
     int argn;
5cf55c
     ZZIP_MEM_DISK* disk;
5cf55c
 
5cf55c
@@ -116,47 +116,50 @@ static int unzzip_cat (int argc, char ** argv, int extract)
5cf55c
 	    FILE* out = stdout;
5cf55c
 	    if (extract) out = create_fopen(name, "w", 1);
5cf55c
 	    if (! out) {
5cf55c
-	        if (errno != EISDIR) done = EXIT_ERRORS;
5cf55c
+	        if (errno != EISDIR) {
5cf55c
+	             DBG3("can not open output file %i %s", errno, strerror(errno));
5cf55c
+	             done = EXIT_ERRORS;
5cf55c
+	        }
5cf55c
 	        continue;
5cf55c
 	    }
5cf55c
 	    unzzip_mem_disk_cat_file (disk, name, out);
5cf55c
 	    if (extract) fclose(out);
5cf55c
 	}
5cf55c
-	return done;
5cf55c
-    }
5cf55c
-
5cf55c
-    if (argc == 3 && !extract)
5cf55c
+    } 
5cf55c
+    else if (argc == 3 && !extract)
5cf55c
     {  /* list from one spec */
5cf55c
 	ZZIP_MEM_ENTRY* entry = 0;
5cf55c
 	while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0)))
5cf55c
 	{
5cf55c
 	     unzzip_mem_entry_fprint (disk, entry, stdout);
5cf55c
 	}
5cf55c
-
5cf55c
-	return 0;
5cf55c
-    }
5cf55c
-
5cf55c
-    for (argn=1; argn < argc; argn++)
5cf55c
-    {   /* list only the matching entries - each in order of commandline */
5cf55c
-	ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
5cf55c
-	for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
5cf55c
-	{
5cf55c
-	    char* name = zzip_mem_entry_to_name (entry);
5cf55c
-	    if (! _zzip_fnmatch (argv[argn], name, 
5cf55c
-		FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD))
5cf55c
+    } else {
5cf55c
+	for (argn=1; argn < argc; argn++)
5cf55c
+	{   /* list only the matching entries - each in order of commandline */
5cf55c
+	    ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
5cf55c
+	    for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
5cf55c
 	    {
5cf55c
-	        FILE* out = stdout;
5cf55c
-	        if (extract) out = create_fopen(name, "w", 1);
5cf55c
-	        if (! out) {
5cf55c
-	            if (errno != EISDIR) done = EXIT_ERRORS;
5cf55c
-	            continue;
5cf55c
-	        }
5cf55c
-		unzzip_mem_disk_cat_file (disk, name, out);
5cf55c
-		if (extract) fclose(out);
5cf55c
-		break; /* match loop */
5cf55c
+	        char* name = zzip_mem_entry_to_name (entry);
5cf55c
+	        if (! _zzip_fnmatch (argv[argn], name, 
5cf55c
+		    FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD))
5cf55c
+	        {
5cf55c
+	            FILE* out = stdout;
5cf55c
+	            if (extract) out = create_fopen(name, "wb", 1);
5cf55c
+		    if (! out) {
5cf55c
+		        if (errno != EISDIR) {
5cf55c
+		            DBG3("can not open output file %i %s", errno, strerror(errno));
5cf55c
+		            done = EXIT_ERRORS;
5cf55c
+		        }
5cf55c
+		        continue;
5cf55c
+		    }
5cf55c
+		    unzzip_mem_disk_cat_file (disk, name, out);
5cf55c
+		    if (extract) fclose(out);
5cf55c
+		    break; /* match loop */
5cf55c
+		}
5cf55c
 	    }
5cf55c
 	}
5cf55c
     }
5cf55c
+    zzip_mem_disk_close(disk);
5cf55c
     return done;
5cf55c
 } 
5cf55c
 
5cf55c
diff --git a/bins/unzzipdir-mem.c b/bins/unzzipdir-mem.c
5cf55c
index e54cd8d..beca788 100644
5cf55c
--- a/bins/unzzipdir-mem.c
5cf55c
+++ b/bins/unzzipdir-mem.c
5cf55c
@@ -68,10 +68,8 @@ unzzip_list (int argc, char ** argv, int verbose)
5cf55c
 		printf ("%lli/%lli %s %s\n", csize, usize, defl, name);
5cf55c
 	    }
5cf55c
 	}
5cf55c
-	return 0;
5cf55c
     }
5cf55c
-
5cf55c
-    if (argc == 3)
5cf55c
+    else if (argc == 3)
5cf55c
     {  /* list from one spec */
5cf55c
 	ZZIP_MEM_ENTRY* entry = 0;
5cf55c
 	while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0)))
5cf55c
@@ -89,9 +87,8 @@ unzzip_list (int argc, char ** argv, int verbose)
5cf55c
 		printf ("%lli/%lli %s %s\n", csize, usize, defl, name);
5cf55c
 	    }
5cf55c
 	}
5cf55c
-	return 0;
5cf55c
     }
5cf55c
-
5cf55c
+    else
5cf55c
     {   /* list only the matching entries - in order of zip directory */
5cf55c
 	ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
5cf55c
 	for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
5cf55c
@@ -118,8 +115,9 @@ unzzip_list (int argc, char ** argv, int verbose)
5cf55c
 		}
5cf55c
 	    }
5cf55c
 	}
5cf55c
-	return 0;
5cf55c
     }
5cf55c
+    zzip_mem_disk_close(disk);
5cf55c
+    return EXIT_OK;
5cf55c
 } 
5cf55c
 
5cf55c
 int