ext/gd: imagecreatefromavif() allocates libavif/dav1d buffers on the native heap, bypassing memory_limit, and they leak on memory-exhaustion bailout

Đang mở
#25,634 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
39/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c
Lĩnh vực
backend, security

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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:

  1. avifRGBImageAllocatePixels() -- native heap, sized purely from the
    attacker-controlled image header
  2. avifImageYUVToRGB() -- native heap
  3. gdImageCreateTrueColor() -- first large ZendMM allocation

Consequences:

  1. 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.
  2. When ZendMM finally runs out (step 3), the fatal bailout longjmps
    past the cleanup: 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.)
  3. 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.

Ngôn ngữ chính
C
Star
41.8k
Fork
8.6k
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
125

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của php/php-src

Tất cả issue của php/php-src

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.