Blame SOURCES/CVE-2018-17828-singlez.patch

b2b6f2
diff --git a/bins/unzip-mem.c b/bins/unzip-mem.c
b2b6f2
index c45cb72..ff564a5 100644
b2b6f2
--- a/bins/unzip-mem.c
b2b6f2
+++ b/bins/unzip-mem.c
b2b6f2
@@ -88,10 +88,53 @@ static void zzip_mem_entry_pipe(ZZIP_MEM_DISK* disk,
b2b6f2
     }
b2b6f2
 }
b2b6f2
b2b6f2
+
b2b6f2
+
b2b6f2
+
b2b6f2
+static inline void
b2b6f2
+remove_dotdotslash(char *path)
b2b6f2
+{
b2b6f2
+    /* Note: removing "../" from the path ALWAYS shortens the path, never adds to it! */
b2b6f2
+    char *dotdotslash;
b2b6f2
+    int warned = 0;
b2b6f2
+
b2b6f2
+    dotdotslash = path;
b2b6f2
+    while ((dotdotslash = strstr(dotdotslash, "../")) != NULL)
b2b6f2
+    {
b2b6f2
+        /*
b2b6f2
+         * Remove only if at the beginning of the pathname ("../path/name")
b2b6f2
+         * or when preceded by a slash ("path/../name"),
b2b6f2
+         * otherwise not ("path../name..")!
b2b6f2
+         */
b2b6f2
+        if (dotdotslash == path || dotdotslash[-1] == '/')
b2b6f2
+        {
b2b6f2
+            char *src, *dst;
b2b6f2
+            if (!warned)
b2b6f2
+            {
b2b6f2
+                /* Note: the first time through the pathname is still intact */
b2b6f2
+                fprintf(stderr, "Removing \"../\" path component(s) in %s\n", path);
b2b6f2
+                warned = 1;
b2b6f2
+            }
b2b6f2
+            /* We cannot use strcpy(), as there "The strings may not overlap" */
b2b6f2
+            for (src = dotdotslash+3, dst=dotdotslash; (*dst = *src) != '\0'; src++, dst++)
b2b6f2
+                ;
b2b6f2
+        }
b2b6f2
+        else
b2b6f2
+            dotdotslash +=3;	/* skip this instance to prevent infinite loop */
b2b6f2
+    }
b2b6f2
+}
b2b6f2
+
b2b6f2
 static void zzip_mem_entry_make(ZZIP_MEM_DISK* disk, 
b2b6f2
 				ZZIP_MEM_ENTRY* entry)
b2b6f2
 {
b2b6f2
-    FILE* file = fopen (entry->zz_name, "w");
b2b6f2
+    char name_stripped[PATH_MAX+1];
b2b6f2
+    FILE* file;
b2b6f2
+
b2b6f2
+    strncpy(name_stripped, entry->zz_name, PATH_MAX);
b2b6f2
+    name_stripped[PATH_MAX]='\0';
b2b6f2
+    remove_dotdotslash(name_stripped);
b2b6f2
+    
b2b6f2
+    file = fopen (name_stripped, "wb");
b2b6f2
     if (file) { zzip_mem_entry_pipe (disk, entry, file); fclose (file); }
b2b6f2
     perror (entry->zz_name);
b2b6f2
     if (status < EXIT_WARNINGS) status = EXIT_WARNINGS;