Details
- As
alice, run the PHPT test with code coverage (when Xdebug is used, prefix the command withXDEBUG_MODE=coverage):
cd ~/project
vendor/bin/phpunit --coverage-text tests/greeter.phpt
PHPUnit reports OK (1 test, 1 assertion), and the code coverage report shows that Greeter::greet() is covered. Nothing in the output indicates that the code of the test was replaced.
- Check which user executed the code that
malloryput in place:
cat /tmp/phpunit-phpt/proof.txt
The file is owned by alice and contains the output of id for alice, for instance:
uid=1000(alice) gid=1000(alice) groups=1000(alice)
Impact
This is a local privilege escalation (CWE-379: Creation of Temporary File in Directory with Incorrect Permissions). A local user can execute arbitrary code with the privileges of another user who runs PHPT tests with code coverage. This gives the attacker access to everything the developer can access, for instance source code, credentials, and SSH keys.
Affected are users of PHPUnit 13.4.0 to 13.4.3 when all of the following apply:
- PHPT tests with code coverage: PHPT tests are run while code coverage is collected, which requires a code coverage driver such as PCOV or Xdebug
- Shared path for temporary files: The path used for temporary files (
sys_get_temp_dir()) is shared between users. This is the case on Linux and other Unix-like systems with a shared/tmp, unless per-user temporary directories are used, for instance through systemd'sPrivateTmp,pam_namespace, or a per-userTMPDIR. On macOS and Windows, the path used for temporary files is specific to the user by default - Local account: The attacker has an account on the same system, or controls a process that runs under another account, for instance a service
- First to create the directory: The attacker creates the directory before the developer runs PHPT tests with code coverage for the first time since the path used for temporary files was last cleaned
Systems on which no other user, including system accounts that run services, can create files in the path used for temporary files are not affected. This is typically the case for a container or an ephemeral CI runner that is used by a single user.