|
|
a3e2b5 |
From 107d75ca9394481bd045385fc45f2ee65b30ad16 Mon Sep 17 00:00:00 2001
|
|
|
a3e2b5 |
From: Lennart Poettering <lennart@poettering.net>
|
|
|
a3e2b5 |
Date: Fri, 19 Oct 2018 11:26:59 +0200
|
|
|
a3e2b5 |
Subject: [PATCH] chown-recursive: let's rework the recursive logic to use
|
|
|
a3e2b5 |
O_PATH
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
That way we can pin a specific inode and analyze it and manipulate it
|
|
|
a3e2b5 |
without it being swapped out beneath our hands.
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
Fixes a vulnerability originally found by Jann Horn from Google.
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
CVE-2018-15687
|
|
|
a3e2b5 |
LP: #1796692
|
|
|
a3e2b5 |
https://bugzilla.redhat.com/show_bug.cgi?id=1639076
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
(cherry-picked from commit 5de6cce58b3e8b79239b6e83653459d91af6e57c)
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
Resolves: #1643368
|
|
|
a3e2b5 |
---
|
|
|
a3e2b5 |
src/core/chown-recursive.c | 146 ++++++++++++++++++-------------------
|
|
|
a3e2b5 |
1 file changed, 70 insertions(+), 76 deletions(-)
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
diff --git a/src/core/chown-recursive.c b/src/core/chown-recursive.c
|
|
|
a3e2b5 |
index c4794501c2..27c64489b5 100644
|
|
|
a3e2b5 |
--- a/src/core/chown-recursive.c
|
|
|
a3e2b5 |
+++ b/src/core/chown-recursive.c
|
|
|
a3e2b5 |
@@ -1,17 +1,19 @@
|
|
|
a3e2b5 |
/* SPDX-License-Identifier: LGPL-2.1+ */
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
-#include <sys/types.h>
|
|
|
a3e2b5 |
-#include <sys/stat.h>
|
|
|
a3e2b5 |
#include <fcntl.h>
|
|
|
a3e2b5 |
+#include <sys/stat.h>
|
|
|
a3e2b5 |
+#include <sys/types.h>
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
-#include "user-util.h"
|
|
|
a3e2b5 |
-#include "macro.h"
|
|
|
a3e2b5 |
-#include "fd-util.h"
|
|
|
a3e2b5 |
-#include "dirent-util.h"
|
|
|
a3e2b5 |
#include "chown-recursive.h"
|
|
|
a3e2b5 |
+#include "dirent-util.h"
|
|
|
a3e2b5 |
+#include "fd-util.h"
|
|
|
a3e2b5 |
+#include "macro.h"
|
|
|
a3e2b5 |
+#include "stdio-util.h"
|
|
|
a3e2b5 |
+#include "strv.h"
|
|
|
a3e2b5 |
+#include "user-util.h"
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
-static int chown_one(int fd, const char *name, const struct stat *st, uid_t uid, gid_t gid) {
|
|
|
a3e2b5 |
- int r;
|
|
|
a3e2b5 |
+static int chown_one(int fd, const struct stat *st, uid_t uid, gid_t gid) {
|
|
|
a3e2b5 |
+ char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
assert(fd >= 0);
|
|
|
a3e2b5 |
assert(st);
|
|
|
a3e2b5 |
@@ -20,90 +22,82 @@ static int chown_one(int fd, const char *name, const struct stat *st, uid_t uid,
|
|
|
a3e2b5 |
(!gid_is_valid(gid) || st->st_gid == gid))
|
|
|
a3e2b5 |
return 0;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- if (name)
|
|
|
a3e2b5 |
- r = fchownat(fd, name, uid, gid, AT_SYMLINK_NOFOLLOW);
|
|
|
a3e2b5 |
- else
|
|
|
a3e2b5 |
- r = fchown(fd, uid, gid);
|
|
|
a3e2b5 |
- if (r < 0)
|
|
|
a3e2b5 |
- return -errno;
|
|
|
a3e2b5 |
+ /* We change ownership through the /proc/self/fd/%i path, so that we have a stable reference that works with
|
|
|
a3e2b5 |
+ * O_PATH. (Note: fchown() and fchmod() do not work with O_PATH, the kernel refuses that. */
|
|
|
a3e2b5 |
+ xsprintf(procfs_path, "/proc/self/fd/%i", fd);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- /* The linux kernel alters the mode in some cases of chown(). Let's undo this. */
|
|
|
a3e2b5 |
- if (name) {
|
|
|
a3e2b5 |
- if (!S_ISLNK(st->st_mode))
|
|
|
a3e2b5 |
- r = fchmodat(fd, name, st->st_mode, 0);
|
|
|
a3e2b5 |
- else /* There's currently no AT_SYMLINK_NOFOLLOW for fchmodat() */
|
|
|
a3e2b5 |
- r = 0;
|
|
|
a3e2b5 |
- } else
|
|
|
a3e2b5 |
- r = fchmod(fd, st->st_mode);
|
|
|
a3e2b5 |
- if (r < 0)
|
|
|
a3e2b5 |
+ if (chown(procfs_path, uid, gid) < 0)
|
|
|
a3e2b5 |
return -errno;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
+ /* The linux kernel alters the mode in some cases of chown(). Let's undo this. We do this only for non-symlinks
|
|
|
a3e2b5 |
+ * however. That's because for symlinks the access mode is ignored anyway and because on some kernels/file
|
|
|
a3e2b5 |
+ * systems trying to change the access mode will succeed but has no effect while on others it actively
|
|
|
a3e2b5 |
+ * fails. */
|
|
|
a3e2b5 |
+ if (!S_ISLNK(st->st_mode))
|
|
|
a3e2b5 |
+ if (chmod(procfs_path, st->st_mode & 07777) < 0)
|
|
|
a3e2b5 |
+ return -errno;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
return 1;
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
static int chown_recursive_internal(int fd, const struct stat *st, uid_t uid, gid_t gid) {
|
|
|
a3e2b5 |
+ _cleanup_closedir_ DIR *d = NULL;
|
|
|
a3e2b5 |
bool changed = false;
|
|
|
a3e2b5 |
+ struct dirent *de;
|
|
|
a3e2b5 |
int r;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
assert(fd >= 0);
|
|
|
a3e2b5 |
assert(st);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- if (S_ISDIR(st->st_mode)) {
|
|
|
a3e2b5 |
- _cleanup_closedir_ DIR *d = NULL;
|
|
|
a3e2b5 |
- struct dirent *de;
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
- d = fdopendir(fd);
|
|
|
a3e2b5 |
- if (!d) {
|
|
|
a3e2b5 |
- r = -errno;
|
|
|
a3e2b5 |
- goto finish;
|
|
|
a3e2b5 |
- }
|
|
|
a3e2b5 |
- fd = -1;
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
- FOREACH_DIRENT_ALL(de, d, r = -errno; goto finish) {
|
|
|
a3e2b5 |
- struct stat fst;
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
- if (dot_or_dot_dot(de->d_name))
|
|
|
a3e2b5 |
- continue;
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
- if (fstatat(dirfd(d), de->d_name, &fst, AT_SYMLINK_NOFOLLOW) < 0) {
|
|
|
a3e2b5 |
- r = -errno;
|
|
|
a3e2b5 |
- goto finish;
|
|
|
a3e2b5 |
- }
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
- if (S_ISDIR(fst.st_mode)) {
|
|
|
a3e2b5 |
- int subdir_fd;
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
- subdir_fd = openat(dirfd(d), de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
|
|
|
a3e2b5 |
- if (subdir_fd < 0) {
|
|
|
a3e2b5 |
- r = -errno;
|
|
|
a3e2b5 |
- goto finish;
|
|
|
a3e2b5 |
- }
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
- r = chown_recursive_internal(subdir_fd, &fst, uid, gid);
|
|
|
a3e2b5 |
- if (r < 0)
|
|
|
a3e2b5 |
- goto finish;
|
|
|
a3e2b5 |
- if (r > 0)
|
|
|
a3e2b5 |
- changed = true;
|
|
|
a3e2b5 |
- } else {
|
|
|
a3e2b5 |
- r = chown_one(dirfd(d), de->d_name, &fst, uid, gid);
|
|
|
a3e2b5 |
- if (r < 0)
|
|
|
a3e2b5 |
- goto finish;
|
|
|
a3e2b5 |
- if (r > 0)
|
|
|
a3e2b5 |
- changed = true;
|
|
|
a3e2b5 |
- }
|
|
|
a3e2b5 |
+ d = fdopendir(fd);
|
|
|
a3e2b5 |
+ if (!d) {
|
|
|
a3e2b5 |
+ safe_close(fd);
|
|
|
a3e2b5 |
+ return -errno;
|
|
|
a3e2b5 |
+ }
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ FOREACH_DIRENT_ALL(de, d, return -errno) {
|
|
|
a3e2b5 |
+ _cleanup_close_ int path_fd = -1;
|
|
|
a3e2b5 |
+ struct stat fst;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ if (dot_or_dot_dot(de->d_name))
|
|
|
a3e2b5 |
+ continue;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ /* Let's pin the child inode we want to fix now with an O_PATH fd, so that it cannot be swapped out
|
|
|
a3e2b5 |
+ * while we manipulate it. */
|
|
|
a3e2b5 |
+ path_fd = openat(dirfd(d), de->d_name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
|
|
|
a3e2b5 |
+ if (path_fd < 0)
|
|
|
a3e2b5 |
+ return -errno;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ if (fstat(path_fd, &fst) < 0)
|
|
|
a3e2b5 |
+ return -errno;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ if (S_ISDIR(fst.st_mode)) {
|
|
|
a3e2b5 |
+ int subdir_fd;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ /* Convert it to a "real" (i.e. non-O_PATH) fd now */
|
|
|
a3e2b5 |
+ subdir_fd = fd_reopen(path_fd, O_RDONLY|O_CLOEXEC|O_NOATIME);
|
|
|
a3e2b5 |
+ if (subdir_fd < 0)
|
|
|
a3e2b5 |
+ return subdir_fd;
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ r = chown_recursive_internal(subdir_fd, &fst, uid, gid); /* takes possession of subdir_fd even on failure */
|
|
|
a3e2b5 |
+ if (r < 0)
|
|
|
a3e2b5 |
+ return r;
|
|
|
a3e2b5 |
+ if (r > 0)
|
|
|
a3e2b5 |
+ changed = true;
|
|
|
a3e2b5 |
+ } else {
|
|
|
a3e2b5 |
+ r = chown_one(path_fd, &fst, uid, gid);
|
|
|
a3e2b5 |
+ if (r < 0)
|
|
|
a3e2b5 |
+ return r;
|
|
|
a3e2b5 |
+ if (r > 0)
|
|
|
a3e2b5 |
+ changed = true;
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
+ }
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- r = chown_one(dirfd(d), NULL, st, uid, gid);
|
|
|
a3e2b5 |
- } else
|
|
|
a3e2b5 |
- r = chown_one(fd, NULL, st, uid, gid);
|
|
|
a3e2b5 |
+ r = chown_one(dirfd(d), st, uid, gid);
|
|
|
a3e2b5 |
if (r < 0)
|
|
|
a3e2b5 |
- goto finish;
|
|
|
a3e2b5 |
+ return r;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- r = r > 0 || changed;
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
-finish:
|
|
|
a3e2b5 |
- safe_close(fd);
|
|
|
a3e2b5 |
- return r;
|
|
|
a3e2b5 |
+ return r > 0 || changed;
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
int path_chown_recursive(const char *path, uid_t uid, gid_t gid) {
|
|
|
a3e2b5 |
@@ -111,7 +105,7 @@ int path_chown_recursive(const char *path, uid_t uid, gid_t gid) {
|
|
|
a3e2b5 |
struct stat st;
|
|
|
a3e2b5 |
int r;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
|
|
|
a3e2b5 |
+ fd = open(path, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
|
|
|
a3e2b5 |
if (fd < 0)
|
|
|
a3e2b5 |
return -errno;
|
|
|
a3e2b5 |
|