Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloning a faked SplFileInfo object may segfault #7809

Closed
b1rdex opened this issue Dec 22, 2021 · 3 comments
Closed

Cloning a faked SplFileInfo object may segfault #7809

b1rdex opened this issue Dec 22, 2021 · 3 comments

Comments

@b1rdex
Copy link
Contributor

b1rdex commented Dec 22, 2021

Description

We have some code that worked without any issues on PHP 8.0, but it's failing with a segmentation fault on 8.1.
I'm not sure whether it's a PHPUnit or PHP issue... So please see the code. Mock creation triggers the problem.

Test repo: b1rdex/php-8.1-segfault

I've opened sebastianbergmann/phpunit#4844 for PHPUnit, but the issue was closed with a piece of advice to report it to PHP. So please see the test repo for the details about the issue.

PHP Version

8.1.1

Operating System

Docker official image

@7snovic
Copy link
Contributor

7snovic commented Dec 22, 2021

the same on my local machine.

I am using the ondrej/php build

image

$ php8.1 vendor/bin/phpunit -vvv Test.php
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.0

Segmentation fault (core dumped)

verified on 8.2 too

sapi/cli/php vendor/bin/phpunit -vvv Test.php
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.0-dev

Segmentation fault (core dumped)

image

@cmb69
Copy link
Member

cmb69 commented Dec 22, 2021

Relevant part of the stack backtrace:

php8_debug.dll!spl_filesystem_object_clone(_zend_object * old_object) Line 360 (c:\php-sdk\phpdev\vs16\x64\php-src-8.0\ext\spl\spl_directory.c:360)
php8_debug.dll!ZEND_CLONE_SPEC_CV_HANDLER(_zend_execute_data * execute_data) Line 38008 (c:\php-sdk\phpdev\vs16\x64\php-src-8.0\Zend\zend_vm_execute.h:38008)
php8_debug.dll!execute_ex(_zend_execute_data * ex) Line 54509 (c:\php-sdk\phpdev\vs16\x64\php-src-8.0\Zend\zend_vm_execute.h:54509)
php8_debug.dll!zend_execute(_zend_op_array * op_array, _zval_struct * return_value) Line 59051 (c:\php-sdk\phpdev\vs16\x64\php-src-8.0\Zend\zend_vm_execute.h:59051)
php8_debug.dll!zend_execute_scripts(int type, _zval_struct * retval, int file_count, ...) Line 1681 (c:\php-sdk\phpdev\vs16\x64\php-src-8.0\Zend\zend.c:1681)
php8_debug.dll!php_execute_script(_zend_file_handle * primary_file) Line 2539 (c:\php-sdk\phpdev\vs16\x64\php-src-8.0\main\main.c:2539)

The problem is at

intern->path = zend_string_copy(source->path);

source->path is NULL, and zend_string_copy() can't handle that. Prior to 13e4ce3, there were no zend_strings but rather char*s so no problem with PHP 8.0.

@cmb69
Copy link
Member

cmb69 commented Dec 22, 2021

The following patch might do:

 ext/spl/spl_directory.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c
index 1161858468..0ba11f4800 100644
--- a/ext/spl/spl_directory.c
+++ b/ext/spl/spl_directory.c
@@ -385,8 +385,12 @@ static zend_object *spl_filesystem_object_clone(zend_object *old_object)
 
 	switch (source->type) {
 		case SPL_FS_INFO:
-			intern->path = zend_string_copy(source->path);
-			intern->file_name = zend_string_copy(source->file_name);
+			if (source->path != NULL) {
+				intern->path = zend_string_copy(source->path);
+			}
+			if (source->file_name != NULL) {
+				intern->file_name = zend_string_copy(source->file_name);
+			}
 			break;
 		case SPL_FS_DIR:
 			spl_filesystem_dir_open(intern, source->path);

cmb69 added a commit to cmb69/php-src that referenced this issue Dec 22, 2021
While the `path` is not supposed to be `NULL` for normal operation, it
is possible to create `SplFileInfo` objects where that is the case, and
we must not follow the null pointer.
@cmb69 cmb69 changed the title Segmentation fault @ PHP 8.1 Cloning a faked SplFileInfo object may segfault Dec 22, 2021
@cmb69 cmb69 closed this as completed in 0ed39ed Dec 23, 2021
cmb69 added a commit that referenced this issue Dec 23, 2021
* PHP-8.1:
  Fix GH-7809: Cloning a faked SplFileInfo object may segfault
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@b1rdex @cmb69 @7snovic and others