| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
|
| 6 |
#include "chown-recursive.h" |
| 7 |
#include "dirent-util.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "fs-util.h" |
| 11 |
#include "path-util.h" |
| 12 |
#include "stat-util.h" |
| 13 |
#include "strv.h" |
| 14 |
#include "user-util.h" |
| 15 |
#include "xattr-util.h" |
| 16 |
|
| 17 |
static int chown_one( |
| 18 |
int fd, |
| 19 |
const struct stat *st, |
| 20 |
uid_t uid, |
| 21 |
gid_t gid, |
| 22 |
mode_t mask) { |
| 23 |
|
| 24 |
int r; |
| 25 |
|
| 26 |
assert(fd >= 0); |
| 27 |
assert(st); |
| 28 |
|
| 29 |
/* Drop any ACL if there is one */ |
| 30 |
FOREACH_STRING(n, "system.posix_acl_access", "system.posix_acl_default") { |
| 31 |
r = xremovexattr(fd, /* path= */ NULL, AT_EMPTY_PATH, n); |
| 32 |
if (r < 0 && !ERRNO_IS_NEG_XATTR_ABSENT(r)) |
| 33 |
return r; |
| 34 |
} |
| 35 |
|
| 36 |
r = fchmod_and_chown(fd, st->st_mode & mask, uid, gid); |
| 37 |
if (r < 0) |
| 38 |
return r; |
| 39 |
|
| 40 |
return 1; |
| 41 |
} |
| 42 |
|
| 43 |
static int chown_recursive_internal( |
| 44 |
int fd, |
| 45 |
const struct stat *st, |
| 46 |
uid_t uid, |
| 47 |
gid_t gid, |
| 48 |
mode_t mask) { |
| 49 |
|
| 50 |
_cleanup_closedir_ DIR *d = NULL; |
| 51 |
bool changed = false; |
| 52 |
int r; |
| 53 |
|
| 54 |
assert(fd >= 0); |
| 55 |
assert(st); |
| 56 |
|
| 57 |
d = fdopendir(fd); |
| 58 |
if (!d) { |
| 59 |
safe_close(fd); |
| 60 |
return -errno; |
| 61 |
} |
| 62 |
|
| 63 |
FOREACH_DIRENT_ALL(de, d, return -errno) { |
| 64 |
_cleanup_close_ int path_fd = -EBADF; |
| 65 |
struct stat fst; |
| 66 |
|
| 67 |
if (dot_or_dot_dot(de->d_name)) |
| 68 |
continue; |
| 69 |
|
| 70 |
/* Let's pin the child inode we want to fix now with an O_PATH fd, so that it cannot be swapped out |
| 71 |
* while we manipulate it. */ |
| 72 |
path_fd = openat(dirfd(d), de->d_name, O_PATH|O_CLOEXEC|O_NOFOLLOW); |
| 73 |
if (path_fd < 0) |
| 74 |
return -errno; |
| 75 |
|
| 76 |
if (fstat(path_fd, &fst) < 0) |
| 77 |
return -errno; |
| 78 |
|
| 79 |
if (S_ISDIR(fst.st_mode)) { |
| 80 |
int subdir_fd; |
| 81 |
|
| 82 |
/* Convert it to a "real" (i.e. non-O_PATH) fd now */ |
| 83 |
subdir_fd = fd_reopen(path_fd, O_RDONLY|O_CLOEXEC|O_NOATIME); |
| 84 |
if (subdir_fd < 0) |
| 85 |
return subdir_fd; |
| 86 |
|
| 87 |
r = chown_recursive_internal(subdir_fd, &fst, uid, gid, mask); /* takes possession of subdir_fd even on failure */ |
| 88 |
if (r < 0) |
| 89 |
return r; |
| 90 |
if (r > 0) |
| 91 |
changed = true; |
| 92 |
} else { |
| 93 |
r = chown_one(path_fd, &fst, uid, gid, mask); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
if (r > 0) |
| 97 |
changed = true; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
r = chown_one(dirfd(d), st, uid, gid, mask); |
| 102 |
if (r < 0) |
| 103 |
return r; |
| 104 |
|
| 105 |
return r > 0 || changed; |
| 106 |
} |
| 107 |
|
| 108 |
int path_chown_recursive( |
| 109 |
const char *path, |
| 110 |
uid_t uid, |
| 111 |
gid_t gid, |
| 112 |
mode_t mask, |
| 113 |
int flags) { |
| 114 |
|
| 115 |
_cleanup_close_ int fd = -EBADF; |
| 116 |
struct stat st; |
| 117 |
|
| 118 |
assert((flags & ~AT_SYMLINK_FOLLOW) == 0); |
| 119 |
|
| 120 |
fd = open(path, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOATIME|(FLAGS_SET(flags, AT_SYMLINK_FOLLOW) ? 0 : O_NOFOLLOW)); |
| 121 |
if (fd < 0) |
| 122 |
return -errno; |
| 123 |
|
| 124 |
if (!uid_is_valid(uid) && !gid_is_valid(gid) && FLAGS_SET(mask, 07777)) |
| 125 |
return 0; /* nothing to do */ |
| 126 |
|
| 127 |
if (fstat(fd, &st) < 0) |
| 128 |
return -errno; |
| 129 |
|
| 130 |
/* Let's take a shortcut: if the top-level directory is properly owned, we don't descend into the |
| 131 |
* whole tree, under the assumption that all is OK anyway. */ |
| 132 |
if ((!uid_is_valid(uid) || st.st_uid == uid) && |
| 133 |
(!gid_is_valid(gid) || st.st_gid == gid) && |
| 134 |
((st.st_mode & ~mask & 07777) == 0)) |
| 135 |
return 0; |
| 136 |
|
| 137 |
return chown_recursive_internal(TAKE_FD(fd), &st, uid, gid, mask); /* we donate the fd to the call, regardless if it succeeded or failed */ |
| 138 |
} |
| 139 |
|
| 140 |
int fd_chown_recursive( |
| 141 |
int fd, |
| 142 |
uid_t uid, |
| 143 |
gid_t gid, |
| 144 |
mode_t mask) { |
| 145 |
|
| 146 |
int duplicated_fd = -EBADF; |
| 147 |
struct stat st; |
| 148 |
int r; |
| 149 |
|
| 150 |
/* Note that the slightly different order of fstat() and the checks here and in |
| 151 |
* path_chown_recursive(). That's because when we open the directory ourselves we can specify |
| 152 |
* O_DIRECTORY and we always want to ensure we are operating on a directory before deciding whether |
| 153 |
* the operation is otherwise redundant. */ |
| 154 |
|
| 155 |
if (fstat(fd, &st) < 0) |
| 156 |
return -errno; |
| 157 |
|
| 158 |
r = stat_verify_directory(&st); |
| 159 |
if (r < 0) |
| 160 |
return r; |
| 161 |
|
| 162 |
if (!uid_is_valid(uid) && !gid_is_valid(gid) && FLAGS_SET(mask, 07777)) |
| 163 |
return 0; /* nothing to do */ |
| 164 |
|
| 165 |
/* Shortcut, as above */ |
| 166 |
if ((!uid_is_valid(uid) || st.st_uid == uid) && |
| 167 |
(!gid_is_valid(gid) || st.st_gid == gid) && |
| 168 |
((st.st_mode & ~mask & 07777) == 0)) |
| 169 |
return 0; |
| 170 |
|
| 171 |
/* Let's duplicate the fd here, as opendir() wants to take possession of it and close it afterwards */ |
| 172 |
duplicated_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
| 173 |
if (duplicated_fd < 0) |
| 174 |
return -errno; |
| 175 |
|
| 176 |
return chown_recursive_internal(duplicated_fd, &st, uid, gid, mask); /* fd donated even on failure */ |
| 177 |
} |