Описание
PHP-CGI out-of-bounds read and write during PATH_INFO correction
Summary
The PHP CGI binary (php-cgi) can access memory outside the supplied path information when a web server passes inconsistent script and path values. Depending on the request and configuration, an unauthenticated attacker may be able to crash the PHP process, expose process memory, or corrupt memory.
This affects php-cgi in both CGI and FastCGI mode when cgi.fix_pathinfo=1, the default. PHP-FPM and PHP running as an Apache module are not affected.
Details
The problem occurs when PHP removes a trailing path from SCRIPT_FILENAME, or from PATH_TRANSLATED in a redirected CGI request, but the supplied PATH_INFO is shorter than the path being removed. A present but empty PATH_INFO can also trigger the problem. This requires particular web server mappings; using CGI, FastCGI, or URL rewriting alone is not enough.
In init_request_info(), PHP calculates the replacement PATH_INFO by subtracting the length of the removed suffix from the end of the supplied value. If the suffix is longer, the resulting pointer lies before the PATH_INFO buffer. Subsequent path correction can read, write and copy memory through this invalid pointer. The fix checks the lengths before deriving the pointer and skips the rewrite when no valid replacement exists.
Apache CGI example
A legitimate configuration that can produce these values is a legacy Apache CGI installation using Action, combined with a rewrite to an absolute filesystem path. For example, this virtual-host configuration routes a short public URL to a longer route handled by an existing /srv/site/index.php:
This fragment assumes an otherwise working Apache CGI installation with the required modules and access permissions. The Action arrangement is described in the PHP CGI installation documentation.
A request for /news/x can then reach PHP with:
PHP finds the existing index.php, but the remaining /content/articles/x is longer than the original /news/x. Attempting to reconcile these values triggers the invalid memory access. This was reproduced through an actual HTTP request with Apache 2.4.58 and an unmodified PHP CGI binary. An Alias that maps /news to /srv/site/index.php/content/articles produced the same result.
The ordinary Action setup, a conventional rewrite to index.php, and a rewrite to the URL /index.php/content/articles/x did not trigger the issue in these tests. The vulnerable example is a valid routing configuration, but it does not imply that typical Apache PHP installations are affected.
Other FastCGI front ends
A front end can also expose the issue if it builds SCRIPT_FILENAME from an unsplit request path while supplying a shorter or empty PATH_INFO. Review custom FastCGI parameter mappings and configurations that forward requests without checking that the selected script exists. For nginx, this includes checking how fastcgi_split_path_info, SCRIPT_FILENAME, PATH_INFO, and any try_files check work together. A correctly split and validated script path avoids this form of the issue.
An exposed php-cgi FastCGI listener also allows an untrusted client to supply these parameters directly. PHP-FPM remains unaffected.
Impact
- Denial of service: a single unauthenticated request can crash the
php-cgiprocess handling it. Crashes were reproduced on default, non-debug builds of PHP 8.2.35, 8.3.36, 8.4.27, and 8.5.12. - Information disclosure: unrelated memory can appear in
$_SERVER['PATH_INFO']orPATH_TRANSLATED. This may include other request parameters or values added by the front end. Disclosure to the attacker depends on the application returning these values or making them available through logs or other output. - Memory corruption: PHP can overwrite memory during path correction. Code execution has been claimed, but has not been confirmed.
Affected versions
The out-of-bounds read has been present since PHP 4.3.0 and the out-of-bounds write since PHP 5.2.4. This is based on code inspection.
Mitigation
Upgrade to the fixed release for your PHP branch when it becomes available. As a workaround, set cgi.fix_pathinfo=0 in the configuration used by php-cgi, and check that the application's routing still works.
Alternatively, correct the web server mapping so that the path PHP uses identifies the existing script and any additional path information is consistent. For the Apache example above, rewriting to the URL instead of the absolute filesystem path avoided the issue:
See Apache's documentation for the PT rewrite flag. Other rewrites and aliases can affect the result, so verify the complete configuration. Direct access to a FastCGI listener should also be restricted to trusted front ends.
Пакеты
php
>=4.3.0, <8.2.36
8.2.36
php
>=8.3.0, <8.3.37
8.3.37
php
>=8.4.0, <8.4.28
8.4.28
php
>=8.5.0, <8.5.13
8.5.13
Связанные уязвимости
PHP-CGI out-of-bounds read and write during PATH_INFO correction. The PHP CGI binary (php-cgi), in CGI and FastCGI mode with cgi.fix_pathinfo enabled (the default), can perform an out-of-bounds read and write when the path removed from SCRIPT_FILENAME or PATH_TRANSLATED is longer than the supplied PATH_INFO, or when PATH_INFO is empty. This requires a web server mapping that passes such inconsistent values, or a FastCGI listener reachable by untrusted clients. An unauthenticated remote attacker can crash the process, may obtain process memory if the application exposes PATH_INFO or PATH_TRANSLATED, and may corrupt memory. PHP-FPM and the Apache module are not affected. This issue affects PHP versions before 8.2.36, 8.3.* before 8.3.37, 8.4.* before 8.4.28 and 8.5.* before 8.5.13. This affects php-cgi in both CGI and FastCGI mode when cgi.fix_pathinfo=1, the default. PHP-FPM and PHP running as an Apache module are not affected.
PHP-CGI out-of-bounds read and write during PATH_INFO correction. The PHP CGI binary (php-cgi), in CGI and FastCGI mode with cgi.fix_pathinfo enabled (the default), can perform an out-of-bounds read and write when the path removed from SCRIPT_FILENAME or PATH_TRANSLATED is longer than the supplied PATH_INFO, or when PATH_INFO is empty. This requires a web server mapping that passes such inconsistent values, or a FastCGI listener reachable by untrusted clients. An unauthenticated remote attacker can crash the process, may obtain process memory if the application exposes PATH_INFO or PATH_TRANSLATED, and may corrupt memory. PHP-FPM and the Apache module are not affected. This issue affects PHP versions before 8.2.36, 8.3.* before 8.3.37, 8.4.* before 8.4.28 and 8.5.* before 8.5.13.
PHP-CGI out-of-bounds read and write during PATH_INFO correction. The PHP CGI binary (php-cgi), in CGI and FastCGI mode with cgi.fix_pathinfo enabled (the default), can perform an out-of-bounds read and write when the path removed from SCRIPT_FILENAME or PATH_TRANSLATED is longer than the supplied PATH_INFO, or when PATH_INFO is empty. This requires a web server mapping that passes such inconsistent values, or a FastCGI listener reachable by untrusted clients. An unauthenticated remote attacker can crash the process, may obtain process memory if the application exposes PATH_INFO or PATH_TRANSLATED, and may corrupt memory. PHP-FPM and the Apache module are not affected. This issue affects PHP versions before 8.2.36, 8.3.* before 8.3.37, 8.4.* before 8.4.28 and 8.5.* before 8.5.13.
PHP-CGI out-of-bounds read and write during PATH_INFO correction. The ...