From fec7619a5c35c55b983ffd73f5df1a10c79b63ce Mon Sep 17 00:00:00 2001 From: Lu Jing Date: Tue, 1 Dec 2026 09:28:24 +0800 Subject: [PATCH] Fix GH-25961: Reject truncated JPEG SOF headers 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. --- NEWS | 2 ++ ext/standard/image.c | 15 ++++++++++----- ext/standard/tests/image/gh25961.phpt | 10 ++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 ext/standard/tests/image/gh25961.phpt diff --git a/NEWS b/NEWS index 37c3cd8a16a4..b041aad8d79b 100644 --- a/NEWS +++ b/NEWS @@ -312,6 +312,8 @@ PHP NEWS Lazizbek Ergashev) . Fixed getimagesize() reporting wrapped dimensions for XBM images with out-of-range width or height. (Ilia Alshanetsky) + . Fixed GH-25961 (getimagesize() exposing EOF as the channel count for a + truncated JPEG SOF). (Lu Jing) . Fixed bug GH-25281 (Use-after-free when proc_open() pipe is closed from its user filter). (Jakub Zelenka) diff --git a/ext/standard/image.c b/ext/standard/image.c index 58795384f811..5324862cdfd4 100644 --- a/ext/standard/image.c +++ b/ext/standard/image.c @@ -478,11 +478,16 @@ static struct gfxinfo *php_handle_jpeg (php_stream * stream, zval *info) /* handle SOFn block */ result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo)); length = php_read2(stream); - result->bits = php_stream_getc(stream); - result->height = php_read2(stream); - result->width = php_read2(stream); - 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)) { + efree(result); + return NULL; + } + result->bits = sof[0]; + result->height = ((unsigned short) sof[1] << 8) | sof[2]; + result->width = ((unsigned short) sof[3] << 8) | sof[4]; + result->channels = sof[5]; + if (!info) { /* if we don't want extended info -> return */ return result; } if (php_stream_seek(stream, length - 8, SEEK_CUR)) { /* file error after info */ diff --git a/ext/standard/tests/image/gh25961.phpt b/ext/standard/tests/image/gh25961.phpt new file mode 100644 index 000000000000..ed8cf3493e00 --- /dev/null +++ b/ext/standard/tests/image/gh25961.phpt @@ -0,0 +1,10 @@ +--TEST-- +GH-25961: getimagesize() rejects a JPEG SOF with a missing component count +--FILE-- + +--EXPECT-- +bool(false)