Skip to content
Memory corruption Disclosed report Advanced

memcpy negative size parameter in php_resolve_path

A negative size parameter in a memcpy call within php_resolve_path could be triggered by passing a filename of length 0x7fffffff to several PHP file functions. An attacker could potentially cause a crash or memory corruption, though the report does not demonstrate exploitation beyond the negative size.

fms Follow

Originally disclosed on HackerOne

Bounty$500 Low View original
  • Fixed in 53 days
  • Paid in 53 days
  • Disclosed after 4 months

Upstream Bug

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

Summary

Multiple PHP functions are vulnerable to negative size parameter in memcpy call through php_resolve_path. Some of the affected functions are: file_get_contents, file_put_contents, file, readfile, get_meta_tags, gzopen, readgzfile, gzfile, tidy_repair_file, php_strip_whitespace, parse_ini_file, highlight_file.

When filename parameter size is equal to 0x7fffffff, memcpy function will receive a negative size value in php_resolve_path. This was identified on a 64 bits linux build.

GDB output:

gdb -q --args /home/operac/build4/bin/php -n poc.php
No symbol table is loaded.  Use the "file" command.
Breakpoint 1 (__asan_report_error) pending.
Reading symbols from /home/operac/build4/bin/php...done.
gdb-peda$ b fopen_wrappers.c:555
Breakpoint 2 at 0x17333ac: file /home/operac/build4/php-src/main/fopen_wrappers.c, line 555.
gdb-peda$ r
Starting program: /home/operac/build4/bin/php -n poc.php
...
Breakpoint 2, php_resolve_path (filename=0x7fff6ec00018 'A' <repeats 200 times>..., filename_length=<optimized out>, 
    path=<optimized out>) at /home/operac/build4/php-src/main/fopen_wrappers.c:556
556				ptr = NULL;
gdb-peda$ b memcpy
Breakpoint 3 at 0x455469: memcpy. (43 locations)
gdb-peda$ c
...
Breakpoint 3, 0x00007ffff6ef6aa0 in memcpy () from /usr/lib/x86_64-linux-gnu/libasan.so.2
gdb-peda$ p/d $rdx
$1 = -2147483648

Patch

http://git.php.net/?p=php-src.git;a=commit;h=da7e89cde880c66887caacd0a3eae7ecdacf9b2a

Fixed for PHP 5.6.27, PHP 7.0.12

http://php.net/ChangeLog-5.php http://php.net/ChangeLog-7.php#7.0.12

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 an integer overflow or missing length check allows a filename length of 0x7fffffff to be passed to memcpy as a negative size.

Impact

Could cause a crash or memory corruption, but the report does not show code execution or data leakage.

What the attacker needed

Ability to call affected PHP functions with a long filename

The fix

The issue was fixed in PHP 5.6.27 and PHP 7.0.12.

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.

PHP has functions like file_get_contents that read files. When given a very long filename, a memory copy operation inside PHP receives a negative size, which is invalid. This could crash the program or corrupt memory, but the report only shows the negative value, not full exploitation.

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.