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