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