Commit Graph

3309 Commits

Author SHA1 Message Date
Even Rouault
324aa65c0d
EstimateStripByteCounts(): avoid harmless unsigned integer overflow 2019-08-20 18:09:46 +02:00
Even Rouault
dd50fedc2f
_TIFFPartialReadStripArray(): avoid triggering unsigned integer overflow with -fsanitize=unsigned-integer-overflow (not a bug, this is well defined by itself) 2019-08-20 15:29:06 +02:00
Even Rouault
b04da30e11
tiff2ps: fix use of wrong data type that caused issues (/Height being written as 0) on 64-bit big endian platforms 2019-08-18 10:52:45 +02:00
Even Rouault
1a11c9df6e
setByteArray(): fix previous commit 2019-08-16 19:59:18 +02:00
Even Rouault
1302ffb350
setByteArray(): avoid potential signed integer overflow. Pointed by Hendra Gunadi. No actual problem known (which does not mean there wouldn't be any. Particularly on 32bit builds) 2019-08-16 19:47:42 +02:00
Even Rouault
4bb584a35f
RGBA interface: fix integer overflow potentially causing write heap buffer overflow, especially on 32 bit builds. Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16443. Credit to OSS Fuzz 2019-08-15 15:05:28 +02:00
Even Rouault
2218055ca6 Merge branch 'fix_integer_overflow' into 'master'
Fix integer overflow in _TIFFCheckMalloc() and other implementation-defined behaviour (CVE-2019-14973)

See merge request libtiff/libtiff!90
2019-08-14 09:47:58 +00:00
Even Rouault
1b5e3b6a23
Fix integer overflow in _TIFFCheckMalloc() and other implementation-defined behaviour (CVE-2019-14973)
_TIFFCheckMalloc()/_TIFFCheckRealloc() used a unsafe way to detect overflow
in the multiplication of nmemb and elem_size (which are of type tmsize_t, thus
signed), which was especially easily triggered on 32-bit builds (with recent
enough compilers that assume that signed multiplication cannot overflow, since
this is undefined behaviour by the C standard). The original issue which lead to
this fix was trigged from tif_fax3.c

There were also unsafe (implementation defied), and broken in practice on 64bit
builds, ways of checking that a uint64 fits of a (signed) tmsize_t by doing
(uint64)(tmsize_t)uint64_var != uint64_var comparisons. Those have no known
at that time exploits, but are better to fix in a more bullet-proof way.
Or similarly use of (int64)uint64_var <= 0.
2019-08-13 10:40:08 +02:00
Even Rouault
12768a24b1
TIFFClientOpen(): fix memory leak if one of the required callbacks is not provided. Fixed Coverity GDAL CID 1404110 2019-08-12 22:51:09 +02:00
Even Rouault
ea69462ea2
OJPEGReadBufferFill(): avoid very long processing time on corrupted files. Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16400. master only 2019-08-12 17:55:56 +02:00
Even Rouault
187e596861
oss-fuzz/tiff_read_rgba_fuzzer.cc: fix wrong env variable value in previous commit 2019-08-11 00:36:31 +02:00
Even Rouault
2c7e74245a
oss-fuzz/tiff_read_rgba_fuzzer.cc: avoid issue with libjpeg-turbo and MSAN 2019-08-11 00:24:41 +02:00
Even Rouault
43908ce15e
OJPEG: fix integer division by zero on corrupted subsampling factors. Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=15824. Credit to OSS Fuzz 2019-08-10 19:36:09 +02:00
Even Rouault
75c1cf5e91
Merge branch 'ossfuzz_i386' 2019-08-10 18:45:16 +02:00
Even Rouault
76f1531f5f
contrib/oss-fuzz/build.sh: fix for i386 build of jbigkit, and use $LIB_FUZZING_ENGINE 2019-08-10 18:40:50 +02:00
Even Rouault
a7fa3410d9 Merge branch 'patch-1' into 'master'
fix two tiny typos

See merge request libtiff/libtiff!89
2019-08-10 16:00:00 +00:00
Reto Kromer
3fd6bb95dc fix two tiny typos 2019-08-10 15:54:23 +00:00
Even Rouault
66ff50ec79 Merge branch 'patch-1' into 'master'
fix a typo in man page

See merge request libtiff/libtiff!88
2019-08-09 17:26:44 +00:00
Reto Kromer
2d4bb5bdc2 fix typo 2019-08-09 17:20:26 +00:00
Even Rouault
c9cb49177b Merge branch 'TIFFTAGID_Zero_reading_IGNORE' into 'master'
Suppressed Reading of Tiff tags with ID = 0 (like GPSVERSIONID) corrected.

See merge request libtiff/libtiff!77
2019-08-04 13:23:07 +00:00
Su Laus
6f5c9477dd Reading of Tiff tags with ID = 0 (like GPSVERSIONID) corrected.
IGNORE placeholder in tif_dirread.c is now replaced by a field dir_ignore in the TIFFDirEntry structure

  Currently, in tif_dirread.c a special IGNORE value for the tif tags is defined
  in order to flag status preventing already processed tags from further processing.
  This irrational behaviour prevents reading of custom tags with id code 0 - like tag GPSVERSIONID from EXIF 2.31 definition.

  An additional field 'tdir_ignore' is now added to the TIFFDirEntry structure and code is changed
  to allow tags with id code 0 to be read correctly.

  This change was already proposed as pending improvement in tif_dirread.c around line 32.

    Reference is also made to:
	- Discussion in https://gitlab.com/libtiff/libtiff/merge_requests/39
	- http://bugzilla.maptools.org/show_bug.cgi?id=2540

Comments and indention adapted.

Preparation to rebase onto master
2019-08-04 13:23:07 +00:00
Even Rouault
d8e707a953 Merge branch 'cmake_amd64' into 'master'
CMakeLists.txt: properly set value of HOST_FILLORDER to LSB2MSB for Windows CMake builds

See merge request libtiff/libtiff!87
2019-07-16 09:54:55 +00:00
Even Rouault
5e7e9b597f
CMakeLists.txt: properly set value of HOST_FILLORDER to LSB2MSB for Windows CMake builds
As can be seen in https://ci.appveyor.com/project/rleigh-codelibre/libtiff-didfs/builds/25846668/job/ory5w098j8wcij9x
log, the HOST_FILLORDER is not properly set:

[00:02:58] -- CMAKE_HOST_SYSTEM_PROCESSOR set to AMD64
[00:02:58] -- HOST_FILLORDER set to FILLORDER_MSB2LSB

Ther reason is that we match the "amd64.*" lowercase string whereas
CMAKE_HOST_SYSTEM_PROCESSOR is set to AMD64 uppercase.
2019-07-15 12:19:27 +02:00
Even Rouault
a21714f028
TIFFWriteCheck(): call TIFFForceStrileArrayWriting() when needed (should have gone with eaeca6274a) (master only) 2019-07-09 13:56:18 +02:00
Even Rouault
6662e2b388 Merge branch 'fix_chromium_925269' into 'master'
OJPEG: avoid use of unintialized memory on corrupted files

See merge request libtiff/libtiff!86
2019-07-09 11:47:43 +00:00
Even Rouault
cf3ce6fab8
OJPEG: avoid use of unintialized memory on corrupted files
Fixes https://bugs.chromium.org/p/chromium/issues/detail?id=925269
Patch from Lei Zhang with little adaptations.
2019-07-05 18:51:46 +02:00
Even Rouault
ab3204b167 Merge branch 'fix-division-by-zero' into 'master'
Return infinite distance when denominator is zero.

See merge request libtiff/libtiff!85
2019-06-29 16:54:50 +00:00
Dirk Lemstra
b381187db6
Return infinite distance when denominator is zero. 2019-06-29 18:44:38 +02:00
Even Rouault
424972255f Merge branch 'typetests' into 'master'
Add test to check that libtiff types have the correct size

See merge request libtiff/libtiff!57
2019-06-29 09:41:17 +00:00
Thomas Bernard
4d26884a15
make TIFF_SSIZE_T the same bitwidth as TIFF_SIZE_T
it was previously the same bitwidth as unsigned char *
Pointers can be larger than size_t.
2019-05-31 20:10:51 +02:00
Thomas Bernard
69ce2652ef
Add test to check that libtiff types have the correct size
in configure/CMakeList.txt :

- TIFF_INT8_T/TIFF_UINT8_T is signed/unsigned char
sizeof(char)==1 in C standard
- TIFF_INT16_T/TIFF_UINT16_T is signed/unsigned short
sizeof(short)>=2 in C standard
- TIFF_INT32_T/TIFF_UINT32_T is defined so its sizeof() is 4

- TIFF_INT64_T/TIFF_UINT64_T is defined so its sizeof() is 8

- TIFF_SIZE_T is defined so it has same sizeof() than size_t

- TIFF_SSIZE_T is defined so it has same sizeof() than unsigned char *
2019-05-31 20:10:51 +02:00
Even Rouault
91480d3d3c Merge branch 'defer_strile_writing' into 'master'
Add TIFFDeferStrileArrayWriting() and TIFFForceStrileArrayWriting()

See merge request libtiff/libtiff!82
2019-05-29 20:11:57 +00:00
Even Rouault
9cf3a97bea Merge branch 'TIFFReadFromUserBuffer' into 'master'
Add TIFFReadFromUserBuffer()

See merge request libtiff/libtiff!81
2019-05-29 20:11:43 +00:00
Even Rouault
f273e83262
Fix vulnerability in 'D' (DeferStrileLoad) mode (master only) (fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=14908) 2019-05-26 11:33:47 +02:00
Even Rouault
cc84dc7409
Replace 'stripped' by 'striped' in error messages 2019-05-25 13:46:50 +02:00
Even Rouault
eaeca6274a
Add TIFFDeferStrileArrayWriting() and TIFFForceStrileArrayWriting()
Those advanced writing functions must be used in a particular sequence
to make their intended effect. Their aim is to control when/where
the [Strip/Tile][Offsets/ByteCounts] arrays are written into the file.

The purpose of this is to generate 'cloud-optimized geotiff' files where
the first KB of the file only contain the IFD entries without the potentially
large strile arrays. Those are written afterwards.

The typical sequence of calls is:
TIFFOpen()
[ TIFFCreateDirectory(tif) ]
Set fields with calls to TIFFSetField(tif, ...)
TIFFDeferStrileArrayWriting(tif)
TIFFWriteCheck(tif, ...)
 TIFFWriteDirectory(tif)
 ... potentially create other directories and come back to the above directory
TIFFForceStrileArrayWriting(tif): emit the arrays at the end of file

See test/defer_strile_writing.c for a practical example.
2019-05-25 11:37:34 +02:00
Even Rouault
0f797bf386
Fix vulnerability introduced by defer strile loading (master only)
Found on GDAL with https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=14894
Disabling the TIFF_DEFERSTRILELOAD bit in ChopupStripArray() was a
bad idea since when using TIFFReadDirectory() to reload the directory again
would lead to a different value of td_rowsperstrip, which could confuse
readers if they relied on the value found initially.
2019-05-24 11:20:27 +02:00
Even Rouault
fdec42f217
Fix typo in error message (master only) 2019-05-24 10:34:39 +02:00
Even Rouault
4e74734ded
Add TIFFReadFromUserBuffer()
This function replaces the use of TIFFReadEncodedStrip()/TIFFReadEncodedTile()
when the user can provide the buffer for the input data, for example when
he wants to avoid libtiff to read the strile offset/count values from the
[Strip|Tile][Offsets/ByteCounts] array.
2019-05-23 00:38:59 +02:00
Even Rouault
06337fdcfd
libtiff.def: add missing new symbols 2019-05-22 22:57:41 +02:00
Even Rouault
d2e2b4eeac
test/defer_strile_loading.c: fix warning with Visual C++ 2019-05-22 22:34:27 +02:00
Even Rouault
f017d2fae3
_TIFFRewriteField(): fix for bigtiff case (master only)
116cf67f4c made StripByteCount/TileByteCount to
always be rewritten as TIFF_LONG8.
2019-05-22 21:55:01 +02:00
Even Rouault
ef82728f44 Merge branch 'ondemand_strile_offbytecount_loading' into 'master'
Make defer strile offset/bytecount loading available at runtime

See merge request libtiff/libtiff!79
2019-05-21 13:46:23 +00:00
Even Rouault
57bc1126ba Merge branch 'bigtiff_write_bytecount_on_long_when_possible' into 'master'
Create TileByteCounts/StripByteCounts tag with SHORT (ClassicTIFF/BigTIFF) or  LONG (BigTIFF) type when possible

See merge request libtiff/libtiff!78
2019-05-21 13:46:03 +00:00
Even Rouault
d0ec0aeab6 Merge branch 'html_link' into 'master'
libtiff.html, bigtiffpr.html: absolute => relative link

See merge request libtiff/libtiff!80
2019-05-21 13:45:33 +00:00
Thomas Bernard
02c16a3e2e
libtiff.html, bigtiffpr.html: absolute => relative link 2019-05-14 20:05:13 +02:00
Even Rouault
371ad2658c
Make defer strile offset/bytecount loading available at runtime
... and add per-strile offset/bytecount loading capabilities.

Part of this commit makes the behaviour that was previously met when
libtiff was compiled with -DDEFER_STRILE_LOAD available for default builds
when specifying the new 'D' (Deferred) TIFFOpen() flag. In that mode, the [Tile/Strip][ByteCounts/Offsets]
arrays are only loaded when first accessed. This can speed-up the opening
of files stored on the network when just metadata retrieval is needed.
This mode has been used for years by the GDAL library when compiled with
its embeded libtiff copy.

To avoid potential out-of-tree code (typically codecs) that would use
the td_stripbytecount and td_stripoffset array inconditionnaly assuming they
have been loaded, those have been suffixed with _p (for protected). The
use of the new functions mentionned below is then recommended.

Another addition of this commit is the capability of loading only the
values of the offset/bytecount of the strile of interest instead of the
whole array. This is enabled with the new 'O' (Ondemand) flag of TIFFOpen()
(which implies 'D'). That behaviour has also been used by GDAL, which hacked
into the td_stripoffset/td_stripbytecount arrays directly. The new code
added in the _TIFFFetchStrileValue() and _TIFFPartialReadStripArray() internal
functions is mostly a port of what was in GDAL GTiff driver previously.

Related to that, the public TIFFGetStrileOffset[WithErr]() and TIFFGetStrileByteCount[WithErr]()
functions have been added to API. They are of particular interest when
using sparse files (with offset == bytecount == 0) and you want to detect
if a strile is present or not without decompressing the data, or updating
an existing sparse file.
They will also be used to enable a future enhancement where client code can entirely
skip bytecount loading in some situtations

A new test/defer_strile_loading.c test has been added to test the above
capabilities.
2019-05-10 14:46:45 +02:00
Even Rouault
458c211ae2
Creation: use SHORT type when possible for StripByteCounts/TileByteCounts
This follows the same logic as previous commit.
2019-05-10 11:08:01 +02:00
Even Rouault
116cf67f4c
BigTIFF creation: write TileByteCounts/StripByteCounts tag with LONG when possible
In most situations of BigTIFF file, the tile/strip sizes are of reasonable size,
that is they fit on a 4-byte LONG. So in that case, use LONG instead of LONG8
to save some space. For uncompressed file, it is easy to detect such situations
by checking at the TIFFTileSize64()/TIFFStripSize64() return. For compressed file,
we must take into account the fact that compression may sometimes result in
larger compressed data. So we allow this optimization only for a few select
compression times, and take a huge security margin (10x factor). We also only
apply this optimization on multi-strip files, so as to allow easy on-the-fly
growing of single-strip files whose strip size could grow above the 4GB threshold.

This change is compatible with the BigTIFF specification. According to
https://www.awaresystems.be/imaging/tiff/bigtiff.html:
"The StripOffsets, StripByteCounts, TileOffsets, and TileByteCounts tags are
allowed to have the datatype TIFF_LONG8 in BigTIFF. Old datatypes TIFF_LONG,
and TIFF_SHORT where allowed in the TIFF 6.0 specification, are still valid in BigTIFF, too. "
On a practical point of view, this is also compatible on reading/writing of
older libtiff 4.X versions.

The only glitch I found, which is rather minor, is when using such a BigTIFF
file with TileByteCounts/StripByteCounts written with TIFF_LONG, and updating
it with an older libtiff 4.X version with a change in the
[Tile/Strip][ByteCounts/Offsets] array. In that case the _TIFFRewriteField()
function will rewrite the directory and array with TIFF_LONG8, instead of updating
the existing array (this is an issue fixed by this commit). The file will
still be valid however, hence the minor severity of this.
2019-05-09 22:47:15 +02:00
Even Rouault
b9b93f661e Merge branch 'bug2799' into 'master'
fix fax2tiff

See merge request libtiff/libtiff!55
2019-05-08 08:36:34 +00:00