[php-src] Issue #25001: ext/session files handler: GC deletes a session file another request still holds locked

From: Date: Wed, 28 Oct 2026 14:47:46 +0000
Subject: [php-src] Issue #25001: ext/session files handler: GC deletes a session file another request still holds locked
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-253894@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/25001 Author: AsahiSoftWareEngineer ### Description The files session save handler's garbage collector (ps_files_cleanup_dir() in ext/session/mod_files.c) deletes any sess_* file whose mtime is older than gc_maxlifetime, without checking whether another request currently has that file open and locked via flock(LOCK_EX) -- a lock ps_files_open() holds for the entire lifetime of a session. On POSIX, unlink() on an open file succeeds silently: the directory entry disappears, but the holder's already-open file descriptor keeps working against the now-orphaned inode. session_write_close() therefore returns success while the session data is permanently lost, and neither the request that held it open nor the next one that reads the same session ID ever sees an error. ```c // ext/session/mod_files.c, ps_files_cleanup_dir() if ((now - sbuf.st_mtime) > maxlifetime) { VCWD_UNLINK(buf); // no lock check; return value also discarded nrdels++; } ``` This is reachable without any special timing: every ordinary session_start() call runs this sweep internally with probability session.gc_probability/session.gc_divisor (default 1/100), so it will eventually collide with a slow request's still-locked session file on any site under normal traffic. It's also reachable deterministically via the public session_gc() function, or even without calling it at all: session.gc_divisor is PHP_INI_ALL, so ini_set('session.gc_divisor', 1) before an ordinary session_start() makes the sweep that normally fires with 1/100 odds fire with certainty on that single request. ### Reproduction ```php <?php // victim.php -- simulates any slow request holding its session open // (a large upload, a slow payment callback, etc.) session_start(); sleep(10); $_SESSION['x'] = 'still here?'; var_dump(session_write_close()); // true -- no error ``` ```php <?php // attacker.php -- run concurrently; needs only its own, unrelated session ini_set('session.gc_maxlifetime', 0); session_id('attackerownsession0000000000000'); session_start(); var_dump(session_gc()); // deletes victim.php's session file mid-request ``` Run victim.php, and while it's sleeping, run attacker.php from a second process sharing the same session.save_path. session_gc() reports a deletion, victim.php's session_write_close() still returns true, and re-reading the same session ID afterward returns an empty session. ### Patch A candidate fix is up on my fork: https://github.com/AsahiSoftWareEngineer/php-src/tree/fix/session-gc-flock-race It probes each expired candidate with a non-blocking flock(LOCK_EX | LOCK_NB) before deleting it, skips files another process still holds open, and only counts a deletion once unlink() actually succeeds. A regression test (ext/session/tests/session_gc_active_session_race.phpt) is included. Happy to open this as a PR if that's useful. ### PHP Version Reproduced on PHP 8.5.12 and current master. Source comparison shows the same code (no lock check in ps_files_cleanup_dir()) on PHP-7.4 through PHP-8.6 as well.

« previous php.bugs (#253894) next »