From b54271d729e4acaedf584254f19fcf675af1b642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brian=20Sch=C3=A4ffner?= Date: Mon, 16 Nov 2026 14:44:47 +0100 Subject: [PATCH 1/2] fix(cache): fall back to a private sibling when the temp root is taken The implicit temporary cache root /tmp/sympress-kernel-- is predictable. Another local user could create it first (as a directory or symlink), and the owner/symlink checks then made every boot fail: a local denial of service for projects that use the temporary fallback (webroot projects, read-only cold projects). Such a path is still never adopted. Instead the kernel reuses a randomly suffixed sibling owned by the PHP user with no group/world write bits, or atomically creates one with mkdir 0700, and logs the switch. Only this user can own such a directory and the sticky temporary parent prevents swapping it, so ownership is enough to find it again on later requests. --- docs/dependency-injection.md | 5 +- src/Kernel/CachePath.php | 33 ++++++++++++- .../Kernel/CacheRequestCompatibilityTest.php | 48 ++++++++++++++++--- 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/docs/dependency-injection.md b/docs/dependency-injection.md index e237355..4ec614e 100644 --- a/docs/dependency-injection.md +++ b/docs/dependency-injection.md @@ -1015,7 +1015,10 @@ path is shared by CLI and PHP-FPM even with systemd PrivateTmp. If the project i the webroot or a cold project cannot be written, the implicit cache uses a project-specific, owner-checked directory under the system temporary directory, outside DOCUMENT_ROOT/WP_CONTENT_DIR. Pre-created symlinks, other owners and -group/world-writable directories are rejected. Every cache directory, implicit or +group/world-writable directories are never adopted: because the name is predictable, +the kernel then reuses or atomically creates a randomly suffixed private sibling +(mode 0700, owned by the PHP user) and logs it, so another local user cannot block +the boot. Every cache directory, implicit or configured, must be owned by the PHP process user, and no parent up to the project (or up to `/` for a cache outside the project) may be group/world-writable unless it has the sticky bit; compiled containers and discovery manifests must be owned by the diff --git a/src/Kernel/CachePath.php b/src/Kernel/CachePath.php index f55f66b..63a5fa2 100644 --- a/src/Kernel/CachePath.php +++ b/src/Kernel/CachePath.php @@ -118,8 +118,8 @@ private static function privateFallback(string $project, string $environment): s if (self::replaceableByOthers(fileperms($temporary))) { throw new \RuntimeException('A private kernel cache requires a trusted temporary parent or APP_CACHE_DIR; shared temporary directories must have the sticky bit.'); } - $root = rtrim($temporary, '/') . '/sympress-kernel-' . $user . '-' - . substr(hash('sha256', self::canonical($project)), 0, 24); + $root = self::temporaryRoot(rtrim($temporary, '/') . '/sympress-kernel-' . $user . '-' + . substr(hash('sha256', self::canonical($project)), 0, 24), $owner); } if (self::isPublic($root)) { throw new \RuntimeException('No private kernel cache is available outside the webroot. Set APP_CACHE_DIR to a durable private directory and warm it as the PHP-FPM user.'); @@ -135,6 +135,35 @@ private static function privateFallback(string $project, string $environment): s return $root . '/' . $environment . '/kernel'; } + /** + * The temporary root name is predictable, so another local user can create it first. + * Never adopt such a path and never fail the boot over it: reuse or atomically create + * a randomly named sibling. Only this user can own one, and the sticky temporary + * parent keeps others from swapping it, so ownership is enough to find it again. + */ + private static function temporaryRoot(string $root, int $owner): string + { + if ((!file_exists($root) && !is_link($root)) || self::privateDirectory($root, $owner)) { + return $root; + } + foreach (glob($root . '.*', GLOB_NOESCAPE) ?: [] as $candidate) { + if (self::privateDirectory($candidate, $owner)) { + return $candidate; + } + } + $candidate = $root . '.' . bin2hex(random_bytes(8)); + if (!mkdir($candidate, 0700)) { + throw new \RuntimeException('Unable to create a private kernel cache next to ' . $root . '; configure APP_CACHE_DIR for the PHP-FPM identity.'); + } + error_log('SymPress found ' . $root . ' not owned privately by the PHP user and uses ' . $candidate . ' as its kernel cache instead. Configure APP_CACHE_DIR for a durable private cache.'); + return $candidate; + } + + private static function privateDirectory(string $path, int $owner): bool + { + return !is_link($path) && is_dir($path) && fileowner($path) === $owner && (fileperms($path) & 0022) === 0; + } + private static function isPublic(string $path): bool { $resolved = self::canonical($path); diff --git a/tests/Kernel/CacheRequestCompatibilityTest.php b/tests/Kernel/CacheRequestCompatibilityTest.php index 3c890c9..87eb04b 100644 --- a/tests/Kernel/CacheRequestCompatibilityTest.php +++ b/tests/Kernel/CacheRequestCompatibilityTest.php @@ -199,7 +199,7 @@ public function testReadOnlyColdProjectBootsWithPrivateFallback(): void } } - public function testTemporaryFallbackRejectsPrecreatedSymlink(): void + public function testTemporaryFallbackAvoidsPrecreatedSymlink(): void { $project = $this->tmpPath('public-symlink-cache'); $outside = $this->tmpPath('untrusted-fallback'); @@ -210,15 +210,49 @@ public function testTemporaryFallbackRejectsPrecreatedSymlink(): void $root = dirname(CachePath::resolve($project, 'test'), 2); rmdir($root); symlink($outside, $root); - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('symlinked'); - CachePath::resolve($project, 'test'); + $this->assertPrivateSiblingOf($root, CachePath::resolve($project, 'test')); + self::assertSame([], glob($outside . '/*')); } finally { unset($_SERVER['DOCUMENT_ROOT']); - if ($root !== null && is_link($root)) { - unlink($root); - } + $this->removeTemporaryRoots($root); + } + } + + public function testTemporaryFallbackAvoidsPrecreatedReplaceableDirectory(): void + { + $project = $this->tmpPath('public-replaceable-cache'); + $_SERVER['DOCUMENT_ROOT'] = $project; + $root = null; + try { + $root = dirname(CachePath::resolve($project, 'test'), 2); + // Stands in for a directory another local user created first. + chmod($root, 0777); + $cache = CachePath::resolve($project, 'test'); + $this->assertPrivateSiblingOf($root, $cache); + self::assertSame($cache, CachePath::resolve($project, 'test'), 'The private sibling is reused.'); + } finally { + unset($_SERVER['DOCUMENT_ROOT']); + $this->removeTemporaryRoots($root); + } + } + + private function assertPrivateSiblingOf(string $root, string $cache): void + { + $sibling = dirname($cache, 2); + self::assertMatchesRegularExpression('/^' . preg_quote($root, '/') . '\\.[0-9a-f]{16}$/', $sibling); + self::assertTrue(CachePath::ownedByProcess($sibling)); + self::assertSame(0700, fileperms($sibling) & 0777); + } + + private function removeTemporaryRoots(?string $root): void + { + if ($root === null) { + return; + } + if (is_link($root)) { + unlink($root); } + (new Filesystem())->remove([$root, ...(glob($root . '.*') ?: [])]); } public function testPrivateFallbackVerifiesProcessOwnerWithoutPosixExtension(): void From 5cdc1d7452cc61d6e723b8d171b80ea882cb6419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brian=20Sch=C3=A4ffner?= Date: Mon, 16 Nov 2026 14:44:53 +0100 Subject: [PATCH 2/2] chore(release): prepare kernel v1.1.17 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83d92e6..ec9d354 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html) where applicable. +## 1.1.17 — 2026-11-16 + +- Stop failing the boot when another local user pre-creates the predictable temporary cache root (`/sympress-kernel--`) as a directory or symlink. The kernel still never adopts such a path; it reuses or atomically creates a randomly suffixed private sibling (mode 0700, owned by the PHP user) and logs the switch. + ## 1.1.16 — 2026-11-16 - Keep compiled-container metadata per bundle set in `meta_.json` instead of one shared `meta.json`. On multisite, sites with different SymPress plugins or themes no longer overwrite each other's metadata, so requests alternating between them stop recompiling the container under the exclusive lock. Cleanup keeps every generation that metadata still references and caps metadata at 32 bundle sets. The legacy `meta.json` is ignored and removed; the first request after the upgrade compiles once per bundle set.