Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/gd/libgd/gd_gd2.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static int _gd2GetHeader(gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, in
gdFree(cidx);
goto fail1;
}
if (cidx[i].offset < 0 || cidx[i].size < 0) {
if (cidx[i].offset < 0 || cidx[i].size < 0 || cidx[i].size == INT_MAX) {
gdFree(cidx);
goto fail1;
}
Expand Down
42 changes: 42 additions & 0 deletions ext/gd/tests/createfromstring-overflow.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
imagecreatefromstring overflow with compressed chunk of size INT_MAX
--EXTENSIONS--
gd
--FILE--
<?php

// Documentation available at https://libgd.github.io/manuals/2.3.3/files/gd_gd2-c.html

@devnexen devnexen Sep 28, 2026 •

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.

nit: I think you can simplify the test like this

--- a/ext/gd/tests/createfromstring-overflow.phpt
+++ b/ext/gd/tests/createfromstring-overflow.phpt
@@ -4,37 +4,9 @@ imagecreatefromstring overflow with compressed chunk of size INT_MAX
 gd
 --FILE--
 <?php
-
-// Documentation available at https://libgd.github.io/manuals/2.3.3/files/gd_gd2-c.html
-$fileHeaderParts = [
-     "signature" => "gd2\x00",
-     "version" => "\x00\x02",
-     "width" => "\x00\x01",
-     "height" => "\x00\x01",
-     "chunk_size" => "\x00\x40",
-     "format" => "\x00\x04", // compressed truecolor image data
-     "x_chunk_count" => "\x00\x01",
-     "y_chunk_count" => "\x00\x01",
-];
-$fileHeader = implode("", $fileHeaderParts);
-
-$chunkHeaderParts = [
-     "offset" => "\x00\x00\x00\x20",
-     "size" =>"\x7F\xFF\xFF\xFF", // INT_MAX
-];
-$chunkHeader = implode("", $chunkHeaderParts);
-
-$trueColorHeaderParts = [
-     "truecolor" => "\x01",
-     "transparent" => "\x00\x00\x00\x00",
-];
-$trueColorHeader = implode("", $trueColorHeaderParts);
-
-$source = $fileHeader . $chunkHeader . $trueColorHeader;
-$img = imagecreatefromstring($source);
-var_dump($img);
-
+$data = pack('a4n7N2CN', 'gd2', 2, 1, 1, 64, 4, 1, 1, 0, 0x7FFFFFFF, 1, 0);
+var_dump(imagecreatefromstring($data));

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.

Yes, that is possible, but it is entirely unclear to me what the pack string means and how these numbers do anything - is there a problem with having the clarity of array keys?

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.

yes true you would need to know the gd2 format. but definitely not a blocker, the most imporant is the fix :)

$fileHeaderParts = [
"signature" => "gd2\x00",
"version" => "\x00\x02",
"width" => "\x00\x01",
"height" => "\x00\x01",
"chunk_size" => "\x00\x40",
"format" => "\x00\x04", // compressed truecolor image data
"x_chunk_count" => "\x00\x01",
"y_chunk_count" => "\x00\x01",
];
$fileHeader = implode("", $fileHeaderParts);

$chunkHeaderParts = [
"offset" => "\x00\x00\x00\x20",
"size" =>"\x7F\xFF\xFF\xFF", // INT_MAX
];
$chunkHeader = implode("", $chunkHeaderParts);

$trueColorHeaderParts = [
"truecolor" => "\x01",
"transparent" => "\x00\x00\x00\x00",
];
$trueColorHeader = implode("", $trueColorHeaderParts);

$source = $fileHeader . $chunkHeader . $trueColorHeader;
$img = imagecreatefromstring($source);
var_dump($img);

?>
--EXPECTF--
Warning: imagecreatefromstring(): Passed data is not in "GD2" format in %s on line %d

Warning: imagecreatefromstring(): Couldn't create GD Image Stream out of Data in %s on line %d
bool(false)
Loading