Skip to main content
Menu
Advisories

Filesystem create_temporary_file() creates files non-exclusively and with default permissions, exposing their contents to other local users

GHSA-Q8CP-XQ93-R9JRPublished Updated GitHub

Description

Impact

Psl\Filesystem\create_temporary_file() chose a random name in the target directory, by default the system temporary directory, and created the file with Psl\Filesystem\create_file(), which calls PHP's touch(). This had two consequences:

  • Default permissions. The file was created with mode 0666 minus the process umask, which is 0644 under the common umask of 022. In a shared directory such as /tmp, which sys_get_temp_dir() returns on most Linux systems, any other local user could read whatever the application wrote to the file while it existed. The names are not secret, since anyone can list /tmp.
  • Non-exclusive creation. touch() does not fail when the path already exists, and it follows a symbolic link to create or update the link's target. A local user who had created a file or a symbolic link under the chosen name before the call made the application use a file the attacker controls: an attacker-owned, world-writable file receives the data the application writes, and can be changed before the application reads it back. The name holds 32 random bits in 6.0.0 to 6.3.0 (8 random alphanumeric characters, about 47 bits, in 1.5.0 to 5.5.0, published as azjezz/psl), so the attacker has to plant a large number of names and succeeds only with a probability proportional to that number. On Linux, the fs.protected_symlinks and fs.protected_regular protections that most distributions enable by default stop most of these attempts in sticky directories such as /tmp. The 32-bit name space also let two calls in different processes return the same file by accident.

Applications are affected when they run on a host shared with untrusted local users, such as shared hosting, multi-user servers or shared CI runners, call create_temporary_file() on a shared directory, and write data to the file that other users must not read or change. The attacker needs a local account on the same host. Single-tenant containers, private directories passed as $directory, and per-user temporary directories, such as the per-user TMPDIR that macOS sets by default, are not exposed. PSL does not call create_temporary_file() itself.

Patches

Fixed in 6.4.0.

  • The file is created with fopen() mode x (O_CREAT | O_EXCL), which fails if anything, a symbolic link included, already exists under the name, and a collision is retried with a new name, up to 10 attempts.
  • The random part of the name is 8 random bytes (64 bits) instead of 4.
  • The file is created under a temporary umask of 0077, and its mode is set to 0600 before the path is returned; if the mode cannot be set, the file is removed and a RuntimeException is thrown.

Workarounds

  • Pass a private directory as $directory: one owned by the application user with mode 0700. Other users can then neither read the files nor create names in it, which addresses both issues.
  • Setting umask(0o077) around the call makes new files 0600, but does not make the creation exclusive.
  • PHP's tempnam() creates a file exclusively, with mode 0600, and can be used instead.

References

Affected versions

References

GitHub · php-standard-library/php-standard-library