Blame SOURCES/CVE-2018-17828.patch

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