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.

128 lines
4.1 KiB

  1. From 49723b0eb683cca80142b01a48ba1475fed5188a Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <nforro@redhat.com>
  3. Date: Fri, 23 Mar 2018 15:35:39 +0100
  4. Subject: [PATCH] Fix for bug 2772
  5. It is possible to craft a TIFF document where the IFD list is circular,
  6. leading to an infinite loop while traversing the chain. The libtiff
  7. directory reader has a failsafe that will break out of this loop after
  8. reading 65535 directory entries, but it will continue processing,
  9. consuming time and resources to process what is essentially a bogus TIFF
  10. document.
  11. This change fixes the above behavior by breaking out of processing when
  12. a TIFF document has >= 65535 directories and terminating with an error.
  13. ---
  14. contrib/addtiffo/tif_overview.c | 14 +++++++++++++-
  15. tools/tiff2pdf.c | 10 ++++++++++
  16. tools/tiffcrop.c | 13 +++++++++++--
  17. 3 files changed, 34 insertions(+), 3 deletions(-)
  18. diff --git a/contrib/addtiffo/tif_overview.c b/contrib/addtiffo/tif_overview.c
  19. index c61ffbb..03b3573 100644
  20. --- a/contrib/addtiffo/tif_overview.c
  21. +++ b/contrib/addtiffo/tif_overview.c
  22. @@ -65,6 +65,8 @@
  23. # define MAX(a,b) ((a>b) ? a : b)
  24. #endif
  25. +#define TIFF_DIR_MAX 65534
  26. +
  27. void TIFFBuildOverviews( TIFF *, int, int *, int, const char *,
  28. int (*)(double,void*), void * );
  29. @@ -91,6 +93,7 @@ uint32 TIFF_WriteOverview( TIFF *hTIFF, uint32 nXSize, uint32 nYSize,
  30. {
  31. toff_t nBaseDirOffset;
  32. toff_t nOffset;
  33. + tdir_t iNumDir;
  34. (void) bUseSubIFDs;
  35. @@ -147,7 +150,16 @@ uint32 TIFF_WriteOverview( TIFF *hTIFF, uint32 nXSize, uint32 nYSize,
  36. return 0;
  37. TIFFWriteDirectory( hTIFF );
  38. - TIFFSetDirectory( hTIFF, (tdir_t) (TIFFNumberOfDirectories(hTIFF)-1) );
  39. + iNumDir = TIFFNumberOfDirectories(hTIFF);
  40. + if( iNumDir > TIFF_DIR_MAX )
  41. + {
  42. + TIFFErrorExt( TIFFClientdata(hTIFF),
  43. + "TIFF_WriteOverview",
  44. + "File `%s' has too many directories.\n",
  45. + TIFFFileName(hTIFF) );
  46. + exit(-1);
  47. + }
  48. + TIFFSetDirectory( hTIFF, (tdir_t) (iNumDir - 1) );
  49. nOffset = TIFFCurrentDirOffset( hTIFF );
  50. diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c
  51. index 454befb..bdb9126 100644
  52. --- a/tools/tiff2pdf.c
  53. +++ b/tools/tiff2pdf.c
  54. @@ -68,6 +68,8 @@ extern int getopt(int, char**, char*);
  55. #define PS_UNIT_SIZE 72.0F
  56. +#define TIFF_DIR_MAX 65534
  57. +
  58. /* This type is of PDF color spaces. */
  59. typedef enum {
  60. T2P_CS_BILEVEL = 0x01, /* Bilevel, black and white */
  61. @@ -1049,6 +1051,14 @@ void t2p_read_tiff_init(T2P* t2p, TIFF* input){
  62. uint16 xuint16=0;
  63. directorycount=TIFFNumberOfDirectories(input);
  64. + if(directorycount > TIFF_DIR_MAX) {
  65. + TIFFError(
  66. + TIFF2PDF_MODULE,
  67. + "TIFF contains too many directories, %s",
  68. + TIFFFileName(input));
  69. + t2p->t2p_error = T2P_ERR_ERROR;
  70. + return;
  71. + }
  72. t2p->tiff_pages = (T2P_PAGE*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,directorycount,sizeof(T2P_PAGE)));
  73. if(t2p->tiff_pages==NULL){
  74. TIFFError(
  75. diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
  76. index c69177e..c60cb38 100644
  77. --- a/tools/tiffcrop.c
  78. +++ b/tools/tiffcrop.c
  79. @@ -217,6 +217,8 @@ extern int getopt(int argc, char * const argv[], const char *optstring);
  80. #define DUMP_TEXT 1
  81. #define DUMP_RAW 2
  82. +#define TIFF_DIR_MAX 65534
  83. +
  84. /* Offsets into buffer for margins and fixed width and length segments */
  85. struct offset {
  86. uint32 tmargin;
  87. @@ -2233,7 +2235,7 @@ main(int argc, char* argv[])
  88. pageNum = -1;
  89. else
  90. total_images = 0;
  91. - /* read multiple input files and write to output file(s) */
  92. + /* Read multiple input files and write to output file(s) */
  93. while (optind < argc - 1)
  94. {
  95. in = TIFFOpen (argv[optind], "r");
  96. @@ -2241,7 +2243,14 @@ main(int argc, char* argv[])
  97. return (-3);
  98. /* If only one input file is specified, we can use directory count */
  99. - total_images = TIFFNumberOfDirectories(in);
  100. + total_images = TIFFNumberOfDirectories(in);
  101. + if (total_images > TIFF_DIR_MAX)
  102. + {
  103. + TIFFError (TIFFFileName(in), "File contains too many directories");
  104. + if (out != NULL)
  105. + (void) TIFFClose(out);
  106. + return (1);
  107. + }
  108. if (image_count == 0)
  109. {
  110. dirnum = 0;
  111. --
  112. 2.13.6