Debian Codegraph @

src/shared/shift-uid.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/magic.h>
4 #include <sys/statvfs.h>
5 #include <unistd.h>
6
7 #include "acl-util.h"
8 #include "alloc-util.h"
9 #include "dirent-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "fileio.h"
13 #include "fs-util.h"
14 #include "log.h"
15 #include "shift-uid.h"
16 #include "stat-util.h"
17 #include "string-util.h"
18 #include "user-util.h"
19
20 /* While we are chmod()ing a directory tree, we set the top-level UID base to this "busy" base, so that we can always
21 * recognize trees we are were chmod()ing recursively and got interrupted in */
22 #define UID_BUSY_BASE ((uid_t) UINT32_C(0xFFFE0000))
23 #define UID_BUSY_MASK ((uid_t) UINT32_C(0xFFFF0000))
24
25 #if HAVE_ACL
26
27 static int get_acl(int fd, const char *name, acl_type_t type, acl_t *ret) {
28 acl_t acl;
29
30 assert(fd >= 0);
31 assert(ret);
32
33 if (name) {
34 _cleanup_close_ int child_fd = -EBADF;
35
36 child_fd = openat(fd, name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
37 if (child_fd < 0)
38 return -errno;
39
40 acl = sym_acl_get_file(FORMAT_PROC_FD_PATH(child_fd), type);
41 } else if (type == ACL_TYPE_ACCESS)
42 acl = sym_acl_get_fd(fd);
43 else
44 acl = sym_acl_get_file(FORMAT_PROC_FD_PATH(fd), type);
45 if (!acl)
46 return -errno;
47
48 *ret = acl;
49 return 0;
50 }
51
52 static int set_acl(int fd, const char *name, acl_type_t type, acl_t acl) {
53 int r;
54
55 assert(fd >= 0);
56 assert(acl);
57
58 if (name) {
59 _cleanup_close_ int child_fd = -EBADF;
60
61 child_fd = openat(fd, name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
62 if (child_fd < 0)
63 return -errno;
64
65 r = sym_acl_set_file(FORMAT_PROC_FD_PATH(child_fd), type, acl);
66 } else if (type == ACL_TYPE_ACCESS)
67 r = sym_acl_set_fd(fd, acl);
68 else
69 r = sym_acl_set_file(FORMAT_PROC_FD_PATH(fd), type, acl);
70 if (r < 0)
71 return -errno;
72
73 return 0;
74 }
75
76 static int shift_acl(acl_t acl, uid_t shift, acl_t *ret) {
77 _cleanup_(acl_freep) acl_t copy = NULL;
78 acl_entry_t i;
79 int r;
80
81 assert(acl);
82 assert(ret);
83
84 r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
85 if (r < 0)
86 return -errno;
87 while (r > 0) {
88 uid_t *old_uid, new_uid;
89 bool modify = false;
90 acl_tag_t tag;
91
92 if (sym_acl_get_tag_type(i, &tag) < 0)
93 return -errno;
94
95 if (IN_SET(tag, ACL_USER, ACL_GROUP)) {
96
97 /* We don't distinguish here between uid_t and gid_t, let's make sure the compiler checks that
98 * this is actually OK */
99 assert_cc(sizeof(uid_t) == sizeof(gid_t));
100
101 old_uid = sym_acl_get_qualifier(i);
102 if (!old_uid)
103 return -errno;
104
105 new_uid = shift | (*old_uid & UINT32_C(0xFFFF));
106 if (!uid_is_valid(new_uid))
107 return -EINVAL;
108
109 modify = new_uid != *old_uid;
110 if (modify && !copy) {
111 int n;
112
113 /* There's no copy of the ACL yet? if so, let's create one, and start the loop from the
114 * beginning, so that we copy all entries, starting from the first, this time. */
115
116 n = sym_acl_entries(acl);
117 if (n < 0)
118 return -errno;
119
120 copy = sym_acl_init(n);
121 if (!copy)
122 return -errno;
123
124 /* Seek back to the beginning */
125 r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
126 if (r < 0)
127 return -errno;
128 continue;
129 }
130 }
131
132 if (copy) {
133 acl_entry_t new_entry;
134
135 if (sym_acl_create_entry(&copy, &new_entry) < 0)
136 return -errno;
137
138 if (sym_acl_copy_entry(new_entry, i) < 0)
139 return -errno;
140
141 if (modify)
142 if (sym_acl_set_qualifier(new_entry, &new_uid) < 0)
143 return -errno;
144 }
145
146 r = sym_acl_get_entry(acl, ACL_NEXT_ENTRY, &i);
147 if (r < 0)
148 return -errno;
149 }
150
151 *ret = TAKE_PTR(copy);
152
153 return !!*ret;
154 }
155
156 static int patch_acls(int fd, const char *name, const struct stat *st, uid_t shift) {
157 _cleanup_(acl_freep) acl_t acl = NULL, shifted = NULL;
158 bool changed = false;
159 int r;
160
161 assert(fd >= 0);
162 assert(st);
163
164 /* ACLs are not supported on symlinks, there's no point in trying */
165 if (!inode_type_can_acl(st->st_mode))
166 return 0;
167
168 r = dlopen_libacl(LOG_DEBUG);
169 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
170 return 0;
171 if (r < 0)
172 return r;
173
174 r = get_acl(fd, name, ACL_TYPE_ACCESS, &acl);
175 if (r == -EOPNOTSUPP)
176 return 0;
177 if (r < 0)
178 return r;
179
180 r = shift_acl(acl, shift, &shifted);
181 if (r < 0)
182 return r;
183 if (r > 0) {
184 r = set_acl(fd, name, ACL_TYPE_ACCESS, shifted);
185 if (r < 0)
186 return r;
187
188 changed = true;
189 }
190
191 if (S_ISDIR(st->st_mode)) {
192 sym_acl_free(acl);
193
194 if (shifted)
195 sym_acl_free(shifted);
196
197 acl = shifted = NULL;
198
199 r = get_acl(fd, name, ACL_TYPE_DEFAULT, &acl);
200 if (r < 0)
201 return r;
202
203 r = shift_acl(acl, shift, &shifted);
204 if (r < 0)
205 return r;
206 if (r > 0) {
207 r = set_acl(fd, name, ACL_TYPE_DEFAULT, shifted);
208 if (r < 0)
209 return r;
210
211 changed = true;
212 }
213 }
214
215 return changed;
216 }
217
218 #else
219
220 static int patch_acls(int fd, const char *name, const struct stat *st, uid_t shift) {
221 return 0;
222 }
223
224 #endif
225
226 static int patch_fd(int fd, const char *name, const struct stat *st, uid_t shift) {
227 uid_t new_uid;
228 gid_t new_gid;
229 bool changed = false;
230 int r;
231
232 assert(fd >= 0);
233 assert(st);
234
235 new_uid = shift | (st->st_uid & UINT32_C(0xFFFF));
236 new_gid = (gid_t) shift | (st->st_gid & UINT32_C(0xFFFF));
237
238 if (!uid_is_valid(new_uid) || !gid_is_valid(new_gid))
239 return -EINVAL;
240
241 if (st->st_uid != new_uid || st->st_gid != new_gid) {
242 if (name)
243 r = fchownat(fd, name, new_uid, new_gid, AT_SYMLINK_NOFOLLOW);
244 else
245 r = fchown(fd, new_uid, new_gid);
246 if (r < 0)
247 return -errno;
248
249 /* The Linux kernel alters the mode in some cases of chown(). Let's undo this. */
250 if (name) {
251 if (!S_ISLNK(st->st_mode))
252 r = fchmodat(fd, name, st->st_mode, 0);
253 else /* Changing the mode of a symlink is not supported by Linux kernel. Don't bother. */
254 r = 0;
255 } else
256 r = fchmod(fd, st->st_mode);
257 if (r < 0)
258 return -errno;
259
260 changed = true;
261 }
262
263 r = patch_acls(fd, name, st, shift);
264 if (r < 0)
265 return r;
266
267 return r > 0 || changed;
268 }
269
270 /*
271 * Check if the filesystem is fully compatible with user namespaces or
272 * UID/GID patching. Some filesystems in this list can be fully mounted inside
273 * user namespaces, however their inodes may relate to host resources or only
274 * valid in the global user namespace, therefore no patching should be applied.
275 */
276 static int is_fs_fully_userns_compatible(const struct statfs *sfs) {
277
278 assert(sfs);
279
280 return F_TYPE_EQUAL(sfs->f_type, BINFMTFS_MAGIC) ||
281 F_TYPE_EQUAL(sfs->f_type, CGROUP_SUPER_MAGIC) ||
282 F_TYPE_EQUAL(sfs->f_type, CGROUP2_SUPER_MAGIC) ||
283 F_TYPE_EQUAL(sfs->f_type, DEBUGFS_MAGIC) ||
284 F_TYPE_EQUAL(sfs->f_type, DEVPTS_SUPER_MAGIC) ||
285 F_TYPE_EQUAL(sfs->f_type, EFIVARFS_MAGIC) ||
286 F_TYPE_EQUAL(sfs->f_type, HUGETLBFS_MAGIC) ||
287 F_TYPE_EQUAL(sfs->f_type, MQUEUE_MAGIC) ||
288 F_TYPE_EQUAL(sfs->f_type, PROC_SUPER_MAGIC) ||
289 F_TYPE_EQUAL(sfs->f_type, PSTOREFS_MAGIC) ||
290 F_TYPE_EQUAL(sfs->f_type, SELINUX_MAGIC) ||
291 F_TYPE_EQUAL(sfs->f_type, SMACK_MAGIC) ||
292 F_TYPE_EQUAL(sfs->f_type, SECURITYFS_MAGIC) ||
293 F_TYPE_EQUAL(sfs->f_type, BPF_FS_MAGIC) ||
294 F_TYPE_EQUAL(sfs->f_type, TRACEFS_MAGIC) ||
295 F_TYPE_EQUAL(sfs->f_type, SYSFS_MAGIC);
296 }
297
298 static int recurse_fd(int fd, const struct stat *st, uid_t shift, bool is_toplevel) {
299 _cleanup_closedir_ DIR *d = NULL;
300 bool changed = false;
301 struct statfs sfs;
302 int r;
303
304 assert(fd >= 0);
305
306 if (fstatfs(fd, &sfs) < 0)
307 return -errno;
308
309 /* We generally want to permit crossing of mount boundaries when patching the UIDs/GIDs. However, we probably
310 * shouldn't do this for /proc and /sys if that is already mounted into place. Hence, let's stop the recursion
311 * when we hit procfs, sysfs or some other special file systems. */
312
313 r = is_fs_fully_userns_compatible(&sfs);
314 if (r < 0)
315 return r;
316 if (r > 0) {
317 r = 0; /* don't recurse */
318 return r;
319 }
320
321 /* Also, if we hit a read-only file system, then don't bother, skip the whole subtree */
322 if ((sfs.f_flags & ST_RDONLY) ||
323 access_fd(fd, W_OK) == -EROFS)
324 goto read_only;
325
326 if (S_ISDIR(st->st_mode)) {
327 d = take_fdopendir(&fd);
328 if (!d)
329 return -errno;
330
331 FOREACH_DIRENT_ALL(de, d, return -errno) {
332 struct stat fst;
333
334 if (dot_or_dot_dot(de->d_name))
335 continue;
336
337 if (fstatat(dirfd(d), de->d_name, &fst, AT_SYMLINK_NOFOLLOW) < 0)
338 return -errno;
339
340 if (S_ISDIR(fst.st_mode)) {
341 int subdir_fd;
342
343 subdir_fd = openat(dirfd(d), de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
344 if (subdir_fd < 0)
345 return -errno;
346
347 r = recurse_fd(subdir_fd, &fst, shift, false);
348 if (r < 0)
349 return r;
350 if (r > 0)
351 changed = true;
352
353 } else {
354 r = patch_fd(dirfd(d), de->d_name, &fst, shift);
355 if (r < 0)
356 return r;
357 if (r > 0)
358 changed = true;
359 }
360 }
361 }
362
363 /* After we descended, also patch the directory itself. It's key to do this in this order so that the top-level
364 * directory is patched as very last object in the tree, so that we can use it as quick indicator whether the
365 * tree is properly chown()ed already. */
366 r = patch_fd(d ? dirfd(d) : fd, NULL, st, shift);
367 if (r == -EROFS)
368 goto read_only;
369 if (r > 0)
370 changed = true;
371
372 return changed;
373
374 read_only:
375 if (!is_toplevel) {
376 _cleanup_free_ char *name = NULL;
377
378 /* When we hit a read-only subtree we simply skip it, but log about it. */
379 (void) fd_get_path(fd, &name);
380 log_debug("Skipping read-only file or directory %s.", strna(name));
381 r = changed;
382 }
383
384 return r;
385 }
386
387 int path_patch_uid(const char *path, uid_t shift, uid_t range) {
388 _cleanup_close_ int fd = -EBADF;
389 struct stat st;
390 int r;
391
392 assert(path);
393
394 fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
395 if (fd < 0)
396 return log_debug_errno(errno, "Failed to open '%s': %m", path);
397
398 /* Recursively adjusts the UID/GIDs of all files of a directory tree. This is used to automatically fix up an
399 * OS tree to the used user namespace UID range. Note that this automatic adjustment only works for UID ranges
400 * following the concept that the upper 16-bit of a UID identify the container, and the lower 16-bit are the actual
401 * UID within the container. */
402
403 /* We only support containers where the shift starts at a 2^16 boundary */
404 if ((shift & 0xFFFF) != 0)
405 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
406 "UID shift 0x%"PRIx32" is not at a 2^16 boundary.",
407 (uint32_t) shift);
408
409 if (shift == UID_BUSY_BASE)
410 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
411 "UID shift 0x%"PRIx32" conflicts with busy base.",
412 (uint32_t) shift);
413
414 /* We only support containers with 16-bit UID ranges for the patching logic */
415 if (range != 0x10000)
416 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
417 "UID range 0x%"PRIx32" is not supported, must be 0x10000.",
418 (uint32_t) range);
419
420 if (fstat(fd, &st) < 0)
421 return log_debug_errno(errno, "Failed to stat '%s': %m", path);
422
423 /* We only support containers where the uid/gid container ID match */
424 if ((uint32_t) st.st_uid >> 16 != (uint32_t) st.st_gid >> 16)
425 return log_debug_errno(SYNTHETIC_ERRNO(EBADE),
426 "UID container ID 0x%"PRIx32" does not match GID container ID 0x%"PRIx32".",
427 (uint32_t) st.st_uid >> 16,
428 (uint32_t) st.st_gid >> 16);
429
430 /* Try to detect if the range is already right. Of course, this a pretty drastic optimization, as we assume
431 * that if the top-level dir has the right upper 16-bit assigned, then everything below will have too... */
432 if (((uint32_t) (st.st_uid ^ shift) >> 16) == 0)
433 return 0;
434
435 /* Before we start recursively chowning, mark the top-level dir as "busy" by chowning it to the "busy"
436 * range. Should we be interrupted in the middle of our work, we'll see it owned by this user and will start
437 * chown()ing it again, unconditionally, as the busy UID is not a valid UID we'd everpick for ourselves. */
438
439 if ((st.st_uid & UID_BUSY_MASK) != UID_BUSY_BASE)
440 if (fchown(fd,
441 UID_BUSY_BASE | (st.st_uid & ~UID_BUSY_MASK),
442 (gid_t) UID_BUSY_BASE | (st.st_gid & ~(gid_t) UID_BUSY_MASK)) < 0)
443 log_debug_errno(errno, "Failed to mark '%s' as busy, ignoring: %m", path);
444
445 r = recurse_fd(TAKE_FD(fd), &st, shift, /* is_toplevel= */ true);
446 if (r < 0)
447 return log_debug_errno(r, "Failed to recursively patch UID/GID of '%s': %m", path);
448
449 return r;
450 }