Skip to content

ext/standard: Reject out-of-range XBM dimensions - #25293

Open
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/aph-r6oy
Open

iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/aph-r6oy

Conversation

@iliaal

@iliaal iliaal commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

An XBM file declaring #define x_width 4294967298 makes getimagesize() report a width of 2, because php_get_xbm() parses the dimension with sscanf("%d") and that overflow is undefined; negative values are reported as negative dimensions. This parses the value with strtol() and accepts only 1 to INT_MAX. The bundled GD reader has the same bug and will follow in a separate PR after libgd/libgd#1050.

@devnexen

devnexen commented Nov 8, 2026

Copy link
Copy Markdown
Member

If I were you, I would split into 2 pr, the libgd part alone can take an undetermined time to move the needle.

Comment thread ext/gd/tests/xbm_out_of_range.phpt Outdated
--FILE--
<?php
$path = __DIR__ . '/xbm_out_of_range.tmp.xbm';
foreach ([['4294967298', '1'], ['1', '4294967298'], ['2147483648', '1'], ['-5', '1'], ['2147483647', '9']] as [$w, $h]) {

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.

note: the -5 x 1 and 2147483647 x 9 cases already return
false without the patch: -5 wraps to a zero byte count, and gdImageCreate()
rejects 2147483647 x 9 internally.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Dropped them, along with 2147483648 x 1, which gdImageCreate() also rejects, and the row-byte guard, since gdImageCreate() already rejects any final size whose byte count would overflow. The test moves with the GD part.

@iliaal

iliaal commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

Split: this PR now only covers getimagesize(). The bundled GD part will follow once libgd/libgd#1050 lands.

Comment thread ext/standard/image.c Resolved
XBM width and height were read with sscanf("%d"), whose behaviour on
overflow is undefined; glibc wraps 4294967298 to 2, so getimagesize()
reported a width of 2, and negative values were reported as negative
dimensions. Parse the value with strtol() and accept only 1 to INT_MAX.
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