php-src/ext/zend_test
Niels Dossche bc558bf7a3
Fix GH-11078: PHP Fatal error triggers pointer being freed was not allocated and malloc: double free for ptr errors
Although the issue was demonstrated using Curl, the issue is purely in
the streams layer of PHP.

Full analysis is written in GH-11078 [1], but here is the brief version:
Here's what actually happens:
1) We're creating a FILE handle from a stream using the casting mechanism.
   This will create a cookie-based FILE handle using funopen.
2) We're reading stream data using fread from the userspace stream. This will
   temporarily set a buffer into a field _bf.base [2]. This buffer is now equal
   to the upload buffer that Curl allocated and note that that buffer is owned
   by Curl.
3) The fatal error occurs and we bail out from the fread function, notice how
   the reset code is never executed and so the buffer will still point to
   Curl's upload buffer instead of FILE's own buffer [3].
4) The resources are destroyed, this includes our opened stream and because the
   FILE handle is cached, it gets destroyed as well.
   In fact, the stream code calls through fclose on purpose in this case.
5) The fclose code frees the _bs.base buffer [4].
   However, this is not the buffer that FILE owns but the one that Curl owns
   because it isn't reset properly due to the bailout!
6) The objects are getting destroyed, and so the curl free logic is invoked.
   When Curl tries to gracefully clean up, it tries to free the buffer.
   But that buffer is actually already freed mistakingly by the C library!

This also explains why we can't reproduce it on Linux: this bizarre buffer
swapping only happens on macOS and BSD, not on Linux.

To solve this, we switch to an unbuffered mode for cookie-based FILEs.
This avoids any stateful problems related to buffers especially when the
bailout mechanism triggers. As streams have their own buffering
mechanism, I don't expect this to impact performance.

[1] https://github.com/php/php-src/issues/11078#issuecomment-2155616843
[2] 5e566be7a7/stdio/FreeBSD/fread.c (L102-L103)
[3] 5e566be7a7/stdio/FreeBSD/fread.c (L117)
[4] 5e566be7a7/stdio/FreeBSD/fclose.c (L66-L67)

Closes GH-14524.
2024-06-10 19:38:21 +02:00
..
tests Fix GH-11078: PHP Fatal error triggers pointer being freed was not allocated and malloc: double free for ptr errors 2024-06-10 19:38:21 +02:00
config.m4 adds failing test case for #12060 2023-09-05 10:23:22 +01:00
config.w32 adds failing test case for #12060 2023-09-05 10:23:22 +01:00
fiber.c Revert "Port all internally used classes to use default_object_handlers" 2022-09-14 11:13:23 +02:00
fiber.h Flexible fiber bailout handling (#7163) 2021-06-18 11:01:10 -05:00
fiber.stub.php Add support for validation of missing class synopses (#9472) 2022-09-05 14:21:15 +02:00
fiber_arginfo.h Add support for validation of missing class synopses (#9472) 2022-09-05 14:21:15 +02:00
iterators.c adds failing test case for #12060 2023-09-05 10:23:22 +01:00
iterators.h adds failing test case for #12060 2023-09-05 10:23:22 +01:00
iterators.stub.php adds failing test case for #12060 2023-09-05 10:23:22 +01:00
iterators_arginfo.h adds failing test case for #12060 2023-09-05 10:23:22 +01:00
observer.c Always load EX(opline) into the current frame in JIT when observers are enabled 2024-04-08 15:09:14 +02:00
observer.h Fix memory_leak in zend_test 2022-07-30 15:57:08 +00:00
php_test.h Merge branch 'PHP-8.1' into PHP-8.2 2023-11-25 00:59:26 +01:00
test.c Fix GH-11078: PHP Fatal error triggers pointer being freed was not allocated and malloc: double free for ptr errors 2024-06-10 19:38:21 +02:00
test.h Add support for generating namespaced constant 2023-02-23 10:05:30 +01:00
test.stub.php Fix GH-11078: PHP Fatal error triggers pointer being freed was not allocated and malloc: double free for ptr errors 2024-06-10 19:38:21 +02:00
test_arginfo.h Fix GH-11078: PHP Fatal error triggers pointer being freed was not allocated and malloc: double free for ptr errors 2024-06-10 19:38:21 +02:00