[php-src] Issue #20518: Heap use-after-free in var_unserializer caused by recursive unserialize(serialize($GLOBALS))

From: Date: Tue, 18 Nov 2025 07:24:36 +0000
Subject: [php-src] Issue #20518: Heap use-after-free in var_unserializer caused by recursive unserialize(serialize($GLOBALS))
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-251241@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/20518 Author: vi3tL0u1s ### Description The following code: ```php <?php class B implements Serializable { public function serialize() { $a > unserialize(serialize($GLOBALS)); if (!is_array($value)) { return stripslashes($value); } } function __destruct() { global $gandler; $a > unserialize(serialize($GLOBALS)); $a > unserialize(serialize($GLOBALS)); } public function unserialize($data) {} } $data = array(new B); serialize($data); ``` Command: ``` USE_ZEND_ALLOC=0 ./php-src/sapi/cli/php poc.php ``` Resulted in this output: ``` ================================================================= ==2167655==ERROR: AddressSanitizer: heap-use-after-free on address 0x621000012d28 at pc 0x5649f8b3c149 bp 0x7ffc28035bc0 sp 0x7ffc28035bb8 READ of size 1 at 0x621000012d28 thread T0 #0 0x5649f8b3c148 in zval_get_type /path/to/php-src/Zend/zend_types.h:659:18 #1 0x5649f8b3edeb in php_var_unserialize_internal /path/to/php-src/ext/standard/var_unserializer.re:910:27 #2 0x5649f8b47f92 in process_nested_array_data /path/to/php-src/ext/standard/var_unserializer.re:512:8 #3 0x5649f8b433b7 in php_var_unserialize_internal /path/to/php-src/ext/standard/var_unserializer.re:1124:7 #4 0x5649f8b3c3e1 in php_var_unserialize /path/to/php-src/ext/standard/var_unserializer.re:858:11 #5 0x5649f8b57ea4 in php_unserialize_with_options /path/to/php-src/ext/standard/var.c:1459:7 #6 0x5649f8b5931f in zif_unserialize /path/to/php-src/ext/standard/var.c:1517:2 #7 0x5649f9173231 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /path/to/php-src/Zend/zend_vm_execute.h:1421:2 #8 0x5649f8fde5a2 in execute_ex /path/to/php-src/Zend/zend_vm_execute.h:116212:12 #9 0x5649f8fb8bde in zend_call_function /path/to/php-src/Zend/zend_execute_API.c:1014:3 #10 0x5649f8fbade2 in zend_call_known_function /path/to/php-src/Zend/zend_execute_API.c:1108:23 #11 0x5649f9388d6a in zend_call_known_instance_method /path/to/php-src/Zend/zend_API.h:860:2 #12 0x5649f93865cb in zend_call_known_instance_method_with_0_params /path/to/php-src/Zend/zend_API.h:866:2 #13 0x5649f9385fc1 in zend_objects_destroy_object /path/to/php-src/Zend/zend_objects.c:172:3 #14 0x5649f9382886 in zend_objects_store_call_destructors /path/to/php-src/Zend/zend_objects_API.c:57:7 #15 0x5649f8fadabf in shutdown_destructors /path/to/php-src/Zend/zend_execute_API.c:264:3 #16 0x5649f940648e in zend_call_destructors /path/to/php-src/Zend/zend.c:1336:3 #17 0x5649f8c0e0ff in php_request_shutdown /path/to/php-src/main/main.c:1985:3 #18 0x5649f941711b in do_cli /path/to/php-src/sapi/cli/php_cli.c:1158:3 #19 0x5649f9411f3c in main /path/to/php-src/sapi/cli/php_cli.c:1362:18 #20 0x7fee761cfd8f (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: 4f7b0c955c3d81d7cac1501a2498b69d1d82bfe7) #21 0x7fee761cfe3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e3f) (BuildId: 4f7b0c955c3d81d7cac1501a2498b69d1d82bfe7) #22 0x5649f7c01d64 in _start (/path/to/php-src/sapi/cli/php+0x601d64) (BuildId: 2f4d58726259e316f2962e15de07f4156fac78bc) 0x621000012d28 is located 40 bytes inside of 4096-byte region [0x621000012d00,0x621000013d00) freed by thread T0 here: #0 0x5649f7c84902 in free (/path/to/php-src/sapi/cli/php+0x684902) (BuildId: 2f4d58726259e316f2962e15de07f4156fac78bc) #1 0x5649f8e5e733 in __zend_free /path/to/php-src/Zend/zend_alloc.c:3571:2 #2 0x5649f8e628f4 in _efree /path/to/php-src/Zend/zend_alloc.c:2790:3 #3 0x5649f8b3b401 in var_destroy /path/to/php-src/ext/standard/var_unserializer.re:311:3 #4 0x5649f8b3a106 in php_var_unserialize_destroy /path/to/php-src/ext/standard/var_unserializer.re:87:3 #5 0x5649f8b58487 in php_unserialize_with_options /path/to/php-src/ext/standard/var.c:1493:2 #6 0x5649f8b5931f in zif_unserialize /path/to/php-src/ext/standard/var.c:1517:2 #7 0x5649f9173231 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /path/to/php-src/Zend/zend_vm_execute.h:1421:2 #8 0x5649f8fde5a2 in execute_ex /path/to/php-src/Zend/zend_vm_execute.h:116212:12 #9 0x5649f8fb8bde in zend_call_function /path/to/php-src/Zend/zend_execute_API.c:1014:3 #10 0x5649f8fbade2 in zend_call_known_function /path/to/php-src/Zend/zend_execute_API.c:1108:23 #11 0x5649f9388d6a in zend_call_known_instance_method /path/to/php-src/Zend/zend_API.h:860:2 #12 0x5649f93865cb in zend_call_known_instance_method_with_0_params /path/to/php-src/Zend/zend_API.h:866:2 #13 0x5649f9385fc1 in zend_objects_destroy_object /path/to/php-src/Zend/zend_objects.c:172:3 #14 0x5649f9382886 in zend_objects_store_call_destructors /path/to/php-src/Zend/zend_objects_API.c:57:7 #15 0x5649f8fadabf in shutdown_destructors /path/to/php-src/Zend/zend_execute_API.c:264:3 #16 0x5649f940648e in zend_call_destructors /path/to/php-src/Zend/zend.c:1336:3 #17 0x5649f8c0e0ff in php_request_shutdown /path/to/php-src/main/main.c:1985:3 #18 0x5649f941711b in do_cli /path/to/php-src/sapi/cli/php_cli.c:1158:3 #19 0x5649f9411f3c in main /path/to/php-src/sapi/cli/php_cli.c:1362:18 #20 0x7fee761cfd8f (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: 4f7b0c955c3d81d7cac1501a2498b69d1d82bfe7) previously allocated by thread T0 here: #0 0x5649f7c84bae in __interceptor_malloc (/path/to/php-src/sapi/cli/php+0x684bae) (BuildId: 2f4d58726259e316f2962e15de07f4156fac78bc) #1 0x5649f8e62e63 in __zend_malloc /path/to/php-src/Zend/zend_alloc.c:3543:14 #2 0x5649f8e627f0 in _emalloc /path/to/php-src/Zend/zend_alloc.c:2780:10 #3 0x5649f8b3bb03 in tmp_var /path/to/php-src/ext/standard/var_unserializer.re:168:20 #4 0x5649f8b3b979 in var_tmp_var /path/to/php-src/ext/standard/var_unserializer.re:189:12 #5 0x5649f8b57e3a in php_unserialize_with_options /path/to/php-src/ext/standard/var.c:1455:12 #6 0x5649f8b5931f in zif_unserialize /path/to/php-src/ext/standard/var.c:1517:2 #7 0x5649f9173231 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /path/to/php-src/Zend/zend_vm_execute.h:1421:2 #8 0x5649f8fde5a2 in execute_ex /path/to/php-src/Zend/zend_vm_execute.h:116212:12 #9 0x5649f8fb8bde in zend_call_function /path/to/php-src/Zend/zend_execute_API.c:1014:3 #10 0x5649f8fbade2 in zend_call_known_function /path/to/php-src/Zend/zend_execute_API.c:1108:23 #11 0x5649f92e841d in zend_call_method /path/to/php-src/Zend/zend_interfaces.c:83:2 #12 0x5649f92ea44d in zend_call_method_with_0_params /path/to/php-src/Zend/zend_interfaces.h:46:9 #13 0x5649f92ea0ae in zend_user_serialize /path/to/php-src/Zend/zend_interfaces.c:412:2 #14 0x5649f8b54e91 in php_var_serialize_intern /path/to/php-src/ext/standard/var.c:1179:10 #15 0x5649f8b5e061 in php_var_serialize_nested_data /path/to/php-src/ext/standard/var.c:1011:5 #16 0x5649f8b56507 in php_var_serialize_intern /path/to/php-src/ext/standard/var.c:1302:4 #17 0x5649f8b5e040 in php_var_serialize_nested_data /path/to/php-src/ext/standard/var.c:1008:6 #18 0x5649f8b56507 in php_var_serialize_intern /path/to/php-src/ext/standard/var.c:1302:4 #19 0x5649f8b53941 in php_var_serialize /path/to/php-src/ext/standard/var.c:1318:2 #20 0x5649f8b56fe3 in zif_serialize /path/to/php-src/ext/standard/var.c:1364:2 #21 0x5649f9173231 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /path/to/php-src/Zend/zend_vm_execute.h:1421:2 #22 0x5649f8fde5a2 in execute_ex /path/to/php-src/Zend/zend_vm_execute.h:116212:12 #23 0x5649f8fb8bde in zend_call_function /path/to/php-src/Zend/zend_execute_API.c:1014:3 #24 0x5649f8fbade2 in zend_call_known_function /path/to/php-src/Zend/zend_execute_API.c:1108:23 #25 0x5649f9388d6a in zend_call_known_instance_method /path/to/php-src/Zend/zend_API.h:860:2 #26 0x5649f93865cb in zend_call_known_instance_method_with_0_params /path/to/php-src/Zend/zend_API.h:866:2 #27 0x5649f9385fc1 in zend_objects_destroy_object /path/to/php-src/Zend/zend_objects.c:172:3 #28 0x5649f9383b22 in zend_objects_store_del /path/to/php-src/Zend/zend_objects_API.c:181:4 #29 0x5649f93ed396 in rc_dtor_func /path/to/php-src/Zend/zend_variables.c:57:2 SUMMARY: AddressSanitizer: heap-use-after-free /path/to/php-src/Zend/zend_types.h:659:18 in zval_get_type Shadow bytes around the buggy address: 0x0c427fffa550: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c427fffa560: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c427fffa570: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c427fffa580: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c427fffa590: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa =>0x0c427fffa5a0: fd fd fd fd fd[fd]fd fd fd fd fd fd fd fd fd fd 0x0c427fffa5b0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c427fffa5c0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c427fffa5d0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c427fffa5e0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c427fffa5f0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb ==2167655==ABORTING ``` Commit: ``` d750d30a627a143d88670cf0431d6f42b2c77c4b ``` Build configuration: ``` CC="clang" CXX="clang++" CFLAGS="-DZEND_VERIFY_TYPE_INFERENCE" CXXFLAGS="-DZEND_VERIFY_TYPE_INFERENCE" ./configure --enable-debug --enable-address-sanitizer --disable-all --enable-cli ``` ### PHP Version ```plain PHP 8.6.0-dev (cli) (built: Nov 18 2025 15:42:17) (NTS DEBUG) Copyright (c) The PHP Group Zend Engine v4.6.0-dev, Copyright (c) Zend Technologies with Zend OPcache v8.6.0-dev, Copyright (c), by Zend Technologies ``` ### Operating System Ubuntu 22.04

« previous php.bugs (#251241) next »