Skip to content

Fix GH-25961: Reject truncated JPEG SOF headers - #25982

Open
jlu-lujing wants to merge 1 commit into
php:PHP-8.4from
jlu-lujing:gh-25961-truncated-jpeg-sof
Open

jlu-lujing wants to merge 1 commit into
php:PHP-8.4from
jlu-lujing:gh-25961-truncated-jpeg-sof

Conversation

@jlu-lujing

Copy link
Copy Markdown

A truncated JPEG SOF segment currently leaves php_stream_getc() EOF in the unsigned channel field, so getimagesize() and getimagesizefromstring() publish UINT_MAX channels. Read the fixed six-byte SOF payload together, validate the declared segment length, and return failure when any byte is missing.

The regression uses the minimal truncated header from the report. The whole image test directory passes with the change.

Tests:

  • New PHPT fails before the change and passes after it.
  • ext/standard/tests/image: 33 passed, 8 skipped due to optional extensions omitted from the minimal build, 0 failed.

OpenAI Codex assisted with investigating the JPEG parser, preparing the focused code and regression test, and drafting this description. I reviewed and verified the resulting change locally.

Read the fixed-size SOF fields together and fail when fewer than six bytes are available. This prevents EOF from being stored as the channel count and rejects malformed JPEG headers instead of exposing bogus metadata.

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as GH-25985 and GH-25983: this changes what getimagesize() returns for malformed input, with no crash or memory issue behind it, so please retarget to master. Please also add Fixes GH-25961 to the description so the PR is listed on the issue.

Comment thread ext/standard/image.c
result->channels = php_stream_getc(stream);
if (!info || length < 8) { /* if we don't want an extanded info -> return */
unsigned char sof[6];
if (length < 8 || php_read_stream_all_chunks(stream, (char *) sof, sizeof(sof)) != sizeof(sof)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Folding length < 8 into the failure check is a separate behavior change. The old code returned the size without extended info in that case, and a SOF declaring length 7 with a full payload now returns false instead of the dimensions. Please keep the short read as the only failure and the old if (!info || length < 8) early return.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants