aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuel Vadot <manu@FreeBSD.org>2026-11-19 07:58:00 +0000
committerMark Johnston <markj@FreeBSD.org>2026-12-01 19:04:20 +0000
commitabaa5a465964f14c13634470c6bc6f1da5bfdf80 (patch)
tree48c31d746fa365431a910513f8371d3d57cda52c
parentcd3517d5c9576f5d5812d585767f2f3413abf112 (diff)
pam_xdg: Do not follow symbolic links when removing the runtime directory
When the last session of a user ends, the runtime directory is removed by root. The user owns the directory and can still have processes (always without the linger option), so its content can change while it is removed. remove_dir() took the type of each entry from readdir() and opened the directories with openat(fd, name, 0). An entry replaced by a symbolic link between the two was followed and the content of its target was removed as root. Open every entry relative to its parent with O_DIRECTORY | O_NOFOLLOW and unlink what is not a directory at that moment, never what it points to. Also stay on the filesystem of the runtime directory, do not enter a directory that belongs to neither the user nor root, and limit the depth of the recursion. remove_dir() now always consumes the descriptor it is given, the callers closed it a second time. Looking for the last session file also opened a user-controlled entry as root: it followed a symbolic link and waited forever for the writer of a FIFO. Open it with O_NOFOLLOW | O_NONBLOCK. Document in pam_xdg(8) when the directory is removed and what is left in place. Tested on amd64 by changing the tree while the last session was closing: the files of the target tree were removed with the old code and left with the new one. ssh (with pam_xdg in the session stack of sshd) and login still create and remove the directory, also with overlapping sessions. Approved by: so Security: FreeBSD-SA-26:97.pam_xdg Security: CVE-2026-127761 Reported by: Othmane Tamagart Fixes: 6e69612d5df1 ("pam: Add pam_xdg module")
-rw-r--r--lib/libpam/modules/pam_xdg/pam_xdg.814
-rw-r--r--lib/libpam/modules/pam_xdg/pam_xdg.c68
2 files changed, 64 insertions, 18 deletions
diff --git a/lib/libpam/modules/pam_xdg/pam_xdg.8 b/lib/libpam/modules/pam_xdg/pam_xdg.8
index 9b335751a9fb..b55b13f68671 100644
--- a/lib/libpam/modules/pam_xdg/pam_xdg.8
+++ b/lib/libpam/modules/pam_xdg/pam_xdg.8
@@ -22,7 +22,8 @@
.\" * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" * SUCH DAMAGE.
-.Dd May 20, 2025
+.\"
+.Dd November 19, 2026
.Dt PAM_XDG 8
.Os
.Sh NAME
@@ -41,7 +42,16 @@ according to the Cross Desktop Group Base Directory Specification.
By default the directory is created under
.Pa /var/run/xdg/ Ns $ Ns Ev USER .
.Pp
-The following option may be passed to the authentication module:
+When the last session of the user is closed, the directory is removed
+together with its contents.
+Symbolic links in it are removed, never followed.
+A directory that is on another file system, that belongs to neither the
+user nor root, or that is nested more than 64 levels deep is not
+entered: it is left in place, and so is the runtime directory.
+.Pp
+The following options may be passed to the
+.Nm
+module:
.Bl -tag -width "runtime_dir_prefix=directory"
.It Cm runtime_dir_prefix Ns = Ns Ar directory
Use an alternate base directory
diff --git a/lib/libpam/modules/pam_xdg/pam_xdg.c b/lib/libpam/modules/pam_xdg/pam_xdg.c
index 10c6467776a3..eab14bdb3c19 100644
--- a/lib/libpam/modules/pam_xdg/pam_xdg.c
+++ b/lib/libpam/modules/pam_xdg/pam_xdg.c
@@ -175,34 +175,61 @@ out:
return (rv);
}
+#define XDG_MAX_DEPTH 64 /* Arbitrary limit, we recurse */
+
+/*
+ * Remove the content of the directory open as fd, always consuming fd.
+ *
+ * This runs as root on a tree the user owns and can change while we work
+ * (the user can still have processes), so never follow a symbolic link: each
+ * entry is opened relative to its parent with O_NOFOLLOW and its type is not
+ * taken from the directory entry, which can be stale by the time we use it.
+ * What is not a directory when it is opened, including a symbolic link that
+ * replaced one, is unlinked itself. Do not leave the filesystem of the
+ * runtime directory, do not enter a directory that belongs to neither the
+ * user nor root (something moved in from elsewhere) and do not recurse
+ * without a limit.
+ */
static int
-remove_dir(int fd)
+remove_dir(int fd, dev_t dev, uid_t uid, int depth)
{
DIR *dirp;
struct dirent *dp;
+ struct stat sb;
+ int cfd, error, pfd;
+ if (depth > XDG_MAX_DEPTH || fstat(fd, &sb) == -1 ||
+ sb.st_dev != dev || (sb.st_uid != uid && sb.st_uid != 0)) {
+ close(fd);
+ return (-1);
+ }
dirp = fdopendir(fd);
- if (dirp == NULL)
+ if (dirp == NULL) {
+ close(fd);
return (-1);
+ }
+ pfd = dirfd(dirp);
+ error = 0;
while ((dp = readdir(dirp)) != NULL) {
- if (dp->d_type == DT_DIR) {
- int dirfd;
-
- if (strcmp(dp->d_name, ".") == 0 ||
- strcmp(dp->d_name, "..") == 0)
- continue;
- dirfd = openat(fd, dp->d_name, 0);
- remove_dir(dirfd);
- close(dirfd);
- unlinkat(fd, dp->d_name, AT_REMOVEDIR);
+ if (strcmp(dp->d_name, ".") == 0 ||
+ strcmp(dp->d_name, "..") == 0)
+ continue;
+ cfd = openat(pfd, dp->d_name,
+ O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
+ if (cfd >= 0) {
+ if (remove_dir(cfd, dev, uid, depth + 1) == -1)
+ error = -1;
+ if (unlinkat(pfd, dp->d_name, AT_REMOVEDIR) == -1)
+ error = -1;
continue;
}
- unlinkat(fd, dp->d_name, 0);
+ if (unlinkat(pfd, dp->d_name, 0) == -1)
+ error = -1;
}
closedir(dirp);
- return (0);
+ return (error);
}
static int
@@ -214,6 +241,7 @@ _pam_xdg_close(pam_handle_t *pamh __unused, int flags __unused,
const char *runtime_dir_prefix;
struct stat sb;
char *xdg_session_file;
+ dev_t dev;
int rv, rt_dir_prefix, rt_dir, session_file, i;
rt_dir = -1;
@@ -276,7 +304,12 @@ _pam_xdg_close(pam_handle_t *pamh __unused, int flags __unused,
goto out;
}
rv = 0;
- session_file = openat(rt_dir_prefix, xdg_session_file, 0);
+ /*
+ * The user owns the directory: do not follow a symbolic link
+ * and do not wait for the writer of a FIFO.
+ */
+ session_file = openat(rt_dir_prefix, xdg_session_file,
+ O_RDONLY | O_NOFOLLOW | O_NONBLOCK);
if (session_file >= 0) {
unlinkat(rt_dir_prefix, xdg_session_file, 0);
free(xdg_session_file);
@@ -293,7 +326,10 @@ _pam_xdg_close(pam_handle_t *pamh __unused, int flags __unused,
/* Final cleanup if last user session */
if (i == 0) {
- remove_dir(rt_dir);
+ /* remove_dir() consumes the descriptor */
+ dev = fstat(rt_dir, &sb) == 0 ? sb.st_dev : (dev_t)-1;
+ remove_dir(rt_dir, dev, passwd->pw_uid, 0);
+ rt_dir = -1;
if (unlinkat(rt_dir_prefix, user, AT_REMOVEDIR) != 0) {
PAM_VERBOSE_ERROR("Can't cleanup %s/%s\n", runtime_dir_prefix, user);
rv = PAM_SESSION_ERR;