-
Notifications
You must be signed in to change notification settings - Fork 7.8k
FFI use after free for nested FFI::addr() calls #9598
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
Comments
This is not a bug. With FFI you're in general required to keep references to things you want to address. FFI is not refcounting addresses for you. After all that'll break down in many other scenarios, e.g.:
Now, if you do While it would be technically feasible in some limited scenarios to refcount an addr of a value, it probably removes also some of the explicit-ness of FFI. If you want to, though, you could introduce a owned reference (like the |
…n test (iluuu1994) This PR was merged into the 6.2 branch. Discussion ---------- [VarDumper] Fix use-after-free with nested FFI::addr() in test | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | php/php-src#9598 | License | MIT | Doc PR | This issue was found by the PHP community build, after which I created php/php-src#9598. Basically, the inner `\FFI::addr()` `CData` will be deallocated after the second `\FFI::addr()` call which will be stored and then points to invalid memory. However, [it turns out](php/php-src#9599 (comment)) nesting `\FFI::addr()` calls without temporarily storing the result is actually not allowed. https://www.php.net/manual/en/ffi.addr.php > Creates an unmanaged pointer to the C data represented by the given `FFI\CData`. The source ptr must survive the resulting pointer. This function is mainly useful to pass arguments to C functions by pointer. We'll see if we can improve `FFI` by throwing an exception when passing temporary values to `FFI::addr()`. Either way, this test is not valid. Commits ------- 4c79915 Fix use-after-free with nested FFI::addr() in VarDumper test
…n test (iluuu1994) This PR was merged into the 6.2 branch. Discussion ---------- [VarDumper] Fix use-after-free with nested FFI::addr() in test | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | php/php-src#9598 | License | MIT | Doc PR | This issue was found by the PHP community build, after which I created php/php-src#9598. Basically, the inner `\FFI::addr()` `CData` will be deallocated after the second `\FFI::addr()` call which will be stored and then points to invalid memory. However, [it turns out](php/php-src#9599 (comment)) nesting `\FFI::addr()` calls without temporarily storing the result is actually not allowed. https://www.php.net/manual/en/ffi.addr.php > Creates an unmanaged pointer to the C data represented by the given `FFI\CData`. The source ptr must survive the resulting pointer. This function is mainly useful to pass arguments to C functions by pointer. We'll see if we can improve `FFI` by throwing an exception when passing temporary values to `FFI::addr()`. Either way, this test is not valid. Commits ------- 4c799152ec Fix use-after-free with nested FFI::addr() in VarDumper test
Description
The following code:
Resulted in this output:
But I expected this output instead:
PHP Version
PHP-8.1, probably also PHP-8.0
Operating System
No response
The text was updated successfully, but these errors were encountered: