dd1bcc7abb26094e93636e85520f0d8f81ab0fab.patch 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. From dd1bcc7abb26094e93636e85520f0d8f81ab0fab Mon Sep 17 00:00:00 2001
  2. From: 4ugustus <wangdw.augustus@qq.com>
  3. Date: Sat, 11 Jun 2022 09:31:43 +0000
  4. Subject: [PATCH] fix the FPE in tiffcrop (#415, #427, and #428)
  5. ---
  6. libtiff/tif_aux.c | 9 +++++++
  7. libtiff/tiffiop.h | 1 +
  8. tools/tiffcrop.c | 62 ++++++++++++++++++++++++++---------------------
  9. 3 files changed, 44 insertions(+), 28 deletions(-)
  10. diff --git a/libtiff/tif_aux.c b/libtiff/tif_aux.c
  11. index 140f26c7..5b88c8d0 100644
  12. --- a/libtiff/tif_aux.c
  13. +++ b/libtiff/tif_aux.c
  14. @@ -402,6 +402,15 @@ float _TIFFClampDoubleToFloat( double val )
  15. return (float)val;
  16. }
  17. +uint32_t _TIFFClampDoubleToUInt32(double val)
  18. +{
  19. + if( val < 0 )
  20. + return 0;
  21. + if( val > 0xFFFFFFFFU || val != val )
  22. + return 0xFFFFFFFFU;
  23. + return (uint32_t)val;
  24. +}
  25. +
  26. int _TIFFSeekOK(TIFF* tif, toff_t off)
  27. {
  28. /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */
  29. diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h
  30. index e3af461d..4e8bdac2 100644
  31. --- a/libtiff/tiffiop.h
  32. +++ b/libtiff/tiffiop.h
  33. @@ -365,6 +365,7 @@ extern double _TIFFUInt64ToDouble(uint64_t);
  34. extern float _TIFFUInt64ToFloat(uint64_t);
  35. extern float _TIFFClampDoubleToFloat(double);
  36. +extern uint32_t _TIFFClampDoubleToUInt32(double);
  37. extern tmsize_t
  38. _TIFFReadEncodedStripAndAllocBuffer(TIFF* tif, uint32_t strip,
  39. diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
  40. index 1f827b2b..90286a5e 100644
  41. --- a/tools/tiffcrop.c
  42. +++ b/tools/tiffcrop.c
  43. @@ -5268,17 +5268,17 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image,
  44. {
  45. if ((crop->res_unit == RESUNIT_INCH) || (crop->res_unit == RESUNIT_CENTIMETER))
  46. {
  47. - x1 = (uint32_t) (crop->corners[i].X1 * scale * xres);
  48. - x2 = (uint32_t) (crop->corners[i].X2 * scale * xres);
  49. - y1 = (uint32_t) (crop->corners[i].Y1 * scale * yres);
  50. - y2 = (uint32_t) (crop->corners[i].Y2 * scale * yres);
  51. + x1 = _TIFFClampDoubleToUInt32(crop->corners[i].X1 * scale * xres);
  52. + x2 = _TIFFClampDoubleToUInt32(crop->corners[i].X2 * scale * xres);
  53. + y1 = _TIFFClampDoubleToUInt32(crop->corners[i].Y1 * scale * yres);
  54. + y2 = _TIFFClampDoubleToUInt32(crop->corners[i].Y2 * scale * yres);
  55. }
  56. else
  57. {
  58. - x1 = (uint32_t) (crop->corners[i].X1);
  59. - x2 = (uint32_t) (crop->corners[i].X2);
  60. - y1 = (uint32_t) (crop->corners[i].Y1);
  61. - y2 = (uint32_t) (crop->corners[i].Y2);
  62. + x1 = _TIFFClampDoubleToUInt32(crop->corners[i].X1);
  63. + x2 = _TIFFClampDoubleToUInt32(crop->corners[i].X2);
  64. + y1 = _TIFFClampDoubleToUInt32(crop->corners[i].Y1);
  65. + y2 = _TIFFClampDoubleToUInt32(crop->corners[i].Y2);
  66. }
  67. /* a) Region needs to be within image sizes 0.. width-1; 0..length-1
  68. * b) Corners are expected to be submitted as top-left to bottom-right.
  69. @@ -5357,17 +5357,17 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image,
  70. {
  71. if (crop->res_unit != RESUNIT_INCH && crop->res_unit != RESUNIT_CENTIMETER)
  72. { /* User has specified pixels as reference unit */
  73. - tmargin = (uint32_t)(crop->margins[0]);
  74. - lmargin = (uint32_t)(crop->margins[1]);
  75. - bmargin = (uint32_t)(crop->margins[2]);
  76. - rmargin = (uint32_t)(crop->margins[3]);
  77. + tmargin = _TIFFClampDoubleToUInt32(crop->margins[0]);
  78. + lmargin = _TIFFClampDoubleToUInt32(crop->margins[1]);
  79. + bmargin = _TIFFClampDoubleToUInt32(crop->margins[2]);
  80. + rmargin = _TIFFClampDoubleToUInt32(crop->margins[3]);
  81. }
  82. else
  83. { /* inches or centimeters specified */
  84. - tmargin = (uint32_t)(crop->margins[0] * scale * yres);
  85. - lmargin = (uint32_t)(crop->margins[1] * scale * xres);
  86. - bmargin = (uint32_t)(crop->margins[2] * scale * yres);
  87. - rmargin = (uint32_t)(crop->margins[3] * scale * xres);
  88. + tmargin = _TIFFClampDoubleToUInt32(crop->margins[0] * scale * yres);
  89. + lmargin = _TIFFClampDoubleToUInt32(crop->margins[1] * scale * xres);
  90. + bmargin = _TIFFClampDoubleToUInt32(crop->margins[2] * scale * yres);
  91. + rmargin = _TIFFClampDoubleToUInt32(crop->margins[3] * scale * xres);
  92. }
  93. if ((lmargin + rmargin) > image->width)
  94. @@ -5397,24 +5397,24 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image,
  95. if (crop->res_unit != RESUNIT_INCH && crop->res_unit != RESUNIT_CENTIMETER)
  96. {
  97. if (crop->crop_mode & CROP_WIDTH)
  98. - width = (uint32_t)crop->width;
  99. + width = _TIFFClampDoubleToUInt32(crop->width);
  100. else
  101. width = image->width - lmargin - rmargin;
  102. if (crop->crop_mode & CROP_LENGTH)
  103. - length = (uint32_t)crop->length;
  104. + length = _TIFFClampDoubleToUInt32(crop->length);
  105. else
  106. length = image->length - tmargin - bmargin;
  107. }
  108. else
  109. {
  110. if (crop->crop_mode & CROP_WIDTH)
  111. - width = (uint32_t)(crop->width * scale * image->xres);
  112. + width = _TIFFClampDoubleToUInt32(crop->width * scale * image->xres);
  113. else
  114. width = image->width - lmargin - rmargin;
  115. if (crop->crop_mode & CROP_LENGTH)
  116. - length = (uint32_t)(crop->length * scale * image->yres);
  117. + length = _TIFFClampDoubleToUInt32(crop->length * scale * image->yres);
  118. else
  119. length = image->length - tmargin - bmargin;
  120. }
  121. @@ -5868,13 +5868,13 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image,
  122. {
  123. if (page->res_unit == RESUNIT_INCH || page->res_unit == RESUNIT_CENTIMETER)
  124. { /* inches or centimeters specified */
  125. - hmargin = (uint32_t)(page->hmargin * scale * page->hres * ((image->bps + 7) / 8));
  126. - vmargin = (uint32_t)(page->vmargin * scale * page->vres * ((image->bps + 7) / 8));
  127. + hmargin = _TIFFClampDoubleToUInt32(page->hmargin * scale * page->hres * ((image->bps + 7) / 8));
  128. + vmargin = _TIFFClampDoubleToUInt32(page->vmargin * scale * page->vres * ((image->bps + 7) / 8));
  129. }
  130. else
  131. { /* Otherwise user has specified pixels as reference unit */
  132. - hmargin = (uint32_t)(page->hmargin * scale * ((image->bps + 7) / 8));
  133. - vmargin = (uint32_t)(page->vmargin * scale * ((image->bps + 7) / 8));
  134. + hmargin = _TIFFClampDoubleToUInt32(page->hmargin * scale * ((image->bps + 7) / 8));
  135. + vmargin = _TIFFClampDoubleToUInt32(page->vmargin * scale * ((image->bps + 7) / 8));
  136. }
  137. if ((hmargin * 2.0) > (pwidth * page->hres))
  138. @@ -5912,13 +5912,13 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image,
  139. {
  140. if (page->mode & PAGE_MODE_PAPERSIZE )
  141. {
  142. - owidth = (uint32_t)((pwidth * page->hres) - (hmargin * 2));
  143. - olength = (uint32_t)((plength * page->vres) - (vmargin * 2));
  144. + owidth = _TIFFClampDoubleToUInt32((pwidth * page->hres) - (hmargin * 2));
  145. + olength = _TIFFClampDoubleToUInt32((plength * page->vres) - (vmargin * 2));
  146. }
  147. else
  148. {
  149. - owidth = (uint32_t)(iwidth - (hmargin * 2 * page->hres));
  150. - olength = (uint32_t)(ilength - (vmargin * 2 * page->vres));
  151. + owidth = _TIFFClampDoubleToUInt32(iwidth - (hmargin * 2 * page->hres));
  152. + olength = _TIFFClampDoubleToUInt32(ilength - (vmargin * 2 * page->vres));
  153. }
  154. }
  155. @@ -5927,6 +5927,12 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image,
  156. if (olength > ilength)
  157. olength = ilength;
  158. + if (owidth == 0 || olength == 0)
  159. + {
  160. + TIFFError("computeOutputPixelOffsets", "Integer overflow when calculating the number of pages");
  161. + exit(EXIT_FAILURE);
  162. + }
  163. +
  164. /* Compute the number of pages required for Portrait or Landscape */
  165. switch (page->orient)
  166. {
  167. --
  168. GitLab