Skip to content

Prevent potential buffer overflow for large value of php_cli_server_workers_max #9000

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

yiyuaner
Copy link
Contributor

Fixes issue 8989.

@cmb69
Copy link
Member

cmb69 commented Jul 13, 2022

Thank you for the PR! I don't like the silent fall back to 1 worker, though. Can't we use safe_emalloc() instead of calloc() (fails hard if unsuccessful), and clear the memory afterwards?

@devnexen
Copy link
Member

I agree here in the sense it s not correct to hide it, you can possibly catch out of range from the value before allocation (like nginx does to catch silly values for its worker processes) or as @cmb69 said, either way need clarity from user's perspective.

@yiyuaner
Copy link
Contributor Author

If I understand it correctly, replacing calloc with safe_emalloc can resolve this. When integer overflow happens, an error message is printed and the program aborts.

php_cli_server_workers = calloc(
php_cli_server_workers_max, sizeof(pid_t));
php_cli_server_workers = safe_emalloc(
php_cli_server_workers_max, sizeof(pid_t), 0);
Copy link
Member

@devnexen devnexen Jul 14, 2022

Choose a reason for hiding this comment

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

Two keys here:
1/ > and clear the memory afterwards.
since you re moving from calloc.
2/ You might need a change for free(php_cli_server_workers).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I have replaced it with efree.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed. Don t forget the first point :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ecalloc is a safe version of calloc that checks for multiply overflow. I think it can be used here.

Copy link
Member

Choose a reason for hiding this comment

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

Alright, @cmb69 will have a last look in case I missed something. Nice work.

@yiyuaner yiyuaner force-pushed the master branch 2 times, most recently from 2822fb3 to 8cb3735 Compare July 14, 2022 05:14
@@ -2421,7 +2421,7 @@ static void php_cli_server_startup_workers(void) {
if (php_cli_server_workers_max > 1) {
zend_long php_cli_server_worker;

php_cli_server_workers = calloc(
php_cli_server_workers = ecalloc(
Copy link
Member

Choose a reason for hiding this comment

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

ecalloc() and friends allocate memory which is freed at the end of each request. That would cause issues here. Instead you'd need pecalloc() with the third argument being 1 (and use pefree() instead of efree() above). Note that the Zend memory allocation functions are infallible, i.e. they never return NULL (but instead terminate the process), so the following NULL check is superfluous.

Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

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

Thank you!

@devnexen devnexen closed this in 789a37f Jul 14, 2022
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