-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix use-after-free in FE_FREE with GC interaction #20766
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
base: PHP-8.3
Are you sure you want to change the base?
Conversation
3cb81eb to
ae65a02
Compare
|
This really is a bug in live_range computation :-( Essentially a proper fix would be creating gaps in live ranges from any freeing op having ZEND_FREE_ON_RETURN as extended_value until the actual ZEND_RETURN; then marking the ZEND_RETURN as starting op for the new live range. |
ae65a02 to
8f9bd3b
Compare
|
@bwoebi OK, I've updated the live ranges calculation. Now I have: I need to look at the failures, but is this the direction? |
|
I agree that splitting the live-range is the best approch. This is complicated by Furthermore, nested loops will insert multiple |
When FE_FREE with ZEND_FREE_ON_RETURN frees the loop variable during an early return from a foreach loop, the live range for the loop variable was incorrectly extending past the FE_FREE to the normal loop end. This caused GC to access the already-freed loop variable when it ran after the RETURN opcode, resulting in use-after-free. Fix by splitting the ZEND_LIVE_LOOP range when an FE_FREE with ZEND_FREE_ON_RETURN is encountered: - One range covers the early return path up to the FE_FREE - A separate range covers the normal loop end FE_FREE - Multiple early returns create multiple separate ranges
8f9bd3b to
0c6e1b5
Compare
|
OK, I'll leave it to someone more in the know, since my changes don't cover those cases and still break 3 opcache tests (assertions that the range is non-empty). |
|
Exception handling is also affected by the incorrect live ranges when Lines 8184 to 8189 in ef52252
This updates throw_in_fe_free.phpfunction f() {
foreach (it() as $v) {
return;
}
}
function it() {
return new class([0]) extends ArrayIterator {
public function __destruct() {
throw new Exception();
}
};
}
f();This was introduced in b0af9ac. Apparently we used handle For GC, we could apply the same special case. Delaying GC until a safe point would also work: #19787. |
Applying the same special case isn't that easy, because, unlike normal exceptions (which are deferred to caller frame when happening during return), GC can actually trigger within the ZEND_RETURN opcode, which is not a FE_FREE. This also goes against the original premise of b0af9ac to simplify. But I agree that the original handling before that commit was too complex. Let me push a PR. |
b0af9ac removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION. This was sort-of elegant, until it was realized in 8258b77 that it would leak the return variable, requiring some more special handling. At some point we added live tmpvar rooting in 52cf7ab, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions. This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically. Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now. Closes php#20766. Signed-off-by: Bob Weinand <[email protected]>
b0af9ac removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION. This was sort-of elegant, until it was realized in 8258b77 that it would leak the return variable, requiring some more special handling. At some point we added live tmpvar rooting in 52cf7ab, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions. This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically. Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now. Closes php#20766. Signed-off-by: Bob Weinand <[email protected]>
b0af9ac removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION. This was sort-of elegant, until it was realized in 8258b77 that it would leak the return variable, requiring some more special handling. At some point we added live tmpvar rooting in 52cf7ab, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions. This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically. Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now. Closes php#20766. Signed-off-by: Bob Weinand <[email protected]>
b0af9ac removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION. This was sort-of elegant, until it was realized in 8258b77 that it would leak the return variable, requiring some more special handling. At some point we added live tmpvar rooting in 52cf7ab, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions. This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically. Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now. Closes php#20766. Signed-off-by: Bob Weinand <[email protected]>
b0af9ac removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION. This was sort-of elegant, until it was realized in 8258b77 that it would leak the return variable, requiring some more special handling. At some point we added live tmpvar rooting in 52cf7ab, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions. This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically. Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now. Closes php#20766. Signed-off-by: Bob Weinand <[email protected]>
b0af9ac removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION. This was sort-of elegant, until it was realized in 8258b77 that it would leak the return variable, requiring some more special handling. At some point we added live tmpvar rooting in 52cf7ab, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions. This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically. Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now. Closes php#20766. Signed-off-by: Bob Weinand <[email protected]>
|
@arnaud-lb Feel free to have a look at #20865. That PR has about the same number of lines added than removed (aside from tests). I think It's fine from complexity perspective too. |
compiles to:
Since we're returning early, in instruction 8, V4 has been freed. However,
ZEND_RETURNmay start GC:Eventually,
zend_gc_collect_cyclescallszend_gc_remove_root_tmpvars. This function is slighly misnamed, because it also removes from the GC buffer VARs that are loop variables, like V4. And instruction 8 is within the live range of V4. Therefore, it attempts to remove V4 from the buffer. But at this point, the zend_refcount behind V4 has already been destroyed. Hence, we have a use-after free:The solution adopted was to mark the loop variable zval as UNDEF.