imagesettile() and imagesetbrush() keep a dangling pointer once the tile is freed

Đang mở
#25,334 1 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á

Issue này chưa được đánh giá.

Mô tả

Description

imagesettile() passes the libgd pointer of the tile to gdImageSetTile() without keeping a reference to the tile object. Once the tile GdImage is freed, the image keeps a dangling pointer, and the next fill with IMG_COLOR_TILED reads freed memory. imagesetbrush() has the same shape, with IMG_COLOR_BRUSHED.

The following code:

<?php
$canvas = imagecreatetruecolor(100, 100);
(function () use ($canvas) {
    $tile = imagecreatetruecolor(10, 10);
    imagesettile($canvas, $tile);
})();
// The tile is freed: its memory is reused
$strings = [];
for ($i = 0; $i < 10000; ++$i) {
    $strings[] = str_repeat("\xff", 7000 + $i % 64);
}
imagefilledrectangle($canvas, 0, 0, 99, 99, IMG_COLOR_TILED);
echo "survived\n";

Resulted in this output:

Segmentation fault

But I expected this output instead:

survived

It crashes every time on PHP 8.5.12 (macOS, system libgd 2.3.3) and on PHP 8.4.27 (the php:8.4-cli Docker image, bundled GD). The same script with imagesetbrush() and imageline(..., IMG_COLOR_BRUSHED) also crashes on 8.5.12.

In ext/gd/gd.c on master, PHP_FUNCTION(imagesettile) and PHP_FUNCTION(imagesetbrush) only call gdImageSetTile(im, tile) and gdImageSetBrush(im, tile), and php_gd_image_object has no field that would hold the tile or the brush. A fix could keep a reference to the tile and brush objects in php_gd_image_object, released when they are replaced and when the image is freed (and reported by get_gc).

I found it while drawing SVG patterns with GD in a userland library: keeping the tile in a variable until the drawing ends works around it.

PHP Version
PHP 8.5.12 (cli) (ZTS)
PHP 8.4.27 (cli) (php:8.4-cli Docker image)
Operating System

macOS 27; Debian (Docker)

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

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.