← All posts

PHP. heap corruption detected in phpQuery and Segmentation fault in PHP CLI

suhosin\_bug

After reinstalling PHP 5.2.10, I ran into a problem: “linked list corrupt on efree() - heap corruption detected” when using the phpQuery library, along with a “Segmentation fault” in the CLI version of the freshly installed PHP.

The “linked list corrupt on efree() - heap corruption detected” error appeared in only one place and seemed to me to be related to the DOM module. Before reinstalling, I hadn’t used phpQuery in production, so I couldn’t say for sure whether the reinstall had caused it or whether the error would have occurred anyway.

Whenever I ran any script in CLI mode, I got something like this:


php -f /www/script.php
Segmentation fault

Fixing the Segmentation fault in CLI

After Googling a bit, I couldn’t find anything useful, so I posted a thread on Pykh about my problem. We spent a couple of days thinking it over and suggesting various solutions. On the first day, we managed to beat the “Segmentation fault” in CLI by reordering the PHP modules (thanks to user OFF). Here’s what I have now:


extension=mbstring.so
extension=simplexml.so
extension=spl.so
extension=mysqli.so
extension=mysql.so
extension=session.so
extension=mcrypt.so
extension=gd.so
extension=json.so
extension=ctype.so
extension=iconv.so
extension=hash.so
extension=curl.so
extension=zlib.so
extension=imagick.so
extension=ffmpeg.so
extension=sphinx.so
extension=pcre.so
extension=memcache.so
extension=memcached.so
extension=dom.so

CLI has worked properly ever since and still does.

Fixing heap corruption detected in phpQuery

I’ll admit, I was blaming the “php5-dom” PHP module, but it turned out not to be the problem. The error looked like this:

[Thu Sep 17 01:48:23 2009] [error] [client 93.111.111.111] ALERT - linked list corrupt on efree() - heap corruption detected (attacker ‘93.111.111.111’, file ‘/path/to/kinsburg/path/to/libs/phpquery.php’, line 5689), referer: http://kinsburg.ru/path/to/admin/path/to/parser/

where 93.111.111.111 is the IP address of my local computer (aka the client).

The solution turned out to be anything but obvious to me: the culprit was “Suhosin Patch”, which is intended to provide security. I simply reinstalled PHP, but this time I disabled Suhosin.

A post by user OFF on Pykh.ru put me on the right track, and I’m very grateful to him! There were other proposed solutions to this problem, but they didn’t work out.

At the moment, I’m planning to enable Suhosin Patch again, because I only need to run that ill-fated script once every 3–6 months, and in that time patches for the bug might appear, or a much more effective solution might be found.

Comments are closed. Read the original discussion in Russian →