gegl-0.2.0-CVE-2012-4433.patch 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. From ffa77a246652c7e706d690682fe659f50fbe5656 Mon Sep 17 00:00:00 2001
  2. From: Nils Philippsen <nils@redhat.com>
  3. Date: Mon, 1 Jul 2013 12:03:51 +0200
  4. Subject: [PATCH] patch: CVE-2012-4433
  5. Squashed commit of the following:
  6. commit 2a9071e2dc4cfe1aaa7a726805985281936f9874
  7. Author: Nils Philippsen <nils@redhat.com>
  8. Date: Tue Oct 16 16:57:37 2012 +0200
  9. ppm-load: bring comment in line with reality
  10. (cherry picked from commit 6975a9cfeaf0698b42ac81b1c2f00d13c8755453)
  11. commit 8bb88ebf78e54837322d3be74688f98800e9f33a
  12. Author: Nils Philippsen <nils@redhat.com>
  13. Date: Tue Oct 16 16:56:40 2012 +0200
  14. ppm-load: CVE-2012-4433: add plausibility checks for header fields
  15. Refuse values that are non-decimal, negative or overflow the target
  16. type.
  17. (cherry picked from commit 4757cdf73d3675478d645a3ec8250ba02168a230)
  18. commit 2b099886969bf055a8635d06a4d89f20fed1ee42
  19. Author: Nils Philippsen <nils@redhat.com>
  20. Date: Tue Oct 16 16:58:27 2012 +0200
  21. ppm-load: CVE-2012-4433: don't overflow memory allocation
  22. Carefully selected width/height values could cause the size of a later
  23. allocation to overflow, resulting in a buffer much too small to store
  24. the data which would then written beyond its end.
  25. (cherry picked from commit 1e92e5235ded0415d555aa86066b8e4041ee5a53)
  26. ---
  27. operations/external/ppm-load.c | 64 +++++++++++++++++++++++++++++++++++-------
  28. 1 file changed, 54 insertions(+), 10 deletions(-)
  29. diff --git a/operations/external/ppm-load.c b/operations/external/ppm-load.c
  30. index efe6d56..e22521c 100644
  31. --- a/operations/external/ppm-load.c
  32. +++ b/operations/external/ppm-load.c
  33. @@ -36,6 +36,7 @@ gegl_chant_file_path (path, _("File"), "", _("Path of file to load."))
  34. #include "gegl-chant.h"
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. +#include <errno.h>
  38. typedef enum {
  39. PIXMAP_ASCII = 51,
  40. @@ -44,8 +45,8 @@ typedef enum {
  41. typedef struct {
  42. map_type type;
  43. - gint width;
  44. - gint height;
  45. + glong width;
  46. + glong height;
  47. gsize numsamples; /* width * height * channels */
  48. gsize bpc; /* bytes per channel */
  49. guchar *data;
  50. @@ -61,7 +62,7 @@ ppm_load_read_header(FILE *fp,
  51. gchar header[MAX_CHARS_IN_ROW];
  52. gint maxval;
  53. - /* Check the PPM file Type P2 or P5 */
  54. + /* Check the PPM file Type P3 or P6 */
  55. fgets (header,MAX_CHARS_IN_ROW,fp);
  56. if (header[0] != ASCII_P ||
  57. @@ -82,12 +83,33 @@ ppm_load_read_header(FILE *fp,
  58. }
  59. /* Get Width and Height */
  60. - img->width = strtol (header,&ptr,0);
  61. - img->height = atoi (ptr);
  62. - img->numsamples = img->width * img->height * CHANNEL_COUNT;
  63. + errno = 0;
  64. + img->width = strtol (header,&ptr,10);
  65. + if (errno)
  66. + {
  67. + g_warning ("Error reading width: %s", strerror(errno));
  68. + return FALSE;
  69. + }
  70. + else if (img->width < 0)
  71. + {
  72. + g_warning ("Error: width is negative");
  73. + return FALSE;
  74. + }
  75. +
  76. + img->height = strtol (ptr,&ptr,10);
  77. + if (errno)
  78. + {
  79. + g_warning ("Error reading height: %s", strerror(errno));
  80. + return FALSE;
  81. + }
  82. + else if (img->width < 0)
  83. + {
  84. + g_warning ("Error: height is negative");
  85. + return FALSE;
  86. + }
  87. fgets (header,MAX_CHARS_IN_ROW,fp);
  88. - maxval = strtol (header,&ptr,0);
  89. + maxval = strtol (header,&ptr,10);
  90. if ((maxval != 255) && (maxval != 65535))
  91. {
  92. @@ -109,6 +131,16 @@ ppm_load_read_header(FILE *fp,
  93. g_warning ("%s: Programmer stupidity error", G_STRLOC);
  94. }
  95. + /* Later on, img->numsamples is multiplied with img->bpc to allocate
  96. + * memory. Ensure it doesn't overflow. */
  97. + if (!img->width || !img->height ||
  98. + G_MAXSIZE / img->width / img->height / CHANNEL_COUNT < img->bpc)
  99. + {
  100. + g_warning ("Illegal width/height: %ld/%ld", img->width, img->height);
  101. + return FALSE;
  102. + }
  103. + img->numsamples = img->width * img->height * CHANNEL_COUNT;
  104. +
  105. return TRUE;
  106. }
  107. @@ -229,12 +261,24 @@ process (GeglOperation *operation,
  108. if (!ppm_load_read_header (fp, &img))
  109. goto out;
  110. - rect.height = img.height;
  111. - rect.width = img.width;
  112. -
  113. /* Allocating Array Size */
  114. +
  115. + /* Should use g_try_malloc(), but this causes crashes elsewhere because the
  116. + * error signalled by returning FALSE isn't properly acted upon. Therefore
  117. + * g_malloc() is used here which aborts if the requested memory size can't be
  118. + * allocated causing a controlled crash. */
  119. img.data = (guchar*) g_malloc (img.numsamples * img.bpc);
  120. + /* No-op without g_try_malloc(), see above. */
  121. + if (! img.data)
  122. + {
  123. + g_warning ("Couldn't allocate %" G_GSIZE_FORMAT " bytes, giving up.", ((gsize)img.numsamples * img.bpc));
  124. + goto out;
  125. + }
  126. +
  127. + rect.height = img.height;
  128. + rect.width = img.width;
  129. +
  130. switch (img.bpc)
  131. {
  132. case 1:
  133. --
  134. 1.8.3.1