Hello,
I reported this issue as bug #63180, but I am also posting it here. We are
going to use the attached patch on our production servers, so I would like to
know if any of the developers sees any problems with the attached patch.
The text of the bug report follows below:
PHP 5.4 has a bug in the handling if interned (literal) strings.
Each time an HTTP request terminates, the interned strings are restored to the
snapshot that was taken at the end of the startup sequence, which
effectively removes any interned strings that have been added after the
snapshot has been made.
Usually when a hash item is added, the code in zend_hash.c allocates extra
space after the end of the Bucket structure, copies the key there and then
points Bucket::arKey to the key copy. However when dealing with interned hash
keys the code tries to optimize the algorithm by not allocating extra
space after the end of the Bucket structure, but just pointing Bucket::arKey
to the passed arKey parameter.
if (IS_INTERNED(arKey)) {
p = (Bucket *) pemalloc(sizeof(Bucket), ht->persistent);
if (!p) {
return FAILURE;
}
p->arKey = arKey;
} else {
p = (Bucket *) pemalloc(sizeof(Bucket) + nKeyLength, ht->persistent);
if (!p) {
return FAILURE;
}
p->arKey = (const char*)(p + 1);
memcpy((char*)p->arKey, arKey, nKeyLength);
}
The problem happens when a persistent hash table gets an interned key as a
parameter. What happens is that the table and its bucket are persistent, i.e.
they remain even after the current HTTP request has been terminated, but the
bucket key is removed from the interned keys table and its memory will be
reused by other interned keys upon the next request. This leads to corruption
of the array keys in the persistent hash table.
One such case is with the PCRE cache. It is initialized in:
php_pcre.c:PHP_GINIT_FUNCTION(pcre)
by the following code:
zend_hash_init(&pcre_globals->pcre_cache, 0, NULL, php_free_pcre_cache, 1);
The last parameter (1) means that it is a persistent hash table. However the
code in
php_pcre.c:pcre_get_compiled_regex_cache(char *regex, int regex_len TSRMLS_DC)
just passes the regex parameter to zend_hash_update:
zend_hash_update(&PCRE_G(pcre_cache), regex, regex_len+1, (void )&new_entry,
sizeof(pcre_cache_entry), (void*)&pce);
Given that in most cases the regex parameter is created from string literals
in the compiled code, this means that in most cases we end up with
interned, non-persistent keys in the persistent PCRE cache table. So when the
next HTTP request comes and we create different interned strings
they will overwrite the previous ones in the PCRE cache table.
The suggested solution to this bug is to modify the code in zend_hash.c and
change it in such a way that copying of keys is skipped only when the
key is interned and the hash table is persistent. When either the key is non-
interned or the hash table is persistent, the key is copied after the
Bucket structure, so that its maximum lifetime will be the same as the
lifetime of the hash table.
I am attaching a patch which uses the suggested solution. I tested this patch
and it solves the problems with PCRE cache corruption that we observed in our
PHP code. The patch is for PHP 5.4.5 but it also applies cleanly to the latest
git version of PHP 5.4
Regards,
Vesselin Atanasov