Skip to content

Fix use-after-free when the error handler releases the assignment target - #25701

Open
jorgsowa wants to merge 2 commits into
php:PHP-8.4from
jorgsowa:fix-assign-dim-undef-op-data-uaf
Open

jorgsowa wants to merge 2 commits into
php:PHP-8.4from
jorgsowa:fix-assign-dim-undef-op-data-uaf

Conversation

@jorgsowa

@jorgsowa jorgsowa commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

An error handler for "Undefined variable" on the assigned value can free or modify the array or object being written to. The engine still holds a pointer into it. This is what breaks assign_dim_undef_op_data_008.phpt (added in #24665) on the nightly MSAN and PPC64 ASAN builds.

VM (commit 1) — on its own this fixes the nightly builds, which run without JIT:

  • ASSIGN_DIM / ASSIGN_DIM_OP: hold the array across the warning, and skip the write if the handler destroyed or shared it. This matches zend_undefined_offset_write().
  • ASSIGN_OBJ / ASSIGN_OBJ_OP with a fetched container ($a[0]->p = $undef): copy the container into the instruction's own slot before the warning.

JIT (commit 2):

  • Function JIT hands ASSIGN_OBJ to the VM when the value may be undefined. (trade-off)
  • Tracing JIT exits to the VM when the value is undefined.
  • The typed-reference assignment helpers hold the reference across the warning.

…ainer

When OP_DATA is an undefined variable, the warning was raised while
ASSIGN_DIM, ASSIGN_DIM_OP, ASSIGN_OBJ and ASSIGN_OBJ_OP still held a
pointer into the container, so an error handler releasing or modifying
it left that pointer dangling. Keep the array or the fetched container
alive across the warning, and skip the assignment if the array was
destroyed or shared.
… target

Function JIT leaves ASSIGN_OBJ with a possibly undefined OP_DATA to the
VM handler and tracing JIT exits to the VM on it, so the object is no
longer used after the warning. The typed reference helpers keep the
reference alive across the warning.
@jorgsowa
jorgsowa force-pushed the fix-assign-dim-undef-op-data-uaf branch from 4bae794 to 8e9fe07 Compare November 24, 2026 22:28
@iluuu1994

iluuu1994 commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

See #20018. I don't think this should be fixed (especially not on stable branches).

@jorgsowa

Copy link
Copy Markdown
Contributor Author

If so, I may skip failing test assign_dim_undef_op_data_008.phpt in nightly. I will try to do it tomorrow.

@iluuu1994

Copy link
Copy Markdown
Member

@jorgsowa I can do that right now. I was just checking whether any other tests also fail, but the rest seem to be ok.

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.

2 participants