December 3, 2026
CVE-2026β127761: How a Logout Could Delete Any File on FreeBSD
A symlink race in pam_xdg let any local user make root delete arbitrary directories. It sat in the default login configuration of everyβ¦

By OhTea
7 min read
A symlink race in pam_xdg let any local user make root delete arbitrary directories. It sat in the default login configuration of every supported FreeBSD release for two and a half years.
On 2 December 2026 the FreeBSD Project published FreeBSD-SA-26:97.pam_xdg, "Arbitrary file deletion via pam_xdg(8) symlink race", and assigned it CVE-2026β127761. I reported this bug on 18 November and I'm credited on the advisory.
This post explains what the bug is, why it was easy to exploit despite being a race, how I found it, and what the fix looks like.
TL;DR
- When your last login session ends, FreeBSD's
pam_xdgmodule deletes your runtime directory,/var/run/xdg/<you>. - It does that as root, walking a directory you own.
- It decided what was a subdirectory from one system call, then opened it by name with another, and it followed symbolic links.
- Swap a subdirectory for a symlink in between, and root walks into wherever the link points and deletes everything there.
No memory corruption, no special privileges, no unusual configuration. A normal account on a default install was enough.
Background: what pam_xdg does
The XDG Base Directory Specification says each logged-in user should have a private runtime directory, exposed as XDG_RUNTIME_DIR. Desktop software uses it for sockets and other short-lived files.
FreeBSD implements this with a PAM session module, pam_xdg(8), added in February 2024. On login it creates /var/run/xdg/<user>, hands ownership to the user, and drops a marker file xdg_session.N inside for each open session. On logout it removes that session's marker, and if it was the last session, it deletes the whole directory.
The module is enabled in /etc/pam.d/system, which the login service includes. So it runs for console logins and for anything that calls login(1).
The bug
Here is the cleanup function as it shipped:
static int
remove_dir(int fd)
{
DIR *dirp;
struct dirent *dp;
dirp = fdopendir(fd);
if (dirp == NULL)
return (-1);
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);
continue;
}
unlinkat(fd, dp->d_name, 0);
}
closedir(dirp);
return (0);
}static int
remove_dir(int fd)
{
DIR *dirp;
struct dirent *dp;
dirp = fdopendir(fd);
if (dirp == NULL)
return (-1);
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);
continue;
}
unlinkat(fd, dp->d_name, 0);
}
closedir(dirp);
return (0);
}Three details combine into a vulnerability.
The type check is stale. readdir() doesn't ask the kernel about each entry as you go. It fetches a whole block of directory entries at once and hands them out from a buffer. dp->d_type == DT_DIR tells you what the entry was when that block was read, not what it is now.
The open follows symlinks. openat(fd, dp->d_name, 0) has no O_NOFOLLOW and no O_DIRECTORY. If the name is now a symbolic link, the kernel resolves it and returns a descriptor for the target.
Root is walking the user's tree. The process doing this is the privileged parent of the login session, with effective uid 0. The directory belongs to the user, mode 0700. The user can rename, delete and create entries in it at any moment, including while root is in the middle of the walk.
This is a classic time-of-check to time-of-use bug: check the type, then act on the name, with an attacker able to change what the name means in between.
"It's a race" doesn't mean "it's hard"
Races have a reputation for being unreliable. This one isn't, because the attacker controls how wide the window is.
Making root slow. Fill the runtime directory with a few dozen subdirectories, each containing a few thousand empty files. When cleanup starts, root reads one block of entries, sees them all marked as directories, and then has to unlink thousands of files in the first one before it touches the second. That takes long enough to be comfortably beaten by another process.
Knowing when to move. On ZFS, a directory's st_size is its number of entries. A watcher process can stat() each subdirectory in a loop; the instant one of them shrinks, cleanup has begun.
The swap. The watcher renames every subdirectory root hasn't reached yet and creates a symlink with the original name, pointing at the victim directory. Root's buffered listing still says those names are directories. It opens each one, follows the link, and recursively deletes the target.
Choosing the moment. The module only cleans up when the closing session is the last one, which it decides by looking for xdg_session.0 in the user's directory. The user owns that directory, so they control that too.
Getting a session at all. pam_xdg isn't in the sshd policy, so an SSH login alone doesn't trigger it. But /usr/bin/login is setuid root and world-executable, and the login policy includes pam_self, which lets you log in as yourself without a password. Any user with a shell can start and end a login session on demand.
Put together: log in to yourself, populate the directory, start a watcher in the background, log out. In my testing it worked on the first attempt, every time I set it up correctly.
Demonstration
On a fully patched FreeBSD 15.1-RELEASE-p7 virtual machine, I had root create a small tree inside /root:
/root/target:
drwxr-xr-x 2 root wheel 3 Nov 18 20:36 directory
/root/target/directory:
-rw-r--r-- 1 root wheel 12 Nov 18 20:36 file/root/target:
drwxr-xr-x 2 root wheel 3 Nov 18 20:36 directory
/root/target/directory:
-rw-r--r-- 1 root wheel 12 Nov 18 20:36 fileThe test account is unprivileged and can't even list it:
$ id
uid=1001(othmane) gid=1001(othmane) groups=1001(othmane)
$ ls -la /root/target
ls: /root/target: Permission denied$ id
uid=1001(othmane) gid=1001(othmane) groups=1001(othmane)
$ ls -la /root/target
ls: /root/target: Permission deniedThen, as that user: start a login session, populate the runtime directory, start the watcher aimed at /root/target, and exit.
setup done
XDG=/var/run/xdg/othmane
othmane@:~ $ exit
Can't cleanup (null)/othmane
$ cat ~/watch.log
cleanup seen in d39, swapped 47 dirs for symlinks to /root/targetsetup done
XDG=/var/run/xdg/othmane
othmane@:~ $ exit
Can't cleanup (null)/othmane
$ cat ~/watch.log
cleanup seen in d39, swapped 47 dirs for symlinks to /root/targetBack as root:
/root/target:
drwxr-xr-x 2 root wheel 2 Nov 18 20:38 .
drwxr-x--- 4 root wheel 11 Nov 18 20:36 ../root/target:
drwxr-xr-x 2 root wheel 2 Nov 18 20:38 .
drwxr-x--- 4 root wheel 11 Nov 18 20:36 ..The subdirectory and the file inside it are gone. An account with no access to /root had root empty a directory inside it.
I'm not publishing a ready-to-run exploit with this post. The patches are days old and many systems won't have applied them yet. The technique is fully described above for anyone who needs to understand or test it.
Impact
The primitive is: an unprivileged local user can make root recursively delete the contents of any directory.
Point it at /etc and nobody can log in. Point it at /boot and the machine won't come back up. Point it at another user's home directory and their data is gone.
What it does not give you directly is a root shell. It can only delete. I looked for a way to turn deletion into code execution on a default install and didn't find one: removing PAM policies, password databases or the SSH configuration makes authentication fail closed. I reported it as arbitrary file deletion, not as privilege escalation.
FreeBSD's advisory describes the impact as denial of service "and, depending on the system configuration, possibly privilege escalation". That's a fair hedge. On a customised system there may well be a file whose absence opens a door.
Who was affected
The advisory says: all supported versions of FreeBSD.
The fix commit carries a Fixes: tag pointing at the commit that first added the module in February 2024, and I confirmed that the very first version of the file already contained this exact function. The bug shipped in 14.1-RELEASE in mid-2024 and in every release after it, including all of 15.x.
The realistic exposure is narrower than "every FreeBSD machine". You need a local account first, so the systems at real risk are multi-user ones: shared hosting, university and research machines, shell providers, build servers, jump hosts. A server where only its administrators can log in was running the vulnerable code, but had nobody in a position to abuse it.
The fix
The patch, by Emmanuel Vadot, rewrites remove_dir(). The core of it:
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;
}
if (unlinkat(pfd, dp->d_name, 0) == -1)
error = -1;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;
}
if (unlinkat(pfd, dp->d_name, 0) == -1)
error = -1;What changed:
d_typeis no longer trusted. The code tries to open every entry as a directory withO_DIRECTORY | O_NOFOLLOW. If that succeeds, it really is a directory, right now. If it fails, the entry is unlinked as a file. A symlink that replaced a directory fails the open and gets removed itself; its target is never touched.- It stays on one filesystem. Each directory's device is compared with the runtime directory's.
- It checks ownership. A directory that belongs to neither the user nor root is not entered.
- Recursion is capped at 64 levels.
The patch also fixes a smaller issue I noted in the report. When looking for the last session marker, the module opened xdg_session.N in the user's directory with plain flags, so root would follow a planted symlink, or block forever on a FIFO. That open now uses O_NOFOLLOW | O_NONBLOCK.
It's a thorough fix. The comment added above the function is a good one-paragraph summary of how to delete a tree you don't trust.
How I found it
I was auditing a patched FreeBSD 15.1 install for local privilege escalation. After mapping the attack surface, the kernel paths I looked at first were well defended, so I turned to newer userland code that runs as root on user-controlled input.
pam_xdg stood out: it's recent, it's enabled by default, and its whole job is to create and destroy things as root inside a directory it then gives away. Once you read remove_dir() with that in mind, the missing O_NOFOLLOW is hard to unsee.
I think that's worth saying plainly. The bug is simple enough that a careful human reviewer would catch it, and yet it survived in a default-on, root-privileged code path for two and a half years.
Timeline
- 26β02β2026:
pam_xdgadded to FreeBSD (commit6e69612d5df1) with the vulnerable function. - 31β05β2024: 14.1-RELEASE tagged, the first release to include it
- 18β11β2024: I reported the issue to the FreeBSD Security Team.
- 19β11β2026: Fix authored.
- 02β12β2026: FreeBSD-SA-26:97.pam_xdg published, CVE-2026β127761 assigned, fixes committed to all supported branches.
A fix written the day after the report and an advisory two weeks later is a fast, professional response. My thanks to the FreeBSD Security Team and to Emmanuel Vadot.
What you should do
Update. Fixed versions are 15.2-RC1-p1, 15.1-RELEASE-p8, 14.5-RELEASE-p5 and 14.4-RELEASE-p14, plus the corresponding stable branches.
freebsd-update fetch
freebsd-update installfreebsd-update fetch
freebsd-update installOn systems installed from base packages:
pkg upgrade -r FreeBSD-basepkg upgrade -r FreeBSD-baseThen end existing sessions or reboot. The advisory points out that login sessions opened before the update still have the old module loaded.
If you can't update yet, check where the module is used and remove it from those policies:
grep -rl pam_xdg /etc/pam.d /usr/local/etc/pam.dgrep -rl pam_xdg /etc/pam.d /usr/local/etc/pam.dPer-user runtime directories won't be created while it's disabled, which mostly matters for desktop sessions.
Lessons for anyone writing privileged cleanup code
- Never trust
d_typefor a security decision. It's a hint from a buffer. Open the thing andfstat()the descriptor. - Always pass
O_NOFOLLOWwhen root touches a path an unprivileged user controls. AddO_DIRECTORYwhen you expect a directory. - Work relative to descriptors, not names.
openat()andunlinkat()on a directory fd you already hold remove a whole class of path-swapping tricks, provided you also follow rule 2. - Better still, drop privileges. If the cleanup had run with the user's own credentials, redirecting it would have gained the attacker nothing.
- Assume the user is still there. "They've logged out" is not the same as "they have no processes running."