gd-CVE-2019-6978.patch 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. Index: libgd-2.2.5/src/gd_gif_out.c
  2. ===================================================================
  3. --- libgd-2.2.5.orig/src/gd_gif_out.c 2017-08-30 13:05:54.000000000 +0200
  4. +++ libgd-2.2.5/src/gd_gif_out.c 2019-01-31 09:47:44.703693790 +0100
  5. @@ -99,6 +99,7 @@ static void char_init(GifCtx *ctx);
  6. static void char_out(int c, GifCtx *ctx);
  7. static void flush_char(GifCtx *ctx);
  8. +static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out);
  9. @@ -131,8 +132,11 @@ BGD_DECLARE(void *) gdImageGifPtr(gdImag
  10. void *rv;
  11. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  12. if (out == NULL) return NULL;
  13. - gdImageGifCtx(im, out);
  14. - rv = gdDPExtractData(out, size);
  15. + if (!_gdImageGifCtx(im, out)) {
  16. + rv = gdDPExtractData(out, size);
  17. + } else {
  18. + rv = NULL;
  19. + }
  20. out->gd_free(out);
  21. return rv;
  22. }
  23. @@ -221,6 +225,12 @@ BGD_DECLARE(void) gdImageGif(gdImagePtr
  24. */
  25. BGD_DECLARE(void) gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
  26. {
  27. + _gdImageGifCtx(im, out);
  28. +}
  29. +
  30. +/* returns 0 on success, 1 on failure */
  31. +static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
  32. +{
  33. gdImagePtr pim = 0, tim = im;
  34. int interlace, BitsPerPixel;
  35. interlace = im->interlace;
  36. @@ -231,7 +241,7 @@ BGD_DECLARE(void) gdImageGifCtx(gdImageP
  37. based temporary image. */
  38. pim = gdImageCreatePaletteFromTrueColor(im, 1, 256);
  39. if(!pim) {
  40. - return;
  41. + return 1;
  42. }
  43. tim = pim;
  44. }
  45. @@ -247,6 +257,8 @@ BGD_DECLARE(void) gdImageGifCtx(gdImageP
  46. /* Destroy palette based temporary image. */
  47. gdImageDestroy( pim);
  48. }
  49. +
  50. + return 0;
  51. }
  52. Index: libgd-2.2.5/src/gd_jpeg.c
  53. ===================================================================
  54. --- libgd-2.2.5.orig/src/gd_jpeg.c 2017-08-30 13:05:54.000000000 +0200
  55. +++ libgd-2.2.5/src/gd_jpeg.c 2019-01-31 09:47:44.707693815 +0100
  56. @@ -123,6 +123,8 @@ static void fatal_jpeg_error(j_common_pt
  57. exit(99);
  58. }
  59. +static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality);
  60. +
  61. /*
  62. * Write IM to OUTFILE as a JFIF-formatted JPEG image, using quality
  63. * QUALITY. If QUALITY is in the range 0-100, increasing values
  64. @@ -237,8 +239,11 @@ BGD_DECLARE(void *) gdImageJpegPtr(gdIma
  65. void *rv;
  66. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  67. if (out == NULL) return NULL;
  68. - gdImageJpegCtx(im, out, quality);
  69. - rv = gdDPExtractData(out, size);
  70. + if (!_gdImageJpegCtx(im, out, quality)) {
  71. + rv = gdDPExtractData(out, size);
  72. + } else {
  73. + rv = NULL;
  74. + }
  75. out->gd_free(out);
  76. return rv;
  77. }
  78. @@ -260,6 +265,12 @@ void jpeg_gdIOCtx_dest(j_compress_ptr ci
  79. */
  80. BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
  81. {
  82. + _gdImageJpegCtx(im, outfile, quality);
  83. +}
  84. +
  85. +/* returns 0 on success, 1 on failure */
  86. +static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
  87. +{
  88. struct jpeg_compress_struct cinfo;
  89. struct jpeg_error_mgr jerr;
  90. int i, j, jidx;
  91. @@ -293,7 +304,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImage
  92. if(row) {
  93. gdFree(row);
  94. }
  95. - return;
  96. + return 1;
  97. }
  98. cinfo.err->emit_message = jpeg_emit_message;
  99. @@ -334,7 +345,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImage
  100. if(row == 0) {
  101. gd_error("gd-jpeg: error: unable to allocate JPEG row structure: gdCalloc returns NULL\n");
  102. jpeg_destroy_compress(&cinfo);
  103. - return;
  104. + return 1;
  105. }
  106. rowptr[0] = row;
  107. @@ -411,6 +422,7 @@ BGD_DECLARE(void) gdImageJpegCtx(gdImage
  108. jpeg_finish_compress(&cinfo);
  109. jpeg_destroy_compress(&cinfo);
  110. gdFree(row);
  111. + return 0;
  112. }
  113. Index: libgd-2.2.5/src/gd_wbmp.c
  114. ===================================================================
  115. --- libgd-2.2.5.orig/src/gd_wbmp.c 2017-08-30 13:05:54.000000000 +0200
  116. +++ libgd-2.2.5/src/gd_wbmp.c 2019-01-31 09:47:44.707693815 +0100
  117. @@ -88,6 +88,8 @@ int gd_getin(void *in)
  118. return (gdGetC((gdIOCtx *)in));
  119. }
  120. +static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
  121. +
  122. /*
  123. Function: gdImageWBMPCtx
  124. @@ -101,13 +103,19 @@ int gd_getin(void *in)
  125. */
  126. BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
  127. {
  128. + _gdImageWBMPCtx(image, fg, out);
  129. +}
  130. +
  131. +/* returns 0 on success, 1 on failure */
  132. +static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
  133. +{
  134. int x, y, pos;
  135. Wbmp *wbmp;
  136. /* create the WBMP */
  137. if((wbmp = createwbmp(gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL) {
  138. gd_error("Could not create WBMP\n");
  139. - return;
  140. + return 1;
  141. }
  142. /* fill up the WBMP structure */
  143. @@ -123,11 +131,15 @@ BGD_DECLARE(void) gdImageWBMPCtx(gdImage
  144. /* write the WBMP to a gd file descriptor */
  145. if(writewbmp(wbmp, &gd_putout, out)) {
  146. + freewbmp(wbmp);
  147. gd_error("Could not save WBMP\n");
  148. + return 1;
  149. }
  150. /* des submitted this bugfix: gdFree the memory. */
  151. freewbmp(wbmp);
  152. +
  153. + return 0;
  154. }
  155. /*
  156. @@ -271,8 +283,11 @@ BGD_DECLARE(void *) gdImageWBMPPtr(gdIma
  157. void *rv;
  158. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  159. if (out == NULL) return NULL;
  160. - gdImageWBMPCtx(im, fg, out);
  161. - rv = gdDPExtractData(out, size);
  162. + if (!_gdImageWBMPCtx(im, fg, out)) {
  163. + rv = gdDPExtractData(out, size);
  164. + } else {
  165. + rv = NULL;
  166. + }
  167. out->gd_free(out);
  168. return rv;
  169. }