Skip to content

Commit c2ff5c2

Browse files
committed
fix: race condition in TIFF reader (AcademySoftwareFoundation#3772)
* test: Add a corrupted test file * fix(IC): upon tile read error, mark the file as broken The idea is to keep fruitlessly reading from a corrupted file. * fix: race condition in TIFF reader TIFFInput::read_native_tiles had a very subtle race condition. There is a section where it has a task_set of decompression tasks that are sent to the thread queue. When the method ends, the task_set goes out of scope, and its destructor will wait for all the remaining tasks to finish. All fine, right? Except that other variables in the function include unique_ptr's with allocated memory that the tasks need. So what can happen is that if the compiler generates the code such that the destructor of the unique_ptr happens before the destructor of the task_set, the memory that the unique_ptr held may be deallocated before some of the tasks still running in threads have finished. In practice, we seem only to hit this race condition for particular cases involving corrupted files, because the early `return` seems quite likely to run those destructors before all the threads are done, but it was possible to encounter it for valid files as well, if the timing is just right. Shocking that we never had it reported. The solution is just to have an explitit tasks.wait(), which will block until all the tasks have finished before hitting the actual end of the method and letting the other destructors to run. And for the "early return" case, change it to a break so that we go through the wait call at the end. I believe that this fix addresses TALOS-2023-1709 / CVE-2023-24472.
1 parent 58153cd commit c2ff5c2

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

src/libtexture/imagecache.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ ImageCacheFile::read_tile(ImageCachePerThreadInfo* thread_info, int subimage,
840840
}
841841
}
842842
if (!ok) {
843+
m_broken = true;
843844
std::string err = inp->geterror();
844845
if (errors_should_issue()) {
845846
imagecache().error("{}",

src/tiff.imageio/tiffinput.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,8 +2253,9 @@ TIFFInput::read_native_tiles(int subimage, int miplevel, int xbegin, int xend,
22532253
// xbegin, xend, ybegin, yend, zbegin, zend);
22542254
size_t tileidx = 0;
22552255
for (int z = zbegin; z < zend; z += m_spec.tile_depth) {
2256-
for (int y = ybegin; y < yend; y += m_spec.tile_height) {
2257-
for (int x = xbegin; x < xend; x += m_spec.tile_width, ++tileidx) {
2256+
for (int y = ybegin; ok && y < yend; y += m_spec.tile_height) {
2257+
for (int x = xbegin; ok && x < xend;
2258+
x += m_spec.tile_width, ++tileidx) {
22582259
char* cbuf = compressed_scratch.get() + tileidx * cbound;
22592260
char* ubuf = scratch.get() + tileidx * tile_bytes;
22602261
auto csize = TIFFReadRawTile(m_tif, tile_index(x, y, z), cbuf,
@@ -2264,7 +2265,8 @@ TIFFInput::read_native_tiles(int subimage, int miplevel, int xbegin, int xend,
22642265
errorfmt(
22652266
"TIFFReadRawTile failed reading tile x={},y={},z={}: {}",
22662267
x, y, z, err.size() ? err.c_str() : "unknown error");
2267-
return false;
2268+
ok = false;
2269+
break;
22682270
}
22692271
// Push the rest of the work onto the thread pool queue
22702272
auto out = this;
@@ -2289,6 +2291,7 @@ TIFFInput::read_native_tiles(int subimage, int miplevel, int xbegin, int xend,
22892291
}
22902292
}
22912293
}
2294+
tasks.wait();
22922295
return ok;
22932296
}
22942297

testsuite/tiff-misc/ref/out-libtiff403.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Full command line was:
2222
> oiiotool --oiioattrib try_all_readers 0 --info -v src/crash-1633.tif
2323
oiiotool ERROR: read : File does not exist: "src/crash-1643.tif"
2424
Full command line was:
25-
> oiiotool --oiioattrib try_all_readers 0 --info -v src/crash-1643.tif
25+
> oiiotool --oiioattrib try_all_readers 0 --info src/crash-1643.tif -o out.exr
26+
iconvert ERROR copying "src/crash-1709.tif" to "crash-1709.exr" :
27+
Decoding error at scanline 0, incorrect header check
2628
Comparing "check1.tif" and "ref/check1.tif"
2729
PASS
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Reading src/separate.tif
2+
src/separate.tif : 128 x 128, 3 channel, uint8 tiff
3+
SHA-1: 486088DECAE711C444FDCAB009C378F7783AD9C5
4+
channel list: R, G, B
5+
compression: "zip"
6+
DateTime: "2020:10:25 15:32:04"
7+
Orientation: 1 (normal)
8+
planarconfig: "separate"
9+
Software: "OpenImageIO 2.3.0dev : oiiotool --pattern fill:topleft=0,0,0:topright=1,0,0:bottomleft=0,1,0:bottomright=1,1,1 128x128 3 --planarconfig separate -scanline -attrib tiff:rowsperstrip 17 -d uint8 -o separate.tif"
10+
oiio:BitsPerSample: 8
11+
tiff:Compression: 8
12+
tiff:PhotometricInterpretation: 2
13+
tiff:PlanarConfiguration: 2
14+
tiff:RowsPerStrip: 7
15+
Comparing "src/separate.tif" and "separate.tif"
16+
PASS
17+
oiiotool ERROR: read : No support for data format of "src/corrupt1.tif"
18+
Full command line was:
19+
> oiiotool --oiioattrib try_all_readers 0 --info -v src/corrupt1.tif
20+
oiiotool ERROR: read : File does not exist: "src/crash-1633.tif"
21+
Full command line was:
22+
> oiiotool --oiioattrib try_all_readers 0 --info -v src/crash-1633.tif
23+
oiiotool ERROR: read : File does not exist: "src/crash-1643.tif"
24+
Full command line was:
25+
> oiiotool --oiioattrib try_all_readers 0 --info src/crash-1643.tif -o out.exr
26+
iconvert ERROR copying "src/crash-1709.tif" to "crash-1709.exr" :
27+
Decoding error at scanline 0, incorrect header check
28+
Comparing "check1.tif" and "ref/check1.tif"
29+
PASS

testsuite/tiff-misc/ref/out.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Full command line was:
2222
> oiiotool --oiioattrib try_all_readers 0 --info -v src/crash-1633.tif
2323
oiiotool ERROR: read : File does not exist: "src/crash-1643.tif"
2424
Full command line was:
25-
> oiiotool --oiioattrib try_all_readers 0 --info -v src/crash-1643.tif
25+
> oiiotool --oiioattrib try_all_readers 0 --info src/crash-1643.tif -o out.exr
26+
iconvert ERROR copying "src/crash-1709.tif" to "crash-1709.exr" :
27+
2628
Comparing "check1.tif" and "ref/check1.tif"
2729
PASS

testsuite/tiff-misc/run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# Test bugs we had until OIIO 2.4 for these corrupt file
1818
command += oiiotool ("--oiioattrib try_all_readers 0 --info -v src/corrupt1.tif", failureok = True)
1919
command += oiiotool ("--oiioattrib try_all_readers 0 --info -v src/crash-1633.tif", failureok = True)
20-
command += oiiotool ("--oiioattrib try_all_readers 0 --info -v src/crash-1643.tif", failureok = True)
20+
command += oiiotool ("--oiioattrib try_all_readers 0 --info src/crash-1643.tif -o out.exr", failureok = True)
21+
command += iconvert ("src/crash-1709.tif crash-1709.exr", failureok=True)
2122

2223
outputs = [ "check1.tif", "out.txt" ]
36.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)