aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;