Blame SOURCES/fastjar-CVE-2010-0831.patch

56d343
2010-06-10  Jakub Jelinek  <jakub@redhat.com>
56d343
	    Dan Rosenberg  <dan.j.rosenberg@gmail.com>
56d343
56d343
	* jartool.c (extract_jar): Fix up checks for traversal to parent
56d343
	directories, disallow absolute paths, make the code slightly more
56d343
	efficient.
56d343
56d343
--- fastjar-0.97/jartool.c.jj	2009-09-07 00:10:47.000000000 +0200
56d343
+++ fastjar-0.97/jartool.c	2010-06-08 20:00:29.000000000 +0200
56d343
@@ -1730,7 +1730,17 @@ int extract_jar(int fd, const char **fil
56d343
       struct stat sbuf;
56d343
       int depth = 0;
56d343
 
56d343
-      tmp_buff = malloc(sizeof(char) * strlen((const char *)filename));
56d343
+      if(*filename == '/'){
56d343
+	fprintf(stderr, "Absolute path names are not allowed.\n");
56d343
+	exit(EXIT_FAILURE);
56d343
+      }
56d343
+
56d343
+      tmp_buff = malloc(strlen((const char *)filename));
56d343
+
56d343
+      if(tmp_buff == NULL) {
56d343
+	fprintf(stderr, "Out of memory.\n");
56d343
+	exit(EXIT_FAILURE);
56d343
+      }
56d343
 
56d343
       for(;;){
56d343
         const ub1 *idx = (const unsigned char *)strchr((const char *)start, '/');
56d343
@@ -1738,25 +1748,28 @@ int extract_jar(int fd, const char **fil
56d343
         if(idx == NULL)
56d343
           break;
56d343
         else if(idx == start){
56d343
+	  tmp_buff[idx - filename] = '/';
56d343
           start++;
56d343
           continue;
56d343
         }
56d343
-        start = idx + 1;
56d343
 
56d343
-        strncpy(tmp_buff, (const char *)filename, (idx - filename));
56d343
-        tmp_buff[(idx - filename)] = '\0';
56d343
+	memcpy(tmp_buff + (start - filename), (const char *)start, (idx - start));
56d343
+	tmp_buff[idx - filename] = '\0';
56d343
 
56d343
 #ifdef DEBUG    
56d343
         printf("checking the existance of %s\n", tmp_buff);
56d343
 #endif
56d343
-	if(strcmp(tmp_buff, "..") == 0){
56d343
+	if(idx - start == 2 && memcmp(start, "..", 2) == 0){
56d343
 	  --depth;
56d343
 	  if (depth < 0){
56d343
 	    fprintf(stderr, "Traversal to parent directories during unpacking!\n");
56d343
 	    exit(EXIT_FAILURE);
56d343
 	  }
56d343
-	} else if (strcmp(tmp_buff, ".") != 0)
56d343
+	} else if (idx - start != 1 || *start != '.')
56d343
 	  ++depth;
56d343
+
56d343
+        start = idx + 1;
56d343
+
56d343
         if(stat(tmp_buff, &sbuf) < 0){
56d343
           if(errno != ENOENT)
56d343
             exit_on_error("stat");
56d343
@@ -1765,6 +1778,7 @@ int extract_jar(int fd, const char **fil
56d343
 #ifdef DEBUG    
56d343
           printf("Directory exists\n");
56d343
 #endif
56d343
+	  tmp_buff[idx - filename] = '/';
56d343
           continue;
56d343
         }else {
56d343
           fprintf(stderr, "Hmmm.. %s exists but isn't a directory!\n",
56d343
@@ -1781,10 +1795,11 @@ int extract_jar(int fd, const char **fil
56d343
         if(verbose && handle)
56d343
           printf("%10s: %s/\n", "created", tmp_buff);
56d343
 
56d343
+	tmp_buff[idx - filename] = '/';
56d343
       }
56d343
 
56d343
       /* only a directory */
56d343
-      if(strlen((const char *)start) == 0)
56d343
+      if(*start == '\0')
56d343
         dir = TRUE;
56d343
 
56d343
 #ifdef DEBUG    
56d343
@@ -1792,7 +1807,7 @@ int extract_jar(int fd, const char **fil
56d343
 #endif
56d343
 
56d343
       /* If the entry was just a directory, don't write to file, etc */
56d343
-      if(strlen((const char *)start) == 0)
56d343
+      if(*start == '\0')
56d343
         f_fd = -1;
56d343
 
56d343
       free(tmp_buff);
56d343
@@ -1876,7 +1891,8 @@ int extract_jar(int fd, const char **fil
56d343
       exit(EXIT_FAILURE);
56d343
     }
56d343
 
56d343
-    close(f_fd);
56d343
+    if (f_fd != -1)
56d343
+      close(f_fd);
56d343
 
56d343
     if(verbose && dir == FALSE && handle)
56d343
       printf("%10s: %s\n",