Skip to content
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

php-fpm: zend_mm_heap corrupted with cgi-fcgi request #15395

Closed
verfriemelt-dot-org opened this issue Aug 13, 2024 · 7 comments
Closed

php-fpm: zend_mm_heap corrupted with cgi-fcgi request #15395

verfriemelt-dot-org opened this issue Aug 13, 2024 · 7 comments

Comments

@verfriemelt-dot-org
Copy link

Description

after making a request with a HTTP_AUTHORIZATION header, a subsequent cgi-fcgi -bind -connect 127.0.0.1:9000 will fail and cause heap corruption + SIGABRT on the child process.

poc:

$ docker run --rm -it --entrypoint bash php:fpm
# apt-get update && apt-get install tzdata locales libfcgi0ldbl -y
# php-fpm &
[1] 706
[13-Aug-2024 21:41:57] NOTICE: fpm is running, pid 706
[13-Aug-2024 21:41:57] NOTICE: ready to handle connections

# HTTP_AUTHORIZATION="Basic Zm9vOg==" SCRIPT_NAME=/ SCRIPT_FILENAME=/ REQUEST_METHOD=GET cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 - foo 13/Aug/2024:21:42:21 +0000 "GET /" 404
Status: 404 Not Found
X-Powered-By: PHP/8.3.10
Content-type: text/html; charset=UTF-8

File not found.

with that, the setup is complete and you can now trigger the bug with this running in the container:

# cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 -  13/Aug/2024:21:42:39 +0000 "- " 200
X-Powered-By: PHP/8.3.10
Content-type: text/html; charset=UTF-8

# cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 - 0 B* 13/Aug/2024:21:42:40 +0000 "- " 200
zend_mm_heap corrupted
[13-Aug-2024 21:42:40] WARNING: [pool www] child 707 exited on signal 6 (SIGABRT) after 42.713030 seconds from start
[13-Aug-2024 21:42:40] NOTICE: [pool www] child 712 started

you might need to retry to hit the correct child which served the previous request.

this seems to affect at least all php 8 versions starting with 8.0.0
i did not check older version though.

root@abbd54a452e3:/var/www/html# [13-Aug-2024 21:44:47] NOTICE: fpm is running, pid 876
[13-Aug-2024 21:44:47] NOTICE: ready to handle connections
HTTP_AUTHORIZATION="Basic Zm9vOg==" SCRIPT_NAME=/ SCRIPT_FILENAME=/ REQUEST_METHOD=GET cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 - foo 13/Aug/2024:21:44:50 +0000 "GET /" 404
Primary script unknownStatus: 404 Not Found
X-Powered-By: PHP/8.0.0
Content-type: text/html; charset=UTF-8

File not found.
root@abbd54a452e3:/var/www/html# cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 -  13/Aug/2024:21:44:54 +0000 "- " 200
X-Powered-By: PHP/8.0.0
Content-type: text/html; charset=UTF-8

root@abbd54a452e3:/var/www/html# cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 - 0��& 13/Aug/2024:21:44:55 +0000 "- " 200
zend_mm_heap corrupted
[13-Aug-2024 21:44:55] WARNING: [pool www] child 877 exited with code 1 after 8.593094 seconds from start
root@abbd54a452e3:/var/www/html# [13-Aug-2024 21:44:55] NOTICE: [pool www] child 882 started

we use the connect to the port 9000 as a liveness probe for kubernetes like this:

livenessProbe:
    initialDelaySeconds: 10
    exec:
        command:
            - /usr/bin/cgi-fcgi -bind -connect 127.0.0.1:9000

and we noticed a service with that setup failing due to the command returning a non-zero exit code and the thus the pod being restarted while the service receives requests with HTTP_AUTH headers.
the bug can not be triggered without the HTTP_AUTHORIZATION request. and it looks like memory contents might get dumped into the log too 🤔

PHP Version

PHP 8.3.10

Operating System

debian

@verfriemelt-dot-org
Copy link
Author

