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.

170 lines
6.5 KiB

  1. From de5385cd882a5ff0970f63f4d93da0cbc87230c2 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <nforro@redhat.com>
  3. Date: Tue, 17 Apr 2018 18:42:09 +0200
  4. Subject: [PATCH] Fix NULL pointer dereference in TIFFPrintDirectory
  5. The TIFFPrintDirectory function relies on the following assumptions,
  6. supposed to be guaranteed by the specification:
  7. (a) A Transfer Function field is only present if the TIFF file has
  8. photometric type < 3.
  9. (b) If SamplesPerPixel > Color Channels, then the ExtraSamples field
  10. has count SamplesPerPixel - (Color Channels) and contains
  11. information about supplementary channels.
  12. While respect of (a) and (b) are essential for the well functioning of
  13. TIFFPrintDirectory, no checks are realized neither by the callee nor
  14. by TIFFPrintDirectory itself. Hence, following scenarios might happen
  15. and trigger the NULL pointer dereference:
  16. (1) TIFF File of photometric type 4 or more has illegal Transfer
  17. Function field.
  18. (2) TIFF File has photometric type 3 or less and defines a
  19. SamplesPerPixel field such that SamplesPerPixel > Color Channels
  20. without defining all extra samples in the ExtraSamples fields.
  21. In this patch, we address both issues with respect of the following
  22. principles:
  23. (A) In the case of (1), the defined transfer table should be printed
  24. safely even if it isn't 'legal'. This allows us to avoid expensive
  25. checks in TIFFPrintDirectory. Also, it is quite possible that
  26. an alternative photometric type would be developed (not part of the
  27. standard) and would allow definition of Transfer Table. We want
  28. libtiff to be able to handle this scenario out of the box.
  29. (B) In the case of (2), the transfer table should be printed at its
  30. right size, that is if TIFF file has photometric type Palette
  31. then the transfer table should have one row and not three, even
  32. if two extra samples are declared.
  33. In order to fulfill (A) we simply add a new 'i < 3' end condition to
  34. the broken TIFFPrintDirectory loop. This makes sure that in any case
  35. where (b) would be respected but not (a), everything stays fine.
  36. (B) is fulfilled by the loop condition
  37. 'i < td->td_samplesperpixel - td->td_extrasamples'. This is enough as
  38. long as (b) is respected.
  39. Naturally, we also make sure (b) is respected. This is done in the
  40. TIFFReadDirectory function by making sure any non-color channel is
  41. counted in ExtraSamples.
  42. This commit addresses CVE-2018-7456.
  43. ---
  44. libtiff/tif_dirread.c | 62 +++++++++++++++++++++++++++++++++++++++++++
  45. libtiff/tif_print.c | 2 +-
  46. 2 files changed, 63 insertions(+), 1 deletion(-)
  47. diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
  48. index 5e62e81..80aaf8d 100644
  49. --- a/libtiff/tif_dirread.c
  50. +++ b/libtiff/tif_dirread.c
  51. @@ -167,6 +167,7 @@ static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uin
  52. static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
  53. static void ChopUpSingleUncompressedStrip(TIFF*);
  54. static uint64 TIFFReadUInt64(const uint8 *value);
  55. +static int _TIFFGetMaxColorChannels(uint16 photometric);
  56. static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount );
  57. @@ -3506,6 +3507,35 @@ static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, c
  58. }
  59. }
  60. +/*
  61. + * Return the maximum number of color channels specified for a given photometric
  62. + * type. 0 is returned if photometric type isn't supported or no default value
  63. + * is defined by the specification.
  64. + */
  65. +static int _TIFFGetMaxColorChannels( uint16 photometric )
  66. +{
  67. + switch (photometric) {
  68. + case PHOTOMETRIC_PALETTE:
  69. + case PHOTOMETRIC_MINISWHITE:
  70. + case PHOTOMETRIC_MINISBLACK:
  71. + return 1;
  72. + case PHOTOMETRIC_YCBCR:
  73. + case PHOTOMETRIC_RGB:
  74. + case PHOTOMETRIC_CIELAB:
  75. + return 3;
  76. + case PHOTOMETRIC_SEPARATED:
  77. + case PHOTOMETRIC_MASK:
  78. + return 4;
  79. + case PHOTOMETRIC_LOGL:
  80. + case PHOTOMETRIC_LOGLUV:
  81. + case PHOTOMETRIC_CFA:
  82. + case PHOTOMETRIC_ITULAB:
  83. + case PHOTOMETRIC_ICCLAB:
  84. + default:
  85. + return 0;
  86. + }
  87. +}
  88. +
  89. /*
  90. * Read the next TIFF directory from a file and convert it to the internal
  91. * format. We read directories sequentially.
  92. @@ -3522,6 +3552,7 @@ TIFFReadDirectory(TIFF* tif)
  93. uint32 fii=FAILED_FII;
  94. toff_t nextdiroff;
  95. int bitspersample_read = FALSE;
  96. + int color_channels;
  97. tif->tif_diroff=tif->tif_nextdiroff;
  98. if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
  99. @@ -4026,6 +4057,37 @@ TIFFReadDirectory(TIFF* tif)
  100. }
  101. }
  102. }
  103. +
  104. + /*
  105. + * Make sure all non-color channels are extrasamples.
  106. + * If it's not the case, define them as such.
  107. + */
  108. + color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
  109. + if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {
  110. + uint16 old_extrasamples;
  111. + uint16 *new_sampleinfo;
  112. +
  113. + TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "
  114. + "color channels and ExtraSamples doesn't match SamplesPerPixel. "
  115. + "Defining non-color channels as ExtraSamples.");
  116. +
  117. + old_extrasamples = tif->tif_dir.td_extrasamples;
  118. + tif->tif_dir.td_extrasamples = (tif->tif_dir.td_samplesperpixel - color_channels);
  119. +
  120. + // sampleinfo should contain information relative to these new extra samples
  121. + new_sampleinfo = (uint16*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));
  122. + if (!new_sampleinfo) {
  123. + TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "
  124. + "temporary new sampleinfo array (%d 16 bit elements)",
  125. + tif->tif_dir.td_extrasamples);
  126. + goto bad;
  127. + }
  128. +
  129. + memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16));
  130. + _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);
  131. + _TIFFfree(new_sampleinfo);
  132. + }
  133. +
  134. /*
  135. * Verify Palette image has a Colormap.
  136. */
  137. diff --git a/libtiff/tif_print.c b/libtiff/tif_print.c
  138. index 24d4b98..10a588e 100644
  139. --- a/libtiff/tif_print.c
  140. +++ b/libtiff/tif_print.c
  141. @@ -546,7 +546,7 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
  142. uint16 i;
  143. fprintf(fd, " %2ld: %5u",
  144. l, td->td_transferfunction[0][l]);
  145. - for (i = 1; i < td->td_samplesperpixel; i++)
  146. + for (i = 1; i < td->td_samplesperpixel - td->td_extrasamples && i < 3; i++)
  147. fprintf(fd, " %5u",
  148. td->td_transferfunction[i][l]);
  149. fputc('\n', fd);
  150. --
  151. 2.17.0