Skip to content
Memory corruption Disclosed report Advanced

Integer underflow / arbitrary null write in fread/gzread

An integer underflow in the length parameter of fread/gzread allows writing an arbitrary null byte on 64-bit platforms. An attacker could potentially corrupt memory by triggering this underflow.

fms Follow

Originally disclosed on HackerOne

Bounty$500 View original
  • Disclosed after 3.4 years

https://bugs.php.net/bug.php?id=72114

Integer underflow in the fread/gzread length parameter allows to write an arbitrary null byte on 64 bit platforms. This was identified with the help of ASAN and a custom fuzzer.

(gdb) run gzread2.php 
Starting program: /home/operac/php/php-56/sapi/cli/php gzread2.php

Program received signal SIGSEGV, Segmentation fault.
0x0000000000727b66 in zif_fread (ht=2, return_value=0x7ffff7fd7d00, return_value_ptr=0x7ffff7fa21c8, this_ptr=0x0, return_value_used=0)
    at /home/operac/php/php-56/ext/standard/file.c:1769
1769            Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0;
(gdb) print (*return_value)
$2 = {value = {lval = 140735140003952, dval = 6,9532397838610798e-310, str = {val = 0x7fff74070070 "", len = -2147483648}, ht = 0x7fff74070070, 
    obj = {handle = 1946615920, handlers = 0x5a5a5a5a80000000}, ast = 0x7fff74070070}, refcount__gc = 1, type = 0 '\000', is_ref__gc = 0 '\000'}
(gdb) print (*return_value).value.str.len
$1 = -2147483648

Len has got a negative value here and it will be later used to write the null terminator

 /* needed because recv/read/gzread doesnt put a null at the end*/
 Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0; 

This affected PHP version 5.5, 5.6 and 7.0, patch released today:

http://php.net/ChangeLog-5.php#5.5.36

The original report as published on HackerOne, by fms. 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 the length parameter is not properly validated, allowing an integer underflow that results in a negative string length used for a null-terminator write.

Impact

Could allow an attacker to write an arbitrary null byte, potentially leading to memory corruption or a crash.

What the attacker needed

64-bit platform; ability to call fread/gzread with crafted length

The fix

A patch was released for PHP versions 5.5, 5.6, and 7.0.

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.

A length value used by file-reading functions can become negative due to an integer underflow. This negative length is then used to write a null byte at an unintended memory location, which can crash the program or corrupt 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.

More by fms

View all

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