[php-src] PHP-8.6: ext/session: don't follow symlinked subdirectories in files GC (GH-25671)

From: Date: Wed, 25 Nov 2026 09:14:25 +0000
Subject: [php-src] PHP-8.6: ext/session: don't follow symlinked subdirectories in files GC (GH-25671)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144151@lists.php.net to get a copy of this message
Author: Jorg Sowa (jorgsowa) Date: 2026-11-25T10:12:36+01:00 Commit: https://github.com/php/php-src/commit/d09481d694643ce2469d11b55373c298e468f0dd Raw diff: https://github.com/php/php-src/commit/d09481d694643ce2469d11b55373c298e468f0dd.diff ext/session: don't follow symlinked subdirectories in files GC (GH-25671) With dirdepth > 0, GC used stat() to descend, so a symlink in save_path made it delete expired sess_* files in the directory it points to. Closes GH-25671 Changed paths: A ext/session/tests/mod_files/gc_dirdepth_symlink.phpt M NEWS M ext/session/mod_files.c Diff: diff --git a/NEWS b/NEWS index 7de98e000e1b..3a8b3fcd7dd8 100644 --- a/NEWS +++ b/NEWS @@ -203,6 +203,8 @@ PHP NEWS - Session: . Fixed session upload progress to respect session.use_only_cookies. (jorgsowa) + . Fixed files save handler GC following symlinked subdirectories when + session.save_path sets a directory depth. (Jorg Sowa) - SimpleXML: . Fixed a crash with opcache when simplexml_load_string(), diff --git a/ext/session/mod_files.c b/ext/session/mod_files.c index 15214c215d11..c569584a68d3 100644 --- a/ext/session/mod_files.c +++ b/ext/session/mod_files.c @@ -332,8 +332,8 @@ static int ps_files_cleanup_dir(const zend_string *dirname, zend_long maxlifetim nrdels++; } } else { - /* intermediate depth: recurse into subdirectories */ - if (VCWD_STAT(buf, &sbuf) != 0) { + /* intermediate depth: recurse into subdirectories, never through symlinks */ + if (VCWD_LSTAT(buf, &sbuf) != 0) { continue; } if (S_ISDIR(sbuf.st_mode)) { diff --git a/ext/session/tests/mod_files/gc_dirdepth_symlink.phpt b/ext/session/tests/mod_files/gc_dirdepth_symlink.phpt new file mode 100644 index 000000000000..532bc3b77963 --- /dev/null +++ b/ext/session/tests/mod_files/gc_dirdepth_symlink.phpt @@ -0,0 +1,45 @@ +--TEST-- +session GC with save_path dirdepth does not follow symlinked subdirectories +--EXTENSIONS-- +session +--SKIPIF-- +<?php +include(__DIR__ . '/../skipif.inc'); +$probe = __DIR__ . '/gc_dirdepth_symlink_probe'; +@mkdir($probe); +$ok = @symlink($probe, "$probe.lnk"); +@rmdir("$probe.lnk") || @unlink("$probe.lnk"); +@rmdir($probe); +if (!$ok) die('skip cannot create directory symlinks'); +?> +--INI-- +session.gc_probability=0 +session.gc_maxlifetime=10 +session.use_strict_mode=0 +--FILE-- +<?php +$base = __DIR__ . '/gc_dirdepth_symlink'; +$outside = "$base-outside"; +@mkdir("$base/b", 0777, true); +@mkdir($outside); +symlink($outside, "$base/a"); +touch("$outside/sess_a", time() - 2000); + +session_save_path("1;$base"); +session_id('b0'); +session_start(); +var_dump(session_gc(), file_exists("$outside/sess_a")); +?> +--CLEAN-- +<?php +$base = __DIR__ . '/gc_dirdepth_symlink'; +@unlink("$base/b/sess_b0"); +@rmdir("$base/a") || @unlink("$base/a"); +@rmdir("$base/b"); +@rmdir($base); +@unlink("$base-outside/sess_a"); +@rmdir("$base-outside"); +?> +--EXPECT-- +int(0) +bool(true)

« previous php.cvs (#144151) next »