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

Conversation

nielsdos
Copy link
Member

Fixes GH-8556

The return type from a ffi trampoline was just copied over without taking into account that the cdata may outlive the cdef. Fix it in the same way that others have fixed it: by using the zend_ffi_remember_type function.
This by itself was not enough. The FFI::cdef function sets the default type attr to ZEND_FFI_ATTR_STORED, but the type isn't actually stored into the weak_types hashtable. This causes the lifetime management code to mistakenly think it is already in the table and therefore leads to use after frees. Fix it by changing the default flags to 0.

Fixes phpGH-8556

The return type from a ffi trampoline was just copied over without
taking into account that the cdata may outlive the cdef. Fix it in the
same way that others have fixed it: by using the zend_ffi_remember_type
function.
This by itself was not enough. The FFI::cdef function sets the default
type attr to ZEND_FFI_ATTR_STORED, but the type isn't actually stored
into the weak_types hashtable. This causes the lifetime management code
to mistakenly think it is already in the table and therefore leads to
use after frees. Fix it by changing the default flags to 0.
Comment on lines -2922 to +2932
FFI_G(default_type_attr) = ZEND_FFI_ATTR_STORED;
FFI_G(default_type_attr) = 0;
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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants