Skip to content

Fix GH-8556: FFI\CData use-after-free after FFI object destroyed #10574

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

Open
wants to merge 1 commit into
base: PHP-8.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2777,7 +2777,17 @@ static ZEND_FUNCTION(ffi_trampoline) /* {{{ */
free_alloca(arg_values, arg_values_use_heap);
}

zend_ffi_cdata_to_zval(NULL, ret, ZEND_FFI_TYPE(type->func.ret_type), BP_VAR_R, return_value, 0, 1, 0);
zend_ffi_type *func_ret_type = type->func.ret_type;

if (ZEND_FFI_TYPE_IS_OWNED(func_ret_type)) {
func_ret_type = ZEND_FFI_TYPE(func_ret_type);
if (!(func_ret_type->attr & ZEND_FFI_ATTR_STORED)
&& func_ret_type->kind == ZEND_FFI_TYPE_POINTER) {
type->func.ret_type = func_ret_type = zend_ffi_remember_type(func_ret_type);
}
}

zend_ffi_cdata_to_zval(NULL, ret, func_ret_type, BP_VAR_R, return_value, 0, 1, 0);
free_alloca(ret, ret_use_heap);

exit:
Expand Down Expand Up @@ -2919,7 +2929,7 @@ ZEND_METHOD(FFI, cdef) /* {{{ */

if (code && ZSTR_LEN(code)) {
/* Parse C definitions */
FFI_G(default_type_attr) = ZEND_FFI_ATTR_STORED;
FFI_G(default_type_attr) = 0;
Comment on lines -2922 to +2932
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I afraid this was a design decision - cdef should relive all CData objects. Otherwise almost any type has to be remembered. The problem is not limited to return type, but to any declared type.

<?php
$cdef = FFI::cdef("typedef int T[5];");
$a = $cdef->new("T");
var_dump($a);
unset($cdef);
var_dump($a);

It would be great to find a common efficient solution. Fixing this only for return type doesn't make a lot of sense.

I would agree to merge this fix only for return type without this s/ZEND_FFI_ATTR_STORED/0/ hunk.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, thanks for checking. I'll see later this week if I can come up with something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this :)


if (zend_ffi_parse_decl(ZSTR_VAL(code), ZSTR_LEN(code)) != SUCCESS) {
if (FFI_G(symbols)) {
Expand Down Expand Up @@ -3144,6 +3154,7 @@ static void zend_ffi_cleanup_type(zend_ffi_type *old, zend_ffi_type *type) /* {{

static zend_ffi_type *zend_ffi_remember_type(zend_ffi_type *type) /* {{{ */
{
ZEND_ASSERT(!ZEND_FFI_TYPE_IS_OWNED(type) && "type argument must not be a tagged pointer");
if (!FFI_G(weak_types)) {
FFI_G(weak_types) = emalloc(sizeof(HashTable));
zend_hash_init(FFI_G(weak_types), 0, NULL, zend_ffi_type_hash_dtor, 0);
Expand Down
39 changes: 39 additions & 0 deletions ext/ffi/tests/gh8556.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
GH-8556 (FFI\CData use-after-free after FFI object destroyed)
--EXTENSIONS--
ffi
--SKIPIF--
<?php
try {
$libc = FFI::cdef("char *getenv(char *);", "libc.so.6");
} catch (Throwable $_) {
die('skip libc.so.6 not available');
}
?>
--INI--
ffi.enable=1
--FILE--
<?php

putenv("TEST=123");

// Original
$c = (FFI::cdef("char *getenv(char *);", "libc.so.6"))->getenv("TEST");
var_dump($c);

// Variant on the original
$libc = FFI::cdef("char *getenv(char *);", "libc.so.6");
$result = $libc->getenv("TEST");
unset($libc); // Frees ffi cdata ->type member
var_dump($result);

?>
--EXPECT--
object(FFI\CData:char*)#2 (1) {
[0]=>
string(1) "1"
}
object(FFI\CData:char*)#3 (1) {
[0]=>
string(1) "1"
}