ext/gd: imagecreatefromavif() allocates libavif/dav1d buffers on the native heap, bypassing memory_limit, and they leak on memory-exhaustion bailout
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with the AVIF decode path in ext/gd and compare its allocation order and cleanup behavior with the GH-25442 fix path referenced in the issue. Check how libavif allocations interact with ZendMM and what happens on memory exhaustion; done means decode allocations respect PHP's memory_limit and buffers are released when decoding fails.
由索引模型根据 Issue 内容生成。
描述
Description
imagecreatefromavif() runs its decode chain (libavif + dav1d) without
installing a libavif allocator callback, so frame buffers and the RGB
pixel allocation happen on the native C heap -- while every other
allocation in the bundled libgd goes through gdMalloc/gdRealloc
(= ZendMM). The allocation order in gdImageCreateFromAvifCtx() makes
this visible:
avifRGBImageAllocatePixels()-- native heap, sized purely from the
attacker-controlled image headeravifImageYUVToRGB()-- native heapgdImageCreateTrueColor()-- first large ZendMM allocation
Consequences:
- memory_limit does not cover the decode allocations. With
memory_limit = 10M, decoding a 4096x4096 AVIF drives the process
peak RSS to ~115 MB while ZendMM still reports ~10 MB. - When ZendMM finally runs out (step 3), the fatal bailout longjmps
past thecleanup:label, so the already-allocated native pixel
and decoder buffers are not freed. Under FPM every such request
leaks 64 MB (4096x4096) into the worker; a 16000x16000 image (valid
AV1 dimensions) is ~1 GB per request against the default 128M limit.
(The leak follows from the longjmp semantics -- we did not run an
FPM accumulation test.) - Control experiment: identical-size WebP and PNG decode under the
same 10M limit peak at ~14.7 MB -- no native-heap bypass.
Same root cause as GH-25442 (libavif native-heap allocations bypass the
Zend memory manager; encode side, ~1.6 MB leak per call). That fix
(036bed01ce) only covered the readFromCtx reader; the decode-side
allocation bypass and its fatal-bailout behavior are not addressed.
Tested on PHP 8.5.12 (bundled libgd). Master's copy is refactored but
has the same allocation order (native pixels before the first large
ZendMM allocation) and the same cleanup-on-error structure.
Proof of concept
4096x4096 solid-color files: big.avif is 1355 bytes (pillow-avif-plugin).
ini_set('memory_limit', '10M');
imagecreatefromavif('/tmp/big.avif'); // Fatal, but peak RSS ~115 MB
imagecreatefromwebp('/tmp/big.webp'); // Fatal at ~14.7 MB peak (control)
Measured with /usr/bin/time -l (libavif master + dav1d 1.5.4):
| file | limit | outcome | max RSS |
|---|---|---|---|
| big.avif | 10M | Fatal | 115 MB |
| big.webp | 10M | Fatal | 14.7 MB |
| big.png | 10M | Fatal | 14.8 MB |
| big.avif | none | decodes OK | 172 MB |
Fix
Either install libavif memory callbacks (avifSetMemoryCallbacks ->
gdMalloc/gdFree) so decode allocations enter ZendMM and are covered by
memory_limit, or pre-check dimensions against the remaining limit
before the native allocations. The GH-25442 fix path (036bed01ce) does
not affect this issue.
- 主要语言
- C
- 星标
- 41.8k
- 派生
- 8.6k
- 平均合并
- 2 天 15 小时
- 30 天内合并 PR
- 125
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
php/php-src 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)未关闭Extension: openssl Status: Needs Triage
难度 2/5 1-3 小时 新手友好度 79/100
维护者通常 1 天内回复
-
Bug Status: Needs Triage
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
SNMP accepts malformed remote ports可能已有人在做 @somethingwithproof 于 21 天前认领。 未关闭
难度 2/5 半天 新手友好度 62/100
维护者通常 1 天内回复
-
Bug Status: Needs Triage
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
ZIP/ZIPS decode dereferences NULL when the cached libdeflate decompressor can't be allocated (3.5 regression)可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
AcademySoftwareFoundation/openexr#2884 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
python-pillow/Pillow#10590 ·
维护者通常 1 天内回复
-
status: needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 62/100
-
bug
难度 2/5 1-3 小时 新手友好度 62/100
openwrt/packages#31407 · 1 条评论 ·
维护者通常 1 天内回复