oldest version i could test was 7.1.33 and the bug is present there as well.

@devnexen
Copy link
Member

So it seems, it is the auth password part which triggers the fault, with ASAN we can see it crashes on request shutdown, root cause seems to be the auth data handling.

devnexen added a commit to devnexen/php-src that referenced this issue Aug 14, 2024
But we still consider the authentication handling Basic part successful.
@verfriemelt-dot-org
Copy link
Author

just to be clear, this example is just with an user, because that was enough.
the same fault will happen, if you provider user and password!

@devnexen
Copy link
Member

I cannot reproduce with a password, do you have an example where it crashes still ?

@verfriemelt-dot-org
Copy link
Author

verfriemelt-dot-org commented Aug 14, 2024

so within our k8s setup it definitley crashed with a password provided.

using foo:bar as credentials, you are right, the poc will not trigger the crash:

root@7bf33716acfb:/var/www/html# HTTP_AUTHORIZATION="Basic Zm9vOmJhcgo=" SCRIPT_NAME=/ SCRIPT_FILENAME=/ REQUEST_METHOD=GET cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 - foo 14/Aug/2024:20:05:07 +0000 "GET /" 404
Status: 404 Not Found
X-Powered-By: PHP/8.3.10
Content-type: text/html; charset=UTF-8

File not found.
root@7bf33716acfb:/var/www/html#
root@7bf33716acfb:/var/www/html# cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 - 0 �y 14/Aug/2024:20:05:11 +0000 "- " 200
X-Powered-By: PHP/8.3.10
Content-type: text/html; charset=UTF-8

but still dump out a weird user into the log 🤔 notice the 0 �y

@verfriemelt-dot-org
Copy link
Author

verfriemelt-dot-org commented Aug 14, 2024

here is an example which triggered the heap corruption:

root@edfc0d8d4ee2:/var/www/html# php-fpm &
[1] 706
root@edfc0d8d4ee2:/var/www/html# [14-Aug-2024 20:09:03] NOTICE: fpm is running, pid 706
[14-Aug-2024 20:09:03] NOTICE: ready to handle connections
root@edfc0d8d4ee2:/var/www/html# HTTP_AUTHORIZATION="Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" SCRIPT_NAME=/ SCRIPT_FILENAME=/ REQUEST_METHOD=GET cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 - Aladdin 14/Aug/2024:20:09:07 +0000 "GET /" 404
Status: 404 Not Found
X-Powered-By: PHP/8.3.10
Content-type: text/html; charset=UTF-8

File not found.
root@edfc0d8d4ee2:/var/www/html# cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 -  14/Aug/2024:20:09:14 +0000 "- " 200
X-Powered-By: PHP/8.3.10
Content-type: text/html; charset=UTF-8

root@edfc0d8d4ee2:/var/www/html# cgi-fcgi -bind -connect 127.0.0.1:9000
127.0.0.1 - P��# 14/Aug/2024:20:09:17 +0000 "- " 200
zend_mm_heap corrupted
[14-Aug-2024 20:09:17] WARNING: [pool www] child 707 exited on signal 6 (SIGABRT) after 14.328219 seconds from start
root@edfc0d8d4ee2:/var/www/html# [14-Aug-2024 20:09:17] NOTICE: [pool www] child 712 started

@devnexen
Copy link
Member

devnexen commented Aug 14, 2024

I could reproduce the dangling display in my side. I use your example, only I build my own php inside the container.

devnexen added a commit to devnexen/php-src that referenced this issue Aug 18, 2024
But we still consider the authentication handling Basic part successful.
devnexen added a commit to devnexen/php-src that referenced this issue Aug 23, 2024
But we still consider the authentication handling Basic part successful.
bukka added a commit to bukka/php-src that referenced this issue Oct 6, 2024
Co-authored-by: David Carlier <devnexen@gmail.com>
Closes phpGH-16227
@bukka bukka closed this as completed in 5a47f27 Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@verfriemelt-dot-org @cmb69 @devnexen and others