1003-fastjar-CVE-2010-0831.patch 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 2010-06-10 Jakub Jelinek <jakub@redhat.com>
  2. Dan Rosenberg <dan.j.rosenberg@gmail.com>
  3. * jartool.c (extract_jar): Fix up checks for traversal to parent
  4. directories, disallow absolute paths, make the code slightly more
  5. efficient.
  6. --- fastjar-0.97/jartool.c.jj 2009-09-07 00:10:47.000000000 +0200
  7. +++ fastjar-0.97/jartool.c 2010-06-08 20:00:29.000000000 +0200
  8. @@ -1730,7 +1730,17 @@ int extract_jar(int fd, const char **fil
  9. struct stat sbuf;
  10. int depth = 0;
  11. - tmp_buff = malloc(sizeof(char) * strlen((const char *)filename));
  12. + if(*filename == '/'){
  13. + fprintf(stderr, "Absolute path names are not allowed.\n");
  14. + exit(EXIT_FAILURE);
  15. + }
  16. +
  17. + tmp_buff = malloc(strlen((const char *)filename));
  18. +
  19. + if(tmp_buff == NULL) {
  20. + fprintf(stderr, "Out of memory.\n");
  21. + exit(EXIT_FAILURE);
  22. + }
  23. for(;;){
  24. const ub1 *idx = (const unsigned char *)strchr((const char *)start, '/');
  25. @@ -1738,25 +1748,28 @@ int extract_jar(int fd, const char **fil
  26. if(idx == NULL)
  27. break;
  28. else if(idx == start){
  29. + tmp_buff[idx - filename] = '/';
  30. start++;
  31. continue;
  32. }
  33. - start = idx + 1;
  34. - strncpy(tmp_buff, (const char *)filename, (idx - filename));
  35. - tmp_buff[(idx - filename)] = '\0';
  36. + memcpy(tmp_buff + (start - filename), (const char *)start, (idx - start));
  37. + tmp_buff[idx - filename] = '\0';
  38. #ifdef DEBUG
  39. printf("checking the existance of %s\n", tmp_buff);
  40. #endif
  41. - if(strcmp(tmp_buff, "..") == 0){
  42. + if(idx - start == 2 && memcmp(start, "..", 2) == 0){
  43. --depth;
  44. if (depth < 0){
  45. fprintf(stderr, "Traversal to parent directories during unpacking!\n");
  46. exit(EXIT_FAILURE);
  47. }
  48. - } else if (strcmp(tmp_buff, ".") != 0)
  49. + } else if (idx - start != 1 || *start != '.')
  50. ++depth;
  51. +
  52. + start = idx + 1;
  53. +
  54. if(stat(tmp_buff, &sbuf) < 0){
  55. if(errno != ENOENT)
  56. exit_on_error("stat");
  57. @@ -1765,6 +1778,7 @@ int extract_jar(int fd, const char **fil
  58. #ifdef DEBUG
  59. printf("Directory exists\n");
  60. #endif
  61. + tmp_buff[idx - filename] = '/';
  62. continue;
  63. }else {
  64. fprintf(stderr, "Hmmm.. %s exists but isn't a directory!\n",
  65. @@ -1781,10 +1795,11 @@ int extract_jar(int fd, const char **fil
  66. if(verbose && handle)
  67. printf("%10s: %s/\n", "created", tmp_buff);
  68. + tmp_buff[idx - filename] = '/';
  69. }
  70. /* only a directory */
  71. - if(strlen((const char *)start) == 0)
  72. + if(*start == '\0')
  73. dir = TRUE;
  74. #ifdef DEBUG
  75. @@ -1792,7 +1807,7 @@ int extract_jar(int fd, const char **fil
  76. #endif
  77. /* If the entry was just a directory, don't write to file, etc */
  78. - if(strlen((const char *)start) == 0)
  79. + if(*start == '\0')
  80. f_fd = -1;
  81. free(tmp_buff);
  82. @@ -1876,7 +1891,8 @@ int extract_jar(int fd, const char **fil
  83. exit(EXIT_FAILURE);
  84. }
  85. - close(f_fd);
  86. + if (f_fd != -1)
  87. + close(f_fd);
  88. if(verbose && dir == FALSE && handle)
  89. printf("%10s: %s\n",