003-fix_potential_out-of-bound_writes_in_decode_functions.patch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. From 3899f0ab62dd307f63f87ec99aaf289e104f4070 Mon Sep 17 00:00:00 2001
  2. From: erouault <erouault>
  3. Date: Sun, 27 Dec 2015 16:25:11 +0000
  4. Subject: [PATCH] * libtiff/tif_luv.c: fix potential out-of-bound writes in
  5. decode functions in non debug builds by replacing assert()s by regular if
  6. checks (bugzilla #2522). Fix potential out-of-bound reads in case of short
  7. input data.
  8. ---
  9. ChangeLog | 7 +++++++
  10. libtiff/tif_luv.c | 57 +++++++++++++++++++++++++++++++++++++++++++------------
  11. 2 files changed, 52 insertions(+), 12 deletions(-)
  12. diff --git a/ChangeLog b/ChangeLog
  13. index 4beb30b..b8aa23c 100644
  14. --- a/ChangeLog
  15. +++ b/ChangeLog
  16. @@ -1,3 +1,10 @@
  17. +2015-12-27 Even Rouault <even.rouault at spatialys.com>
  18. +
  19. + * libtiff/tif_luv.c: fix potential out-of-bound writes in decode
  20. + functions in non debug builds by replacing assert()s by regular if
  21. + checks (bugzilla #2522).
  22. + Fix potential out-of-bound reads in case of short input data.
  23. +
  24. 2015-12-26 Even Rouault <even.rouault at spatialys.com>
  25. * libtiff/tif_getimage.c: fix out-of-bound reads in TIFFRGBAImage
  26. diff --git a/libtiff/tif_luv.c b/libtiff/tif_luv.c
  27. index 4e328ba..60a174d 100644
  28. --- a/libtiff/tif_luv.c
  29. +++ b/libtiff/tif_luv.c
  30. @@ -1,4 +1,4 @@
  31. -/* $Id: tif_luv.c,v 1.40 2015-06-21 01:09:09 bfriesen Exp $ */
  32. +/* $Id: tif_luv.c,v 1.41 2015-12-27 16:25:11 erouault Exp $ */
  33. /*
  34. * Copyright (c) 1997 Greg Ward Larson
  35. @@ -202,7 +202,11 @@ LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  36. if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
  37. tp = (int16*) op;
  38. else {
  39. - assert(sp->tbuflen >= npixels);
  40. + if(sp->tbuflen < npixels) {
  41. + TIFFErrorExt(tif->tif_clientdata, module,
  42. + "Translation buffer too short");
  43. + return (0);
  44. + }
  45. tp = (int16*) sp->tbuf;
  46. }
  47. _TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0]));
  48. @@ -211,9 +215,11 @@ LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  49. cc = tif->tif_rawcc;
  50. /* get each byte string */
  51. for (shft = 2*8; (shft -= 8) >= 0; ) {
  52. - for (i = 0; i < npixels && cc > 0; )
  53. + for (i = 0; i < npixels && cc > 0; ) {
  54. if (*bp >= 128) { /* run */
  55. - rc = *bp++ + (2-128); /* TODO: potential input buffer overrun when decoding corrupt or truncated data */
  56. + if( cc < 2 )
  57. + break;
  58. + rc = *bp++ + (2-128);
  59. b = (int16)(*bp++ << shft);
  60. cc -= 2;
  61. while (rc-- && i < npixels)
  62. @@ -223,6 +229,7 @@ LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  63. while (--cc && rc-- && i < npixels)
  64. tp[i++] |= (int16)*bp++ << shft;
  65. }
  66. + }
  67. if (i != npixels) {
  68. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  69. TIFFErrorExt(tif->tif_clientdata, module,
  70. @@ -268,13 +275,17 @@ LogLuvDecode24(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  71. if (sp->user_datafmt == SGILOGDATAFMT_RAW)
  72. tp = (uint32 *)op;
  73. else {
  74. - assert(sp->tbuflen >= npixels);
  75. + if(sp->tbuflen < npixels) {
  76. + TIFFErrorExt(tif->tif_clientdata, module,
  77. + "Translation buffer too short");
  78. + return (0);
  79. + }
  80. tp = (uint32 *) sp->tbuf;
  81. }
  82. /* copy to array of uint32 */
  83. bp = (unsigned char*) tif->tif_rawcp;
  84. cc = tif->tif_rawcc;
  85. - for (i = 0; i < npixels && cc > 0; i++) {
  86. + for (i = 0; i < npixels && cc >= 3; i++) {
  87. tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2];
  88. bp += 3;
  89. cc -= 3;
  90. @@ -325,7 +336,11 @@ LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  91. if (sp->user_datafmt == SGILOGDATAFMT_RAW)
  92. tp = (uint32*) op;
  93. else {
  94. - assert(sp->tbuflen >= npixels);
  95. + if(sp->tbuflen < npixels) {
  96. + TIFFErrorExt(tif->tif_clientdata, module,
  97. + "Translation buffer too short");
  98. + return (0);
  99. + }
  100. tp = (uint32*) sp->tbuf;
  101. }
  102. _TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0]));
  103. @@ -334,11 +349,13 @@ LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  104. cc = tif->tif_rawcc;
  105. /* get each byte string */
  106. for (shft = 4*8; (shft -= 8) >= 0; ) {
  107. - for (i = 0; i < npixels && cc > 0; )
  108. + for (i = 0; i < npixels && cc > 0; ) {
  109. if (*bp >= 128) { /* run */
  110. + if( cc < 2 )
  111. + break;
  112. rc = *bp++ + (2-128);
  113. b = (uint32)*bp++ << shft;
  114. - cc -= 2; /* TODO: potential input buffer overrun when decoding corrupt or truncated data */
  115. + cc -= 2;
  116. while (rc-- && i < npixels)
  117. tp[i++] |= b;
  118. } else { /* non-run */
  119. @@ -346,6 +363,7 @@ LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  120. while (--cc && rc-- && i < npixels)
  121. tp[i++] |= (uint32)*bp++ << shft;
  122. }
  123. + }
  124. if (i != npixels) {
  125. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  126. TIFFErrorExt(tif->tif_clientdata, module,
  127. @@ -413,6 +431,7 @@ LogLuvDecodeTile(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  128. static int
  129. LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  130. {
  131. + static const char module[] = "LogL16Encode";
  132. LogLuvState* sp = EncoderState(tif);
  133. int shft;
  134. tmsize_t i;
  135. @@ -433,7 +452,11 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  136. tp = (int16*) bp;
  137. else {
  138. tp = (int16*) sp->tbuf;
  139. - assert(sp->tbuflen >= npixels);
  140. + if(sp->tbuflen < npixels) {
  141. + TIFFErrorExt(tif->tif_clientdata, module,
  142. + "Translation buffer too short");
  143. + return (0);
  144. + }
  145. (*sp->tfunc)(sp, bp, npixels);
  146. }
  147. /* compress each byte string */
  148. @@ -506,6 +529,7 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  149. static int
  150. LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  151. {
  152. + static const char module[] = "LogLuvEncode24";
  153. LogLuvState* sp = EncoderState(tif);
  154. tmsize_t i;
  155. tmsize_t npixels;
  156. @@ -521,7 +545,11 @@ LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  157. tp = (uint32*) bp;
  158. else {
  159. tp = (uint32*) sp->tbuf;
  160. - assert(sp->tbuflen >= npixels);
  161. + if(sp->tbuflen < npixels) {
  162. + TIFFErrorExt(tif->tif_clientdata, module,
  163. + "Translation buffer too short");
  164. + return (0);
  165. + }
  166. (*sp->tfunc)(sp, bp, npixels);
  167. }
  168. /* write out encoded pixels */
  169. @@ -553,6 +581,7 @@ LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  170. static int
  171. LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  172. {
  173. + static const char module[] = "LogLuvEncode32";
  174. LogLuvState* sp = EncoderState(tif);
  175. int shft;
  176. tmsize_t i;
  177. @@ -574,7 +603,11 @@ LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  178. tp = (uint32*) bp;
  179. else {
  180. tp = (uint32*) sp->tbuf;
  181. - assert(sp->tbuflen >= npixels);
  182. + if(sp->tbuflen < npixels) {
  183. + TIFFErrorExt(tif->tif_clientdata, module,
  184. + "Translation buffer too short");
  185. + return (0);
  186. + }
  187. (*sp->tfunc)(sp, bp, npixels);
  188. }
  189. /* compress each byte string */