Skip to content

fix(tiff): fix buffer overrun and make better error reporting#5082

Merged
lgritz merged 1 commit intoAcademySoftwareFoundation:mainfrom
lgritz:lg-tiff-cmyk-fix
Mar 16, 2026
Merged

fix(tiff): fix buffer overrun and make better error reporting#5082
lgritz merged 1 commit intoAcademySoftwareFoundation:mainfrom
lgritz:lg-tiff-cmyk-fix

Conversation

@lgritz
Copy link
Copy Markdown
Collaborator

@lgritz lgritz commented Mar 12, 2026

Certain CMYK files (maybe only corrupt ones?) hit an edge case that caused us to read the original 4 channels but into space for only the 3 channels we ultimately report back. Fix that logic.

Also noticed several spots where we didn't correctly report back error messages for newer versions of libtiff. This was some kind of oversight from long ago.

Certain CMYK files (maybe only corrupt ones?) hit an edge case that
caused us to read the original 4 channels but into space for only the
3 channels we ultimately report back. Fix that logic.

Also noticed several spots where we didn't correctly report back
error messages for newer versions of libtiff. This was some kind of
oversight from long ago.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
@lgritz lgritz force-pushed the lg-tiff-cmyk-fix branch from 306aa76 to 5b16b8e Compare March 12, 2026 20:55
Copy link
Copy Markdown
Contributor

@jessey-git jessey-git left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core changes seem fine, but like some of the other PRs, there's a variety of arithmetic vulns still left. For TIFF, there's many calculations like (1 << m_bitspersample) that will give the wrong result for m_bitspersample >= 32. It needs to be (1ull << m_bitspersample)

I haven't checked the other calculations but with TIFFs very high channel count limit, there might be other expressions that overflow too.

@lgritz
Copy link
Copy Markdown
Collaborator Author

lgritz commented Mar 16, 2026

How about if I split it and leave this as-is to fix the immediate problem that was reported, and I will make a second PR that does a more comprehensive check for overflows and fixex for them?

@lgritz
Copy link
Copy Markdown
Collaborator Author

lgritz commented Mar 16, 2026

It turns out that the (1 << m_bitspersample) is not a real problem -- because you can't have palette images with more than 16 bits per sample.

Actually, reading the TIFF 6.0 spec, on p. 23, it sure looks like palette images can only be 4 or 8 bits per sample. But the libtiff set of test images indeed has 2, 4, 8, and yes, 16 bps palette images. So I think we should support up to 16, since we might encounter it in the wild, but no higher.

We don't currently check for this, though, so I will add that as a separate PR.

@jessey-git
Copy link
Copy Markdown
Contributor

Yeah, a followup PR is fine. Good that we're limited by the "spec" but I don't know if a hand-crafted file can worm its way to that point in the code. I'd opt for fixing those shifts if nothing else to prevent static analysis from flagging them (which is how I found them).

@lgritz
Copy link
Copy Markdown
Collaborator Author

lgritz commented Mar 16, 2026

I think that the static analysis won't flag it if it is preceded by a proper check and early exit if m_bitspersample > 16.

Copy link
Copy Markdown
Contributor

@jessey-git jessey-git left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are ready I think.

@lgritz lgritz merged commit f2e6f0c into AcademySoftwareFoundation:main Mar 16, 2026
31 checks passed
@lgritz lgritz deleted the lg-tiff-cmyk-fix branch March 16, 2026 02:25
lgritz added a commit to lgritz/OpenImageIO that referenced this pull request Mar 16, 2026
…ySoftwareFoundation#5082)

Certain CMYK files (maybe only corrupt ones?) hit an edge case that
caused us to read the original 4 channels but into space for only the 3
channels we ultimately report back. Fix that logic.

Also noticed several spots where we didn't correctly report back error
messages for newer versions of libtiff. This was some kind of oversight
from long ago.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
ssh4net pushed a commit to ssh4net/OpenImageIO that referenced this pull request Apr 1, 2026
…ySoftwareFoundation#5082)

Certain CMYK files (maybe only corrupt ones?) hit an edge case that
caused us to read the original 4 channels but into space for only the 3
channels we ultimately report back. Fix that logic.

Also noticed several spots where we didn't correctly report back error
messages for newer versions of libtiff. This was some kind of oversight
from long ago.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
Signed-off-by: Vlad (Kuzmin) Erium <libalias@gmail.com>
ssh4net pushed a commit to ssh4net/OpenImageIO that referenced this pull request Apr 1, 2026
…ySoftwareFoundation#5082)

Certain CMYK files (maybe only corrupt ones?) hit an edge case that
caused us to read the original 4 channels but into space for only the 3
channels we ultimately report back. Fix that logic.

Also noticed several spots where we didn't correctly report back error
messages for newer versions of libtiff. This was some kind of oversight
from long ago.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
Signed-off-by: Vlad (Kuzmin) Erium <libalias@gmail.com>
Signed-off-by: Vlad <shaamaan@gmail.com>
ssh4net pushed a commit to ssh4net/OpenImageIO that referenced this pull request Apr 1, 2026
…ySoftwareFoundation#5082)

Certain CMYK files (maybe only corrupt ones?) hit an edge case that
caused us to read the original 4 channels but into space for only the 3
channels we ultimately report back. Fix that logic.

Also noticed several spots where we didn't correctly report back error
messages for newer versions of libtiff. This was some kind of oversight
from long ago.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
Signed-off-by: Vlad <shaamaan@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants