php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49753 Current directory in shutdown function leaks between requests
Submitted: 2009-10-02 19:00 UTC Modified: 2017-05-14 16:51 UTC
From: ant at specialops dot ath dot cx Assigned: pollita (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.0 OS: Linux 2.6.30 (gentoo)
Private report: No CVE-ID: None
 [2009-10-02 19:00 UTC] ant at specialops dot ath dot cx
Description:
------------
In 5.3.0 one of my scripts stopped working due to the behaviour documented at the bottom of the register_shutdown_function page.

The actual problem is that if I change the directory in the shutdown function, that change persists across requests and can end up in completely unrelated scripts' shutdown function calls.

Reproduce code:
---------------
<?php
echo "Script A\n";
function test() {
    echo getcwd() . "\n";
    if ( file_exists('file.inc') ) {
        include 'file.inc';
    }
}
register_shutdown_function('test');
?>

<?php
echo "Script B\n";
function change() {
    chdir('/tmp/');
    file_put_contents('file.inc', '<?php echo "something nasty\n"; ?>');
}
register_shutdown_function('change');
?>

Expected result:
----------------
Script A
/var/www/localhost/htdocs/
Script B
Script A
/var/www/localhost/htdocs/

Actual result:
--------------
Script A
/var/www/localhost/htdocs/
Script B
Script A
/tmp
something nasty

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-04 15:21 UTC] kalle@php.net
We could store the cwd before trying to execute the shutdown handlers, and then restore it to that dir after the handlers are executed. However I'm not sure whether we should do this on per SAPI level or per function implementation level, below patch patches it per function level.

Could you try apply this and see if it solves the problem?

Index: basic_functions.c
===================================================================
--- basic_functions.c	(revision 289184)
+++ basic_functions.c	(working copy)
@@ -5026,10 +5026,17 @@
 void php_call_shutdown_functions(TSRMLS_D) /* {{{ */
 {
 	if (BG(user_shutdown_function_names)) {
+		char *cwd = getcwd(NULL, 0);
+
 		zend_try {
 			zend_hash_apply(BG(user_shutdown_function_names), (apply_func_t) user_shutdown_function_call TSRMLS_CC);
 		}
 		zend_end_try();
+
+		if (cwd) {
+			chdir(cwd);
+		}
+
 		php_free_shutdown_functions(TSRMLS_C);
 	}
 }
 [2009-10-07 16:38 UTC] ant at specialops dot ath dot cx
I've just tested it, and it does indeed fix the problem.
 [2009-10-12 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2010-10-09 00:12 UTC] felipe@php.net
-Status: No Feedback +Status: Open
 [2017-05-14 16:51 UTC] pollita@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: pollita
 [2017-05-14 16:51 UTC] pollita@php.net
I'm fairly certain this is fixed in current PHP.
If I'm wrong, please feel free to reopen this bug report.
Please include webserver version and output of: `php -i | grep 'Configure Command'`
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 12:01:27 2024 UTC