Blame SOURCES/CVE-2018-17828.patch

a995b3
From 81dfa6b3e08f6934885ba5c98939587d6850d08e Mon Sep 17 00:00:00 2001
a995b3
From: Josef Moellers <jmoellers@suse.de>
a995b3
Date: Thu, 4 Oct 2018 14:21:48 +0200
a995b3
Subject: [PATCH] Fix issue #62: Remove any "../" components from pathnames of
a995b3
 extracted files. [CVE-2018-17828]
a995b3
a995b3
---
a995b3
 bins/unzzipcat-big.c | 57 +++++++++++++++++++++++++++++++++++++++++++-
a995b3
 bins/unzzipcat-mem.c | 57 +++++++++++++++++++++++++++++++++++++++++++-
a995b3
 bins/unzzipcat-mix.c | 57 +++++++++++++++++++++++++++++++++++++++++++-
a995b3
 bins/unzzipcat-zip.c | 57 +++++++++++++++++++++++++++++++++++++++++++-
a995b3
 4 files changed, 224 insertions(+), 4 deletions(-)
a995b3
a995b3
diff --git a/bins/unzzipcat-big.c b/bins/unzzipcat-big.c
a995b3
index 982d262..88c4d65 100644
a995b3
--- a/bins/unzzipcat-big.c
a995b3
+++ b/bins/unzzipcat-big.c
a995b3
@@ -53,6 +53,48 @@ static void unzzip_cat_file(FILE* disk, char* name, FILE* out)
a995b3
     }
a995b3
 }
a995b3
 
a995b3
+/*
a995b3
+ * NAME: remove_dotdotslash
a995b3
+ * PURPOSE: To remove any "../" components from the given pathname
a995b3
+ * ARGUMENTS: path: path name with maybe "../" components
a995b3
+ * RETURNS: Nothing, "path" is modified in-place
a995b3
+ * NOTE: removing "../" from the path ALWAYS shortens the path, never adds to it!
a995b3
+ *	Also, "path" is not used after creating it.
a995b3
+ *	So modifying "path" in-place is safe to do.
a995b3
+ */
a995b3
+static inline void
a995b3
+remove_dotdotslash(char *path)
a995b3
+{
a995b3
+    /* Note: removing "../" from the path ALWAYS shortens the path, never adds to it! */
a995b3
+    char *dotdotslash;
a995b3
+    int warned = 0;
a995b3
+
a995b3
+    dotdotslash = path;
a995b3
+    while ((dotdotslash = strstr(dotdotslash, "../")) != NULL)
a995b3
+    {
a995b3
+        /*
a995b3
+         * Remove only if at the beginning of the pathname ("../path/name")
a995b3
+         * or when preceded by a slash ("path/../name"),
a995b3
+         * otherwise not ("path../name..")!
a995b3
+         */
a995b3
+        if (dotdotslash == path || dotdotslash[-1] == '/')
a995b3
+        {
a995b3
+            char *src, *dst;
a995b3
+            if (!warned)
a995b3
+            {
a995b3
+                /* Note: the first time through the pathname is still intact */
a995b3
+                fprintf(stderr, "Removing \"../\" path component(s) in %s\n", path);
a995b3
+                warned = 1;
a995b3
+            }
a995b3
+            /* We cannot use strcpy(), as there "The strings may not overlap" */
a995b3
+            for (src = dotdotslash+3, dst=dotdotslash; (*dst = *src) != '\0'; src++, dst++)
a995b3
+                ;
a995b3
+        }
a995b3
+        else
a995b3
+            dotdotslash +=3;	/* skip this instance to prevent infinite loop */
a995b3
+    }
a995b3
+}
a995b3
+
a995b3
 static void makedirs(const char* name)
