Skip to content

Commit 11582e5

Browse files
authored
Fix heap buffer overflow in _tiffWriteProc when saving TIFF to BytesIO (#10224)
2 parents 392ee6f + 78c08bd commit 11582e5

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎Tests/test_file_libtiff.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,41 @@ def test_save_bytesio_encoder_error(self) -> None:
805805
tiffinfo={TiffImagePlugin.PREDICTOR: 3},
806806
)
807807

808+
def test_save_bytesio_seek_past_end(self, tmp_path: Path) -> None:
809+
# When writing the directory, libtiff seeks past the end of the data
810+
# written so far, and then writes the tag values there. Check that the
811+
# in-memory write buffer grows to fit, and that the bytes that are
812+
# skipped over are zero, as they would be when writing to a file.
813+
814+
# 7 distinct bytes per row: PackBits emits 8 bytes per row, so the strip
815+
# plus the header are exactly 64 KiB, the initial size of the buffer
816+
im = Image.new("L", (7, 8191))
817+
im.putdata([(i * 37) & 0xFF for i in range(7 * 8191)])
818+
kwargs: dict[str, Any] = {
819+
"compression": "packbits",
820+
"description": "D" * 65535,
821+
}
822+
823+
outfile = tmp_path / "temp.tif"
824+
im.save(outfile, **kwargs)
825+
buffer_io = io.BytesIO()
826+
im.save(buffer_io, format="tiff", **kwargs)
827+
assert buffer_io.getvalue() == outfile.read_bytes()
828+
829+
# odd-length tag values, so that libtiff pads between them. Large enough
830+
# that the buffer has to be reallocated
831+
im = hopper().resize((300, 300))
832+
kwargs = {
833+
"compression": "tiff_lzw",
834+
"description": "abcdef",
835+
"software": "xyz0",
836+
"tiffinfo": {315: "artist", 33432: "copyright!"},
837+
}
838+
im.save(outfile, **kwargs)
839+
buffer_io = io.BytesIO()
840+
im.save(buffer_io, format="tiff", **kwargs)
841+
assert buffer_io.getvalue() == outfile.read_bytes()
842+
808843
def test_save_ycbcr(self, tmp_path: Path) -> None:
809844
im = hopper("YCbCr")
810845
outfile = tmp_path / "temp.tif"

‎src/libImaging/TiffDecode.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ _tiffWriteProc(thandle_t hdata, tdata_t buf, tsize_t size) {
6767
TIFFSTATE *state = (TIFFSTATE *)hdata;
6868
tsize_t to_write;
6969

70+
// libtiff may have seeked past the end of the buffer, e.g. when writing the
71+
// directory, so the required size is based on the current location.
7072
to_write = MIN(size, state->size - (tsize_t)state->loc);
7173
if (state->flrealloc && size > to_write) {
7274
tdata_t new_data;
7375
tsize_t newsize = state->size;
74-
while (newsize < (size + state->size)) {
76+
while (newsize < (size + (tsize_t)state->loc)) {
7577
if (newsize > INT_MAX - 64 * 1024) {
7678
return 0;
7779
}
@@ -84,9 +86,20 @@ _tiffWriteProc(thandle_t hdata, tdata_t buf, tsize_t size) {
8486
// fail out
8587
return 0;
8688
}
89+
// zero the new space, so that any bytes libtiff seeks over without
90+
// writing are 0 in the output, as they would be in a file
91+
memset((UINT8 *)new_data + state->size, 0, newsize - state->size);
8792
state->data = new_data;
8893
state->size = newsize;
8994
to_write = size;
95+
} else if (to_write < 0) {
96+
TIFFError(
97+
"_tiffWriteProc",
98+
"Invalid Write at loc %" PRIu64 ", size: %" PRIu64,
99+
state->loc,
100+
(toff_t)state->size
101+
);
102+
return 0;
90103
}
91104

92105
_TIFFmemcpy((UINT8 *)state->data + state->loc, buf, to_write);

0 commit comments

Comments
 (0)