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