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