Skip to content

Fix GH-18033: NULL-ptr dereference when using register_tick_function in destructor #18047

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

Closed
wants to merge 1 commit into from

Conversation

nielsdos
Copy link
Member

The problem is that php_request_shutdown calls php_deactivate_ticks prior to running destructors and the shutdown functions and finalizing output handlers.
So if a destructor or shutdown function re-registers a tick function, then the user tick functions handler will be added back to PG(tick_functions). When the next request happens, the list PG(tick_functions) still contains an entry to call the user tick functions (added in the previous request during shutdown). This causes a NULL deref eventually because run_user_tick_functions assumes that if it is called then BG(user_tick_functions) must be non-NULL.

Fix this by moving the tick handler deactivation.

@nielsdos nielsdos linked an issue Mar 13, 2025 that may be closed by this pull request
…on in destructor

The problem is that `php_request_shutdown` calls `php_deactivate_ticks` prior
to running destructors and the shutdown functions and finalizing output
handlers.
So if a destructor or shutdown function re-registers a tick function,
then the user tick functions handler will be added back to `PG(tick_functions)`.
When the next request happens, the list `PG(tick_functions)` still contains an
entry to call the user tick functions (added in the previous request
during shutdown). This causes a NULL deref eventually because
`run_user_tick_functions` assumes that if it is called then
`BG(user_tick_functions)` must be non-NULL.

Fix this by moving the tick handler deactivation.
@@ -1926,6 +1924,8 @@ void php_request_shutdown(void *dummy)
php_output_end_all();
} zend_end_try();

php_deactivate_ticks();
Copy link
Member

Choose a reason for hiding this comment

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

I think the same issue can occur with buffered output handlers. Does this need to be moved down further?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is already below php_output_end_all and I don't think php_output_deactivate can call user code?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, you're probably right! Can't verify as I'm ok mobile.

Copy link
Member

Choose a reason for hiding this comment

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

I think it doesn't call user code, at least not on purpose. Destructors are already called, so destroying the user output handler will not call any user code. The only way for php_output_deactivate() to execute user code would be if it invoked an error handler. But this would be unsafe after zend_deactivate_modules(), so it probably doesn't.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed. There's even a test. I just didn't look at it well enough.

@nielsdos nielsdos requested a review from arnaud-lb March 13, 2025 19:48
@nielsdos nielsdos marked this pull request as ready for review March 13, 2025 19:48
@nielsdos nielsdos requested a review from bukka as a code owner March 13, 2025 19:48
@nielsdos nielsdos removed the request for review from bukka March 13, 2025 19:48
Copy link
Member

@arnaud-lb arnaud-lb left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@@ -1926,6 +1924,8 @@ void php_request_shutdown(void *dummy)
php_output_end_all();
} zend_end_try();

php_deactivate_ticks();
Copy link
Member

Choose a reason for hiding this comment

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

I think it doesn't call user code, at least not on purpose. Destructors are already called, so destroying the user output handler will not call any user code. The only way for php_output_deactivate() to execute user code would be if it invoked an error handler. But this would be unsafe after zend_deactivate_modules(), so it probably doesn't.

@nielsdos nielsdos closed this in 867ed15 Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NULL-ptr dereference when using register_tick_function in destructor
3 participants