a995b3
 {
a995b3
       char* p = strrchr(name, '/');
a995b3
@@ -70,6 +112,16 @@ static void makedirs(const char* name)
a995b3
 
a995b3
 static FILE* create_fopen(char* name, char* mode, int subdirs)
a995b3
 {
a995b3
+   char *name_stripped;
a995b3
+   FILE *fp;
a995b3
+   int mustfree = 0;
a995b3
+
a995b3
+   if ((name_stripped = strdup(name)) != NULL)
a995b3
+   {
a995b3
+       remove_dotdotslash(name_stripped);
a995b3
+       name = name_stripped;
a995b3
+       mustfree = 1;
a995b3
+   }
a995b3
    if (subdirs)
a995b3
    {
a995b3
       char* p = strrchr(name, '/');
a995b3
@@ -79,7 +131,10 @@ static FILE* create_fopen(char* name, char* mode, int subdirs)
a995b3
           free (dir_name);
a995b3
       }
a995b3
    }
a995b3
-   return fopen(name, mode);      
a995b3
+   fp = fopen(name, mode);
a995b3
+   if (mustfree)
a995b3
+       free(name_stripped);
a995b3
+    return fp;
a995b3
 }
a995b3
 
a995b3
 
a995b3
diff --git a/bins/unzzipcat-mem.c b/bins/unzzipcat-mem.c
a995b3
index 9bc966b..793bde8 100644
a995b3
--- a/bins/unzzipcat-mem.c
a995b3
+++ b/bins/unzzipcat-mem.c
a995b3
@@ -58,6 +58,48 @@ static void unzzip_mem_disk_cat_file(ZZIP_MEM_DISK* disk, char* name, FILE* out)
a995b3
     }
a995b3
 }
a995b3
 
a995b3
+/*
a995b3
+ * NAME: remove_dotdotslash
a995b3
+ * PURPOSE: To remove any "../" components from the given pathname
a995b3
+ * ARGUMENTS: path: path name with maybe "../" components
a995b3
+ * RETURNS: Nothing, "path" is modified in-place
a995b3
+ * NOTE: removing "../" from the path ALWAYS shortens the path, never adds to it!
a995b3
+ *	Also, "path" is not used after creating it.
a995b3
+ *	So modifying "path" in-place is safe to do.
a995b3
+ */
a995b3
+static inline void
a995b3
+remove_dotdotslash(char *path)
a995b3
+{
a995b3
+    /* Note: removing "../" from the path ALWAYS shortens the path, never adds to it! */
a995b3
+    char *dotdotslash;
a995b3
+    int warned = 0;
a995b3
+
a995b3
+    dotdotslash = path;
a995b3
+    while ((dotdotslash = strstr(dotdotslash, "../")) != NULL)
a995b3
+    {
a995b3
+        /*
a995b3
+         * Remove only if at the beginning of the pathname ("../path/name")
a995b3
+         * or when preceded by a slash ("path/../name"),
a995b3
+         * otherwise not ("path../name..")!
a995b3
+         */
a995b3
+        if (dotdotslash == path || dotdotslash[-1] == '/')
a995b3
+        {
a995b3
+            char *src, *dst;
a995b3
+            if (!warned)
a995b3
+            {
a995b3
+                /* Note: the first time through the pathname is still intact */
a995b3
+                fprintf(stderr, "Removing \"../\" path component(s) in %s\n", path);
a995b3
+                warned = 1;
a995b3
+            }
a995b3
+            /* We cannot use strcpy(), as there "The strings may not overlap" */
a995b3
+            for (src = dotdotslash+3, dst=dotdotslash; (*dst = *src) != '\0'; src++, dst++)
a995b3
+                ;
a995b3
+        }
a995b3
+        else
a995b3
+            dotdotslash +=3;	/* skip this instance to prevent infinite loop */
a995b3
+    }
a995b3
+}
a995b3
+
a995b3
 static void makedirs(const char* name)
a995b3
 {
a995b3
       char* p = strrchr(name, '/');
a995b3
@@ -75,6 +117,16 @@ static void makedirs(const char* name)
a995b3
 
a995b3
 static FILE* create_fopen(char* name, char* mode, int subdirs)
a995b3
 {
a995b3
+   char *name_stripped;
a995b3
+   FILE *fp;
a995b3
+   int mustfree = 0;
a995b3
+
a995b3
+   if ((name_stripped = strdup(name)) != NULL)
a995b3
+   {
a995b3
+       remove_dotdotslash(name_stripped);
a995b3
+       name = name_stripped;
a995b3
+       mustfree = 1;
a995b3
+   }
a995b3
    if (subdirs)
a995b3
    {
a995b3
       char* p = strrchr(name, '/');
a995b3
@@ -84,7 +136,10 @@ static FILE* create_fopen(char* name, char* mode, int subdirs)
a995b3
           free (dir_name);
a995b3
       }
a995b3
    }
a995b3
-   return fopen(name, mode);      
a995b3
+   fp = fopen(name, mode);
a995b3
+   if (mustfree)
a995b3
+       free(name_stripped);
a995b3
+    return fp;
a995b3
 }
