Repository navigation
Keep what a root PHP CLI writes usable by php-fpm - #658
Merged
Merged
Conversation
php-fpm runs as the site's user, but a docker/kubectl exec runs as root, and what PHP created there came out root-owned: the quanta_db lock dir and lock files, the metrics arena, new node dirs and documents, the trashbin dirs, and everything doctor writes. php-fpm then got EACCES on every write that needed one of them, until someone chowned them back. doctor started as root now switches to the owner of the site dir before it boots, in the same process. QUANTA_DOCTOR_AS_ROOT=1 keeps root; the entrypoint and quanta-build-assets set it, as they fix ownership themselves. The extension creates its files with the filesystem identity of the owner of the directory they go in (setfsuid/setfsgid), not with a chown afterwards, which a symlink swapped in between could redirect. A lock file that cannot be opened for writing is opened read-only: flock works the same on it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
php-fpm runs as the site's user (
www-data). A command started withdocker exec/kubectl execruns as root unless the caller rememberssu, and what PHP creates in that process comes outroot:root0755/0644. php-fpm then can't use it:quanta_dbtakes a per-nodeflockon<base>/locks/<node>.lock, which it opens withcreate(true).write(true). If a root process is the first to create thelocksdir or a lock file, php-fpm can neither create a new lock file in that dir nor open an existing one for writing. Every request that writes such a node then fails withQuantaDbException: Permission denied (os error 13). The client gets an empty 500, and nothing is written to the app log.doctoritself writes the class map, caches and node documents as root.The entrypoint's
chownonly covers what exists at boot. Whatever a later rootexeccreates stays root-owned until someone chowns it by hand.Change
doctordrops root. Started as root, it switches to the owner ofsites/<host>beforeboot.phpruns (gid, then supplementary groups, then uid). It does this in the same process, so the arguments,-doptions, output and exit status don't change. It keeps root when the site dir is root-owned (an image build) or whenQUANTA_DOCTOR_AS_ROOT=1is set. If the switch fails, it exits 1 with a message instead of carrying on as root.The entrypoint and
quanta-build-assetskeep root (QUANTA_DOCTOR_AS_ROOT=1). They run doctor on dirs they have just created as root, and they fix ownership afterwards, as before.The extension creates files as the owner of the target dir. In the new
qdb/src/own.rs, while a root process creates an entry, its filesystem uid/gid is set to the owner of the directory the entry goes in. This usessetfsuid/setfsgid, which act per thread; the real and effective ids stay root. It applies to:Opening, renaming and removing existing entries keep root's rights. Nothing changes unless euid is 0.
This is deliberately not a
chownafterwards. These dirs are writable by the site user, so a root chown by path could be redirected to any file on the system by swapping in a symlink between the create and the chown. A create done as the site user can only reach what that user can reach anyway.Read-only fallback for lock files. If opening a lock file for writing is refused (EACCES), it is opened read-only instead.
flock(2)takes the same exclusive lock through a read-only descriptor. This covers root-owned lock files left by an earlier build.Docs: the image section of the README, and a new "Running PHP as root" section in
qdb/docs/usage.md.Other things that can be created as root were checked and are not affected:
qdbdwrites them, and supervisord starts it as the site user.Behaviour change
php doctorstarted as root now runs as the owner of the site dir. A caller that needs doctor to write somewhere only root may write must setQUANTA_DOCTOR_AS_ROOT=1and fix ownership itself.This includes
/docker-entrypoint.d/hooks. They run before the entrypoint's ownership pass, so a hook that runs doctor on dirs this boot created as root needs the variable too.Not covered
php -r …, scripts) still writes its own files (caches, the class map, …) as root. Onlydoctordrops privileges, and only the extension's own files are created as the dir owner./tmp/quanta_dbon every start.Testing
New conformance test
qdb/tests/php/16_root_cli.php. It needs root and runs in fallback mode only; in daemon mode the harness would start qdbd as root, which production doesn't do. Root creates, rewrites and deletes nodes in a tree owned by an unprivileged user. The trashbin is on another filesystem (/dev/shm), so the delete exercises the copy path. Then a separate process running as that user (workers/app_user_writes.php):Against the extension in the published
sha-02919b7dimage, 8 of its 12 assertions fail. With this change all 12 pass.New parity test
qdb/tests/quanta/10_doctor_root.php, run in all three modes. A rootdoctor class_mapon a site owned bywww-datawrites the class map and the doctor recipe aswww-data. Doctor's output and exit status come through, and in the extension modes so do its-doptions.QUANTA_DOCTOR_AS_ROOT=1and a root-owned site both keep root. Against master'sdoctor, 2 of its 10 assertions fail.Full suites on the rebuilt image with
sh qdb/tests/run-in-docker.sh: conformance in fallback and daemon modes, parity in noext, fallback and daemon modes, plusnginx -tandphp-fpm -t. All pass.cargo test: 168 passed.End to end in the image, with supervisord running and qdbd as
www-data:docker exec, ranphp doctor localhost checkandphp -rwrites (create, rewrite, delete) through the extension. Afterwards,find /tmp/quanta_db sites/localhost static ! -user www-datareturns nothing.root:root0644 by hand. Awww-dataprocess rewrote the node root created, created a child in it, created a new node, wrote behind the root-owned lock file and deleted into the trashbin; all succeeded.GET /returns 200.