heap-use-after-free of a user stream wrapper when a STDOUT user filter opens a stream during shutdown

Ouverte
#25,587 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
40/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
Active
Stack technique
c, php

Piste de recherche

Start with the reported stack trace in main/streams/streams.c at php_stream_locate_url_wrapper (line 2039), then trace wrapper registration and cleanup through main/streams/userspace.c, especially stream_wrapper_dtor. Reproduce with the supplied PHP script under AddressSanitizer using the stated command. Done means opening a stream from the STDOUT filter during shutdown no longer produces a heap-use-after-free.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Bug Status: Needs Triage
Description

The following code:

<?php
class W {
    public $context;
    function stream_open($path, $mode, $options, &$opened_path) { return true; }
}
class F extends php_user_filter {
    function filter($in, $out, &$consumed, bool $closing): int {
        if ($closing) {
            fopen("w2://x", "r");
        }
        return PSFS_PASS_ON;
    }
}
stream_wrapper_register("w2", "W");
stream_filter_register("f", "F");
stream_filter_append(STDOUT, "f");

Resulted in this output:

==2253707==ERROR: AddressSanitizer: heap-use-after-free on address 0x6040000f5f60 at pc 0x0000055f9ef4 bp 0x7ffd5858dcf0 sp 0x7ffd5858dce8
READ of size 4 at 0x6040000f5f60 thread T0
    #0 0x55f9ef3 in php_stream_locate_url_wrapper main/streams/streams.c:2039:26
    #1 0x55fc355 in _php_stream_open_wrapper_ex main/streams/streams.c:2192:12
    #2 0x4570374 in zif_fopen ext/standard/file.c:766:11
    #3 0x383d6e3 in zif_phar_fopen ext/phar/func_interceptors.c:310:2
    #4 0x636166a in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER Zend/zend_vm_execute.h:1323:2
    #5 0x5f1c61c in execute_ex Zend/zend_vm_execute.h:110621:12
    #6 0x5eb4d18 in zend_call_function Zend/zend_execute_API.c:1079:3
    ...
    #11 0x497abab in userfilter_filter ext/standard/user_filters.c:232:28
    #12 0x55e8e86 in php_stream_write_filtered main/streams/streams.c:1183:12
    #13 0x55d9972 in php_stream_flush_ex main/streams/streams.c:1245:3
    #14 0x55d68e8 in php_stream_free main/streams/streams.c:339:18
    #15 0x55f5564 in stream_resource_regular_dtor main/streams/streams.c:1798:19
    #16 0x68e1559 in zend_resource_dtor Zend/zend_list.c:72:3
    #17 0x68e3c84 in zend_close_rsrc_list Zend/zend_list.c:227:6
    #18 0x5e95a47 in zend_shutdown_executor_values Zend/zend_execute_API.c:284:2
    #19 0x5ea0a31 in shutdown_executor Zend/zend_execute_API.c:479:2
    #20 0x6aa7f1e in zend_deactivate Zend/zend.c:1370:2
    #21 0x548bf9f in php_request_shutdown main/main.c:2042:2
    ...

0x6040000f5f60 is located 16 bytes inside of 40-byte region [0x6040000f5f50,0x6040000f5f78)

freed by thread T0 here:
    #0 0x681162 in free (sapi/cli/php+0x681162)
    #1 0x5b22f53 in __zend_free Zend/zend_alloc.c:4108:2
    #2 0x5b2d630 in _efree Zend/zend_alloc.c:3060:3
    #3 0x560e725 in stream_wrapper_dtor main/streams/userspace.c:72:2
    #4 0x68e1559 in zend_resource_dtor Zend/zend_list.c:72:3
    #5 0x68e3c84 in zend_close_rsrc_list Zend/zend_list.c:227:6
    #6 0x5e95a47 in zend_shutdown_executor_values Zend/zend_execute_API.c:284:2
    #7 0x5ea0a31 in shutdown_executor Zend/zend_execute_API.c:479:2
    ...

previously allocated by thread T0 here:
    #0 0x6813cd in malloc (sapi/cli/php+0x6813cd)
    #1 0x5b2e3e3 in __zend_malloc Zend/zend_alloc.c:4079:14
    #2 0x5b2d418 in _emalloc Zend/zend_alloc.c:3050:10
    #3 0x5b2ef8c in _ecalloc Zend/zend_alloc.c:3138:6
    #4 0x560f8a0 in zif_stream_wrapper_register main/streams/userspace.c:477:44
    #5 0x636166a in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER Zend/zend_vm_execute.h:1323:2
    ...

SUMMARY: AddressSanitizer: heap-use-after-free main/streams/streams.c:2039:26 in php_stream_locate_url_wrapper

To reproduce: USE_ZEND_ALLOC=0 ./sapi/cli/php test.php

Commit:

ff7a318304d6d18d23d79fe0c07bb0094aee81ed

Configurations:

CC="clang-12" CXX="clang++-12" CFLAGS="-DZEND_VERIFY_TYPE_INFERENCE" CXXFLAGS="-DZEND_VERIFY_TYPE_INFERENCE" ./configure --enable-debug --enable-address-sanitizer --enable-undefined-sanitizer --enable-re2c-cgoto --enable-zts --enable-bcmath --enable-calendar --enable-dba --enable-dl-test --enable-exif --enable-ftp --enable-gd --enable-gd-jis-conv --enable-mbstring --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvmsg --enable-zend-test --with-zlib --with-bz2 --with-curl --with-enchant --with-gettext --with-gmp --with-mhash --with-ldap --with-libedit --with-readline --with-snmp --with-sodium --with-xsl --with-zip --with-mysqli --with-pdo-mysql --with-pdo-pgsql --with-pgsql --with-sqlite3 --with-pdo-sqlite --with-webp --with-jpeg --with-freetype --enable-sigchild --with-pcre-jit --with-iconv

Operating System:

Ubuntu 20.04 Host, Docker 0599jiangyc/flowfusion:latest

This report is automatically generated by FlowFusion

PHP Version
nightly
Operating System

No response

Langage dominant
C
Étoiles
41.8k
Forks
8.6k
Merge moyen
2 j 15 h
PR mergées (30 j)
125

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de php/php-src

Toutes les issues de php/php-src

Issues similaires

Plus d'issues C

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.