a995b3
 
a995b3
 static int unzzip_cat (int argc, char ** argv, int extract)
a995b3
diff --git a/bins/unzzipcat-mix.c b/bins/unzzipcat-mix.c
a995b3
index 91c2f00..73b6ed6 100644
a995b3
--- a/bins/unzzipcat-mix.c
a995b3
+++ b/bins/unzzipcat-mix.c
a995b3
@@ -69,6 +69,48 @@ static void unzzip_cat_file(ZZIP_DIR* disk, char* name, FILE* out)
a995b3
     }
a995b3
 }
a995b3
 
a995b3
+/*
a995b3
+ * NAME: remove_dotdotslash
a995b3
+ * PURPOSE: To remove any "../" components from the given pathname
a995b3
+ * ARGUMENTS: path: path name with maybe "../" components
a995b3
+ * RETURNS: Nothing, "path" is modified in-place
a995b3
+ * NOTE: removing "../" from the path ALWAYS shortens the path, never adds to it!
a995b3
+ *	Also, "path" is not used after creating it.
a995b3
+ *	So modifying "path" in-place is safe to do.
a995b3
+ */
a995b3
+static inline void
a995b3
+remove_dotdotslash(char *path)
a995b3
+{
a995b3
+    /* Note: removing "../" from the path ALWAYS shortens the path, never adds to it! */
a995b3
+    char *dotdotslash;
a995b3
+    int warned = 0;
a995b3
+
a995b3
+    dotdotslash = path;
a995b3
+    while ((dotdotslash = strstr(dotdotslash, "../")) != NULL)
a995b3
+    {
a995b3
+        /*
a995b3
+         * Remove only if at the beginning of the pathname ("../path/name")
a995b3
+         * or when preceded by a slash ("path/../name"),
a995b3
+         * otherwise not ("path../name..")!
a995b3
+         */
a995b3
+        if (dotdotslash == path || dotdotslash[-1] == '/')
a995b3
+        {
a995b3
+            char *src, *dst;
a995b3
+            if (!warned)
a995b3
+            {
a995b3
+                /* Note: the first time through the pathname is still intact */
a995b3
+                fprintf(stderr, "Removing \"../\" path component(s) in %s\n", path);
a995b3
+                warned = 1;
a995b3
+            }
a995b3
+            /* We cannot use strcpy(), as there "The strings may not overlap" */
a995b3
+            for (src = dotdotslash+3, dst=dotdotslash; (*dst = *src) != '\0'; src++, dst++)
a995b3
+                ;
a995b3
+        }
a995b3
+        else
a995b3
+            dotdotslash +=3;	/* skip this instance to prevent infinite loop */
a995b3
+    }
a995b3
+}
a995b3
+
a995b3
 static void makedirs(const char* name)
a995b3
 {
a995b3
       char* p = strrchr(name, '/');
a995b3
@@ -86,6 +128,16 @@ static void makedirs(const char* name)
a995b3
 
a995b3
 static FILE* create_fopen(char* name, char* mode, int subdirs)
a995b3
 {
a995b3
+   char *name_stripped;
a995b3
+   FILE *fp;
a995b3
+   int mustfree = 0;
a995b3
+
a995b3
+   if ((name_stripped = strdup(name)) != NULL)
a995b3
+   {
a995b3
+       remove_dotdotslash(name_stripped);
a995b3
+       name = name_stripped;
a995b3
+       mustfree = 1;
a995b3
+   }
a995b3
    if (subdirs)
a995b3
    {
a995b3
       char* p = strrchr(name, '/');
a995b3
@@ -95,7 +147,10 @@ static FILE* create_fopen(char* name, char* mode, int subdirs)
a995b3
           free (dir_name);
a995b3
       }
a995b3
    }
a995b3
-   return fopen(name, mode);      
a995b3
+   fp = fopen(name, mode);
a995b3
+   if (mustfree)
a995b3
+       free(name_stripped);
a995b3
+    return fp;
a995b3
 }
