Skip to content

Commit 570df03

Browse files
aclark4lifeCopilotclaude
authored andcommitted
Validate TIFF tag values against libtiff's own field definition before TIFFSetField
PyImaging_LibTiffEncoderNew chose the TIFFSetField() argument shape (scalar, tuple, or bytes) from the Python value alone, assuming its own ImagingLibTiffMergeFieldInfo() registration would determine how libtiff reads the varargs. It does not: TIFFMergeFieldInfo() never overrides libtiff's built-in definition for a tag it already knows, so libtiff reads the varargs according to its own field table regardless of what Pillow pushed. For a counted array tag such as DNGPrivateData (50740), a scalar integer from the source file was taken by libtiff as a byte count, and the data pointer was read from an argument slot Pillow never wrote -- copying that many bytes of process memory into the output tag. For a fixed-size array tag such as ActiveArea (50829), the integer was used directly as a source address, causing a segfault. Add ImagingLibTiffFindField() to look up libtiff's actual field definition (type, passcount, writecount) for a tag, and validate the Python value against it before calling TIFFSetField, raising ValueError instead of forwarding a mismatched value. The core tags that have their own dedicated branches (ColorMap, YCbCrSubsampling, ReferenceBlackWhite) are shape-checked too; ColorMap length is already validated in Python. ReferenceBlackWhite no longer needs to be blocklisted and is now written. TransferFunction remains unsupported by the libtiff encoder and stays blocklisted, so it is still dropped. Reported-by: carlini Fixes: GHSA-xvh5-g6gv-559v Co-authored-by: Copilot <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 53318b5 commit 570df03

5 files changed

Lines changed: 314 additions & 32 deletions

File tree

‎Tests/test_file_libtiff.py‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,101 @@ def test_geometry_tags(
415415
with pytest.raises(OSError, match="encoder error -8"):
416416
im.save(out, tiffinfo=tiffinfo)
417417

418+
@pytest.mark.parametrize(
419+
"tag, value",
420+
(
421+
# DNGPrivateData is a BYTE array with a count to libtiff
422+
(50740, 4096),
423+
# ActiveArea is a LONG array of four values to libtiff
424+
(50829, 64),
425+
(50829, (0, 0)),
426+
),
427+
)
428+
def test_libtiff_field_definition(
429+
self, tag: int, value: Any, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
430+
) -> None:
431+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
432+
433+
ifd = TiffImagePlugin.ImageFileDirectory_v2()
434+
ifd[tag] = value
435+
ifd.tagtype[tag] = TiffTags.LONG
436+
437+
out = tmp_path / "temp.tif"
438+
im = Image.new("L", (1, 1))
439+
with pytest.raises(ValueError, match="libtiff field definition"):
440+
im.save(out, tiffinfo=ifd)
441+
442+
# Values that match the libtiff definition are written
443+
ifd = TiffImagePlugin.ImageFileDirectory_v2()
444+
ifd[50740] = b"private"
445+
ifd.tagtype[50740] = TiffTags.BYTE
446+
ifd[50829] = (0, 0, 1, 1)
447+
ifd.tagtype[50829] = TiffTags.LONG
448+
im.save(out, tiffinfo=ifd)
449+
450+
with Image.open(out) as reloaded:
451+
assert isinstance(reloaded, TiffImagePlugin.TiffImageFile)
452+
assert reloaded.tag_v2[50740] == b"private"
453+
assert reloaded.tag_v2[50829] == (0, 0, 1, 1)
454+
455+
def test_transferfunction_not_saved(
456+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
457+
) -> None:
458+
# TransferFunction is not supported by the libtiff encoder, so it is
459+
# dropped rather than raising an error
460+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
461+
462+
out = tmp_path / "temp.tif"
463+
im = Image.new("L", (1, 1))
464+
im.save(out, tiffinfo={TiffImagePlugin.TRANSFERFUNCTION: tuple(range(256))})
465+
466+
with Image.open(out) as reloaded:
467+
assert isinstance(reloaded, TiffImagePlugin.TiffImageFile)
468+
assert TiffImagePlugin.TRANSFERFUNCTION not in reloaded.tag_v2
469+
470+
@pytest.mark.parametrize(
471+
"tag, mode, value, invalid_value",
472+
(
473+
(320, "L", tuple((i % 256) * 256 for i in range(768)), (0, 0, 0)),
474+
(530, "YCbCr", (1, 1), (1,)),
475+
(532, "YCbCr", (0, 255, 128, 255, 128, 255), (0, 255)),
476+
),
477+
)
478+
def test_libtiff_core_field_definition(
479+
self,
480+
tag: int,
481+
mode: str,
482+
value: tuple[int, ...],
483+
invalid_value: tuple[int, ...],
484+
monkeypatch: pytest.MonkeyPatch,
485+
tmp_path: Path,
486+
) -> None:
487+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
488+
489+
out = tmp_path / "temp.tif"
490+
im = Image.new(mode, (1, 1))
491+
492+
ifd = TiffImagePlugin.ImageFileDirectory_v2()
493+
ifd[tag] = invalid_value
494+
# The ColorMap length is checked in Python first
495+
match = "Requiring 768 items" if tag == 320 else "libtiff field definition"
496+
with pytest.raises(ValueError, match=match):
497+
im.save(out, tiffinfo=ifd)
498+
499+
ifd = TiffImagePlugin.ImageFileDirectory_v2()
500+
ifd[tag] = value
501+
502+
im.save(out, tiffinfo=ifd)
503+
504+
with Image.open(out) as reloaded:
505+
assert isinstance(reloaded, TiffImagePlugin.TiffImageFile)
506+
if tag == 532:
507+
assert tuple(float(v) for v in reloaded.tag_v2[tag]) == pytest.approx(
508+
value
509+
)
510+
else:
511+
assert tuple(reloaded.tag_v2[tag]) == value
512+
418513
def test_inknames_tag(
419514
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
420515
) -> None:

‎src/PIL/TiffImagePlugin.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,13 +1981,10 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
19811981
# STRIPOFFSETS and STRIPBYTECOUNTS are added by the library
19821982
# based on the data in the strip.
19831983
# OSUBFILETYPE is deprecated.
1984-
# The other tags expect arrays with a certain length (fixed or depending on
1985-
# BITSPERSAMPLE, etc), passing arrays with a different length will result in
1986-
# segfaults. Block these tags until we add extra validation.
1984+
# TRANSFERFUNCTION is not supported by the libtiff encoder.
19871985
# SUBIFD may also cause a segfault.
19881986
blocklist += [
19891987
OSUBFILETYPE,
1990-
REFERENCEBLACKWHITE,
19911988
STRIPBYTECOUNTS,
19921989
STRIPOFFSETS,
19931990
TRANSFERFUNCTION,

0 commit comments

Comments
 (0)