You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.8 KiB

  1. commit da99990ba6e1203798a59eb836fc6433ed6e3d66
  2. Author: erouault <erouault>
  3. Date: Fri Dec 2 23:05:51 2016 +0000
  4. * libtiff/tif_pixarlog.c, libtiff/tif_luv.c: fix heap-based buffer
  5. overflow on generation of PixarLog / LUV compressed files, with
  6. ColorMap, TransferFunction attached and nasty plays with bitspersample.
  7. The fix for LUV has not been tested, but suffers from the same kind
  8. of issue of PixarLog.
  9. Reported by Agostino Sarubbo.
  10. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2604
  11. diff --git a/ChangeLog b/ChangeLog
  12. index 0f154d6..93c01f8 100644
  13. --- a/ChangeLog
  14. +++ b/ChangeLog
  15. @@ -1,3 +1,13 @@
  16. +2016-12-03 Even Rouault <even.rouault at spatialys.com>
  17. +
  18. + * libtiff/tif_pixarlog.c, libtiff/tif_luv.c: fix heap-based buffer
  19. + overflow on generation of PixarLog / LUV compressed files, with
  20. + ColorMap, TransferFunction attached and nasty plays with bitspersample.
  21. + The fix for LUV has not been tested, but suffers from the same kind
  22. + of issue of PixarLog.
  23. + Reported by Agostino Sarubbo.
  24. + Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2604
  25. +
  26. 2016-12-02 Even Rouault <even.rouault at spatialys.com>
  27. * tools/tiffcp.c: avoid uint32 underflow in cpDecodedStrips that
  28. diff --git a/libtiff/tif_luv.c b/libtiff/tif_luv.c
  29. index ca08f30..f42ac01 100644
  30. --- a/libtiff/tif_luv.c
  31. +++ b/libtiff/tif_luv.c
  32. @@ -1,4 +1,4 @@
  33. -/* $Id: tif_luv.c,v 1.43 2016-09-04 21:32:56 erouault Exp $ */
  34. +/* $Id: tif_luv.c,v 1.44 2016-12-02 23:05:51 erouault Exp $ */
  35. /*
  36. * Copyright (c) 1997 Greg Ward Larson
  37. @@ -158,6 +158,7 @@
  38. typedef struct logLuvState LogLuvState;
  39. struct logLuvState {
  40. + int encoder_state; /* 1 if encoder correctly initialized */
  41. int user_datafmt; /* user data format */
  42. int encode_meth; /* encoding method */
  43. int pixel_size; /* bytes per pixel */
  44. @@ -1552,6 +1553,7 @@ LogLuvSetupEncode(TIFF* tif)
  45. td->td_photometric, "must be either LogLUV or LogL");
  46. break;
  47. }
  48. + sp->encoder_state = 1;
  49. return (1);
  50. notsupported:
  51. TIFFErrorExt(tif->tif_clientdata, module,
  52. @@ -1563,19 +1565,27 @@ notsupported:
  53. static void
  54. LogLuvClose(TIFF* tif)
  55. {
  56. + LogLuvState* sp = (LogLuvState*) tif->tif_data;
  57. TIFFDirectory *td = &tif->tif_dir;
  58. + assert(sp != 0);
  59. /*
  60. * For consistency, we always want to write out the same
  61. * bitspersample and sampleformat for our TIFF file,
  62. * regardless of the data format being used by the application.
  63. * Since this routine is called after tags have been set but
  64. * before they have been recorded in the file, we reset them here.
  65. + * Note: this is really a nasty approach. See PixarLogClose
  66. */
  67. - td->td_samplesperpixel =
  68. - (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
  69. - td->td_bitspersample = 16;
  70. - td->td_sampleformat = SAMPLEFORMAT_INT;
  71. + if( sp->encoder_state )
  72. + {
  73. + /* See PixarLogClose. Might avoid issues with tags whose size depends
  74. + * on those below, but not completely sure this is enough. */
  75. + td->td_samplesperpixel =
  76. + (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
  77. + td->td_bitspersample = 16;
  78. + td->td_sampleformat = SAMPLEFORMAT_INT;
  79. + }
  80. }
  81. static void
  82. diff --git a/libtiff/tif_pixarlog.c b/libtiff/tif_pixarlog.c
  83. index f4af2ba..9836dce 100644
  84. --- a/libtiff/tif_pixarlog.c
  85. +++ b/libtiff/tif_pixarlog.c
  86. @@ -1,4 +1,4 @@
  87. -/* $Id: tif_pixarlog.c,v 1.48 2016-09-23 22:12:18 erouault Exp $ */
  88. +/* $Id: tif_pixarlog.c,v 1.49 2016-12-02 23:05:51 erouault Exp $ */
  89. /*
  90. * Copyright (c) 1996-1997 Sam Leffler
  91. @@ -1233,8 +1233,10 @@ PixarLogPostEncode(TIFF* tif)
  92. static void
  93. PixarLogClose(TIFF* tif)
  94. {
  95. + PixarLogState* sp = (PixarLogState*) tif->tif_data;
  96. TIFFDirectory *td = &tif->tif_dir;
  97. + assert(sp != 0);
  98. /* In a really sneaky (and really incorrect, and untruthful, and
  99. * troublesome, and error-prone) maneuver that completely goes against
  100. * the spirit of TIFF, and breaks TIFF, on close, we covertly
  101. @@ -1243,8 +1245,19 @@ PixarLogClose(TIFF* tif)
  102. * readers that don't know about PixarLog, or how to set
  103. * the PIXARLOGDATFMT pseudo-tag.
  104. */
  105. - td->td_bitspersample = 8;
  106. - td->td_sampleformat = SAMPLEFORMAT_UINT;
  107. +
  108. + if (sp->state&PLSTATE_INIT) {
  109. + /* We test the state to avoid an issue such as in
  110. + * http://bugzilla.maptools.org/show_bug.cgi?id=2604
  111. + * What appends in that case is that the bitspersample is 1 and
  112. + * a TransferFunction is set. The size of the TransferFunction
  113. + * depends on 1<<bitspersample. So if we increase it, an access
  114. + * out of the buffer will happen at directory flushing.
  115. + * Another option would be to clear those targs.
  116. + */
  117. + td->td_bitspersample = 8;
  118. + td->td_sampleformat = SAMPLEFORMAT_UINT;
  119. + }
  120. }
  121. static void