Skip to content
Memory corruption Disclosed report Advanced

Heap BufferOver Flow in escapeshellargs and escapeshellcmd functions

A heap buffer overflow exists in PHP's escapeshellarg and escapeshellcmd functions when processing very large strings, caused by an integer overflow in the size calculation. An attacker could potentially write past the allocated buffer, but only if the memory limit is set high enough (roughly over 1024MB).

libnex Follow

Originally disclosed on HackerOne

Bounty$500 View original
  • Fixed in 4 months
  • Paid in 4 months
  • Disclosed after 3.8 years
  1. Bug report: https://bugs.php.net/bug.php?id=71270
  2. Patch submitted and accepted: https://github.com/php/php-src/commit/2871c70efaaaa0f102557a17c727fd4d5204dd4b

---Description---

1)I found this vulnerability using a custom PHP engine fuzzer that I wrote. There exist a heap-based buffer over flow that allows one to write a user tainted data pass an allocated buffer. This vulnerability lies in the following functions: escapeshellarg escapeshellcmd

  1. On a default php installation, the memory limit is set to 128MB and this vulnerability is not triggerable. My analysis shows that this is triggerable when memory limit is roughly > 1024mb. A quick search on github shows that it's not uncommon to see code like "ini_set('memory_limit', -1);"

3)I've created a POC that triggers the buffer over write with 0x414141414141.....

  1. A string of 1024mb is created and passed into escapeshellarg. "l" contains the length of this string:

Breakpoint 2, php_escape_shell_arg (str=0x7fffad469028 'A' <repeats 200 times>...) at /home/elaw/php-7.0.0/ext/standard/exec.c:343 343 int x, y = 0, l = (int)strlen(str);

gdb-peda$ print l $43 = 0x40000000 // 1024mb

  1. This length "l" is then passed into zend_string_alloc as "4 * l + 2" which results in an integer overflow:

Temporary breakpoint 3, php_escape_shell_arg (str=0x7fffad000018 'A' <repeats 200 times>...) at /home/elaw/php-7.0.1/ext/standard/exec.c:348 348 cmd = zend_string_alloc(4 * l + 2, 0); /* worst case */

gdb-peda$ print 4* l + 2 $44 = 0x2 //Overflow

  1. Stepping into zend_string_alloc to verify the integer overflow. Notice len=0x2:

zend_string_alloc (persistent=0x0, len=0x2) at /home/elaw/php-7.0.0/Zend/zend_string.h:121
121 zend_string *ret = (zend_string *)pemalloc(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);

  1. Lets confirm the overflow again in the allocated (zend_string *) cmd. Notice cmd.len=0x2:

gdb-peda$ p *cmd $52 = { gc = { refcount = 0x1, u = { v = { type = 0x6, flags = 0x0, gc_info = 0x0 }, type_info = 0x6 } }, h = 0x0, len = 0x2, val = "1" }

  1. The loops then writes pass the allocated buffer in

258 for (x = 0, y = 0; x < l; x++) { .... 321 ZSTR_VAL(cmd)[y++] = str[x];

  1. Verifying the buffer overflow in

gdb-peda$ p (zend_string *)cmd.len $9 = (zend_string *) 0x2 gdb-peda$ x/100b (zend_string *)cmd.val 0x1625a58: 0x27 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625a60: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625a68: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625a70: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625a78: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625a80: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625a88: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625a90: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625a98: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625aa0: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625aa8: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625ab0: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x1625ab8: 0x41 0x41 0x41 0x41

  1. The vulnerability for php_escape_shell_cmd is identical.

The original report as published on HackerOne, by libnex. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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 an integer overflow in the calculation '4 * l + 2' used to allocate the output buffer, where 'l' is the input string length, causing a small buffer to be allocated for a large input.

Impact

An attacker could write past the allocated buffer, causing memory corruption, but only when the PHP memory limit is roughly over 1024MB.

What the attacker needed

PHP memory limit > 1024MB; Ability to pass a very large string to escapeshellarg or…

The fix

The vulnerability was fixed by a patch submitted and accepted to the PHP source repository.

How it was found

  1. Create a string of about 1024MB.
  2. Pass this string to escapeshellarg (or escapeshellcmd).
  3. The length is used in '4 * l + 2', which overflows to a small value.
  4. A small buffer is allocated, but the function writes the entire input into it, causing a heap buffer overflow.

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.

These PHP functions make strings safe for shell commands. With an extremely long string, a maths error reserves too little memory, so data is written beyond it. This only happens on servers with a very high memory limit.

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.

More by libnex

View all

9 more disclosed reports, best first.

More from Internet Bug Bounty

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.