a995b3
 
a995b3
 static int unzzip_cat (int argc, char ** argv, int extract)
a995b3
diff --git a/bins/unzzipcat-zip.c b/bins/unzzipcat-zip.c
a995b3
index 2810f85..7f7f3fa 100644
a995b3
--- a/bins/unzzipcat-zip.c
a995b3
+++ b/bins/unzzipcat-zip.c
a995b3
@@ -69,6 +69,48 @@ static void unzzip_cat_file(ZZIP_DIR* disk, char* name, FILE* out)
a995b3
     }
a995b3
 }
a995b3
 
a995b3
+/*
a995b3
+ * NAME: remove_dotdotslash
a995b3
+ * PURPOSE: To remove any "../" components from the given pathname
a995b3
+ * ARGUMENTS: path: path name with maybe "../" components
a995b3
+ * RETURNS: Nothing, "path" is modified in-place
a995b3
+ * NOTE: removing "../" from the path ALWAYS shortens the path, never adds to it!
a995b3
+ *	Also, "path" is not used after creating it.
a995b3
+ *	So modifying "path" in-place is safe to do.
a995b3
+ */
a995b3
+static inline void
a995b3
+remove_dotdotslash(char *path)
a995b3
+{
a995b3
+    /* Note: removing "../" from the path ALWAYS shortens the path, never adds to it! */
a995b3
+    char *dotdotslash;
a995b3
+    int warned = 0;
a995b3
+
a995b3
+    dotdotslash = path;
a995b3
+    while ((dotdotslash = strstr(dotdotslash, "../")) != NULL)
a995b3
+    {
a995b3
+        /*
a995b3
+         * Remove only if at the beginning of the pathname ("../path/name")
a995b3
+         * or when preceded by a slash ("path/../name"),
a995b3
+         * otherwise not ("path../name..")!
a995b3
+         */
a995b3
+        if (dotdotslash == path || dotdotslash[-1] == '/')
a995b3
+        {
a995b3
+            char *src, *dst;
a995b3
+            if (!warned)
a995b3
+            {
a995b3
+                /* Note: the first time through the pathname is still intact */
a995b3
+                fprintf(stderr, "Removing \"../\" path component(s) in %s\n", path);
a995b3
+                warned = 1;
a995b3
+            }
a995b3
+            /* We cannot use strcpy(), as there "The strings may not overlap" */
a995b3
+            for (src = dotdotslash+3, dst=dotdotslash; (*dst = *src) != '\0'; src++, dst++)
a995b3
+                ;
a995b3
+        }
a995b3
+        else
a995b3
+            dotdotslash +=3;	/* skip this instance to prevent infinite loop */
a995b3
+    }
a995b3
+}
a995b3
+
a995b3
 static void makedirs(const char* name)
a995b3
 {
a995b3
       char* p = strrchr(name, '/');
a995b3
@@ -86,6 +128,16 @@ static void makedirs(const char* name)
a995b3
 
a995b3
 static FILE* create_fopen(char* name, char* mode, int subdirs)
a995b3
 {
a995b3
+   char *name_stripped;
a995b3
+   FILE *fp;
a995b3
+   int mustfree = 0;
a995b3
+
a995b3
+   if ((name_stripped = strdup(name)) != NULL)
a995b3
+   {
a995b3
+       remove_dotdotslash(name_stripped);
a995b3
+       name = name_stripped;
a995b3
+       mustfree = 1;
a995b3
+   }
a995b3
    if (subdirs)
a995b3
    {
a995b3
       char* p = strrchr(name, '/');
a995b3
@@ -95,7 +147,10 @@ static FILE* create_fopen(char* name, char* mode, int subdirs)
a995b3
           free (dir_name);
a995b3
       }
a995b3
    }
a995b3
-   return fopen(name, mode);
a995b3
+   fp = fopen(name, mode);
a995b3
+   if (mustfree)
a995b3
+       free(name_stripped);
a995b3
+    return fp;
a995b3
 }
a995b3
 
a995b3
 static int unzzip_cat (int argc, char ** argv, int extract)