Commit d09481d6946 for php

commit d09481d694643ce2469d11b55373c298e468f0dd
Author: Jorg Sowa <jorg.sowa@gmail.com>
Date:   Wed Nov 25 10:12:36 2026 +0100

    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

diff --git a/NEWS b/NEWS
index 7de98e000e1..3a8b3fcd7dd 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 15214c215d1..c569584a68d 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 00000000000..532bc3b7796
--- /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)