{"vuln_id":"GHSA-jqcx-ccgc-xwhv","aliases":["CVE-2023-3824"],"description":"Buffer overflow and overread in phar_dir_read()","published_at":"2023-08-05T04:14:59Z","modified_at":"2023-08-05T04:14:59Z","first_seen_at":"2026-11-22T14:05:37.633537Z","severity":"critical","severity_source":"ghsa","cvss_score":9.4,"cvss_vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:L","cwe":["CWE-120","CWE-121","CWE-122","CWE-126"],"vendor":null,"product":null,"kev":{"is_kev":false,"date_added":null,"due_date":null,"ransomware":false},"epss":null,"epss_percentile":null,"nvd_status":null,"enriched_at":null,"links":{"nvd":"https://github.com/advisories/GHSA-jqcx-ccgc-xwhv"},"matched_alias":null,"items":[{"id":12679,"source":{"id":"gh-php-src","name":"PHP releases (GitHub php/php-src tags)"},"title":"GHSA-jqcx-ccgc-xwhv / CVE-2023-3824: Buffer overflow and overread in phar_dir_read()","summary":"Buffer overflow and overread in phar_dir_read() ### Summary Buffer mismanagement in `phar_dir_read()` causes a buffer overflow and a buffer overread later. ### Details https://github.com/php/php-src/blob/be71cadc2f899bc39fe27098042139392e2187db/ext/phar/dirstream.c#L89C1-L116 There are few issues here: * The if check `to_read == 0 || count < ZSTR_LEN(str_key)` is not right. In particular, if this is false we know that `count >= ZSTR_LEN(str_key)`. Furthermore, because of `to_read = MIN(ZSTR_LEN(str_key), count);` we now actually have: `to_read == ZSTR_LEN(str_key)`. If we have a filename length (i.e. `ZSTR_LEN(str_key)`) equal to `sizeof(php_stream_dirent)`, then we get `ZSTR_LEN(str_key) == count == to_read == sizeof(php_stream_dirent)`. Take a look now at this line: `((php_stream_dirent *) buf)->d_name[to_read + 1] = '\\0';`. Assume `sizeof(php_stream_dirent)` is 4096 like on most Linuxes. Then we write at offset 4097, which is two bytes outside of `buf`. This line was actually supposed to use `to_read` instead of `to_read + 1`, because now even if it doesn't overflow, it does not properly NUL-terminate the filename. We're just lucky there's a memset above. Correcting that to use `to_read` doesn't solve the whole problem: it would still overflow because it will write at offset 4096 which is still outside `buf`. This means we have a stack information leak and a buffer write overflow. * Both the `memset` and the return value assume that `count == sizeof(php_stream_dirent)`. In principle if there are any callers where that count is smaller, a buffer overflow occurs in the `memset`. However, inside PHP itself I did not find any such caller, but this may be a concern for third-party extensions. ### PoC I created a reproducer using PHP-8.0 with these configure flags: `./configure --enable-debug --disable-all --enable-phar --with-valgrind` using compiler `gcc (GCC) 13.1.1 20230429`. Create the malicious Phar file using: ```php startBuffering(); $phar->addFromString(str_repeat('A', PHP_MAXPATHLEN - 1).'B', 'This is the content of the file.'); $phar->stopBuffering(); ?> ``` Trigger the buffer overflow and overread using this script: ```php ``` If you run this in Valgrind, i.e. `valgrind ./sapi/cli/php trigger.php` you'll find two Valgrind complaints: ``` ==42575== Conditional jump or move depends on uninitialised value(s) ==42575== at 0x4847ED8: strlen (vg_replace_strmem.c:501) ==42575== by 0x53BE9C: zif_readdir (dir.c:389) ==42575== by 0x6D05C4: ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER (zend_vm_execute.h:1295) ==42575== by 0x742F7D: execute_ex (zend_vm_execute.h:55163) ==42575== by 0x747848: zend_execute (zend_vm_execute.h:59523) ==42575== by 0x696376: zend_execute_scripts (zend.c:1694) ==42575== by 0x5F6D45: php_execute_scrip Patched: 8.0.30","url":"https://github.com/php/php-src/security/advisories/GHSA-jqcx-ccgc-xwhv","published_at":"2023-08-05T04:14:59Z","updated_at":"2023-08-05T04:14:59Z","fetched_at":"2026-11-22T14:05:37.431203Z","severity":"critical","cvss_score":9.8,"epss":0.21786,"is_kev":false,"has_exploit":false,"kev_due_date":null,"vendor":"php","product":"php-src","vuln_id":"CVE-2023-3824","vuln_ids":["CVE-2023-3823","CVE-2023-3824","CVE-2024-2756","CVE-2024-3096","GHSA-JQCX-CCGC-XWHV"],"tags":["repo-advisory","CWE-120","CWE-121","CWE-122","CWE-126","security","repo:php/php-src","stack"],"cluster_id":11792,"links":{"self":"https://vulnwatch.ve1ve2.com/api/v1/items/12679","html":"https://vulnwatch.ve1ve2.com/item/12679"}}]}