php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64578 debug_backtrace in set_error_handler corrupts zend heap: segfault
Submitted: 2013-04-03 19:44 UTC Modified: 2013-04-04 05:39 UTC
From: emiel dot mols at gmail dot com Assigned:
Status: Closed Package: Reproducible crash
PHP Version: 5.5Git-2013-04-03 (snap) OS: Irrelevant
Private report: No CVE-ID:
 [2013-04-03 19:44 UTC] emiel dot mols at gmail dot com
Description:
------------
So I thought the other day it might be convenient to grab a stack trace in, you 
know, the place errors are handled. Apparently, PHP thinks this is a terrible 
idea.

The exact cause is unclear, but I've managed to create a decently small test 
case that segfaults both on Debian PHP 5.4.4 and Darwin PHP 5.5 nightly.

In the attached test script, the call to x() should generate an error, because 
accessing a string as associative array is forbidden.

- the segfault occurs in native _zend_mm_free_int
- only able to replicate when there's a function call on the PHP stack
- it appears debug_backtrace is only corrupting the stack -- the call to 
print_r() initiates the segfault.
- i've seen $y change every access (eg containing random other variables, or 
just random heap garbage).
- in narrowing down the specific case, I've also often seen messages along the 
lines of "mm stack corrupt"

Core dumps can be found at:
- Debian: http://db.tt/aA5wAx7a (16MB)
- Darwin: http://db.tt/gxZrP8Pa (400MB)

Test script:
---------------
<?php

set_error_handler(function() { debug_backtrace(); });

function x($s) { $s['a'] = 1; };
$y = '1';
x($y);
print_r($y);


Expected result:
----------------
Being able to use debug_backtrace() in error handlers.

Actual result:
--------------
A corrupt heap, resulting in a freaking segfault.

Patches

bug64578.patch (last revision 2013-04-04 05:35 UTC) by laruence@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-04 03:03 UTC] laruence@php.net
confirmed,  I can reproduce this. looking into it now..
 [2013-04-04 03:03 UTC] laruence@php.net
-Status: Open +Status: Verified
 [2013-04-04 05:35 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug64578.patch
Revision:   1365053756
URL:        https://bugs.php.net/patch-display.php?bug=64578&patch=bug64578.patch&revision=1365053756
 [2013-04-04 05:39 UTC] laruence@php.net
this is actually non-debug-backtrace related issue.

a more simple reproduce script (5.3 also);
<?php
function x($s) { $resource = fopen("php://input", "r"); $s[$resource] = '2'; };
$y = "1";
x($y);
var_dump($y);


result in an buggy $y there:
Warning: Illegal offset type in /tmp/1.php on line 2
UNKNOWN:0


patch attached.
 [2013-04-04 06:43 UTC] laruence@php.net
-Status: Verified +Status: Closed
 [2013-04-04 06:43 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=c96a5bc6be99ff8a4c5dbc1ea3c04d8144bc47a9
Log: Fixed bug #64578 (debug_backtrace in set_error_handler corrupts zend heap: segfault)
 [2013-04-04 06:43 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7fc8077e4caf1ac62e9591775f7779eb1a7c25ad
Log: Fixed Bug #64578
 [2013-04-04 06:45 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=c96a5bc6be99ff8a4c5dbc1ea3c04d8144bc47a9
Log: Fixed bug #64578 (debug_backtrace in set_error_handler corrupts zend heap: segfault)
 [2013-04-04 06:49 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=c96a5bc6be99ff8a4c5dbc1ea3c04d8144bc47a9
Log: Fixed bug #64578 (debug_backtrace in set_error_handler corrupts zend heap: segfault)
 [2013-04-04 06:49 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7fc8077e4caf1ac62e9591775f7779eb1a7c25ad
Log: Fixed Bug #64578
 [2013-04-04 06:49 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=c96a5bc6be99ff8a4c5dbc1ea3c04d8144bc47a9
Log: Fixed bug #64578 (debug_backtrace in set_error_handler corrupts zend heap: segfault)
 [2013-04-04 06:50 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7fc8077e4caf1ac62e9591775f7779eb1a7c25ad
Log: Fixed Bug #64578
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 13:02:46 2014 UTC