Skip to content

Commit

Permalink
pam_namespace: protect_dir(): use O_DIRECTORY to prevent local DoS si…
Browse files Browse the repository at this point in the history
…tuations

Without O_DIRECTORY the path crawling logic is subject to e.g. FIFOs
being placed in user controlled directories, causing the PAM module to
block indefinitely during `openat()`.

Pass O_DIRECTORY to cause the `openat()` to fail if the path does not
refer to a directory.

With this the check whether the final path element is a directory
becomes unnecessary, drop it.
  • Loading branch information
mgerstner authored and ldv-alt committed Jan 17, 2024
1 parent cf8c50f commit 031bb5a
Showing 1 changed file with 1 addition and 17 deletions.
18 changes: 1 addition & 17 deletions modules/pam_namespace/pam_namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ static int protect_dir(const char *path, mode_t mode, int do_mkdir,
int dfd = AT_FDCWD;
int dfd_next;
int save_errno;
int flags = O_RDONLY;
int flags = O_RDONLY | O_DIRECTORY;
int rv = -1;
struct stat st;

Expand Down Expand Up @@ -1255,22 +1255,6 @@ static int protect_dir(const char *path, mode_t mode, int do_mkdir,
rv = openat(dfd, dir, flags);
}

if (rv != -1) {
if (fstat(rv, &st) != 0) {
save_errno = errno;
close(rv);
rv = -1;
errno = save_errno;
goto error;
}
if (!S_ISDIR(st.st_mode)) {
close(rv);
errno = ENOTDIR;
rv = -1;
goto error;
}
}

if (flags & O_NOFOLLOW) {
/* we are inside user-owned dir - protect */
if (protect_mount(rv, p, idata) == -1) {
Expand Down

0 comments on commit 031bb5a

Please sign in to comment.