Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a crash and an infinite loop in stb_image that could occur with specially constructed PGM and HDR files #1223

Merged

Conversation

NBickford-NV
Copy link
Contributor

@NBickford-NV NBickford-NV commented Oct 7, 2021

Fixes two availability issues in stb_image that could occur with specially
constructed HDR and PGM files. Please see issues #1224 and #1225 for full descriptions.

HDR: In certain conditions, the RLE decoder can get stuck in the decoding loop
at the end of a stream: stbi__get8() always returns 0 when at the end of a
stream, which is interpreted as a count and results in an infinite loop.
I believe the solution is to treat a run of 0 as invalid, following the
RGBE_ReadPixels_RLE() function in Bruce Walter's
https://www.graphics.cornell.edu/~bjw/rgbe/rgbe.c.

PGM: Loading large monochrome 16-bit PGM files would cause an access violation
due to reading out of bounds when comverting from 16-bit to 8-bit. In addition,
when loading 16-bit PGM files, stbi__pnm_load() would call
stbi__convert_format() instead of stbi__convert_format16() to convert from
monochrome to the required number of channels, so the buffer would be
interpreted as the wrong type. This pull request also adds an error nessage for
when the stbi__getn() call fails.

Thanks!

musicinmybrain added a commit to musicinmybrain/zxing-cpp that referenced this pull request Dec 8, 2021
Fixes a crash and an infinite loop in stb_image that could occur with
specially constructed PGM and HDR files

nothings/stb#1223

This is a candidate fix for:

  https://nvd.nist.gov/vuln/detail/CVE-2021-42715

  In stb_image's HDR reader, loading a specially constructed invalid HDR
  file can result in an infinite loop within the RLE decoder
  nothings/stb#1224

Additionally, this is a candidate fix for:

  https://nvd.nist.gov/vuln/detail/CVE-2021-42716

  stbi__pnm_load heap-buffer-overflow bug
  nothings/stb#1166

  In stb_image's PNM reader, loading a specially constructed valid
  16-bit PGM file with 4 channels can cause a crash due to an
  out-of-bounds read
  nothings/stb#1225
@NBickford-NV
Copy link
Contributor Author

Following up on this - is there anything I can change to help merge this in? Thanks!

@nothings
Copy link
Owner

nothings commented Jan 7, 2022

stb repository is just slow for me to update

@NBickford-NV
Copy link
Contributor Author

Quick note from the survey of the first 10 ossfuzz issues I did: I think this pull request might resolve ossfuzz issues https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=37628&q=proj%3Dstb&can=2 (this seems to be the infinite HDR reader error) and https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=38394&q=proj%3Dstb&can=2 (I think this is due to the PGM issue). I'm unable to reproduce ossfuzz's crashes with these fixes on Windows with Address Sanitizer enabled.

…d HDR and PGM files.

Signed-off-by: Neil Bickford <nbickford@nvidia.com>
@rygorous
Copy link
Collaborator

Merged into dev branch, will be in the next release.

@rygorous rygorous added the 5 merged-dev Merged into development branch label Jan 22, 2023
@NBickford-NV
Copy link
Contributor Author

Thanks so much ryg!

@nothings nothings merged commit e5da6ac into nothings:master Jan 29, 2023
@NBickford-NV NBickford-NV deleted the neilbickford/image_hdr_pgm_fixes branch January 31, 2023 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5 merged-dev Merged into development branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants