]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-patch-uid.c
Merge pull request #29816 from bluca/rel
[thirdparty/systemd.git] / src / nspawn / nspawn-patch-uid.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "fileio.h"
12 #include "fs-util.h"
13 #include "missing_magic.h"
14 #include "missing_syscall.h"
15 #include "nspawn-def.h"
16 #include "nspawn-patch-uid.h"
17 #include "stat-util.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
20 #include "strv.h"
21 #include "user-util.h"
22
23 #if HAVE_ACL
24
25 static int get_acl(int fd, const char *name, acl_type_t type, acl_t *ret) {
26 acl_t acl;
27
28 assert(fd >= 0);
29 assert(ret);
30
31 if (name) {
32 _cleanup_close_ int child_fd = -EBADF;
33
34 child_fd = openat(fd, name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
35 if (child_fd < 0)
36 return -errno;
37
38 acl = acl_get_file(FORMAT_PROC_FD_PATH(child_fd), type);
39 } else if (type == ACL_TYPE_ACCESS)
40 acl = acl_get_fd(fd);
41 else
42 acl = acl_get_file(FORMAT_PROC_FD_PATH(fd), type);
43 if (!acl)
44 return -errno;
45
46 *ret = acl;
47 return 0;
48 }
49
50 static int set_acl(int fd, const char *name, acl_type_t type, acl_t acl) {
51 int r;
52
53 assert(fd >= 0);
54 assert(acl);
55
56 if (name) {
57 _cleanup_close_ int child_fd = -EBADF;
58
59 child_fd = openat(fd, name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
60 if (child_fd < 0)
61 return -errno;
62
63 r = acl_set_file(FORMAT_PROC_FD_PATH(child_fd), type, acl);
64 } else if (type == ACL_TYPE_ACCESS)
65 r = acl_set_fd(fd, acl);
66 else
67 r = acl_set_file(FORMAT_PROC_FD_PATH(fd), type, acl);
68 if (r < 0)
69 return -errno;
70
71 return 0;
72 }
73
74 static int shift_acl(acl_t acl, uid_t shift, acl_t *ret) {
75 _cleanup_(acl_freep) acl_t copy = NULL;
76 acl_entry_t i;
77 int r;
78
79 assert(acl);
80 assert(ret);
81
82 r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
83 if (r < 0)
84 return -errno;
85 while (r > 0) {
86 uid_t *old_uid, new_uid;
87 bool modify = false;
88 acl_tag_t tag;
89
90 if (acl_get_tag_type(i, &tag) < 0)
91 return -errno;
92
93 if (IN_SET(tag, ACL_USER, ACL_GROUP)) {
94
95 /* We don't distinguish here between uid_t and gid_t, let's make sure the compiler checks that
96 * this is actually OK */
97 assert_cc(sizeof(uid_t) == sizeof(gid_t));
98
99 old_uid = acl_get_qualifier(i);
100 if (!old_uid)
101 return -errno;
102
103 new_uid = shift | (*old_uid & UINT32_C(0xFFFF));
104 if (!uid_is_valid(new_uid))
105 return -EINVAL;
106
107 modify = new_uid != *old_uid;
108 if (modify && !copy) {
109 int n;
110
111 /* There's no copy of the ACL yet? if so, let's create one, and start the loop from the
112 * beginning, so that we copy all entries, starting from the first, this time. */
113
114 n = acl_entries(acl);
115 if (n < 0)
116 return -errno;
117
118 copy = acl_init(n);
119 if (!copy)
120 return -errno;
121
122 /* Seek back to the beginning */
123 r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
124 if (r < 0)
125 return -errno;
126 continue;
127 }
128 }
129
130 if (copy) {
131 acl_entry_t new_entry;
132
133 if (acl_create_entry(&copy, &new_entry) < 0)
134 return -errno;
135
136 if (acl_copy_entry(new_entry, i) < 0)
137 return -errno;
138
139 if (modify)
140 if (acl_set_qualifier(new_entry, &new_uid) < 0)
141 return -errno;
142 }
143
144 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i);
145 if (r < 0)
146 return -errno;
147 }
148
149 *ret = TAKE_PTR(copy);
150
151 return !!*ret;
152 }
153
154 static int patch_acls(int fd, const char *name, const struct stat *st, uid_t shift) {
155 _cleanup_(acl_freep) acl_t acl = NULL, shifted = NULL;
156 bool changed = false;
157 int r;
158
159 assert(fd >= 0);
160 assert(st);
161
162 /* ACLs are not supported on symlinks, there's no point in trying */
163 if (S_ISLNK(st->st_mode))
164 return 0;
165
166 r = get_acl(fd, name, ACL_TYPE_ACCESS, &acl);
167 if (r == -EOPNOTSUPP)
168 return 0;
169 if (r < 0)
170 return r;
171
172 r = shift_acl(acl, shift, &shifted);
173 if (r < 0)
174 return r;
175 if (r > 0) {
176 r = set_acl(fd, name, ACL_TYPE_ACCESS, shifted);
177 if (r < 0)
178 return r;
179
180 changed = true;
181 }
182
183 if (S_ISDIR(st->st_mode)) {
184 acl_free(acl);
185
186 if (shifted)
187 acl_free(shifted);
188
189 acl = shifted = NULL;
190
191 r = get_acl(fd, name, ACL_TYPE_DEFAULT, &acl);
192 if (r < 0)
193 return r;
194
195 r = shift_acl(acl, shift, &shifted);
196 if (r < 0)
197 return r;
198 if (r > 0) {
199 r = set_acl(fd, name, ACL_TYPE_DEFAULT, shifted);
200 if (r < 0)
201 return r;
202
203 changed = true;
204 }
205 }
206
207 return changed;
208 }
209
210 #else
211
212 static int patch_acls(int fd, const char *name, const struct stat *st, uid_t shift) {
213 return 0;
214 }
215
216 #endif
217
218 static int patch_fd(int fd, const char *name, const struct stat *st, uid_t shift) {
219 uid_t new_uid;
220 gid_t new_gid;
221 bool changed = false;
222 int r;
223
224 assert(fd >= 0);
225 assert(st);
226
227 new_uid = shift | (st->st_uid & UINT32_C(0xFFFF));
228 new_gid = (gid_t) shift | (st->st_gid & UINT32_C(0xFFFF));
229
230 if (!uid_is_valid(new_uid) || !gid_is_valid(new_gid))
231 return -EINVAL;
232
233 if (st->st_uid != new_uid || st->st_gid != new_gid) {
234 if (name)
235 r = fchownat(fd, name, new_uid, new_gid, AT_SYMLINK_NOFOLLOW);
236 else
237 r = fchown(fd, new_uid, new_gid);
238 if (r < 0)
239 return -errno;
240
241 /* The Linux kernel alters the mode in some cases of chown(). Let's undo this. */
242 if (name) {
243 /* It looks like older glibc (before 2016) did not support AT_SYMLINK_NOFOLLOW. */
244 if (!S_ISLNK(st->st_mode))
245 r = RET_NERRNO(fchmodat(fd, name, st->st_mode, 0));
246 else {
247 r = RET_NERRNO(fchmodat2(fd, name, st->st_mode, AT_SYMLINK_NOFOLLOW));
248 if (IN_SET(r, -ENOSYS, -EPERM))
249 r = 0;
250 }
251 } else
252 r = RET_NERRNO(fchmod(fd, st->st_mode));
253 if (r < 0)
254 return r;
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 if (!donate_fd) {
324 int copy;
325
326 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
327 if (copy < 0) {
328 r = -errno;
329 goto finish;
330 }
331
332 fd = copy;
333 donate_fd = true;
334 }
335
336 d = take_fdopendir(&fd);
337 if (!d) {
338 r = -errno;
339 goto finish;
340 }
341
342 FOREACH_DIRENT_ALL(de, d, r = -errno; goto finish) {
343 struct stat fst;
344
345 if (dot_or_dot_dot(de->d_name))
346 continue;
347
348 if (fstatat(dirfd(d), de->d_name, &fst, AT_SYMLINK_NOFOLLOW) < 0) {
349 r = -errno;
350 goto finish;
351 }
352
353 if (S_ISDIR(fst.st_mode)) {
354 int subdir_fd;
355
356 subdir_fd = openat(dirfd(d), de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
357 if (subdir_fd < 0) {
358 r = -errno;
359 goto finish;
360
361 }
362
363 r = recurse_fd(subdir_fd, true, &fst, shift, false);
364 if (r < 0)
365 goto finish;
366 if (r > 0)
367 changed = true;
368
369 } else {
370 r = patch_fd(dirfd(d), de->d_name, &fst, shift);
371 if (r < 0)
372 goto finish;
373 if (r > 0)
374 changed = true;
375 }
376 }
377 }
378
379 /* After we descended, also patch the directory itself. It's key to do this in this order so that the top-level
380 * directory is patched as very last object in the tree, so that we can use it as quick indicator whether the
381 * tree is properly chown()ed already. */
382 r = patch_fd(d ? dirfd(d) : fd, NULL, st, shift);
383 if (r == -EROFS)
384 goto read_only;
385 if (r > 0)
386 changed = true;
387
388 r = changed;
389 goto finish;
390
391 read_only:
392 if (!is_toplevel) {
393 _cleanup_free_ char *name = NULL;
394
395 /* When we hit a ready-only subtree we simply skip it, but log about it. */
396 (void) fd_get_path(fd, &name);
397 log_debug("Skipping read-only file or directory %s.", strna(name));
398 r = changed;
399 }
400
401 finish:
402 if (donate_fd)
403 safe_close(fd);
404
405 return r;
406 }
407
408 static int fd_patch_uid_internal(int fd, bool donate_fd, uid_t shift, uid_t range) {
409 struct stat st;
410 int r;
411
412 assert(fd >= 0);
413
414 /* Recursively adjusts the UID/GIDs of all files of a directory tree. This is used to automatically fix up an
415 * OS tree to the used user namespace UID range. Note that this automatic adjustment only works for UID ranges
416 * following the concept that the upper 16-bit of a UID identify the container, and the lower 16-bit are the actual
417 * UID within the container. */
418
419 if ((shift & 0xFFFF) != 0) {
420 /* We only support containers where the shift starts at a 2^16 boundary */
421 r = -EOPNOTSUPP;
422 goto finish;
423 }
424
425 if (shift == UID_BUSY_BASE) {
426 r = -EINVAL;
427 goto finish;
428 }
429
430 if (range != 0x10000) {
431 /* We only support containers with 16-bit UID ranges for the patching logic */
432 r = -EOPNOTSUPP;
433 goto finish;
434 }
435
436 if (fstat(fd, &st) < 0) {
437 r = -errno;
438 goto finish;
439 }
440
441 if ((uint32_t) st.st_uid >> 16 != (uint32_t) st.st_gid >> 16) {
442 /* We only support containers where the uid/gid container ID match */
443 r = -EBADE;
444 goto finish;
445 }
446
447 /* Try to detect if the range is already right. Of course, this a pretty drastic optimization, as we assume
448 * that if the top-level dir has the right upper 16-bit assigned, then everything below will have too... */
449 if (((uint32_t) (st.st_uid ^ shift) >> 16) == 0)
450 return 0;
451
452 /* Before we start recursively chowning, mark the top-level dir as "busy" by chowning it to the "busy"
453 * range. Should we be interrupted in the middle of our work, we'll see it owned by this user and will start
454 * chown()ing it again, unconditionally, as the busy UID is not a valid UID we'd everpick for ourselves. */
455
456 if ((st.st_uid & UID_BUSY_MASK) != UID_BUSY_BASE) {
457 if (fchown(fd,
458 UID_BUSY_BASE | (st.st_uid & ~UID_BUSY_MASK),
459 (gid_t) UID_BUSY_BASE | (st.st_gid & ~(gid_t) UID_BUSY_MASK)) < 0) {
460 r = -errno;
461 goto finish;
462 }
463 }
464
465 return recurse_fd(fd, donate_fd, &st, shift, true);
466
467 finish:
468 if (donate_fd)
469 safe_close(fd);
470
471 return r;
472 }
473
474 int path_patch_uid(const char *path, uid_t shift, uid_t range) {
475 int fd;
476
477 fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
478 if (fd < 0)
479 return -errno;
480
481 return fd_patch_uid_internal(fd, true, shift, range);
482 }