]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/conf.c
conf: move file descriptor synchronization with parent into single function
[mirror_lxc.git] / src / lxc / conf.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <arpa/inet.h>
7 #include <dirent.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <grp.h>
11 #include <inttypes.h>
12 #include <libgen.h>
13 #include <linux/loop.h>
14 #include <net/if.h>
15 #include <netinet/in.h>
16 #include <pwd.h>
17 #include <stdarg.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/mman.h>
23 #include <sys/mount.h>
24 #include <sys/param.h>
25 #include <sys/prctl.h>
26 #include <sys/sendfile.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include <sys/syscall.h>
30 #include <sys/sysmacros.h>
31 #include <sys/types.h>
32 #include <sys/utsname.h>
33 #include <sys/wait.h>
34 #include <time.h>
35 #include <unistd.h>
36
37 #include "af_unix.h"
38 #include "caps.h"
39 #include "cgroups/cgroup.h"
40 #include "compiler.h"
41 #include "conf.h"
42 #include "config.h"
43 #include "confile.h"
44 #include "confile_utils.h"
45 #include "error.h"
46 #include "log.h"
47 #include "lsm/lsm.h"
48 #include "lxclock.h"
49 #include "lxcseccomp.h"
50 #include "macro.h"
51 #include "memory_utils.h"
52 #include "mount_utils.h"
53 #include "namespace.h"
54 #include "network.h"
55 #include "parse.h"
56 #include "process_utils.h"
57 #include "ringbuf.h"
58 #include "start.h"
59 #include "storage/storage.h"
60 #include "storage/overlay.h"
61 #include "sync.h"
62 #include "syscall_wrappers.h"
63 #include "terminal.h"
64 #include "utils.h"
65 #include "uuid.h"
66
67 #ifdef MAJOR_IN_MKDEV
68 #include <sys/mkdev.h>
69 #endif
70
71 #ifdef HAVE_STATVFS
72 #include <sys/statvfs.h>
73 #endif
74
75 #if HAVE_OPENPTY
76 #include <pty.h>
77 #else
78 #include <../include/openpty.h>
79 #endif
80
81 #if HAVE_LIBCAP
82 #include <sys/capability.h>
83 #endif
84
85 #ifndef HAVE_STRLCAT
86 #include "include/strlcat.h"
87 #endif
88
89 #if IS_BIONIC
90 #include <../include/lxcmntent.h>
91 #else
92 #include <mntent.h>
93 #endif
94
95 #if !defined(HAVE_PRLIMIT) && defined(HAVE_PRLIMIT64)
96 #include <../include/prlimit.h>
97 #endif
98
99 #ifndef HAVE_STRLCPY
100 #include "include/strlcpy.h"
101 #endif
102
103 #ifndef HAVE_STRCHRNUL
104 #include "include/strchrnul.h"
105 #endif
106
107 lxc_log_define(conf, lxc);
108
109 /*
110 * The lxc_conf of the container currently being worked on in an API call.
111 * This is used in the error calls.
112 */
113 thread_local struct lxc_conf *current_config;
114
115 char *lxchook_names[NUM_LXC_HOOKS] = {
116 "pre-start",
117 "pre-mount",
118 "mount",
119 "autodev",
120 "start",
121 "stop",
122 "post-stop",
123 "clone",
124 "destroy",
125 "start-host"
126 };
127
128 struct mount_opt {
129 char *name;
130 int clear;
131 bool recursive;
132 __u64 flag;
133 int legacy_flag;
134 };
135
136 struct caps_opt {
137 char *name;
138 int value;
139 };
140
141 struct limit_opt {
142 char *name;
143 int value;
144 };
145
146 static struct mount_opt mount_opt[] = {
147 { "atime", 1, false, MOUNT_ATTR_NOATIME, MS_NOATIME },
148 { "dev", 1, false, MOUNT_ATTR_NODEV, MS_NODEV },
149 { "diratime", 1, false, MOUNT_ATTR_NODIRATIME, MS_NODIRATIME },
150 { "exec", 1, false, MOUNT_ATTR_NOEXEC, MS_NOEXEC },
151 { "noatime", 0, false, MOUNT_ATTR_NOATIME, MS_NOATIME },
152 { "nodev", 0, false, MOUNT_ATTR_NODEV, MS_NODEV },
153 { "nodiratime", 0, false, MOUNT_ATTR_NODIRATIME, MS_NODIRATIME },
154 { "noexec", 0, false, MOUNT_ATTR_NOEXEC, MS_NOEXEC },
155 { "norelatime", 1, false, MOUNT_ATTR_RELATIME, MS_RELATIME },
156 { "nostrictatime", 1, false, MOUNT_ATTR_STRICTATIME, MS_STRICTATIME },
157 { "nosuid", 0, false, MOUNT_ATTR_NOSUID, MS_NOSUID },
158 { "relatime", 0, false, MOUNT_ATTR_RELATIME, MS_RELATIME },
159 { "ro", 0, false, MOUNT_ATTR_RDONLY, MS_RDONLY },
160 { "rw", 1, false, MOUNT_ATTR_RDONLY, MS_RDONLY },
161 { "strictatime", 0, false, MOUNT_ATTR_STRICTATIME, MS_STRICTATIME },
162 { "suid", 1, false, MOUNT_ATTR_NOSUID, MS_NOSUID },
163
164 { "bind", 0, false, 0, MS_BIND },
165 { "defaults", 0, false, 0, 0 },
166 { "rbind", 0, true, 0, MS_BIND | MS_REC },
167
168 { "sync", 0, false, ~0, MS_SYNCHRONOUS },
169 { "async", 1, false, ~0, MS_SYNCHRONOUS },
170 { "dirsync", 0, false, ~0, MS_DIRSYNC },
171 { "lazytime", 0, false, ~0, MS_LAZYTIME },
172 { "mand", 0, false, ~0, MS_MANDLOCK },
173 { "nomand", 1, false, ~0, MS_MANDLOCK },
174 { "remount", 0, false, ~0, MS_REMOUNT },
175
176 { NULL, 0, false, ~0, ~0 },
177 };
178
179 static struct mount_opt propagation_opt[] = {
180 { "private", 0, false, MS_PRIVATE, MS_PRIVATE },
181 { "shared", 0, false, MS_SHARED, MS_SHARED },
182 { "slave", 0, false, MS_SLAVE, MS_SLAVE },
183 { "unbindable", 0, false, MS_UNBINDABLE, MS_UNBINDABLE },
184 { "rprivate", 0, true, MS_PRIVATE, MS_PRIVATE | MS_REC },
185 { "rshared", 0, true, MS_SHARED, MS_SHARED | MS_REC },
186 { "rslave", 0, true, MS_SLAVE, MS_SLAVE | MS_REC },
187 { "runbindable", 0, true, MS_UNBINDABLE, MS_UNBINDABLE | MS_REC },
188 { NULL, 0, 0 },
189 };
190
191 static struct caps_opt caps_opt[] = {
192 #if HAVE_LIBCAP
193 { "chown", CAP_CHOWN },
194 { "dac_override", CAP_DAC_OVERRIDE },
195 { "dac_read_search", CAP_DAC_READ_SEARCH },
196 { "fowner", CAP_FOWNER },
197 { "fsetid", CAP_FSETID },
198 { "kill", CAP_KILL },
199 { "setgid", CAP_SETGID },
200 { "setuid", CAP_SETUID },
201 { "setpcap", CAP_SETPCAP },
202 { "linux_immutable", CAP_LINUX_IMMUTABLE },
203 { "net_bind_service", CAP_NET_BIND_SERVICE },
204 { "net_broadcast", CAP_NET_BROADCAST },
205 { "net_admin", CAP_NET_ADMIN },
206 { "net_raw", CAP_NET_RAW },
207 { "ipc_lock", CAP_IPC_LOCK },
208 { "ipc_owner", CAP_IPC_OWNER },
209 { "sys_module", CAP_SYS_MODULE },
210 { "sys_rawio", CAP_SYS_RAWIO },
211 { "sys_chroot", CAP_SYS_CHROOT },
212 { "sys_ptrace", CAP_SYS_PTRACE },
213 { "sys_pacct", CAP_SYS_PACCT },
214 { "sys_admin", CAP_SYS_ADMIN },
215 { "sys_boot", CAP_SYS_BOOT },
216 { "sys_nice", CAP_SYS_NICE },
217 { "sys_resource", CAP_SYS_RESOURCE },
218 { "sys_time", CAP_SYS_TIME },
219 { "sys_tty_config", CAP_SYS_TTY_CONFIG },
220 { "mknod", CAP_MKNOD },
221 { "lease", CAP_LEASE },
222 { "audit_write", CAP_AUDIT_WRITE },
223 { "audit_control", CAP_AUDIT_CONTROL },
224 { "setfcap", CAP_SETFCAP },
225 { "mac_override", CAP_MAC_OVERRIDE },
226 { "mac_admin", CAP_MAC_ADMIN },
227 { "syslog", CAP_SYSLOG },
228 { "wake_alarm", CAP_WAKE_ALARM },
229 { "block_suspend", CAP_BLOCK_SUSPEND },
230 { "audit_read", CAP_AUDIT_READ },
231 { "perfmon", CAP_PERFMON },
232 { "bpf", CAP_BPF },
233 { "checkpoint_restore", CAP_CHECKPOINT_RESTORE },
234 #endif
235 };
236
237 static struct limit_opt limit_opt[] = {
238 #ifdef RLIMIT_AS
239 { "as", RLIMIT_AS },
240 #endif
241 #ifdef RLIMIT_CORE
242 { "core", RLIMIT_CORE },
243 #endif
244 #ifdef RLIMIT_CPU
245 { "cpu", RLIMIT_CPU },
246 #endif
247 #ifdef RLIMIT_DATA
248 { "data", RLIMIT_DATA },
249 #endif
250 #ifdef RLIMIT_FSIZE
251 { "fsize", RLIMIT_FSIZE },
252 #endif
253 #ifdef RLIMIT_LOCKS
254 { "locks", RLIMIT_LOCKS },
255 #endif
256 #ifdef RLIMIT_MEMLOCK
257 { "memlock", RLIMIT_MEMLOCK },
258 #endif
259 #ifdef RLIMIT_MSGQUEUE
260 { "msgqueue", RLIMIT_MSGQUEUE },
261 #endif
262 #ifdef RLIMIT_NICE
263 { "nice", RLIMIT_NICE },
264 #endif
265 #ifdef RLIMIT_NOFILE
266 { "nofile", RLIMIT_NOFILE },
267 #endif
268 #ifdef RLIMIT_NPROC
269 { "nproc", RLIMIT_NPROC },
270 #endif
271 #ifdef RLIMIT_RSS
272 { "rss", RLIMIT_RSS },
273 #endif
274 #ifdef RLIMIT_RTPRIO
275 { "rtprio", RLIMIT_RTPRIO },
276 #endif
277 #ifdef RLIMIT_RTTIME
278 { "rttime", RLIMIT_RTTIME },
279 #endif
280 #ifdef RLIMIT_SIGPENDING
281 { "sigpending", RLIMIT_SIGPENDING },
282 #endif
283 #ifdef RLIMIT_STACK
284 { "stack", RLIMIT_STACK },
285 #endif
286 };
287
288 static int run_buffer(char *buffer)
289 {
290 __do_free char *output = NULL;
291 __do_lxc_pclose struct lxc_popen_FILE *f = NULL;
292 int fd, ret;
293
294 f = lxc_popen(buffer);
295 if (!f)
296 return log_error_errno(-1, errno, "Failed to popen() %s", buffer);
297
298 output = zalloc(LXC_LOG_BUFFER_SIZE);
299 if (!output)
300 return log_error_errno(-1, ENOMEM, "Failed to allocate memory for %s", buffer);
301
302 fd = fileno(f->f);
303 if (fd < 0)
304 return log_error_errno(-1, errno, "Failed to retrieve underlying file descriptor");
305
306 for (int i = 0; i < 10; i++) {
307 ssize_t bytes_read;
308
309 bytes_read = lxc_read_nointr(fd, output, LXC_LOG_BUFFER_SIZE - 1);
310 if (bytes_read > 0) {
311 output[bytes_read] = '\0';
312 DEBUG("Script %s produced output: %s", buffer, output);
313 continue;
314 }
315
316 break;
317 }
318
319 ret = lxc_pclose(move_ptr(f));
320 if (ret == -1)
321 return log_error_errno(-1, errno, "Script exited with error");
322 else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0)
323 return log_error(-1, "Script exited with status %d", WEXITSTATUS(ret));
324 else if (WIFSIGNALED(ret))
325 return log_error(-1, "Script terminated by signal %d", WTERMSIG(ret));
326
327 return 0;
328 }
329
330 int run_script_argv(const char *name, unsigned int hook_version,
331 const char *section, const char *script,
332 const char *hookname, char **argv)
333 {
334 __do_free char *buffer = NULL;
335 int buf_pos, i, ret;
336 size_t size = 0;
337
338 if (hook_version == 0)
339 INFO("Executing script \"%s\" for container \"%s\", config section \"%s\"",
340 script, name, section);
341 else
342 INFO("Executing script \"%s\" for container \"%s\"", script, name);
343
344 for (i = 0; argv && argv[i]; i++)
345 size += strlen(argv[i]) + 1;
346
347 size += STRLITERALLEN("exec");
348 size++;
349 size += strlen(script);
350 size++;
351
352 if (size > INT_MAX)
353 return -EFBIG;
354
355 if (hook_version == 0) {
356 size += strlen(hookname);
357 size++;
358
359 size += strlen(name);
360 size++;
361
362 size += strlen(section);
363 size++;
364
365 if (size > INT_MAX)
366 return -EFBIG;
367 }
368
369 buffer = zalloc(size);
370 if (!buffer)
371 return -ENOMEM;
372
373 if (hook_version == 0)
374 buf_pos = strnprintf(buffer, size, "exec %s %s %s %s", script, name, section, hookname);
375 else
376 buf_pos = strnprintf(buffer, size, "exec %s", script);
377 if (buf_pos < 0)
378 return log_error_errno(-1, errno, "Failed to create command line for script \"%s\"", script);
379
380 if (hook_version == 1) {
381 ret = setenv("LXC_HOOK_TYPE", hookname, 1);
382 if (ret < 0) {
383 return log_error_errno(-1, errno, "Failed to set environment variable: LXC_HOOK_TYPE=%s", hookname);
384 }
385 TRACE("Set environment variable: LXC_HOOK_TYPE=%s", hookname);
386
387 ret = setenv("LXC_HOOK_SECTION", section, 1);
388 if (ret < 0)
389 return log_error_errno(-1, errno, "Failed to set environment variable: LXC_HOOK_SECTION=%s", section);
390 TRACE("Set environment variable: LXC_HOOK_SECTION=%s", section);
391
392 if (strequal(section, "net")) {
393 char *parent;
394
395 if (!argv || !argv[0])
396 return -1;
397
398 ret = setenv("LXC_NET_TYPE", argv[0], 1);
399 if (ret < 0)
400 return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_TYPE=%s", argv[0]);
401 TRACE("Set environment variable: LXC_NET_TYPE=%s", argv[0]);
402
403 parent = argv[1] ? argv[1] : "";
404
405 if (strequal(argv[0], "macvlan")) {
406 ret = setenv("LXC_NET_PARENT", parent, 1);
407 if (ret < 0)
408 return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PARENT=%s", parent);
409 TRACE("Set environment variable: LXC_NET_PARENT=%s", parent);
410 } else if (strequal(argv[0], "phys")) {
411 ret = setenv("LXC_NET_PARENT", parent, 1);
412 if (ret < 0)
413 return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PARENT=%s", parent);
414 TRACE("Set environment variable: LXC_NET_PARENT=%s", parent);
415 } else if (strequal(argv[0], "veth")) {
416 char *peer = argv[2] ? argv[2] : "";
417
418 ret = setenv("LXC_NET_PEER", peer, 1);
419 if (ret < 0)
420 return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PEER=%s", peer);
421 TRACE("Set environment variable: LXC_NET_PEER=%s", peer);
422
423 ret = setenv("LXC_NET_PARENT", parent, 1);
424 if (ret < 0)
425 return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PARENT=%s", parent);
426 TRACE("Set environment variable: LXC_NET_PARENT=%s", parent);
427 }
428 }
429 }
430
431 for (i = 0; argv && argv[i]; i++) {
432 size_t len = size - buf_pos;
433
434 ret = strnprintf(buffer + buf_pos, len, " %s", argv[i]);
435 if (ret < 0)
436 return log_error_errno(-1, errno, "Failed to create command line for script \"%s\"", script);
437 buf_pos += ret;
438 }
439
440 return run_buffer(buffer);
441 }
442
443 int run_script(const char *name, const char *section, const char *script, ...)
444 {
445 __do_free char *buffer = NULL;
446 int ret;
447 char *p;
448 va_list ap;
449 size_t size = 0;
450
451 INFO("Executing script \"%s\" for container \"%s\", config section \"%s\"",
452 script, name, section);
453
454 va_start(ap, script);
455 while ((p = va_arg(ap, char *)))
456 size += strlen(p) + 1;
457 va_end(ap);
458
459 size += STRLITERALLEN("exec");
460 size += strlen(script);
461 size += strlen(name);
462 size += strlen(section);
463 size += 4;
464
465 if (size > INT_MAX)
466 return -1;
467
468 buffer = must_realloc(NULL, size);
469 ret = strnprintf(buffer, size, "exec %s %s %s", script, name, section);
470 if (ret < 0)
471 return -1;
472
473 va_start(ap, script);
474 while ((p = va_arg(ap, char *))) {
475 int len = size - ret;
476 int rc;
477 rc = strnprintf(buffer + ret, len, " %s", p);
478 if (rc < 0) {
479 va_end(ap);
480 return -1;
481 }
482 ret += rc;
483 }
484 va_end(ap);
485
486 return run_buffer(buffer);
487 }
488
489 int lxc_storage_prepare(struct lxc_conf *conf)
490 {
491 int ret;
492 struct lxc_rootfs *rootfs = &conf->rootfs;
493
494 if (!rootfs->path) {
495 ret = mount("", "/", NULL, MS_SLAVE | MS_REC, 0);
496 if (ret < 0)
497 return log_error_errno(-1, errno, "Failed to recursively turn root mount tree into dependent mount");
498
499 rootfs->dfd_mnt = open_at(-EBADF, "/", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE, 0);
500 if (rootfs->dfd_mnt < 0)
501 return -errno;
502
503 return 0;
504 }
505
506 ret = access(rootfs->mount, F_OK);
507 if (ret != 0)
508 return log_error_errno(-1, errno, "Failed to access to \"%s\". Check it is present",
509 rootfs->mount);
510
511 rootfs->storage = storage_init(conf);
512 if (!rootfs->storage)
513 return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"",
514 rootfs->path, rootfs->mount,
515 rootfs->options ? rootfs->options : "(null)");
516
517 return 0;
518 }
519
520 void lxc_storage_put(struct lxc_conf *conf)
521 {
522 storage_put(conf->rootfs.storage);
523 conf->rootfs.storage = NULL;
524 }
525
526 /* lxc_rootfs_prepare
527 * if rootfs is a directory, then open ${rootfs}/.lxc-keep for writing for
528 * the duration of the container run, to prevent the container from marking
529 * the underlying fs readonly on shutdown. unlink the file immediately so
530 * no name pollution is happens.
531 * don't unlink on NFS to avoid random named stale handles.
532 */
533 int lxc_rootfs_init(struct lxc_conf *conf, bool userns)
534 {
535 __do_close int dfd_path = -EBADF, fd_pin = -EBADF;
536 int ret;
537 struct stat st;
538 struct statfs stfs;
539 struct lxc_rootfs *rootfs = &conf->rootfs;
540
541 ret = lxc_storage_prepare(conf);
542 if (ret)
543 return syserror_set(-EINVAL, "Failed to prepare rootfs storage");
544
545 if (!is_empty_string(rootfs->mnt_opts.userns_path)) {
546 if (!rootfs->path)
547 return syserror_set(-EINVAL, "Idmapped rootfs currently only supported with separate rootfs for container");
548
549 if (rootfs->bdev_type && !strequal(rootfs->bdev_type, "dir"))
550 return syserror_set(-EINVAL, "Idmapped rootfs currently only supports the \"dir\" storage driver");
551 }
552
553 if (!rootfs->path)
554 return log_trace(0, "Not pinning because container does not have a rootfs");
555
556 if (userns)
557 return log_trace(0, "Not pinning because container runs in user namespace");
558
559 if (rootfs->bdev_type) {
560 if (strequal(rootfs->bdev_type, "overlay") ||
561 strequal(rootfs->bdev_type, "overlayfs"))
562 return log_trace_errno(0, EINVAL, "Not pinning on stacking filesystem");
563
564 if (strequal(rootfs->bdev_type, "zfs"))
565 return log_trace_errno(0, EINVAL, "Not pinning on ZFS filesystem");
566 }
567
568 dfd_path = open_at(-EBADF, rootfs->path, PROTECT_OPATH_FILE, 0, 0);
569 if (dfd_path < 0)
570 return syserror("Failed to open \"%s\"", rootfs->path);
571
572 ret = fstat(dfd_path, &st);
573 if (ret < 0)
574 return log_trace_errno(-errno, errno, "Failed to retrieve file status");
575
576 if (!S_ISDIR(st.st_mode))
577 return log_trace_errno(0, ENOTDIR, "Not pinning because file descriptor is not a directory");
578
579 fd_pin = open_at(dfd_path, ".lxc_keep",
580 PROTECT_OPEN | O_CREAT,
581 PROTECT_LOOKUP_BENEATH,
582 S_IWUSR | S_IRUSR);
583 if (fd_pin < 0) {
584 if (errno == EROFS) {
585 return log_trace_errno(0, EROFS, "Not pinning on read-only filesystem");
586 }
587 return syserror("Failed to pin rootfs");
588 }
589
590 TRACE("Pinned rootfs %d(.lxc_keep)", fd_pin);
591
592 ret = fstatfs(fd_pin, &stfs);
593 if (ret < 0) {
594 SYSWARN("Failed to retrieve filesystem status");
595 goto out;
596 }
597
598 if (stfs.f_type == NFS_SUPER_MAGIC) {
599 DEBUG("Not unlinking pinned file on NFS");
600 goto out;
601 }
602
603 if (unlinkat(dfd_path, ".lxc_keep", 0))
604 SYSTRACE("Failed to unlink rootfs pinning file %d(.lxc_keep)", dfd_path);
605 else
606 TRACE("Unlinked pinned file %d(.lxc_keep)", dfd_path);
607
608 out:
609 rootfs->fd_path_pin = move_fd(fd_pin);
610 return 0;
611 }
612
613 int lxc_rootfs_prepare_parent(struct lxc_handler *handler)
614 {
615 __do_close int dfd_idmapped = -EBADF, fd_userns = -EBADF;
616 struct lxc_rootfs *rootfs = &handler->conf->rootfs;
617 struct lxc_storage *storage = rootfs->storage;
618 int ret;
619 const char *path_source;
620
621 if (lxc_list_empty(&handler->conf->id_map))
622 return 0;
623
624 if (is_empty_string(rootfs->mnt_opts.userns_path))
625 return 0;
626
627 if (handler->conf->rootfs_setup)
628 return 0;
629
630 if (rootfs_is_blockdev(handler->conf))
631 return syserror_set(-EOPNOTSUPP, "Idmapped mounts on block-backed storage not yet supported");
632
633 if (!can_use_bind_mounts())
634 return syserror_set(-EOPNOTSUPP, "Kernel does not support the new mount api");
635
636 if (strequal(rootfs->mnt_opts.userns_path, "container"))
637 fd_userns = dup_cloexec(handler->nsfd[LXC_NS_USER]);
638 else
639 fd_userns = open_at(-EBADF, rootfs->mnt_opts.userns_path,
640 PROTECT_OPEN_WITH_TRAILING_SYMLINKS, 0, 0);
641 if (fd_userns < 0)
642 return syserror("Failed to open user namespace");
643
644 path_source = lxc_storage_get_path(storage->src, storage->type);
645
646 dfd_idmapped = create_detached_idmapped_mount(path_source, fd_userns, true);
647 if (dfd_idmapped < 0)
648 return syserror("Failed to create detached idmapped mount");
649
650 ret = lxc_abstract_unix_send_fds(handler->data_sock[0], &dfd_idmapped, 1, NULL, 0);
651 if (ret < 0)
652 return syserror("Failed to send detached idmapped mount fd");
653
654 TRACE("Created detached idmapped mount %d", dfd_idmapped);
655 return 0;
656 }
657
658 static int add_shmount_to_list(struct lxc_conf *conf)
659 {
660 char new_mount[PATH_MAX];
661 /* Offset for the leading '/' since the path_cont
662 * is absolute inside the container.
663 */
664 int offset = 1, ret = -1;
665
666 ret = strnprintf(new_mount, sizeof(new_mount),
667 "%s %s none bind,create=dir 0 0", conf->shmount.path_host,
668 conf->shmount.path_cont + offset);
669 if (ret < 0)
670 return -1;
671
672 return add_elem_to_mount_list(new_mount, conf);
673 }
674
675 static int lxc_mount_auto_mounts(struct lxc_handler *handler, int flags)
676 {
677 int i, ret;
678 static struct {
679 int match_mask;
680 int match_flag;
681 const char *source;
682 const char *destination;
683 const char *fstype;
684 unsigned long flags;
685 const char *options;
686 bool requires_cap_net_admin;
687 } default_mounts[] = {
688 /* Read-only bind-mounting... In older kernels, doing that
689 * required to do one MS_BIND mount and then
690 * MS_REMOUNT|MS_RDONLY the same one. According to mount(2)
691 * manpage, MS_BIND honors MS_RDONLY from kernel 2.6.26
692 * onwards. However, this apparently does not work on kernel
693 * 3.8. Unfortunately, on that very same kernel, doing the same
694 * trick as above doesn't seem to work either, there one needs
695 * to ALSO specify MS_BIND for the remount, otherwise the
696 * entire fs is remounted read-only or the mount fails because
697 * it's busy... MS_REMOUNT|MS_BIND|MS_RDONLY seems to work for
698 * kernels as low as 2.6.32...
699 */
700 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL, false },
701 /* proc/tty is used as a temporary placeholder for proc/sys/net which we'll move back in a few steps */
702 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys/net", "%r/proc/tty", NULL, MS_BIND, NULL, true },
703 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys", "%r/proc/sys", NULL, MS_BIND, NULL, false },
704 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL, false },
705 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/tty", "%r/proc/sys/net", NULL, MS_MOVE, NULL, true },
706 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sysrq-trigger", "%r/proc/sysrq-trigger", NULL, MS_BIND, NULL, false },
707 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sysrq-trigger", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL, false },
708 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL, false },
709 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW, "sysfs", "%r/sys", "sysfs", 0, NULL, false },
710 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO, "sysfs", "%r/sys", "sysfs", MS_RDONLY, NULL, false },
711 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys", "sysfs", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL, false },
712 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/sys", "%r/sys", NULL, MS_BIND, NULL, false },
713 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, NULL, "%r/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL, false },
714 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys/devices/virtual/net", "sysfs", 0, NULL, false },
715 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/sys/devices/virtual/net/devices/virtual/net", "%r/sys/devices/virtual/net", NULL, MS_BIND, NULL, false },
716 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, NULL, "%r/sys/devices/virtual/net", NULL, MS_REMOUNT|MS_BIND|MS_NOSUID|MS_NODEV|MS_NOEXEC, NULL, false },
717 { 0, 0, NULL, NULL, NULL, 0, NULL, false }
718 };
719 struct lxc_conf *conf = handler->conf;
720 struct lxc_rootfs *rootfs = &conf->rootfs;
721 bool has_cap_net_admin;
722
723 if (flags & LXC_AUTO_PROC_MASK) {
724 if (rootfs->path) {
725 /*
726 * Only unmount procfs if we have a separate rootfs so
727 * we can still access it in safe_mount() below.
728 */
729 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/proc",
730 rootfs->path ? rootfs->mount : "");
731 if (ret < 0)
732 return ret_errno(EIO);
733
734 ret = umount2(rootfs->buf, MNT_DETACH);
735 if (ret)
736 SYSDEBUG("Tried to ensure procfs is unmounted");
737 }
738
739 ret = mkdirat(rootfs->dfd_mnt, "proc" , S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
740 if (ret < 0 && errno != EEXIST)
741 return syserror("Failed to create procfs mountpoint under %d", rootfs->dfd_mnt);
742
743 TRACE("Created procfs mountpoint under %d", rootfs->dfd_mnt);
744 }
745
746 if (flags & LXC_AUTO_SYS_MASK) {
747 if (rootfs->path) {
748 /*
749 * Only unmount sysfs if we have a separate rootfs so
750 * we can still access it in safe_mount() below.
751 */
752 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/sys",
753 rootfs->path ? rootfs->mount : "");
754 if (ret < 0)
755 return ret_errno(EIO);
756
757 ret = umount2(rootfs->buf, MNT_DETACH);
758 if (ret)
759 SYSDEBUG("Tried to ensure sysfs is unmounted");
760 }
761
762 ret = mkdirat(rootfs->dfd_mnt, "sys" , S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
763 if (ret < 0 && errno != EEXIST)
764 return syserror("Failed to create sysfs mountpoint under %d", rootfs->dfd_mnt);
765
766 TRACE("Created sysfs mountpoint under %d", rootfs->dfd_mnt);
767 }
768
769 has_cap_net_admin = lxc_wants_cap(CAP_NET_ADMIN, conf);
770 for (i = 0; default_mounts[i].match_mask; i++) {
771 __do_free char *destination = NULL, *source = NULL;
772 unsigned long mflags;
773
774 if ((flags & default_mounts[i].match_mask) != default_mounts[i].match_flag)
775 continue;
776
777 if (default_mounts[i].source) {
778 /* will act like strdup if %r is not present */
779 source = lxc_string_replace("%r", rootfs->path ? rootfs->mount : "", default_mounts[i].source);
780 if (!source)
781 return syserror_set(-ENOMEM, "Failed to create source path");
782 }
783
784 if (!default_mounts[i].destination)
785 return syserror_set(-EINVAL, "BUG: auto mounts destination %d was NULL", i);
786
787 if (!has_cap_net_admin && default_mounts[i].requires_cap_net_admin) {
788 TRACE("Container does not have CAP_NET_ADMIN. Skipping \"%s\" mount", default_mounts[i].source ?: "(null)");
789 continue;
790 }
791
792 /* will act like strdup if %r is not present */
793 destination = lxc_string_replace("%r", rootfs->path ? rootfs->mount : "", default_mounts[i].destination);
794 if (!destination)
795 return syserror_set(-ENOMEM, "Failed to create target path");
796
797 mflags = add_required_remount_flags(source, destination,
798 default_mounts[i].flags);
799 ret = safe_mount(source, destination, default_mounts[i].fstype,
800 mflags, default_mounts[i].options,
801 rootfs->path ? rootfs->mount : NULL);
802 if (ret < 0) {
803 if (errno != ENOENT)
804 return syserror("Failed to mount \"%s\" on \"%s\" with flags %lu", source, destination, mflags);
805
806 INFO("Mount source or target for \"%s\" on \"%s\" does not exist. Skipping", source, destination);
807 continue;
808 }
809 TRACE("Mounted automount \"%s\" on \"%s\" with flags %lu", source, destination, mflags);
810 }
811
812 if (flags & LXC_AUTO_CGROUP_MASK) {
813 int cg_flags;
814
815 cg_flags = flags & (LXC_AUTO_CGROUP_MASK & ~LXC_AUTO_CGROUP_FORCE);
816 /* If the type of cgroup mount was not specified, it depends on
817 * the container's capabilities as to what makes sense: if we
818 * have CAP_SYS_ADMIN, the read-only part can be remounted
819 * read-write anyway, so we may as well default to read-write;
820 * then the admin will not be given a false sense of security.
821 * (And if they really want mixed r/o r/w, then they can
822 * explicitly specify :mixed.) OTOH, if the container lacks
823 * CAP_SYS_ADMIN, do only default to :mixed, because then the
824 * container can't remount it read-write.
825 */
826 if ((cg_flags == LXC_AUTO_CGROUP_NOSPEC) || (cg_flags == LXC_AUTO_CGROUP_FULL_NOSPEC)) {
827 int has_sys_admin = 0;
828
829 if (!lxc_list_empty(&conf->keepcaps))
830 has_sys_admin = in_caplist(CAP_SYS_ADMIN, &conf->keepcaps);
831 else
832 has_sys_admin = !in_caplist(CAP_SYS_ADMIN, &conf->caps);
833
834 if (cg_flags == LXC_AUTO_CGROUP_NOSPEC)
835 cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_RW : LXC_AUTO_CGROUP_MIXED;
836 else
837 cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_FULL_RW : LXC_AUTO_CGROUP_FULL_MIXED;
838 }
839
840 if (flags & LXC_AUTO_CGROUP_FORCE)
841 cg_flags |= LXC_AUTO_CGROUP_FORCE;
842
843 if (!handler->cgroup_ops->mount(handler->cgroup_ops, handler, cg_flags))
844 return log_error_errno(-1, errno, "Failed to mount \"/sys/fs/cgroup\"");
845 }
846
847 if (flags & LXC_AUTO_SHMOUNTS_MASK) {
848 ret = add_shmount_to_list(conf);
849 if (ret < 0)
850 return log_error(-1, "Failed to add shmount entry to container config");
851 }
852
853 return 0;
854 }
855
856 static int setup_utsname(struct utsname *utsname)
857 {
858 int ret;
859
860 if (!utsname)
861 return 0;
862
863 ret = sethostname(utsname->nodename, strlen(utsname->nodename));
864 if (ret < 0)
865 return log_error_errno(-1, errno, "Failed to set the hostname to \"%s\"",
866 utsname->nodename);
867
868 INFO("Set hostname to \"%s\"", utsname->nodename);
869
870 return 0;
871 }
872
873 struct dev_symlinks {
874 const char *oldpath;
875 const char *name;
876 };
877
878 static const struct dev_symlinks dev_symlinks[] = {
879 { "/proc/self/fd", "fd" },
880 { "/proc/self/fd/0", "stdin" },
881 { "/proc/self/fd/1", "stdout" },
882 { "/proc/self/fd/2", "stderr" },
883 };
884
885 static int lxc_setup_dev_symlinks(const struct lxc_rootfs *rootfs)
886 {
887 for (int i = 0; i < sizeof(dev_symlinks) / sizeof(dev_symlinks[0]); i++) {
888 int ret;
889 struct stat s;
890 const struct dev_symlinks *d = &dev_symlinks[i];
891
892 /*
893 * Stat the path first. If we don't get an error accept it as
894 * is and don't try to create it
895 */
896 ret = fstatat(rootfs->dfd_dev, d->name, &s, 0);
897 if (ret == 0)
898 continue;
899
900 ret = symlinkat(d->oldpath, rootfs->dfd_dev, d->name);
901 if (ret) {
902 switch (errno) {
903 case EROFS:
904 WARN("Failed to create \"%s\" on read-only filesystem", d->name);
905 __fallthrough;
906 case EEXIST:
907 break;
908 default:
909 return log_error_errno(-errno, errno, "Failed to create \"%s\"", d->name);
910 }
911 }
912 }
913
914 return 0;
915 }
916
917 /* Build a space-separate list of ptys to pass to systemd. */
918 static bool append_ttyname(char **pp, char *name)
919 {
920 char *p;
921 size_t size;
922
923 if (!*pp) {
924 *pp = zalloc(strlen(name) + strlen("container_ttys=") + 1);
925 if (!*pp)
926 return false;
927
928 sprintf(*pp, "container_ttys=%s", name);
929 return true;
930 }
931
932 size = strlen(*pp) + strlen(name) + 2;
933 p = realloc(*pp, size);
934 if (!p)
935 return false;
936
937 *pp = p;
938 (void)strlcat(p, " ", size);
939 (void)strlcat(p, name, size);
940
941 return true;
942 }
943
944 static int lxc_setup_ttys(struct lxc_conf *conf)
945 {
946 int ret;
947 struct lxc_rootfs *rootfs = &conf->rootfs;
948 const struct lxc_tty_info *ttys = &conf->ttys;
949 char *ttydir = ttys->dir;
950
951 if (!conf->rootfs.path)
952 return 0;
953
954 for (int i = 0; i < ttys->max; i++) {
955 __do_close int fd_to = -EBADF;
956 struct lxc_terminal_info *tty = &ttys->tty[i];
957
958 if (ttydir) {
959 char *tty_name, *tty_path;
960
961 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf),
962 "/dev/%s/tty%d", ttydir, i + 1);
963 if (ret < 0)
964 return ret_errno(-EIO);
965
966 tty_path = &rootfs->buf[STRLITERALLEN("/dev/")];
967 tty_name = tty_path + strlen(ttydir) + 1;
968
969 /* create bind-mount target */
970 fd_to = open_at(rootfs->dfd_dev, tty_path,
971 PROTECT_OPEN_W | O_CREAT,
972 PROTECT_LOOKUP_BENEATH, 0);
973 if (fd_to < 0)
974 return log_error_errno(-errno, errno,
975 "Failed to create tty mount target %d(%s)",
976 rootfs->dfd_dev, tty_path);
977
978 ret = unlinkat(rootfs->dfd_dev, tty_name, 0);
979 if (ret < 0 && errno != ENOENT)
980 return log_error_errno(-errno, errno,
981 "Failed to unlink %d(%s)",
982 rootfs->dfd_dev, tty_name);
983
984 if (can_use_mount_api()) {
985 ret = fd_bind_mount(tty->pty, "",
986 PROTECT_OPATH_FILE,
987 PROTECT_LOOKUP_BENEATH_XDEV,
988 fd_to, "",
989 PROTECT_OPATH_FILE,
990 PROTECT_LOOKUP_BENEATH_XDEV, 0,
991 false);
992 } else {
993 ret = mount(tty->name, rootfs->buf, "none", MS_BIND, 0);
994 }
995 if (ret < 0)
996 return log_error_errno(-errno, errno,
997 "Failed to bind mount \"%s\" onto \"%s\"",
998 tty->name, rootfs->buf);
999 DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name, rootfs->buf);
1000
1001 ret = symlinkat(tty_path, rootfs->dfd_dev, tty_name);
1002 if (ret < 0)
1003 return log_error_errno(-errno, errno,
1004 "Failed to create symlink \"%d(%s)\" -> \"%d(%s)\"",
1005 rootfs->dfd_dev, tty_name,
1006 rootfs->dfd_dev, tty_path);
1007 } else {
1008 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "tty%d", i + 1);
1009 if (ret < 0)
1010 return ret_errno(-EIO);
1011
1012 /* If we populated /dev, then we need to create /dev/tty<idx>. */
1013 fd_to = open_at(rootfs->dfd_dev, rootfs->buf,
1014 PROTECT_OPEN_W | O_CREAT,
1015 PROTECT_LOOKUP_BENEATH, 0);
1016 if (fd_to < 0)
1017 return log_error_errno(-errno, errno,
1018 "Failed to create tty mount target %d(%s)",
1019 rootfs->dfd_dev, rootfs->buf);
1020
1021 if (can_use_mount_api()) {
1022 ret = fd_bind_mount(tty->pty, "",
1023 PROTECT_OPATH_FILE,
1024 PROTECT_LOOKUP_BENEATH_XDEV,
1025 fd_to, "",
1026 PROTECT_OPATH_FILE,
1027 PROTECT_LOOKUP_BENEATH, 0,
1028 false);
1029 } else {
1030 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "/dev/tty%d", i + 1);
1031 if (ret < 0)
1032 return ret_errno(-EIO);
1033
1034 ret = mount(tty->name, rootfs->buf, "none", MS_BIND, 0);
1035 }
1036 if (ret < 0)
1037 return log_error_errno(-errno, errno,
1038 "Failed to bind mount \"%s\" onto \"%s\"",
1039 tty->name, rootfs->buf);
1040 DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name, rootfs->buf);
1041 }
1042
1043 if (!append_ttyname(&conf->ttys.tty_names, tty->name))
1044 return log_error(-1, "Error setting up container_ttys string");
1045 }
1046
1047 INFO("Finished setting up %zu /dev/tty<N> device(s)", ttys->max);
1048 return 0;
1049 }
1050
1051 define_cleanup_function(struct lxc_tty_info *, lxc_delete_tty);
1052
1053 static int lxc_allocate_ttys(struct lxc_conf *conf)
1054 {
1055 call_cleaner(lxc_delete_tty) struct lxc_tty_info *ttys = &conf->ttys;
1056 int ret;
1057
1058 /* no tty in the configuration */
1059 if (ttys->max == 0)
1060 return 0;
1061
1062 ttys->tty = zalloc(sizeof(struct lxc_terminal_info) * ttys->max);
1063 if (!ttys->tty)
1064 return -ENOMEM;
1065
1066 for (size_t i = 0; i < conf->ttys.max; i++) {
1067 struct lxc_terminal_info *tty = &ttys->tty[i];
1068
1069 tty->ptx = -EBADF;
1070 tty->pty = -EBADF;
1071 ret = openpty(&tty->ptx, &tty->pty, NULL, NULL, NULL);
1072 if (ret < 0) {
1073 conf->ttys.max = i;
1074 return log_error_errno(-ENOTTY, ENOTTY, "Failed to create tty %zu", i);
1075 }
1076
1077 ret = ttyname_r(tty->pty, tty->name, sizeof(tty->name));
1078 if (ret < 0) {
1079 conf->ttys.max = i;
1080 return log_error_errno(-ENOTTY, ENOTTY, "Failed to retrieve name of tty %zu pty", i);
1081 }
1082
1083 DEBUG("Created tty with ptx fd %d and pty fd %d", tty->ptx, tty->pty);
1084
1085 /* Prevent leaking the file descriptors to the container */
1086 ret = fd_cloexec(tty->ptx, true);
1087 if (ret < 0)
1088 SYSWARN("Failed to set FD_CLOEXEC flag on ptx fd %d of tty device \"%s\"",
1089 tty->ptx, tty->name);
1090
1091 ret = fd_cloexec(tty->pty, true);
1092 if (ret < 0)
1093 SYSWARN("Failed to set FD_CLOEXEC flag on pty fd %d of tty device \"%s\"",
1094 tty->pty, tty->name);
1095
1096 tty->busy = -1;
1097 }
1098
1099 INFO("Finished creating %zu tty devices", ttys->max);
1100 move_ptr(ttys);
1101 return 0;
1102 }
1103
1104 void lxc_delete_tty(struct lxc_tty_info *ttys)
1105 {
1106 if (!ttys->tty)
1107 return;
1108
1109 for (int i = 0; i < ttys->max; i++) {
1110 struct lxc_terminal_info *tty = &ttys->tty[i];
1111 close_prot_errno_disarm(tty->ptx);
1112 close_prot_errno_disarm(tty->pty);
1113 }
1114
1115 free_disarm(ttys->tty);
1116 }
1117
1118 static int __lxc_send_ttys_to_parent(struct lxc_handler *handler)
1119 {
1120 int i;
1121 int ret = -1;
1122 struct lxc_conf *conf = handler->conf;
1123 struct lxc_tty_info *ttys = &conf->ttys;
1124 int sock = handler->data_sock[0];
1125
1126 if (ttys->max == 0)
1127 return 0;
1128
1129 for (i = 0; i < ttys->max; i++) {
1130 int ttyfds[2];
1131 struct lxc_terminal_info *tty = &ttys->tty[i];
1132
1133 ttyfds[0] = tty->ptx;
1134 ttyfds[1] = tty->pty;
1135
1136 ret = lxc_abstract_unix_send_fds(sock, ttyfds, 2, NULL, 0);
1137 if (ret < 0)
1138 break;
1139
1140 TRACE("Sent tty \"%s\" with ptx fd %d and pty fd %d to parent",
1141 tty->name, tty->ptx, tty->pty);
1142 }
1143
1144 if (ret < 0)
1145 SYSERROR("Failed to send %zu ttys to parent", ttys->max);
1146 else
1147 TRACE("Sent %zu ttys to parent", ttys->max);
1148
1149 return ret;
1150 }
1151
1152 static int lxc_create_ttys(struct lxc_handler *handler)
1153 {
1154 int ret = -1;
1155 struct lxc_conf *conf = handler->conf;
1156
1157 ret = lxc_allocate_ttys(conf);
1158 if (ret < 0) {
1159 ERROR("Failed to allocate ttys");
1160 goto on_error;
1161 }
1162
1163 if (!conf->is_execute) {
1164 ret = lxc_setup_ttys(conf);
1165 if (ret < 0) {
1166 ERROR("Failed to setup ttys");
1167 goto on_error;
1168 }
1169 }
1170
1171 if (conf->ttys.tty_names) {
1172 ret = setenv("container_ttys", conf->ttys.tty_names, 1);
1173 if (ret < 0) {
1174 SYSERROR("Failed to set \"container_ttys=%s\"", conf->ttys.tty_names);
1175 goto on_error;
1176 }
1177 }
1178
1179 return 0;
1180
1181 on_error:
1182 lxc_delete_tty(&conf->ttys);
1183
1184 return -1;
1185 }
1186
1187 static int lxc_send_ttys_to_parent(struct lxc_handler *handler)
1188 {
1189 int ret = -1;
1190
1191 ret = __lxc_send_ttys_to_parent(handler);
1192 lxc_delete_tty(&handler->conf->ttys);
1193 return ret;
1194 }
1195
1196 /* Just create a path for /dev under $lxcpath/$name and in rootfs If we hit an
1197 * error, log it but don't fail yet.
1198 */
1199 static int mount_autodev(const char *name, const struct lxc_rootfs *rootfs,
1200 int autodevtmpfssize, const char *lxcpath)
1201 {
1202 __do_close int fd_fs = -EBADF;
1203 const char *path = rootfs->path ? rootfs->mount : NULL;
1204 size_t tmpfs_size = (autodevtmpfssize != 0) ? autodevtmpfssize : 500000;
1205 int ret;
1206 mode_t cur_mask;
1207 char mount_options[128];
1208
1209 INFO("Preparing \"/dev\"");
1210
1211 cur_mask = umask(S_IXUSR | S_IXGRP | S_IXOTH);
1212 ret = mkdirat(rootfs->dfd_mnt, "dev" , S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1213 if (ret < 0 && errno != EEXIST) {
1214 SYSERROR("Failed to create \"/dev\" directory");
1215 ret = -errno;
1216 goto reset_umask;
1217 }
1218
1219 if (can_use_mount_api()) {
1220 fd_fs = fs_prepare("tmpfs", -EBADF, "", 0, 0);
1221 if (fd_fs < 0)
1222 return log_error_errno(-errno, errno, "Failed to prepare filesystem context for tmpfs");
1223
1224 sprintf(mount_options, "%zu", tmpfs_size);
1225
1226 ret = fs_set_property(fd_fs, "mode", "0755");
1227 if (ret < 0)
1228 return log_error_errno(-errno, errno, "Failed to mount tmpfs onto %d(dev)", fd_fs);
1229
1230 ret = fs_set_property(fd_fs, "size", mount_options);
1231 if (ret < 0)
1232 return log_error_errno(-errno, errno, "Failed to mount tmpfs onto %d(dev)", fd_fs);
1233
1234 ret = fs_attach(fd_fs, rootfs->dfd_mnt, "dev",
1235 PROTECT_OPATH_DIRECTORY,
1236 PROTECT_LOOKUP_BENEATH_XDEV, 0);
1237 } else {
1238 __do_free char *fallback_path = NULL;
1239
1240 sprintf(mount_options, "size=%zu,mode=755", tmpfs_size);
1241 DEBUG("Using mount options: %s", mount_options);
1242
1243 if (path) {
1244 fallback_path = must_make_path(path, "/dev", NULL);
1245 ret = safe_mount("none", fallback_path, "tmpfs", 0, mount_options, path);
1246 } else {
1247 ret = safe_mount("none", "dev", "tmpfs", 0, mount_options, NULL);
1248 }
1249 }
1250 if (ret < 0) {
1251 SYSERROR("Failed to mount tmpfs on \"%s\"", path);
1252 goto reset_umask;
1253 }
1254
1255 /* If we are running on a devtmpfs mapping, dev/pts may already exist.
1256 * If not, then create it and exit if that fails...
1257 */
1258 ret = mkdirat(rootfs->dfd_mnt, "dev/pts", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1259 if (ret < 0 && errno != EEXIST) {
1260 SYSERROR("Failed to create directory \"dev/pts\"");
1261 ret = -errno;
1262 goto reset_umask;
1263 }
1264
1265 ret = 0;
1266
1267 reset_umask:
1268 (void)umask(cur_mask);
1269
1270 INFO("Prepared \"/dev\"");
1271 return ret;
1272 }
1273
1274 struct lxc_device_node {
1275 const char *name;
1276 const mode_t mode;
1277 const int maj;
1278 const int min;
1279 };
1280
1281 static const struct lxc_device_node lxc_devices[] = {
1282 { "full", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 7 },
1283 { "null", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 3 },
1284 { "random", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 8 },
1285 { "tty", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 5, 0 },
1286 { "urandom", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 9 },
1287 { "zero", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 5 },
1288 };
1289
1290
1291 enum {
1292 LXC_DEVNODE_BIND,
1293 LXC_DEVNODE_MKNOD,
1294 LXC_DEVNODE_PARTIAL,
1295 LXC_DEVNODE_OPEN,
1296 };
1297
1298 static int lxc_fill_autodev(struct lxc_rootfs *rootfs)
1299 {
1300 int i, ret;
1301 mode_t cmask;
1302 int use_mknod = LXC_DEVNODE_MKNOD;
1303
1304 if (rootfs->dfd_dev < 0)
1305 return log_info(0, "No /dev directory found, skipping setup");
1306
1307 INFO("Populating \"/dev\"");
1308
1309 cmask = umask(S_IXUSR | S_IXGRP | S_IXOTH);
1310 for (i = 0; i < sizeof(lxc_devices) / sizeof(lxc_devices[0]); i++) {
1311 const struct lxc_device_node *device = &lxc_devices[i];
1312
1313 if (use_mknod >= LXC_DEVNODE_MKNOD) {
1314 ret = mknodat(rootfs->dfd_dev, device->name, device->mode, makedev(device->maj, device->min));
1315 if (ret == 0 || (ret < 0 && errno == EEXIST)) {
1316 DEBUG("Created device node \"%s\"", device->name);
1317 } else if (ret < 0) {
1318 if (errno != EPERM)
1319 return log_error_errno(-1, errno, "Failed to create device node \"%s\"", device->name);
1320
1321 use_mknod = LXC_DEVNODE_BIND;
1322 }
1323
1324 /* Device nodes are fully useable. */
1325 if (use_mknod == LXC_DEVNODE_OPEN)
1326 continue;
1327
1328 if (use_mknod == LXC_DEVNODE_MKNOD) {
1329 __do_close int fd = -EBADF;
1330 /* See
1331 * - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=55956b59df336f6738da916dbb520b6e37df9fbd
1332 * - https://lists.linuxfoundation.org/pipermail/containers/2018-June/039176.html
1333 */
1334 fd = open_at(rootfs->dfd_dev, device->name, PROTECT_OPEN, PROTECT_LOOKUP_BENEATH, 0);
1335 if (fd >= 0) {
1336 /* Device nodes are fully useable. */
1337 use_mknod = LXC_DEVNODE_OPEN;
1338 continue;
1339 }
1340
1341 SYSTRACE("Failed to open \"%s\" device", device->name);
1342 /* Device nodes are only partially useable. */
1343 use_mknod = LXC_DEVNODE_PARTIAL;
1344 }
1345 }
1346
1347 if (use_mknod != LXC_DEVNODE_PARTIAL) {
1348 /* If we are dealing with partially functional device
1349 * nodes the prio mknod() call will have created the
1350 * device node so we can use it as a bind-mount target.
1351 */
1352 ret = mknodat(rootfs->dfd_dev, device->name, S_IFREG | 0000, 0);
1353 if (ret < 0 && errno != EEXIST)
1354 return log_error_errno(-1, errno, "Failed to create file \"%s\"", device->name);
1355 }
1356
1357 /* Fallback to bind-mounting the device from the host. */
1358 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "dev/%s", device->name);
1359 if (ret < 0)
1360 return ret_errno(EIO);
1361
1362 if (can_use_mount_api()) {
1363 ret = fd_bind_mount(rootfs->dfd_host, rootfs->buf,
1364 PROTECT_OPATH_FILE,
1365 PROTECT_LOOKUP_BENEATH_XDEV,
1366 rootfs->dfd_dev, device->name,
1367 PROTECT_OPATH_FILE,
1368 PROTECT_LOOKUP_BENEATH, 0, false);
1369 } else {
1370 char path[PATH_MAX];
1371
1372 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "/dev/%s", device->name);
1373 if (ret < 0)
1374 return ret_errno(EIO);
1375
1376 ret = strnprintf(path, sizeof(path), "%s/dev/%s", get_rootfs_mnt(rootfs), device->name);
1377 if (ret < 0)
1378 return log_error(-1, "Failed to create device path for %s", device->name);
1379
1380 ret = safe_mount(rootfs->buf, path, 0, MS_BIND, NULL, get_rootfs_mnt(rootfs));
1381 if (ret < 0)
1382 return log_error_errno(-1, errno, "Failed to bind mount host device node \"%s\" to \"%s\"", rootfs->buf, path);
1383
1384 DEBUG("Bind mounted host device node \"%s\" to \"%s\"", rootfs->buf, path);
1385 continue;
1386 }
1387 DEBUG("Bind mounted host device %d(%s) to %d(%s)", rootfs->dfd_host, rootfs->buf, rootfs->dfd_dev, device->name);
1388 }
1389 (void)umask(cmask);
1390
1391 INFO("Populated \"/dev\"");
1392 return 0;
1393 }
1394
1395 static int lxc_mount_rootfs(struct lxc_rootfs *rootfs)
1396 {
1397 int ret;
1398
1399 if (!rootfs->path) {
1400 ret = mount("", "/", NULL, MS_SLAVE | MS_REC, 0);
1401 if (ret < 0)
1402 return log_error_errno(-1, errno, "Failed to recursively turn root mount tree into dependent mount");
1403
1404 rootfs->dfd_mnt = open_at(-EBADF, "/", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE, 0);
1405 if (rootfs->dfd_mnt < 0)
1406 return -errno;
1407
1408 return log_trace(0, "Container doesn't use separate rootfs. Opened host's rootfs");
1409 }
1410
1411 ret = access(rootfs->mount, F_OK);
1412 if (ret != 0)
1413 return log_error_errno(-1, errno, "Failed to access to \"%s\". Check it is present",
1414 rootfs->mount);
1415
1416 ret = rootfs->storage->ops->mount(rootfs->storage);
1417 if (ret < 0)
1418 return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"",
1419 rootfs->path, rootfs->mount,
1420 rootfs->options ? rootfs->options : "(null)");
1421
1422 DEBUG("Mounted rootfs \"%s\" onto \"%s\" with options \"%s\"",
1423 rootfs->path, rootfs->mount,
1424 rootfs->options ? rootfs->options : "(null)");
1425
1426 rootfs->dfd_mnt = open_at(-EBADF, rootfs->mount, PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE_XDEV, 0);
1427 if (rootfs->dfd_mnt < 0)
1428 return -errno;
1429
1430 return log_trace(0, "Container uses separate rootfs. Opened container's rootfs");
1431 }
1432
1433 static int lxc_chroot(const struct lxc_rootfs *rootfs)
1434 {
1435 __do_free char *nroot = NULL;
1436 int i, ret;
1437 char *root = rootfs->mount;
1438
1439 nroot = realpath(root, NULL);
1440 if (!nroot)
1441 return log_error_errno(-1, errno, "Failed to resolve \"%s\"", root);
1442
1443 ret = chdir("/");
1444 if (ret < 0)
1445 return -1;
1446
1447 /* We could use here MS_MOVE, but in userns this mount is locked and
1448 * can't be moved.
1449 */
1450 ret = mount(nroot, "/", NULL, MS_REC | MS_BIND, NULL);
1451 if (ret < 0)
1452 return log_error_errno(-1, errno, "Failed to mount \"%s\" onto \"/\" as MS_REC | MS_BIND", nroot);
1453
1454 ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL);
1455 if (ret < 0)
1456 return log_error_errno(-1, errno, "Failed to remount \"/\"");
1457
1458 /* The following code cleans up inherited mounts which are not required
1459 * for CT.
1460 *
1461 * The mountinfo file shows not all mounts, if a few points have been
1462 * unmounted between read operations from the mountinfo. So we need to
1463 * read mountinfo a few times.
1464 *
1465 * This loop can be skipped if a container uses userns, because all
1466 * inherited mounts are locked and we should live with all this trash.
1467 */
1468 for (;;) {
1469 __do_fclose FILE *f = NULL;
1470 __do_free char *line = NULL;
1471 char *slider1, *slider2;
1472 int progress = 0;
1473 size_t len = 0;
1474
1475 f = fopen("./proc/self/mountinfo", "re");
1476 if (!f)
1477 return log_error_errno(-1, errno, "Failed to open \"/proc/self/mountinfo\"");
1478
1479 while (getline(&line, &len, f) > 0) {
1480 for (slider1 = line, i = 0; slider1 && i < 4; i++)
1481 slider1 = strchr(slider1 + 1, ' ');
1482
1483 if (!slider1)
1484 continue;
1485
1486 slider2 = strchr(slider1 + 1, ' ');
1487 if (!slider2)
1488 continue;
1489
1490 *slider2 = '\0';
1491 *slider1 = '.';
1492
1493 if (strequal(slider1 + 1, "/"))
1494 continue;
1495
1496 if (strequal(slider1 + 1, "/proc"))
1497 continue;
1498
1499 ret = umount2(slider1, MNT_DETACH);
1500 if (ret == 0)
1501 progress++;
1502 }
1503
1504 if (!progress)
1505 break;
1506 }
1507
1508 /* This also can be skipped if a container uses userns. */
1509 (void)umount2("./proc", MNT_DETACH);
1510
1511 /* It is weird, but chdir("..") moves us in a new root */
1512 ret = chdir("..");
1513 if (ret < 0)
1514 return log_error_errno(-1, errno, "Failed to chdir(\"..\")");
1515
1516 ret = chroot(".");
1517 if (ret < 0)
1518 return log_error_errno(-1, errno, "Failed to chroot(\".\")");
1519
1520 return 0;
1521 }
1522
1523 /* (The following explanation is copied verbatim from the kernel.)
1524 *
1525 * pivot_root Semantics:
1526 * Moves the root file system of the current process to the directory put_old,
1527 * makes new_root as the new root file system of the current process, and sets
1528 * root/cwd of all processes which had them on the current root to new_root.
1529 *
1530 * Restrictions:
1531 * The new_root and put_old must be directories, and must not be on the
1532 * same file system as the current process root. The put_old must be
1533 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1534 * pointed to by put_old must yield the same directory as new_root. No other
1535 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1536 *
1537 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
1538 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
1539 * in this situation.
1540 *
1541 * Notes:
1542 * - we don't move root/cwd if they are not at the root (reason: if something
1543 * cared enough to change them, it's probably wrong to force them elsewhere)
1544 * - it's okay to pick a root that isn't the root of a file system, e.g.
1545 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1546 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1547 * first.
1548 */
1549 static int lxc_pivot_root(const struct lxc_rootfs *rootfs)
1550 {
1551 __do_close int fd_oldroot = -EBADF;
1552 int ret;
1553
1554 fd_oldroot = open_at(-EBADF, "/", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE, 0);
1555 if (fd_oldroot < 0)
1556 return log_error_errno(-1, errno, "Failed to open old root directory");
1557
1558 /* change into new root fs */
1559 ret = fchdir(rootfs->dfd_mnt);
1560 if (ret < 0)
1561 return log_error_errno(-errno, errno, "Failed to change into new root directory \"%s\"", rootfs->mount);
1562
1563 /* pivot_root into our new root fs */
1564 ret = pivot_root(".", ".");
1565 if (ret < 0)
1566 return log_error_errno(-errno, errno, "Failed to pivot into new root directory \"%s\"", rootfs->mount);
1567
1568 /* At this point the old-root is mounted on top of our new-root. To
1569 * unmounted it we must not be chdir'd into it, so escape back to
1570 * old-root.
1571 */
1572 ret = fchdir(fd_oldroot);
1573 if (ret < 0)
1574 return log_error_errno(-errno, errno, "Failed to enter old root directory");
1575
1576 /*
1577 * Make fd_oldroot a depedent mount to make sure our umounts don't
1578 * propagate to the host.
1579 */
1580 ret = mount("", ".", "", MS_SLAVE | MS_REC, NULL);
1581 if (ret < 0)
1582 return log_error_errno(-errno, errno, "Failed to recursively turn old root mount tree into dependent mount");
1583
1584 ret = umount2(".", MNT_DETACH);
1585 if (ret < 0)
1586 return log_error_errno(-errno, errno, "Failed to detach old root directory");
1587
1588 ret = fchdir(rootfs->dfd_mnt);
1589 if (ret < 0)
1590 return log_error_errno(-errno, errno, "Failed to re-enter new root directory \"%s\"", rootfs->mount);
1591
1592 TRACE("Changed into new rootfs \"%s\"", rootfs->mount);
1593 return 0;
1594 }
1595
1596 static int lxc_setup_rootfs_switch_root(const struct lxc_rootfs *rootfs)
1597 {
1598 if (!rootfs->path)
1599 return log_debug(0, "Container does not have a rootfs");
1600
1601 if (detect_ramfs_rootfs())
1602 return lxc_chroot(rootfs);
1603
1604 return lxc_pivot_root(rootfs);
1605 }
1606
1607 static const struct id_map *find_mapped_nsid_entry(const struct lxc_conf *conf,
1608 unsigned id,
1609 enum idtype idtype)
1610 {
1611 struct lxc_list *it;
1612 struct id_map *map;
1613 struct id_map *retmap = NULL;
1614
1615 /* Shortcut for container's root mappings. */
1616 if (id == 0) {
1617 if (idtype == ID_TYPE_UID)
1618 return conf->root_nsuid_map;
1619
1620 if (idtype == ID_TYPE_GID)
1621 return conf->root_nsgid_map;
1622 }
1623
1624 lxc_list_for_each(it, &conf->id_map) {
1625 map = it->elem;
1626 if (map->idtype != idtype)
1627 continue;
1628
1629 if (id >= map->nsid && id < map->nsid + map->range) {
1630 retmap = map;
1631 break;
1632 }
1633 }
1634
1635 return retmap;
1636 }
1637
1638 static int lxc_setup_devpts_parent(struct lxc_handler *handler)
1639 {
1640 int ret;
1641
1642 if (handler->conf->pty_max <= 0)
1643 return 0;
1644
1645 ret = lxc_abstract_unix_recv_one_fd(handler->data_sock[1],
1646 &handler->conf->devpts_fd,
1647 &handler->conf->devpts_fd,
1648 sizeof(handler->conf->devpts_fd));
1649 if (ret < 0)
1650 return log_error_errno(-1, errno, "Failed to receive devpts fd from child");
1651
1652 TRACE("Received devpts file descriptor %d from child", handler->conf->devpts_fd);
1653 return 0;
1654 }
1655
1656 static int lxc_setup_devpts_child(struct lxc_handler *handler)
1657 {
1658 __do_close int devpts_fd = -EBADF;
1659 int ret;
1660 char **opts;
1661 char devpts_mntopts[256];
1662 char *mntopt_sets[5];
1663 char default_devpts_mntopts[256] = "gid=5,newinstance,ptmxmode=0666,mode=0620";
1664 struct lxc_conf *conf = handler->conf;
1665 struct lxc_rootfs *rootfs = &conf->rootfs;
1666
1667 if (conf->pty_max <= 0)
1668 return log_debug(0, "No new devpts instance will be mounted since no pts devices are requested");
1669
1670 ret = strnprintf(devpts_mntopts, sizeof(devpts_mntopts), "%s,max=%zu",
1671 default_devpts_mntopts, conf->pty_max);
1672 if (ret < 0)
1673 return -1;
1674
1675 (void)umount2("/dev/pts", MNT_DETACH);
1676
1677 /* Create mountpoint for devpts instance. */
1678 ret = mkdirat(rootfs->dfd_dev, "pts", 0755);
1679 if (ret < 0 && errno != EEXIST)
1680 return log_error_errno(-1, errno, "Failed to create \"/dev/pts\" directory");
1681
1682 /* gid=5 && max= */
1683 mntopt_sets[0] = devpts_mntopts;
1684
1685 /* !gid=5 && max= */
1686 mntopt_sets[1] = devpts_mntopts + STRLITERALLEN("gid=5") + 1;
1687
1688 /* gid=5 && !max= */
1689 mntopt_sets[2] = default_devpts_mntopts;
1690
1691 /* !gid=5 && !max= */
1692 mntopt_sets[3] = default_devpts_mntopts + STRLITERALLEN("gid=5") + 1;
1693
1694 /* end */
1695 mntopt_sets[4] = NULL;
1696
1697 for (ret = -1, opts = mntopt_sets; opts && *opts; opts++) {
1698 /* mount new devpts instance */
1699 ret = mount("devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC, *opts);
1700 if (ret == 0)
1701 break;
1702 }
1703
1704 if (ret < 0)
1705 return log_error_errno(-1, errno, "Failed to mount new devpts instance");
1706 DEBUG("Mount new devpts instance with options \"%s\"", *opts);
1707
1708 devpts_fd = open_at(rootfs->dfd_dev, "pts", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_BENEATH_XDEV, 0);
1709 if (devpts_fd < 0) {
1710 devpts_fd = -EBADF;
1711 TRACE("Failed to create detached devpts mount");
1712 }
1713
1714 handler->conf->devpts_fd = move_fd(devpts_fd);
1715
1716 /* Remove any pre-existing /dev/ptmx file. */
1717 ret = unlinkat(rootfs->dfd_dev, "ptmx", 0);
1718 if (ret < 0) {
1719 if (errno != ENOENT)
1720 return log_error_errno(-1, errno, "Failed to remove existing \"/dev/ptmx\" file");
1721 } else {
1722 DEBUG("Removed existing \"/dev/ptmx\" file");
1723 }
1724
1725 /* Create dummy /dev/ptmx file as bind mountpoint for /dev/pts/ptmx. */
1726 ret = mknodat(rootfs->dfd_dev, "ptmx", S_IFREG | 0000, 0);
1727 if (ret < 0 && errno != EEXIST)
1728 return log_error_errno(-1, errno, "Failed to create dummy \"/dev/ptmx\" file as bind mount target");
1729 DEBUG("Created dummy \"/dev/ptmx\" file as bind mount target");
1730
1731 /* Fallback option: create symlink /dev/ptmx -> /dev/pts/ptmx */
1732 ret = mount("/dev/pts/ptmx", "/dev/ptmx", NULL, MS_BIND, NULL);
1733 if (!ret)
1734 return log_debug(0, "Bind mounted \"/dev/pts/ptmx\" to \"/dev/ptmx\"");
1735 else
1736 /* Fallthrough and try to create a symlink. */
1737 ERROR("Failed to bind mount \"/dev/pts/ptmx\" to \"/dev/ptmx\"");
1738
1739 /* Remove the dummy /dev/ptmx file we created above. */
1740 ret = unlinkat(rootfs->dfd_dev, "ptmx", 0);
1741 if (ret < 0)
1742 return log_error_errno(-1, errno, "Failed to remove existing \"/dev/ptmx\"");
1743
1744 /* Fallback option: Create symlink /dev/ptmx -> /dev/pts/ptmx. */
1745 ret = symlinkat("/dev/pts/ptmx", rootfs->dfd_dev, "/dev/ptmx");
1746 if (ret < 0)
1747 return log_error_errno(-1, errno, "Failed to create symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\"");
1748
1749 DEBUG("Created symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\"");
1750 return 0;
1751 }
1752
1753 static int lxc_send_devpts_to_parent(struct lxc_handler *handler)
1754 {
1755 int ret;
1756
1757 if (handler->conf->pty_max <= 0)
1758 return log_debug(0, "No devpts file descriptor will be sent since no pts devices are requested");
1759
1760 ret = lxc_abstract_unix_send_fds(handler->data_sock[0], &handler->conf->devpts_fd, 1, NULL, 0);
1761 if (ret < 0)
1762 SYSERROR("Failed to send devpts file descriptor %d to parent", handler->conf->devpts_fd);
1763 else
1764 TRACE("Sent devpts file descriptor %d to parent", handler->conf->devpts_fd);
1765
1766 close_prot_errno_disarm(handler->conf->devpts_fd);
1767
1768 return 0;
1769 }
1770
1771 static int setup_personality(personality_t persona)
1772 {
1773 int ret;
1774
1775 if (persona == LXC_ARCH_UNCHANGED)
1776 return log_debug(0, "Retaining original personality");
1777
1778 ret = lxc_personality(persona);
1779 if (ret < 0)
1780 return syserror("Failed to set personality to \"0lx%lx\"", persona);
1781
1782 INFO("Set personality to \"0lx%lx\"", persona);
1783 return 0;
1784 }
1785
1786 static inline bool wants_console(const struct lxc_terminal *terminal)
1787 {
1788 return !terminal->path || !strequal(terminal->path, "none");
1789 }
1790
1791 static int lxc_bind_mount_console(const struct lxc_terminal *console,
1792 int dfd_to, const char *path_to)
1793 {
1794 __do_close int fd_pty = -EBADF;
1795
1796 if (is_empty_string(console->name))
1797 return ret_errno(EINVAL);
1798
1799 /*
1800 * When the pty fd stashed in console->pty has been retrieved via the
1801 * TIOCGPTPEER ioctl() to avoid dangerous path-based lookups when
1802 * allocating new pty devices we can't reopen it through openat2() or
1803 * created a detached mount through open_tree() from it. This means we
1804 * would need to mount using the path stased in console->name which is
1805 * unsafe. We could be mounting a device that isn't identical to the
1806 * one we've already safely opened and stashed in console->pty.
1807 * So, what we do is we open an O_PATH file descriptor for
1808 * console->name and verify that the opened fd and the fd we stashed in
1809 * console->pty refer to the same device. If they do we can go on and
1810 * created a detached mount based on the newly opened O_PATH file
1811 * descriptor and then safely mount.
1812 */
1813 fd_pty = open_at(-EBADF, console->name, PROTECT_OPATH_FILE,
1814 PROTECT_LOOKUP_ABSOLUTE_XDEV, 0);
1815 if (fd_pty < 0)
1816 return log_error_errno(-errno, errno, "Failed to open \"%s\"", console->name);
1817
1818 if (!same_file_lax(console->pty, fd_pty))
1819 return log_error_errno(-EINVAL, EINVAL, "Console file descriptor changed");
1820
1821 /*
1822 * Note, there are intentionally no open or lookup restrictions since
1823 * we're operating directly on the fd.
1824 */
1825 return fd_bind_mount(fd_pty, "", 0, 0,
1826 dfd_to, path_to, PROTECT_OPATH_FILE, PROTECT_LOOKUP_BENEATH,
1827 0, false);
1828 }
1829
1830 static int lxc_setup_dev_console(struct lxc_rootfs *rootfs,
1831 const struct lxc_terminal *console)
1832 {
1833 int ret;
1834 char *rootfs_path = rootfs->path ? rootfs->mount : "";
1835
1836 /*
1837 * When we are asked to setup a console we remove any previous
1838 * /dev/console bind-mounts.
1839 */
1840 if (exists_file_at(rootfs->dfd_dev, "console")) {
1841 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/dev/console", rootfs_path);
1842 if (ret < 0)
1843 return -1;
1844
1845 ret = lxc_unstack_mountpoint(rootfs->buf, false);
1846 if (ret < 0)
1847 return log_error_errno(-ret, errno, "Failed to unmount \"%s\"", rootfs->buf);
1848 else
1849 DEBUG("Cleared all (%d) mounts from \"%s\"", ret, rootfs->buf);
1850 }
1851
1852 /*
1853 * For unprivileged containers autodev or automounts will already have
1854 * taken care of creating /dev/console.
1855 */
1856 ret = mknodat(rootfs->dfd_dev, "console", S_IFREG | 0000, 0);
1857 if (ret < 0 && errno != EEXIST)
1858 return log_error_errno(-errno, errno, "Failed to create console");
1859
1860 ret = fchmod(console->pty, 0620);
1861 if (ret < 0)
1862 return log_error_errno(-errno, errno, "Failed to set mode \"0%o\" to \"%s\"", 0620, console->name);
1863
1864 if (can_use_mount_api()) {
1865 ret = lxc_bind_mount_console(console, rootfs->dfd_dev, "console");
1866 } else {
1867 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/dev/console", rootfs_path);
1868 if (ret < 0)
1869 return ret;
1870
1871 ret = safe_mount(console->name, rootfs->buf, "none", MS_BIND, NULL, rootfs_path);
1872 }
1873 if (ret < 0)
1874 return log_error_errno(ret, errno, "Failed to mount %d(%s) on \"%s\"", console->pty, console->name, rootfs->buf);
1875
1876 DEBUG("Mounted pty device %d(%s) onto \"/dev/console\"", console->pty, console->name);
1877 return 0;
1878 }
1879
1880 static int lxc_setup_ttydir_console(struct lxc_rootfs *rootfs,
1881 const struct lxc_terminal *console,
1882 char *ttydir)
1883 {
1884 int ret;
1885 char path[PATH_MAX], lxcpath[PATH_MAX];
1886 char *rootfs_path = rootfs->path ? rootfs->mount : "";
1887
1888 /* create rootfs/dev/<ttydir> directory */
1889 ret = strnprintf(path, sizeof(path), "%s/dev/%s", rootfs_path, ttydir);
1890 if (ret < 0)
1891 return -1;
1892
1893 ret = mkdir(path, 0755);
1894 if (ret && errno != EEXIST)
1895 return log_error_errno(-errno, errno, "Failed to create \"%s\"", path);
1896 DEBUG("Created directory for console and tty devices at \"%s\"", path);
1897
1898 ret = strnprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/console", rootfs_path, ttydir);
1899 if (ret < 0)
1900 return -1;
1901
1902 ret = mknod(lxcpath, S_IFREG | 0000, 0);
1903 if (ret < 0 && errno != EEXIST)
1904 return log_error_errno(-errno, errno, "Failed to create \"%s\"", lxcpath);
1905
1906 ret = strnprintf(path, sizeof(path), "%s/dev/console", rootfs_path);
1907 if (ret < 0)
1908 return -1;
1909
1910 if (file_exists(path)) {
1911 ret = lxc_unstack_mountpoint(path, false);
1912 if (ret < 0)
1913 return log_error_errno(-ret, errno, "Failed to unmount \"%s\"", path);
1914 else
1915 DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path);
1916 }
1917
1918 ret = mknod(path, S_IFREG | 0000, 0);
1919 if (ret < 0 && errno != EEXIST)
1920 return log_error_errno(-errno, errno, "Failed to create console");
1921
1922 ret = fchmod(console->pty, 0620);
1923 if (ret < 0)
1924 return log_error_errno(-errno, errno, "Failed to set mode \"0%o\" to \"%s\"", 0620, console->name);
1925
1926 /* bind mount console->name to '/dev/<ttydir>/console' */
1927 if (can_use_mount_api()) {
1928 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/console", ttydir);
1929 if (ret < 0)
1930 return ret;
1931
1932 ret = lxc_bind_mount_console(console, rootfs->dfd_dev, rootfs->buf);
1933 } else {
1934 ret = safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs_path);
1935 }
1936 if (ret < 0)
1937 return log_error_errno(ret, errno, "Failed to mount %d(%s) on \"%s\"", console->pty, console->name, lxcpath);
1938 DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath);
1939
1940 /* bind mount '/dev/<ttydir>/console' to '/dev/console' */
1941 if (can_use_mount_api()) {
1942 ret = fd_bind_mount(rootfs->dfd_dev, rootfs->buf,
1943 PROTECT_OPATH_FILE, PROTECT_LOOKUP_BENEATH_XDEV,
1944 rootfs->dfd_dev, "console",
1945 PROTECT_OPATH_FILE, PROTECT_LOOKUP_BENEATH,
1946 0, false);
1947 } else {
1948 ret = safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs_path);
1949 }
1950 if (ret < 0)
1951 return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", console->name, lxcpath);
1952 DEBUG("Mounted \"%s\" onto \"%s\"", lxcpath, path);
1953
1954 DEBUG("Console has been setup under \"%s\" and mounted to \"%s\"", lxcpath, path);
1955 return 0;
1956 }
1957
1958 static int lxc_setup_console(const struct lxc_handler *handler,
1959 struct lxc_rootfs *rootfs,
1960 struct lxc_terminal *console, char *ttydir)
1961 {
1962 __do_close int fd_pty = -EBADF;
1963 int ret;
1964
1965 if (!wants_console(console))
1966 return log_trace(0, "Skipping console setup");
1967
1968 if (ttydir)
1969 ret = lxc_setup_ttydir_console(rootfs, console, ttydir);
1970 else
1971 ret = lxc_setup_dev_console(rootfs, console);
1972 fd_pty = move_fd(console->pty);
1973
1974 /*
1975 * Some init's such as busybox will set sane tty settings on stdin,
1976 * stdout, stderr which it thinks is the console. We already set them
1977 * the way we wanted on the real terminal, and we want init to do its
1978 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
1979 * make sure that that pty is stdin,stdout,stderr.
1980 */
1981 if (fd_pty >= 0) {
1982 if (handler->daemonize || !handler->conf->is_execute)
1983 ret = set_stdfds(fd_pty);
1984 else
1985 ret = lxc_terminal_set_stdfds(fd_pty);
1986 if (ret < 0)
1987 return syserror("Failed to redirect std{in,out,err} to pty file descriptor %d", fd_pty);
1988 }
1989
1990 return ret;
1991 }
1992
1993 static int parse_mntopt(char *opt, unsigned long *flags, char **data, size_t size)
1994 {
1995 ssize_t ret;
1996
1997 /* If '=' is contained in opt, the option must go into data. */
1998 if (!strchr(opt, '=')) {
1999 /*
2000 * If opt is found in mount_opt, set or clear flags.
2001 * Otherwise append it to data.
2002 */
2003 size_t opt_len = strlen(opt);
2004 for (struct mount_opt *mo = &mount_opt[0]; mo->name != NULL; mo++) {
2005 size_t mo_name_len = strlen(mo->name);
2006
2007 if (opt_len == mo_name_len && strnequal(opt, mo->name, mo_name_len)) {
2008 if (mo->clear)
2009 *flags &= ~mo->legacy_flag;
2010 else
2011 *flags |= mo->legacy_flag;
2012 return 0;
2013 }
2014 }
2015 }
2016
2017 if (strlen(*data)) {
2018 ret = strlcat(*data, ",", size);
2019 if (ret < 0)
2020 return log_error_errno(ret, errno, "Failed to append \",\" to %s", *data);
2021 }
2022
2023 ret = strlcat(*data, opt, size);
2024 if (ret < 0)
2025 return log_error_errno(ret, errno, "Failed to append \"%s\" to %s", opt, *data);
2026
2027 return 0;
2028 }
2029
2030 int parse_mntopts_legacy(const char *mntopts, unsigned long *mntflags, char **mntdata)
2031 {
2032 __do_free char *mntopts_new = NULL, *mntopts_dup = NULL;
2033 char *mntopt_cur = NULL;
2034 size_t size;
2035
2036 if (*mntdata || *mntflags)
2037 return ret_errno(EINVAL);
2038
2039 if (!mntopts)
2040 return 0;
2041
2042 mntopts_dup = strdup(mntopts);
2043 if (!mntopts_dup)
2044 return ret_errno(ENOMEM);
2045
2046 size = strlen(mntopts_dup) + 1;
2047 mntopts_new = zalloc(size);
2048 if (!mntopts_new)
2049 return ret_errno(ENOMEM);
2050
2051 lxc_iterate_parts(mntopt_cur, mntopts_dup, ",")
2052 if (parse_mntopt(mntopt_cur, mntflags, &mntopts_new, size) < 0)
2053 return ret_errno(EINVAL);
2054
2055 if (*mntopts_new)
2056 *mntdata = move_ptr(mntopts_new);
2057
2058 return 0;
2059 }
2060
2061 static int parse_vfs_attr(struct lxc_mount_options *opts, char *opt, size_t size)
2062 {
2063 /*
2064 * If opt is found in mount_opt, set or clear flags.
2065 * Otherwise append it to data.
2066 */
2067 for (struct mount_opt *mo = &mount_opt[0]; mo->name != NULL; mo++) {
2068 if (!strnequal(opt, mo->name, strlen(mo->name)))
2069 continue;
2070
2071 /* This is a recursive bind-mount. */
2072 if (strequal(mo->name, "rbind")) {
2073 opts->recursive = 1;
2074 opts->bind = 1;
2075 return 0;
2076 }
2077
2078 /* This is a bind-mount. */
2079 if (strequal(mo->name, "bind")) {
2080 opts->bind = 1;
2081 return 0;
2082 }
2083
2084 if (mo->flag == ~0)
2085 return log_info(0, "Ignoring %s mount option", mo->name);
2086
2087 if (mo->clear) {
2088 opts->attr.attr_clr |= mo->flag;
2089 TRACE("Lowering %s", mo->name);
2090 } else {
2091 opts->attr.attr_set |= mo->flag;
2092 TRACE("Raising %s", mo->name);
2093 }
2094
2095 return 0;
2096 }
2097
2098 for (struct mount_opt *mo = &mount_opt[0]; mo->name != NULL; mo++) {
2099 if (!strnequal(opt, mo->name, strlen(mo->name)))
2100 continue;
2101
2102 /* TODO: Handle recursive propagation requests. */
2103 opts->attr.propagation = mo->flag;
2104 return 0;
2105 }
2106
2107 return 0;
2108 }
2109
2110 static int parse_mount_attrs(struct lxc_mount_options *opts, const char *mntopts)
2111 {
2112 __do_free char *mntopts_new = NULL, *mntopts_dup = NULL;
2113 char *end = NULL, *mntopt_cur = NULL;
2114 int ret;
2115 size_t size;
2116
2117 if (!opts)
2118 return ret_errno(EINVAL);
2119
2120 if (!mntopts)
2121 return 0;
2122
2123 mntopts_dup = strdup(mntopts);
2124 if (!mntopts_dup)
2125 return ret_errno(ENOMEM);
2126
2127 size = strlen(mntopts_dup) + 1;
2128 mntopts_new = zalloc(size);
2129 if (!mntopts_new)
2130 return ret_errno(ENOMEM);
2131
2132 lxc_iterate_parts(mntopt_cur, mntopts_dup, ",") {
2133 /* This is a filesystem specific option. */
2134 if (strchr(mntopt_cur, '=')) {
2135 if (!end) {
2136 end = stpcpy(mntopts_new, mntopt_cur);
2137 } else {
2138 end = stpcpy(end, ",");
2139 end = stpcpy(end, mntopt_cur);
2140 }
2141
2142 continue;
2143 }
2144
2145 /* This is a generic vfs option. */
2146 ret = parse_vfs_attr(opts, mntopt_cur, size);
2147 if (ret < 0)
2148 return syserror("Failed to parse mount attributes: \"%s\"", mntopt_cur);
2149 }
2150
2151 if (*mntopts_new)
2152 opts->data = move_ptr(mntopts_new);
2153
2154 return 0;
2155 }
2156
2157 static void parse_propagationopt(char *opt, unsigned long *flags)
2158 {
2159 struct mount_opt *mo;
2160
2161 /* If opt is found in propagation_opt, set or clear flags. */
2162 for (mo = &propagation_opt[0]; mo->name != NULL; mo++) {
2163 if (!strnequal(opt, mo->name, strlen(mo->name)))
2164 continue;
2165
2166 if (mo->clear)
2167 *flags &= ~mo->legacy_flag;
2168 else
2169 *flags |= mo->legacy_flag;
2170
2171 return;
2172 }
2173 }
2174
2175 int parse_propagationopts(const char *mntopts, unsigned long *pflags)
2176 {
2177 __do_free char *s = NULL;
2178 char *p;
2179
2180 if (!mntopts)
2181 return 0;
2182
2183 s = strdup(mntopts);
2184 if (!s)
2185 return log_error_errno(-ENOMEM, errno, "Failed to allocate memory");
2186
2187 *pflags = 0L;
2188 lxc_iterate_parts(p, s, ",")
2189 parse_propagationopt(p, pflags);
2190
2191 return 0;
2192 }
2193
2194 static void null_endofword(char *word)
2195 {
2196 while (*word && *word != ' ' && *word != '\t')
2197 word++;
2198 *word = '\0';
2199 }
2200
2201 /* skip @nfields spaces in @src */
2202 static char *get_field(char *src, int nfields)
2203 {
2204 int i;
2205 char *p = src;
2206
2207 for (i = 0; i < nfields; i++) {
2208 while (*p && *p != ' ' && *p != '\t')
2209 p++;
2210
2211 if (!*p)
2212 break;
2213
2214 p++;
2215 }
2216
2217 return p;
2218 }
2219
2220 static int mount_entry(const char *fsname, const char *target,
2221 const char *fstype, unsigned long mountflags,
2222 unsigned long pflags, const char *data, bool optional,
2223 bool dev, bool relative, const char *rootfs)
2224 {
2225 int ret;
2226 char srcbuf[PATH_MAX];
2227 const char *srcpath = fsname;
2228 #ifdef HAVE_STATVFS
2229 struct statvfs sb;
2230 #endif
2231
2232 if (relative) {
2233 ret = strnprintf(srcbuf, sizeof(srcbuf), "%s/%s", rootfs ? rootfs : "/", fsname ? fsname : "");
2234 if (ret < 0)
2235 return log_error_errno(-1, errno, "source path is too long");
2236 srcpath = srcbuf;
2237 }
2238
2239 ret = safe_mount(srcpath, target, fstype, mountflags & ~MS_REMOUNT, data,
2240 rootfs);
2241 if (ret < 0) {
2242 if (optional)
2243 return log_info_errno(0, errno, "Failed to mount \"%s\" on \"%s\" (optional)",
2244 srcpath ? srcpath : "(null)", target);
2245
2246 return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"",
2247 srcpath ? srcpath : "(null)", target);
2248 }
2249
2250 if ((mountflags & MS_REMOUNT) || (mountflags & MS_BIND)) {
2251
2252 DEBUG("Remounting \"%s\" on \"%s\" to respect bind or remount options",
2253 srcpath ? srcpath : "(none)", target ? target : "(none)");
2254
2255 #ifdef HAVE_STATVFS
2256 if (srcpath && statvfs(srcpath, &sb) == 0) {
2257 unsigned long required_flags = 0;
2258
2259 if (sb.f_flag & MS_NOSUID)
2260 required_flags |= MS_NOSUID;
2261
2262 if (sb.f_flag & MS_NODEV && !dev)
2263 required_flags |= MS_NODEV;
2264
2265 if (sb.f_flag & MS_RDONLY)
2266 required_flags |= MS_RDONLY;
2267
2268 if (sb.f_flag & MS_NOEXEC)
2269 required_flags |= MS_NOEXEC;
2270
2271 DEBUG("Flags for \"%s\" were %lu, required extra flags are %lu",
2272 srcpath, sb.f_flag, required_flags);
2273
2274 /* If this was a bind mount request, and required_flags
2275 * does not have any flags which are not already in
2276 * mountflags, then skip the remount.
2277 */
2278 if (!(mountflags & MS_REMOUNT) &&
2279 (!(required_flags & ~mountflags) && !(mountflags & MS_RDONLY))) {
2280 DEBUG("Mountflags already were %lu, skipping remount", mountflags);
2281 goto skipremount;
2282 }
2283
2284 mountflags |= required_flags;
2285 }
2286 #endif
2287
2288 ret = mount(srcpath, target, fstype, mountflags | MS_REMOUNT, data);
2289 if (ret < 0) {
2290 if (optional)
2291 return log_info_errno(0, errno, "Failed to mount \"%s\" on \"%s\" (optional)",
2292 srcpath ? srcpath : "(null)",
2293 target);
2294
2295 return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"",
2296 srcpath ? srcpath : "(null)",
2297 target);
2298 }
2299 }
2300
2301 #ifdef HAVE_STATVFS
2302 skipremount:
2303 #endif
2304 if (pflags) {
2305 ret = mount(NULL, target, NULL, pflags, NULL);
2306 if (ret < 0) {
2307 if (optional)
2308 return log_info_errno(0, errno, "Failed to change mount propagation for \"%s\" (optional)", target);
2309 else
2310 return log_error_errno(-1, errno, "Failed to change mount propagation for \"%s\" (optional)", target);
2311 }
2312 DEBUG("Changed mount propagation for \"%s\"", target);
2313 }
2314
2315 DEBUG("Mounted \"%s\" on \"%s\" with filesystem type \"%s\"",
2316 srcpath ? srcpath : "(null)", target, fstype);
2317
2318 return 0;
2319 }
2320
2321 const char *lxc_mount_options_info[LXC_MOUNT_MAX] = {
2322 "create=dir",
2323 "create=file",
2324 "optional",
2325 "relative",
2326 "idmap=",
2327 };
2328
2329 /* Remove "optional", "create=dir", and "create=file" from mntopt */
2330 int parse_lxc_mount_attrs(struct lxc_mount_options *opts, char *mnt_opts)
2331 {
2332 for (size_t i = LXC_MOUNT_CREATE_DIR; i < LXC_MOUNT_MAX; i++) {
2333 __do_close int fd_userns = -EBADF;
2334 const char *opt_name = lxc_mount_options_info[i];
2335 size_t len;
2336 char *idmap_path, *opt, *opt_next;
2337
2338 opt = strstr(mnt_opts, opt_name);
2339 if (!opt)
2340 continue;
2341
2342 switch (i) {
2343 case LXC_MOUNT_CREATE_DIR:
2344 opts->create_dir = 1;
2345 break;
2346 case LXC_MOUNT_CREATE_FILE:
2347 opts->create_file = 1;
2348 break;
2349 case LXC_MOUNT_OPTIONAL:
2350 opts->optional = 1;
2351 break;
2352 case LXC_MOUNT_RELATIVE:
2353 opts->relative = 1;
2354 break;
2355 case LXC_MOUNT_IDMAP:
2356 opt_next = opt;
2357 opt_next += STRLITERALLEN("idmap=");
2358 idmap_path = strchrnul(opt_next, ',');
2359 len = idmap_path - opt_next + 1;
2360
2361 if (len >= sizeof(opts->userns_path))
2362 return syserror_set(-EIO, "Excessive idmap path length for \"idmap=<path>\" LXC specific mount option");
2363
2364 strlcpy(opts->userns_path, opt_next, len);
2365
2366 if (is_empty_string(opts->userns_path))
2367 return syserror_set(-EINVAL, "Missing idmap path for \"idmap=<path>\" LXC specific mount option");
2368
2369 if (!strequal(opts->userns_path, "container")) {
2370 fd_userns = open(opts->userns_path, O_RDONLY | O_NOCTTY | O_CLOEXEC);
2371 if (fd_userns < 0)
2372 return syserror("Failed to open user namespace %s", opts->userns_path);
2373 }
2374
2375 TRACE("Parse LXC specific mount option %d->\"idmap=%s\"", fd_userns, opts->userns_path);
2376 break;
2377 default:
2378 return syserror_set(-EINVAL, "Unknown LXC specific mount option");
2379 }
2380
2381 opt_next = strchr(opt, ',');
2382 if (!opt_next)
2383 *opt = '\0'; /* no more mntopts, so just chop it here */
2384 else
2385 memmove(opt, opt_next + 1, strlen(opt_next + 1) + 1);
2386 }
2387
2388 return 0;
2389 }
2390
2391 static int mount_entry_create_dir_file(const struct mntent *mntent,
2392 const char *path,
2393 const struct lxc_rootfs *rootfs,
2394 const char *lxc_name, const char *lxc_path)
2395 {
2396 __do_free char *p1 = NULL;
2397 int ret;
2398 char *p2;
2399
2400 if (strnequal(mntent->mnt_type, "overlay", 7)) {
2401 ret = ovl_mkdir(mntent, rootfs, lxc_name, lxc_path);
2402 if (ret < 0)
2403 return -1;
2404 }
2405
2406 if (hasmntopt(mntent, "create=dir")) {
2407 ret = mkdir_p(path, 0755);
2408 if (ret < 0 && errno != EEXIST)
2409 return log_error_errno(-1, errno, "Failed to create directory \"%s\"", path);
2410 }
2411
2412 if (!hasmntopt(mntent, "create=file"))
2413 return 0;
2414
2415 ret = access(path, F_OK);
2416 if (ret == 0)
2417 return 0;
2418
2419 p1 = strdup(path);
2420 if (!p1)
2421 return -1;
2422
2423 p2 = dirname(p1);
2424
2425 ret = mkdir_p(p2, 0755);
2426 if (ret < 0 && errno != EEXIST)
2427 return log_error_errno(-1, errno, "Failed to create directory \"%s\"", path);
2428
2429 ret = mknod(path, S_IFREG | 0000, 0);
2430 if (ret < 0 && errno != EEXIST)
2431 return -errno;
2432
2433 return 0;
2434 }
2435
2436 /* rootfs, lxc_name, and lxc_path can be NULL when the container is created
2437 * without a rootfs. */
2438 static inline int mount_entry_on_generic(struct mntent *mntent,
2439 const char *path,
2440 const struct lxc_rootfs *rootfs,
2441 const char *lxc_name,
2442 const char *lxc_path)
2443 {
2444 __do_free char *mntdata = NULL;
2445 unsigned long mntflags = 0, pflags = 0;
2446 char *rootfs_path = NULL;
2447 int ret;
2448 bool dev, optional, relative;
2449 struct lxc_mount_options opts = {};
2450
2451 optional = hasmntopt(mntent, "optional") != NULL;
2452 dev = hasmntopt(mntent, "dev") != NULL;
2453 relative = hasmntopt(mntent, "relative") != NULL;
2454
2455 if (rootfs && rootfs->path)
2456 rootfs_path = rootfs->mount;
2457
2458 ret = mount_entry_create_dir_file(mntent, path, rootfs, lxc_name,
2459 lxc_path);
2460 if (ret < 0) {
2461 if (optional)
2462 return 0;
2463
2464 return -1;
2465 }
2466
2467 ret = parse_lxc_mount_attrs(&opts, mntent->mnt_opts);
2468 if (ret < 0)
2469 return ret;
2470
2471 /*
2472 * Idmapped mount entries will be setup by the parent for us. Note that
2473 * we rely on mount_entry_create_dir_file() above to have already
2474 * created the target path for us. So the parent can just open the
2475 * target and send us the target fd.
2476 */
2477 errno = EOPNOTSUPP;
2478 if (!is_empty_string(opts.userns_path))
2479 return systrace_ret(0, "Skipping idmapped mount entry");
2480
2481 ret = parse_propagationopts(mntent->mnt_opts, &pflags);
2482 if (ret < 0)
2483 return -1;
2484
2485 ret = parse_mntopts_legacy(mntent->mnt_opts, &mntflags, &mntdata);
2486 if (ret < 0)
2487 return ret;
2488
2489 ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type, mntflags,
2490 pflags, mntdata, optional, dev, relative, rootfs_path);
2491
2492 return ret;
2493 }
2494
2495 static inline int mount_entry_on_systemfs(struct lxc_rootfs *rootfs,
2496 struct mntent *mntent)
2497 {
2498 int ret;
2499
2500 /* For containers created without a rootfs all mounts are treated as
2501 * absolute paths starting at / on the host.
2502 */
2503 if (mntent->mnt_dir[0] != '/')
2504 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "/%s", mntent->mnt_dir);
2505 else
2506 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s", mntent->mnt_dir);
2507 if (ret < 0)
2508 return -1;
2509
2510 return mount_entry_on_generic(mntent, rootfs->buf, NULL, NULL, NULL);
2511 }
2512
2513 static int mount_entry_on_absolute_rootfs(struct mntent *mntent,
2514 struct lxc_rootfs *rootfs,
2515 const char *lxc_name,
2516 const char *lxc_path)
2517 {
2518 int offset;
2519 char *aux;
2520 const char *lxcpath;
2521 int ret = 0;
2522
2523 lxcpath = lxc_global_config_value("lxc.lxcpath");
2524 if (!lxcpath)
2525 return -1;
2526
2527 /* If rootfs->path is a blockdev path, allow container fstab to use
2528 * <lxcpath>/<name>/rootfs" as the target prefix.
2529 */
2530 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/%s/rootfs", lxcpath, lxc_name);
2531 if (ret < 0)
2532 goto skipvarlib;
2533
2534 aux = strstr(mntent->mnt_dir, rootfs->buf);
2535 if (aux) {
2536 offset = strlen(rootfs->buf);
2537 goto skipabs;
2538 }
2539
2540 skipvarlib:
2541 aux = strstr(mntent->mnt_dir, rootfs->path);
2542 if (!aux)
2543 return log_warn(ret, "Ignoring mount point \"%s\"", mntent->mnt_dir);
2544 offset = strlen(rootfs->path);
2545
2546 skipabs:
2547 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/%s", rootfs->mount, aux + offset);
2548 if (ret < 0)
2549 return -1;
2550
2551 return mount_entry_on_generic(mntent, rootfs->buf, rootfs, lxc_name, lxc_path);
2552 }
2553
2554 static int mount_entry_on_relative_rootfs(struct mntent *mntent,
2555 struct lxc_rootfs *rootfs,
2556 const char *lxc_name,
2557 const char *lxc_path)
2558 {
2559 int ret;
2560
2561 /* relative to root mount point */
2562 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/%s", rootfs->mount, mntent->mnt_dir);
2563 if (ret < 0)
2564 return -1;
2565
2566 return mount_entry_on_generic(mntent, rootfs->buf, rootfs, lxc_name, lxc_path);
2567 }
2568
2569 static int mount_file_entries(struct lxc_rootfs *rootfs, FILE *file,
2570 const char *lxc_name, const char *lxc_path)
2571 {
2572 char buf[PATH_MAX];
2573 struct mntent mntent;
2574
2575 while (getmntent_r(file, &mntent, buf, sizeof(buf))) {
2576 int ret;
2577
2578 if (!rootfs->path)
2579 ret = mount_entry_on_systemfs(rootfs, &mntent);
2580 else if (mntent.mnt_dir[0] != '/')
2581 ret = mount_entry_on_relative_rootfs(&mntent, rootfs,
2582 lxc_name, lxc_path);
2583 else
2584 ret = mount_entry_on_absolute_rootfs(&mntent, rootfs,
2585 lxc_name, lxc_path);
2586 if (ret < 0)
2587 return -1;
2588 }
2589
2590 if (!feof(file) || ferror(file))
2591 return log_error(-1, "Failed to parse mount entries");
2592
2593 return 0;
2594 }
2595
2596 static inline void __auto_endmntent__(FILE **f)
2597 {
2598 if (*f)
2599 endmntent(*f);
2600 }
2601
2602 #define __do_endmntent __attribute__((__cleanup__(__auto_endmntent__)))
2603
2604 static int setup_mount_fstab(struct lxc_rootfs *rootfs, const char *fstab,
2605 const char *lxc_name, const char *lxc_path)
2606 {
2607 __do_endmntent FILE *f = NULL;
2608 int ret;
2609
2610 if (!fstab)
2611 return 0;
2612
2613 f = setmntent(fstab, "re");
2614 if (!f)
2615 return log_error_errno(-1, errno, "Failed to open \"%s\"", fstab);
2616
2617 ret = mount_file_entries(rootfs, f, lxc_name, lxc_path);
2618 if (ret < 0)
2619 ERROR("Failed to set up mount entries");
2620
2621 return ret;
2622 }
2623
2624 /*
2625 * In order for nested containers to be able to mount /proc and /sys they need
2626 * to see a "pure" proc and sysfs mount points with nothing mounted on top
2627 * (like lxcfs).
2628 * For this we provide proc and sysfs in /dev/.lxc/{proc,sys} while using an
2629 * apparmor rule to deny access to them. This is mostly for convenience: The
2630 * container's root user can mount them anyway and thus has access to the two
2631 * file systems. But a non-root user in the container should not be allowed to
2632 * access them as a side effect without explicitly allowing it.
2633 */
2634 static const char nesting_helpers[] =
2635 "proc dev/.lxc/proc proc create=dir,optional 0 0\n"
2636 "sys dev/.lxc/sys sysfs create=dir,optional 0 0\n";
2637
2638 FILE *make_anonymous_mount_file(struct lxc_list *mount,
2639 bool include_nesting_helpers)
2640 {
2641 __do_close int fd = -EBADF;
2642 FILE *f;
2643 int ret;
2644 char *mount_entry;
2645 struct lxc_list *iterator;
2646
2647 fd = memfd_create(".lxc_mount_file", MFD_CLOEXEC);
2648 if (fd < 0) {
2649 char template[] = P_tmpdir "/.lxc_mount_file_XXXXXX";
2650
2651 if (errno != ENOSYS)
2652 return NULL;
2653
2654 fd = lxc_make_tmpfile(template, true);
2655 if (fd < 0)
2656 return log_error_errno(NULL, errno, "Could not create temporary mount file");
2657
2658 TRACE("Created temporary mount file");
2659 }
2660
2661 lxc_list_for_each (iterator, mount) {
2662 size_t len;
2663
2664 mount_entry = iterator->elem;
2665 len = strlen(mount_entry);
2666
2667 ret = lxc_write_nointr(fd, mount_entry, len);
2668 if (ret != len)
2669 return NULL;
2670
2671 ret = lxc_write_nointr(fd, "\n", 1);
2672 if (ret != 1)
2673 return NULL;
2674 }
2675
2676 if (include_nesting_helpers) {
2677 ret = lxc_write_nointr(fd, nesting_helpers,
2678 STRARRAYLEN(nesting_helpers));
2679 if (ret != STRARRAYLEN(nesting_helpers))
2680 return NULL;
2681 }
2682
2683 ret = lseek(fd, 0, SEEK_SET);
2684 if (ret < 0)
2685 return NULL;
2686
2687 f = fdopen(fd, "re+");
2688 if (f)
2689 move_fd(fd); /* Transfer ownership of fd. */
2690 return f;
2691 }
2692
2693 static int setup_mount_entries(const struct lxc_conf *conf,
2694 struct lxc_rootfs *rootfs, struct lxc_list *mount,
2695 const char *lxc_name, const char *lxc_path)
2696 {
2697 __do_fclose FILE *f = NULL;
2698
2699 f = make_anonymous_mount_file(mount, conf->lsm_aa_allow_nesting);
2700 if (!f)
2701 return -1;
2702
2703 return mount_file_entries(rootfs, f, lxc_name, lxc_path);
2704 }
2705
2706 static int __lxc_idmapped_mounts_child(struct lxc_handler *handler, FILE *f)
2707 {
2708 struct lxc_conf *conf = handler->conf;
2709 struct lxc_rootfs *rootfs = &conf->rootfs;
2710 int mnt_seq = 0;
2711 int ret;
2712 char buf[PATH_MAX];
2713 struct mntent mntent;
2714
2715 while (getmntent_r(f, &mntent, buf, sizeof(buf))) {
2716 __do_close int fd_from = -EBADF, fd_to = -EBADF,
2717 fd_userns = -EBADF;
2718 __do_free char *__data = NULL;
2719 int cur_mnt_seq = -1;
2720 struct lxc_mount_options opts = {};
2721 int dfd_from;
2722 const char *source_relative, *target_relative;
2723
2724 ret = parse_lxc_mount_attrs(&opts, mntent.mnt_opts);
2725 if (ret < 0)
2726 return syserror("Failed to parse LXC specific mount options");
2727 __data = opts.data;
2728
2729 ret = parse_mount_attrs(&opts, mntent.mnt_opts);
2730 if (ret < 0)
2731 return syserror("Failed to parse mount options");
2732
2733 /* No idmapped mount entry so skip it. */
2734 if (is_empty_string(opts.userns_path))
2735 continue;
2736
2737 if (!can_use_bind_mounts())
2738 return syserror_set(-EINVAL, "Kernel does not support idmapped mounts");
2739
2740 if (!opts.bind)
2741 return syserror_set(-EINVAL, "Only bind mounts can currently be idmapped");
2742
2743 /* We don't support new filesystem mounts yet. */
2744 if (!is_empty_string(mntent.mnt_type) &&
2745 !strequal(mntent.mnt_type, "none"))
2746 return syserror_set(-EINVAL, "Only bind mounts can currently be idmapped");
2747
2748 /* Someone specified additional mount options for a bind-mount. */
2749 if (!is_empty_string(opts.data))
2750 return syserror_set(-EINVAL, "Bind mounts don't support non-generic mount options");
2751
2752 /*
2753 * The source path is supposed to be taken relative to the
2754 * container's rootfs mount or - if the container does not have
2755 * a separate rootfs - to the host's /.
2756 */
2757 source_relative = deabs(mntent.mnt_fsname);
2758 if (opts.relative || !rootfs->path)
2759 dfd_from = rootfs->dfd_mnt;
2760 else
2761 dfd_from = rootfs->dfd_host;
2762 fd_from = open_tree(dfd_from, source_relative,
2763 OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC |
2764 (opts.recursive ? AT_RECURSIVE : 0));
2765 if (fd_from < 0)
2766 return syserror("Failed to create detached %smount of %d/%s",
2767 opts.recursive ? "recursive " : "",
2768 dfd_from, source_relative);
2769
2770 if (strequal(opts.userns_path, "container"))
2771 fd_userns = openat(dfd_from, "proc/self/ns/user", O_RDONLY | O_CLOEXEC);
2772 else
2773 fd_userns = open_at(-EBADF, opts.userns_path,
2774 PROTECT_OPEN_WITH_TRAILING_SYMLINKS, 0, 0);
2775 if (fd_userns < 0) {
2776 if (opts.optional) {
2777 TRACE("Skipping optional idmapped mount");
2778 continue;
2779 }
2780
2781 return syserror("Failed to open user namespace \"%s\" for detached %smount of %d/%s",
2782 opts.userns_path, opts.recursive ? "recursive " : "",
2783 dfd_from, source_relative);
2784 }
2785
2786 ret = __lxc_abstract_unix_send_two_fds(handler->data_sock[0],
2787 fd_from, fd_userns,
2788 &opts, sizeof(opts));
2789 if (ret <= 0) {
2790 if (opts.optional) {
2791 TRACE("Skipping optional idmapped mount");
2792 continue;
2793 }
2794
2795 return syserror("Failed to send file descriptor %d for detached %smount of %d/%s and file descriptor %d of user namespace \"%s\" to parent",
2796 fd_from, opts.recursive ? "recursive " : "",
2797 dfd_from, source_relative, fd_userns,
2798 opts.userns_path);
2799 }
2800
2801 ret = lxc_abstract_unix_rcv_credential(handler->data_sock[0],
2802 &cur_mnt_seq,
2803 sizeof(cur_mnt_seq));
2804 if (ret <= 0) {
2805 if (opts.optional) {
2806 TRACE("Skipping optional idmapped mount");
2807 continue;
2808 }
2809
2810 return syserror("Failed to receive notification that parent idmapped detached %smount %d/%s to user namespace %d",
2811 opts.recursive ? "recursive " : "",
2812 dfd_from, source_relative, fd_userns);
2813 }
2814
2815 if (mnt_seq != cur_mnt_seq)
2816 return syserror("Expected mount sequence number and mount sequence number from parent mismatch: %d != %d",
2817 mnt_seq, cur_mnt_seq);
2818 mnt_seq++;
2819
2820 /* Set remaining mount options. */
2821 ret = mount_setattr(fd_from, "", AT_EMPTY_PATH |
2822 (opts.recursive ? AT_RECURSIVE : 0),
2823 &opts.attr, sizeof(opts.attr));
2824 if (ret < 0) {
2825 if (opts.optional) {
2826 TRACE("Skipping optional idmapped mount");
2827 continue;
2828 }
2829
2830 return syserror("Failed to receive notification that parent idmapped detached %smount %d/%s to user namespace %d",
2831 opts.recursive ? "recursive " : "",
2832 dfd_from, source_relative, fd_userns);
2833 }
2834
2835 /*
2836 * In contrast to the legacy mount codepath we will simplify
2837 * our lifes and just always treat the target mountpoint to be
2838 * relative to the container's rootfs mountpoint or - if the
2839 * container does not have a separate rootfs - to the host's /.
2840 */
2841
2842 target_relative = deabs(mntent.mnt_dir);
2843 if (rootfs->path)
2844 dfd_from = rootfs->dfd_mnt;
2845 else
2846 dfd_from = rootfs->dfd_host;
2847 fd_to = open_at(dfd_from, target_relative, PROTECT_OPATH_FILE, PROTECT_LOOKUP_BENEATH_WITH_SYMLINKS, 0);
2848 if (fd_to < 0) {
2849 if (opts.optional) {
2850 TRACE("Skipping optional idmapped mount");
2851 continue;
2852 }
2853
2854 return syserror("Failed to open target mountpoint %d/%s for detached idmapped %smount %d:%d/%s",
2855 dfd_from, target_relative,
2856 opts.recursive ? "recursive " : "",
2857 fd_userns, dfd_from, source_relative);
2858 }
2859
2860 ret = move_detached_mount(fd_from, fd_to, "", 0, 0);
2861 if (ret) {
2862 if (opts.optional) {
2863 TRACE("Skipping optional idmapped mount");
2864 continue;
2865 }
2866
2867 return syserror("Failed to attach detached idmapped %smount %d:%d/%s to target mountpoint %d/%s",
2868 opts.recursive ? "recursive " : "",
2869 fd_userns, dfd_from, source_relative, dfd_from, target_relative);
2870 }
2871
2872 TRACE("Attached detached idmapped %smount %d:%d/%s to target mountpoint %d/%s",
2873 opts.recursive ? "recursive " : "", fd_userns, dfd_from,
2874 source_relative, dfd_from, target_relative);
2875 }
2876
2877 if (!feof(f) || ferror(f))
2878 return syserror_set(-EINVAL, "Failed to parse mount entries");
2879
2880 return 0;
2881 }
2882
2883 static int lxc_idmapped_mounts_child(struct lxc_handler *handler)
2884 {
2885 __do_fclose FILE *f_entries = NULL;
2886 int fret = -1;
2887 struct lxc_conf *conf = handler->conf;
2888 const char *fstab = conf->fstab;
2889 struct lxc_list *mount = &conf->mount_list;
2890 int ret;
2891
2892 f_entries = make_anonymous_mount_file(mount, conf->lsm_aa_allow_nesting);
2893 if (!f_entries) {
2894 SYSERROR("Failed to create anonymous mount file");
2895 goto out;
2896 }
2897
2898 ret = __lxc_idmapped_mounts_child(handler, f_entries);
2899 if (ret) {
2900 SYSERROR("Failed to setup idmapped mount entries");
2901 goto out;
2902 }
2903
2904 TRACE("Finished setting up idmapped mounts");
2905
2906 if (fstab) {
2907 __do_endmntent FILE *f_fstab = NULL;
2908
2909 f_fstab = setmntent(fstab, "re");
2910 if (!f_fstab) {
2911 SYSERROR("Failed to open fstab format file \"%s\"", fstab);
2912 goto out;
2913 }
2914
2915 ret = __lxc_idmapped_mounts_child(handler, f_fstab);
2916 if (ret) {
2917 SYSERROR("Failed to setup idmapped mount entries specified in fstab");
2918 goto out;
2919 }
2920
2921 TRACE("Finished setting up idmapped mounts specified in fstab");
2922 }
2923
2924 fret = 0;
2925
2926 out:
2927 ret = lxc_abstract_unix_send_credential(handler->data_sock[0], NULL, 0);
2928 if (ret < 0)
2929 return syserror("Failed to inform child that we are done setting up mounts");
2930 TRACE("AAAA");
2931
2932 return fret;
2933 }
2934
2935 static int parse_cap(const char *cap)
2936 {
2937 size_t i;
2938 int capid = -1;
2939 size_t end = sizeof(caps_opt) / sizeof(caps_opt[0]);
2940 char *ptr = NULL;
2941
2942 if (strequal(cap, "none"))
2943 return -2;
2944
2945 for (i = 0; i < end; i++) {
2946 if (!strequal(cap, caps_opt[i].name))
2947 continue;
2948
2949 capid = caps_opt[i].value;
2950 break;
2951 }
2952
2953 if (capid < 0) {
2954 /* Try to see if it's numeric, so the user may specify
2955 * capabilities that the running kernel knows about but we
2956 * don't
2957 */
2958 errno = 0;
2959 capid = strtol(cap, &ptr, 10);
2960 if (!ptr || *ptr != '\0' || errno != 0)
2961 /* not a valid number */
2962 capid = -1;
2963 else if (capid > lxc_caps_last_cap())
2964 /* we have a number but it's not a valid
2965 * capability */
2966 capid = -1;
2967 }
2968
2969 return capid;
2970 }
2971
2972 int in_caplist(int cap, struct lxc_list *caps)
2973 {
2974 int capid;
2975 struct lxc_list *iterator;
2976
2977 lxc_list_for_each (iterator, caps) {
2978 capid = parse_cap(iterator->elem);
2979 if (capid == cap)
2980 return 1;
2981 }
2982
2983 return 0;
2984 }
2985
2986 static int setup_caps(struct lxc_list *caps)
2987 {
2988 int capid;
2989 char *drop_entry;
2990 struct lxc_list *iterator;
2991
2992 lxc_list_for_each (iterator, caps) {
2993 int ret;
2994
2995 drop_entry = iterator->elem;
2996
2997 capid = parse_cap(drop_entry);
2998 if (capid < 0)
2999 return log_error(-1, "unknown capability %s", drop_entry);
3000
3001 ret = prctl(PR_CAPBSET_DROP, prctl_arg(capid), prctl_arg(0),
3002 prctl_arg(0), prctl_arg(0));
3003 if (ret < 0)
3004 return log_error_errno(-1, errno, "Failed to remove %s capability", drop_entry);
3005 DEBUG("Dropped %s (%d) capability", drop_entry, capid);
3006 }
3007
3008 DEBUG("Capabilities have been setup");
3009 return 0;
3010 }
3011
3012 static int dropcaps_except(struct lxc_list *caps)
3013 {
3014 __do_free int *caplist = NULL;
3015 int i, capid, numcaps;
3016 char *keep_entry;
3017 struct lxc_list *iterator;
3018
3019 numcaps = lxc_caps_last_cap() + 1;
3020 if (numcaps <= 0 || numcaps > 200)
3021 return -1;
3022 TRACE("Found %d capabilities", numcaps);
3023
3024 /* caplist[i] is 1 if we keep capability i */
3025 caplist = must_realloc(NULL, numcaps * sizeof(int));
3026 memset(caplist, 0, numcaps * sizeof(int));
3027
3028 lxc_list_for_each (iterator, caps) {
3029 keep_entry = iterator->elem;
3030
3031 capid = parse_cap(keep_entry);
3032 if (capid == -2)
3033 continue;
3034
3035 if (capid < 0)
3036 return log_error(-1, "Unknown capability %s", keep_entry);
3037
3038 DEBUG("Keep capability %s (%d)", keep_entry, capid);
3039 caplist[capid] = 1;
3040 }
3041
3042 for (i = 0; i < numcaps; i++) {
3043 int ret;
3044
3045 if (caplist[i])
3046 continue;
3047
3048 ret = prctl(PR_CAPBSET_DROP, prctl_arg(i), prctl_arg(0),
3049 prctl_arg(0), prctl_arg(0));
3050 if (ret < 0)
3051 return log_error_errno(-1, errno, "Failed to remove capability %d", i);
3052 }
3053
3054 DEBUG("Capabilities have been setup");
3055 return 0;
3056 }
3057
3058 static int parse_resource(const char *res)
3059 {
3060 int ret;
3061 size_t i;
3062 int resid = -1;
3063
3064 for (i = 0; i < sizeof(limit_opt) / sizeof(limit_opt[0]); ++i)
3065 if (strequal(res, limit_opt[i].name))
3066 return limit_opt[i].value;
3067
3068 /* Try to see if it's numeric, so the user may specify
3069 * resources that the running kernel knows about but
3070 * we don't.
3071 */
3072 ret = lxc_safe_int(res, &resid);
3073 if (ret < 0)
3074 return -1;
3075
3076 return resid;
3077 }
3078
3079 int setup_resource_limits(struct lxc_list *limits, pid_t pid)
3080 {
3081 int resid;
3082 struct lxc_list *it;
3083 struct lxc_limit *lim;
3084
3085 lxc_list_for_each (it, limits) {
3086 lim = it->elem;
3087
3088 resid = parse_resource(lim->resource);
3089 if (resid < 0)
3090 return log_error(-1, "Unknown resource %s", lim->resource);
3091
3092 #if HAVE_PRLIMIT || HAVE_PRLIMIT64
3093 if (prlimit(pid, resid, &lim->limit, NULL) != 0)
3094 return log_error_errno(-1, errno, "Failed to set limit %s", lim->resource);
3095
3096 TRACE("Setup \"%s\" limit", lim->resource);
3097 #else
3098 return log_error(-1, "Cannot set limit \"%s\" as prlimit is missing", lim->resource);
3099 #endif
3100 }
3101
3102 return 0;
3103 }
3104
3105 int setup_sysctl_parameters(struct lxc_list *sysctls)
3106 {
3107 __do_free char *tmp = NULL;
3108 struct lxc_list *it;
3109 struct lxc_sysctl *elem;
3110 int ret = 0;
3111 char filename[PATH_MAX] = {0};
3112
3113 lxc_list_for_each (it, sysctls) {
3114 elem = it->elem;
3115 tmp = lxc_string_replace(".", "/", elem->key);
3116 if (!tmp)
3117 return log_error(-1, "Failed to replace key %s", elem->key);
3118
3119 ret = strnprintf(filename, sizeof(filename), "/proc/sys/%s", tmp);
3120 if (ret < 0)
3121 return log_error(-1, "Error setting up sysctl parameters path");
3122
3123 ret = lxc_write_to_file(filename, elem->value,
3124 strlen(elem->value), false, 0666);
3125 if (ret < 0)
3126 return log_error_errno(-1, errno, "Failed to setup sysctl parameters %s to %s",
3127 elem->key, elem->value);
3128 }
3129
3130 return 0;
3131 }
3132
3133 int setup_proc_filesystem(struct lxc_list *procs, pid_t pid)
3134 {
3135 __do_free char *tmp = NULL;
3136 struct lxc_list *it;
3137 struct lxc_proc *elem;
3138 int ret = 0;
3139 char filename[PATH_MAX] = {0};
3140
3141 lxc_list_for_each (it, procs) {
3142 elem = it->elem;
3143 tmp = lxc_string_replace(".", "/", elem->filename);
3144 if (!tmp)
3145 return log_error(-1, "Failed to replace key %s", elem->filename);
3146
3147 ret = strnprintf(filename, sizeof(filename), "/proc/%d/%s", pid, tmp);
3148 if (ret < 0)
3149 return log_error(-1, "Error setting up proc filesystem path");
3150
3151 ret = lxc_write_to_file(filename, elem->value,
3152 strlen(elem->value), false, 0666);
3153 if (ret < 0)
3154 return log_error_errno(-1, errno, "Failed to setup proc filesystem %s to %s", elem->filename, elem->value);
3155 }
3156
3157 return 0;
3158 }
3159
3160 static char *default_rootfs_mount = LXCROOTFSMOUNT;
3161
3162 struct lxc_conf *lxc_conf_init(void)
3163 {
3164 int i;
3165 struct lxc_conf *new;
3166
3167 new = zalloc(sizeof(*new));
3168 if (!new)
3169 return NULL;
3170
3171 new->loglevel = LXC_LOG_LEVEL_NOTSET;
3172 new->personality = LXC_ARCH_UNCHANGED;
3173 new->autodev = 1;
3174 new->console.buffer_size = 0;
3175 new->console.log_path = NULL;
3176 new->console.log_fd = -1;
3177 new->console.log_size = 0;
3178 new->console.path = NULL;
3179 new->console.peer = -1;
3180 new->console.proxy.busy = -1;
3181 new->console.proxy.ptx = -1;
3182 new->console.proxy.pty = -1;
3183 new->console.ptx = -EBADF;
3184 new->console.pty = -EBADF;
3185 new->console.name[0] = '\0';
3186 memset(&new->console.ringbuf, 0, sizeof(struct lxc_ringbuf));
3187 new->maincmd_fd = -1;
3188 new->monitor_signal_pdeath = SIGKILL;
3189 new->nbd_idx = -1;
3190 new->rootfs.mount = strdup(default_rootfs_mount);
3191 if (!new->rootfs.mount) {
3192 free(new);
3193 return NULL;
3194 }
3195 new->rootfs.managed = true;
3196 new->rootfs.dfd_mnt = -EBADF;
3197 new->rootfs.dfd_dev = -EBADF;
3198 new->rootfs.dfd_host = -EBADF;
3199 new->rootfs.fd_path_pin = -EBADF;
3200 new->rootfs.dfd_idmapped = -EBADF;
3201 new->logfd = -1;
3202 lxc_list_init(&new->cgroup);
3203 lxc_list_init(&new->cgroup2);
3204 /* Block ("allowlist") all devices by default. */
3205 new->bpf_devices.list_type = LXC_BPF_DEVICE_CGROUP_ALLOWLIST;
3206 lxc_list_init(&(new->bpf_devices).device_item);
3207 lxc_list_init(&new->network);
3208 lxc_list_init(&new->mount_list);
3209 lxc_list_init(&new->caps);
3210 lxc_list_init(&new->keepcaps);
3211 lxc_list_init(&new->id_map);
3212 new->root_nsuid_map = NULL;
3213 new->root_nsgid_map = NULL;
3214 lxc_list_init(&new->includes);
3215 lxc_list_init(&new->aliens);
3216 lxc_list_init(&new->environment);
3217 lxc_list_init(&new->limits);
3218 lxc_list_init(&new->sysctls);
3219 lxc_list_init(&new->procs);
3220 new->hooks_version = 0;
3221 for (i = 0; i < NUM_LXC_HOOKS; i++)
3222 lxc_list_init(&new->hooks[i]);
3223 lxc_list_init(&new->groups);
3224 lxc_list_init(&new->state_clients);
3225 new->lsm_aa_profile = NULL;
3226 lxc_list_init(&new->lsm_aa_raw);
3227 new->lsm_se_context = NULL;
3228 new->lsm_se_keyring_context = NULL;
3229 new->keyring_disable_session = false;
3230 new->transient_procfs_mnt = false;
3231 new->shmount.path_host = NULL;
3232 new->shmount.path_cont = NULL;
3233
3234 /* if running in a new user namespace, init and COMMAND
3235 * default to running as UID/GID 0 when using lxc-execute */
3236 new->init_uid = 0;
3237 new->init_gid = 0;
3238 memset(&new->init_groups, 0, sizeof(lxc_groups_t));
3239 memset(&new->cgroup_meta, 0, sizeof(struct lxc_cgroup));
3240 memset(&new->ns_share, 0, sizeof(char *) * LXC_NS_MAX);
3241 memset(&new->timens, 0, sizeof(struct timens_offsets));
3242 seccomp_conf_init(new);
3243
3244 return new;
3245 }
3246
3247 int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf,
3248 size_t buf_size)
3249 {
3250 __do_close int fd = -EBADF;
3251 int ret;
3252 char path[PATH_MAX];
3253
3254 if (geteuid() != 0 && idtype == ID_TYPE_GID) {
3255 __do_close int setgroups_fd = -EBADF;
3256
3257 ret = strnprintf(path, sizeof(path), "/proc/%d/setgroups", pid);
3258 if (ret < 0)
3259 return -E2BIG;
3260
3261 setgroups_fd = open(path, O_WRONLY);
3262 if (setgroups_fd < 0 && errno != ENOENT)
3263 return log_error_errno(-1, errno, "Failed to open \"%s\"", path);
3264
3265 if (setgroups_fd >= 0) {
3266 ret = lxc_write_nointr(setgroups_fd, "deny\n",
3267 STRLITERALLEN("deny\n"));
3268 if (ret != STRLITERALLEN("deny\n"))
3269 return log_error_errno(-1, errno, "Failed to write \"deny\" to \"/proc/%d/setgroups\"", pid);
3270 TRACE("Wrote \"deny\" to \"/proc/%d/setgroups\"", pid);
3271 }
3272 }
3273
3274 ret = strnprintf(path, sizeof(path), "/proc/%d/%cid_map", pid,
3275 idtype == ID_TYPE_UID ? 'u' : 'g');
3276 if (ret < 0)
3277 return -E2BIG;
3278
3279 fd = open(path, O_WRONLY | O_CLOEXEC);
3280 if (fd < 0)
3281 return log_error_errno(-1, errno, "Failed to open \"%s\"", path);
3282
3283 ret = lxc_write_nointr(fd, buf, buf_size);
3284 if (ret != buf_size)
3285 return log_error_errno(-1, errno, "Failed to write %cid mapping to \"%s\"",
3286 idtype == ID_TYPE_UID ? 'u' : 'g', path);
3287
3288 return 0;
3289 }
3290
3291 /* Check whether a binary exist and has either CAP_SETUID, CAP_SETGID or both.
3292 *
3293 * @return 1 if functional binary was found
3294 * @return 0 if binary exists but is lacking privilege
3295 * @return -ENOENT if binary does not exist
3296 * @return -EINVAL if cap to check is neither CAP_SETUID nor CAP_SETGID
3297 */
3298 static int idmaptool_on_path_and_privileged(const char *binary, cap_value_t cap)
3299 {
3300 __do_free char *path = NULL;
3301 int ret;
3302 struct stat st;
3303
3304 if (cap != CAP_SETUID && cap != CAP_SETGID)
3305 return ret_errno(EINVAL);
3306
3307 path = on_path(binary, NULL);
3308 if (!path)
3309 return ret_errno(ENOENT);
3310
3311 ret = stat(path, &st);
3312 if (ret < 0)
3313 return -errno;
3314
3315 /* Check if the binary is setuid. */
3316 if (st.st_mode & S_ISUID)
3317 return log_debug(1, "The binary \"%s\" does have the setuid bit set", path);
3318
3319 #if HAVE_LIBCAP && LIBCAP_SUPPORTS_FILE_CAPABILITIES
3320 /* Check if it has the CAP_SETUID capability. */
3321 if ((cap & CAP_SETUID) &&
3322 lxc_file_cap_is_set(path, CAP_SETUID, CAP_EFFECTIVE) &&
3323 lxc_file_cap_is_set(path, CAP_SETUID, CAP_PERMITTED))
3324 return log_debug(1, "The binary \"%s\" has CAP_SETUID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path);
3325
3326 /* Check if it has the CAP_SETGID capability. */
3327 if ((cap & CAP_SETGID) &&
3328 lxc_file_cap_is_set(path, CAP_SETGID, CAP_EFFECTIVE) &&
3329 lxc_file_cap_is_set(path, CAP_SETGID, CAP_PERMITTED))
3330 return log_debug(1, "The binary \"%s\" has CAP_SETGID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path);
3331
3332 return 0;
3333 #else
3334 /*
3335 * If we cannot check for file capabilities we need to give the benefit
3336 * of the doubt. Otherwise we might fail even though all the necessary
3337 * file capabilities are set.
3338 */
3339 DEBUG("Cannot check for file capabilities as full capability support is missing. Manual intervention needed");
3340 return 1;
3341 #endif
3342 }
3343
3344 static int lxc_map_ids_exec_wrapper(void *args)
3345 {
3346 execl("/bin/sh", "sh", "-c", (char *)args, (char *)NULL);
3347 return -1;
3348 }
3349
3350 static struct id_map *find_mapped_hostid_entry(const struct lxc_list *idmap,
3351 unsigned id, enum idtype idtype);
3352
3353 int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
3354 {
3355 int fill, left;
3356 char u_or_g;
3357 char *pos;
3358 char cmd_output[PATH_MAX];
3359 struct id_map *map;
3360 struct lxc_list *iterator;
3361 enum idtype type;
3362 int ret = 0, gidmap = 0, uidmap = 0;
3363 char mapbuf[STRLITERALLEN("new@idmap") + STRLITERALLEN(" ") +
3364 INTTYPE_TO_STRLEN(pid_t) + STRLITERALLEN(" ") +
3365 LXC_IDMAPLEN] = {0};
3366 bool had_entry = false, maps_host_root = false, use_shadow = false;
3367 int hostuid, hostgid;
3368
3369 hostuid = geteuid();
3370 hostgid = getegid();
3371
3372 /*
3373 * Check whether caller wants to map host root.
3374 * Due to a security fix newer kernels require CAP_SETFCAP when mapping
3375 * host root into the child userns as you would be able to write fscaps
3376 * that would be valid in the ancestor userns. Mapping host root should
3377 * rarely be the case but LXC is being clever in a bunch of cases.
3378 */
3379 if (find_mapped_hostid_entry(idmap, 0, ID_TYPE_UID))
3380 maps_host_root = true;
3381
3382 /* If new{g,u}idmap exists, that is, if shadow is handing out subuid
3383 * ranges, then insist that root also reserve ranges in subuid. This
3384 * will protected it by preventing another user from being handed the
3385 * range by shadow.
3386 */
3387 uidmap = idmaptool_on_path_and_privileged("newuidmap", CAP_SETUID);
3388 if (uidmap == -ENOENT)
3389 WARN("newuidmap binary is missing");
3390 else if (!uidmap)
3391 WARN("newuidmap is lacking necessary privileges");
3392
3393 gidmap = idmaptool_on_path_and_privileged("newgidmap", CAP_SETGID);
3394 if (gidmap == -ENOENT)
3395 WARN("newgidmap binary is missing");
3396 else if (!gidmap)
3397 WARN("newgidmap is lacking necessary privileges");
3398
3399 if (maps_host_root) {
3400 INFO("Caller maps host root. Writing mapping directly");
3401 } else if (uidmap > 0 && gidmap > 0) {
3402 DEBUG("Functional newuidmap and newgidmap binary found");
3403 use_shadow = true;
3404 } else {
3405 /* In case unprivileged users run application containers via
3406 * execute() or a start*() there are valid cases where they may
3407 * only want to map their own {g,u}id. Let's not block them from
3408 * doing so by requiring geteuid() == 0.
3409 */
3410 DEBUG("No newuidmap and newgidmap binary found. Trying to "
3411 "write directly with euid %d", hostuid);
3412 }
3413
3414 /* Check if we really need to use newuidmap and newgidmap.
3415 * If the user is only remapping his own {g,u}id, we don't need it.
3416 */
3417 if (use_shadow && lxc_list_len(idmap) == 2) {
3418 use_shadow = false;
3419 lxc_list_for_each(iterator, idmap) {
3420 map = iterator->elem;
3421 if (map->idtype == ID_TYPE_UID && map->range == 1 &&
3422 map->nsid == hostuid && map->hostid == hostuid)
3423 continue;
3424 if (map->idtype == ID_TYPE_GID && map->range == 1 &&
3425 map->nsid == hostgid && map->hostid == hostgid)
3426 continue;
3427 use_shadow = true;
3428 break;
3429 }
3430 }
3431
3432 for (type = ID_TYPE_UID, u_or_g = 'u'; type <= ID_TYPE_GID;
3433 type++, u_or_g = 'g') {
3434 pos = mapbuf;
3435
3436 if (use_shadow)
3437 pos += sprintf(mapbuf, "new%cidmap %d", u_or_g, pid);
3438
3439 lxc_list_for_each(iterator, idmap) {
3440 map = iterator->elem;
3441 if (map->idtype != type)
3442 continue;
3443
3444 had_entry = true;
3445
3446 left = LXC_IDMAPLEN - (pos - mapbuf);
3447 fill = strnprintf(pos, left, "%s%lu %lu %lu%s",
3448 use_shadow ? " " : "", map->nsid,
3449 map->hostid, map->range,
3450 use_shadow ? "" : "\n");
3451 /*
3452 * The kernel only takes <= 4k for writes to
3453 * /proc/<pid>/{g,u}id_map
3454 */
3455 if (fill <= 0)
3456 return log_error_errno(-1, errno, "Too many %cid mappings defined", u_or_g);
3457
3458 pos += fill;
3459 }
3460 if (!had_entry)
3461 continue;
3462
3463 /* Try to catch the output of new{g,u}idmap to make debugging
3464 * easier.
3465 */
3466 if (use_shadow) {
3467 ret = run_command(cmd_output, sizeof(cmd_output),
3468 lxc_map_ids_exec_wrapper,
3469 (void *)mapbuf);
3470 if (ret < 0)
3471 return log_error(-1, "new%cidmap failed to write mapping \"%s\": %s", u_or_g, cmd_output, mapbuf);
3472 TRACE("new%cidmap wrote mapping \"%s\"", u_or_g, mapbuf);
3473 } else {
3474 ret = write_id_mapping(type, pid, mapbuf, pos - mapbuf);
3475 if (ret < 0)
3476 return log_error(-1, "Failed to write mapping: %s", mapbuf);
3477 TRACE("Wrote mapping \"%s\"", mapbuf);
3478 }
3479
3480 memset(mapbuf, 0, sizeof(mapbuf));
3481 }
3482
3483 return 0;
3484 }
3485
3486 /*
3487 * Return the host uid/gid to which the container root is mapped in val.
3488 * Return true if id was found, false otherwise.
3489 */
3490 static id_t get_mapped_rootid(const struct lxc_conf *conf, enum idtype idtype)
3491 {
3492 unsigned nsid;
3493 struct id_map *map;
3494 struct lxc_list *it;
3495
3496 if (idtype == ID_TYPE_UID)
3497 nsid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid;
3498 else
3499 nsid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid;
3500
3501 lxc_list_for_each (it, &conf->id_map) {
3502 map = it->elem;
3503 if (map->idtype != idtype)
3504 continue;
3505 if (map->nsid != nsid)
3506 continue;
3507 return map->hostid;
3508 }
3509
3510 if (idtype == ID_TYPE_UID)
3511 return LXC_INVALID_UID;
3512
3513 return LXC_INVALID_GID;
3514 }
3515
3516 int mapped_hostid(unsigned id, const struct lxc_conf *conf, enum idtype idtype)
3517 {
3518 struct id_map *map;
3519 struct lxc_list *it;
3520
3521 lxc_list_for_each (it, &conf->id_map) {
3522 map = it->elem;
3523 if (map->idtype != idtype)
3524 continue;
3525
3526 if (id >= map->hostid && id < map->hostid + map->range)
3527 return (id - map->hostid) + map->nsid;
3528 }
3529
3530 return -1;
3531 }
3532
3533 int find_unmapped_nsid(const struct lxc_conf *conf, enum idtype idtype)
3534 {
3535 struct id_map *map;
3536 struct lxc_list *it;
3537 unsigned int freeid = 0;
3538
3539 again:
3540 lxc_list_for_each (it, &conf->id_map) {
3541 map = it->elem;
3542 if (map->idtype != idtype)
3543 continue;
3544
3545 if (freeid >= map->nsid && freeid < map->nsid + map->range) {
3546 freeid = map->nsid + map->range;
3547 goto again;
3548 }
3549 }
3550
3551 return freeid;
3552 }
3553
3554 /*
3555 * Mount a proc under @rootfs if proc self points to a pid other than
3556 * my own. This is needed to have a known-good proc mount for setting
3557 * up LSMs both at container startup and attach.
3558 *
3559 * NOTE: not to be called from inside the container namespace!
3560 */
3561 static int lxc_transient_proc(struct lxc_rootfs *rootfs)
3562 {
3563 __do_close int fd_proc = -EBADF;
3564 int link_to_pid, link_len, pid_self, ret;
3565 char link[INTTYPE_TO_STRLEN(pid_t) + 1];
3566
3567 link_len = readlinkat(rootfs->dfd_mnt, "proc/self", link, sizeof(link));
3568 if (link_len < 0) {
3569 ret = mkdirat(rootfs->dfd_mnt, "proc", 0000);
3570 if (ret < 0 && errno != EEXIST)
3571 return log_error_errno(-errno, errno, "Failed to create %d(proc)", rootfs->dfd_mnt);
3572
3573 goto domount;
3574 } else if (link_len >= sizeof(link)) {
3575 return log_error_errno(-EIO, EIO, "Truncated link target");
3576 }
3577 link[link_len] = '\0';
3578
3579 pid_self = lxc_raw_getpid();
3580 INFO("Caller's PID is %d; /proc/self points to %s", pid_self, link);
3581
3582 ret = lxc_safe_int(link, &link_to_pid);
3583 if (ret)
3584 return log_error_errno(-ret, ret, "Failed to parse %s", link);
3585
3586 /* Correct procfs is already mounted. */
3587 if (link_to_pid == pid_self)
3588 return log_trace(0, "Correct procfs instance mounted");
3589
3590 fd_proc = open_at(rootfs->dfd_mnt, "proc", PROTECT_OPATH_DIRECTORY,
3591 PROTECT_LOOKUP_BENEATH_XDEV, 0);
3592 if (fd_proc < 0)
3593 return log_error_errno(-errno, errno, "Failed to open transient procfs mountpoint");
3594
3595 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "/proc/self/fd/%d", fd_proc);
3596 if (ret < 0)
3597 return ret_errno(EIO);
3598
3599 ret = umount2(rootfs->buf, MNT_DETACH);
3600 if (ret < 0)
3601 SYSWARN("Failed to umount \"%s\" with MNT_DETACH", rootfs->buf);
3602
3603 domount:
3604 /* rootfs is NULL */
3605 if (!rootfs->path) {
3606 ret = mount("proc", rootfs->buf, "proc", 0, NULL);
3607 } else {
3608 ret = safe_mount_beneath_at(rootfs->dfd_mnt, "none", "proc", "proc", 0, NULL);
3609 if (ret < 0) {
3610 ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/proc", rootfs->path ? rootfs->mount : "");
3611 if (ret < 0)
3612 return ret_errno(EIO);
3613
3614 ret = safe_mount("proc", rootfs->buf, "proc", 0, NULL, rootfs->mount);
3615 }
3616 }
3617 if (ret < 0)
3618 return log_error_errno(-1, errno, "Failed to mount temporary procfs");
3619
3620 INFO("Created transient procfs mount");
3621 return 1;
3622 }
3623
3624 /* NOTE: Must not be called from inside the container namespace! */
3625 static int lxc_create_tmp_proc_mount(struct lxc_conf *conf)
3626 {
3627 int mounted;
3628
3629 mounted = lxc_transient_proc(&conf->rootfs);
3630 if (mounted == -1) {
3631 /* continue only if there is no rootfs */
3632 if (conf->rootfs.path)
3633 return log_error_errno(-EPERM, EPERM, "Failed to create transient procfs mount");
3634 } else if (mounted == 1) {
3635 conf->transient_procfs_mnt = true;
3636 }
3637
3638 return 0;
3639 }
3640
3641 void tmp_proc_unmount(struct lxc_conf *lxc_conf)
3642 {
3643 if (lxc_conf->transient_procfs_mnt) {
3644 (void)umount2("/proc", MNT_DETACH);
3645 lxc_conf->transient_procfs_mnt = false;
3646 }
3647 }
3648
3649 /* Walk /proc/mounts and change any shared entries to dependent mounts. */
3650 static void turn_into_dependent_mounts(const struct lxc_rootfs *rootfs)
3651 {
3652 __do_free char *line = NULL;
3653 __do_fclose FILE *f = NULL;
3654 __do_close int memfd = -EBADF, mntinfo_fd = -EBADF;
3655 size_t len = 0;
3656 ssize_t copied;
3657 int ret;
3658
3659 mntinfo_fd = open_at(rootfs->dfd_host, "proc/self/mountinfo", PROTECT_OPEN,
3660 (PROTECT_LOOKUP_BENEATH_XDEV & ~RESOLVE_NO_SYMLINKS), 0);
3661 if (mntinfo_fd < 0) {
3662 SYSERROR("Failed to open %d/proc/self/mountinfo", rootfs->dfd_host);
3663 return;
3664 }
3665
3666 memfd = memfd_create(".lxc_mountinfo", MFD_CLOEXEC);
3667 if (memfd < 0) {
3668 char template[] = P_tmpdir "/.lxc_mountinfo_XXXXXX";
3669
3670 if (errno != ENOSYS) {
3671 SYSERROR("Failed to create temporary in-memory file");
3672 return;
3673 }
3674
3675 memfd = lxc_make_tmpfile(template, true);
3676 if (memfd < 0) {
3677 WARN("Failed to create temporary file");
3678 return;
3679 }
3680 }
3681
3682 copied = fd_to_fd(mntinfo_fd, memfd);
3683 if (copied < 0) {
3684 SYSERROR("Failed to copy \"/proc/self/mountinfo\"");
3685 return;
3686 }
3687
3688 ret = lseek(memfd, 0, SEEK_SET);
3689 if (ret < 0) {
3690 SYSERROR("Failed to reset file descriptor offset");
3691 return;
3692 }
3693
3694 f = fdopen(memfd, "re");
3695 if (!f) {
3696 SYSERROR("Failed to open copy of \"/proc/self/mountinfo\" to mark all shared. Continuing");
3697 return;
3698 }
3699
3700 /*
3701 * After a successful fdopen() memfd will be closed when calling
3702 * fclose(f). Calling close(memfd) afterwards is undefined.
3703 */
3704 move_fd(memfd);
3705
3706 while (getline(&line, &len, f) != -1) {
3707 char *opts, *target;
3708
3709 target = get_field(line, 4);
3710 if (!target)
3711 continue;
3712
3713 opts = get_field(target, 2);
3714 if (!opts)
3715 continue;
3716
3717 null_endofword(opts);
3718 if (!strstr(opts, "shared"))
3719 continue;
3720
3721 null_endofword(target);
3722 ret = mount(NULL, target, NULL, MS_SLAVE, NULL);
3723 if (ret < 0) {
3724 SYSERROR("Failed to recursively turn old root mount tree into dependent mount. Continuing...");
3725 continue;
3726 }
3727 }
3728 TRACE("Turned all mount table entries into dependent mount");
3729 }
3730
3731 static int lxc_execute_bind_init(struct lxc_handler *handler)
3732 {
3733 int ret;
3734 char *p;
3735 char path[PATH_MAX], destpath[PATH_MAX];
3736 struct lxc_conf *conf = handler->conf;
3737
3738 /* If init exists in the container, don't bind mount a static one */
3739 p = choose_init(conf->rootfs.mount);
3740 if (p) {
3741 __do_free char *old = p;
3742
3743 p = strdup(old + strlen(conf->rootfs.mount));
3744 if (!p)
3745 return -ENOMEM;
3746
3747 INFO("Found existing init at \"%s\"", p);
3748 goto out;
3749 }
3750
3751 ret = strnprintf(path, sizeof(path), SBINDIR "/init.lxc.static");
3752 if (ret < 0)
3753 return -1;
3754
3755 if (!file_exists(path))
3756 return log_error_errno(-1, errno, "The file \"%s\" does not exist on host", path);
3757
3758 ret = strnprintf(destpath, sizeof(path), "%s" P_tmpdir "%s", conf->rootfs.mount, "/.lxc-init");
3759 if (ret < 0)
3760 return -1;
3761
3762 if (!file_exists(destpath)) {
3763 ret = mknod(destpath, S_IFREG | 0000, 0);
3764 if (ret < 0 && errno != EEXIST)
3765 return log_error_errno(-1, errno, "Failed to create dummy \"%s\" file as bind mount target", destpath);
3766 }
3767
3768 ret = safe_mount(path, destpath, "none", MS_BIND, NULL, conf->rootfs.mount);
3769 if (ret < 0)
3770 return log_error_errno(-1, errno, "Failed to bind mount lxc.init.static into container");
3771
3772 p = strdup(destpath + strlen(conf->rootfs.mount));
3773 if (!p)
3774 return -ENOMEM;
3775
3776 INFO("Bind mounted lxc.init.static into container at \"%s\"", path);
3777 out:
3778 ((struct execute_args *)handler->data)->init_fd = -1;
3779 ((struct execute_args *)handler->data)->init_path = p;
3780 return 0;
3781 }
3782
3783 /* This does the work of remounting / if it is shared, calling the container
3784 * pre-mount hooks, and mounting the rootfs.
3785 */
3786 int lxc_setup_rootfs_prepare_root(struct lxc_conf *conf, const char *name,
3787 const char *lxcpath)
3788 {
3789 int ret;
3790
3791 conf->rootfs.dfd_host = open_at(-EBADF, "/", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE, 0);
3792 if (conf->rootfs.dfd_host < 0)
3793 return log_error_errno(-errno, errno, "Failed to open \"/\"");
3794
3795 turn_into_dependent_mounts(&conf->rootfs);
3796
3797 if (conf->rootfs_setup) {
3798 const char *path = conf->rootfs.mount;
3799
3800 /*
3801 * The rootfs was set up in another namespace. bind-mount it to
3802 * give us a mount in our own ns so we can pivot_root to it
3803 */
3804 ret = mount(path, path, "rootfs", MS_BIND, NULL);
3805 if (ret < 0)
3806 return log_error(-1, "Failed to bind mount container / onto itself");
3807
3808 conf->rootfs.dfd_mnt = openat(-EBADF, path, O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_PATH | O_NOCTTY);
3809 if (conf->rootfs.dfd_mnt < 0)
3810 return log_error_errno(-errno, errno, "Failed to open file descriptor for container rootfs");
3811
3812 return log_trace(0, "Bind mounted container / onto itself");
3813 }
3814
3815 ret = run_lxc_hooks(name, "pre-mount", conf, NULL);
3816 if (ret < 0)
3817 return log_error(-1, "Failed to run pre-mount hooks");
3818
3819 ret = lxc_mount_rootfs(&conf->rootfs);
3820 if (ret < 0)
3821 return log_error(-1, "Failed to setup rootfs for");
3822
3823 conf->rootfs_setup = true;
3824 return 0;
3825 }
3826
3827 static bool verify_start_hooks(struct lxc_conf *conf)
3828 {
3829 char path[PATH_MAX];
3830 struct lxc_list *it;
3831
3832 lxc_list_for_each (it, &conf->hooks[LXCHOOK_START]) {
3833 int ret;
3834 char *hookname = it->elem;
3835
3836 ret = strnprintf(path, sizeof(path), "%s%s",
3837 conf->rootfs.path ? conf->rootfs.mount : "",
3838 hookname);
3839 if (ret < 0)
3840 return false;
3841
3842 ret = access(path, X_OK);
3843 if (ret < 0)
3844 return log_error_errno(false, errno, "Start hook \"%s\" not found in container", hookname);
3845
3846 return true;
3847 }
3848
3849 return true;
3850 }
3851
3852 static bool execveat_supported(void)
3853 {
3854 execveat(-1, "", NULL, NULL, AT_EMPTY_PATH);
3855 if (errno == ENOSYS)
3856 return false;
3857
3858 return true;
3859 }
3860
3861 static int lxc_setup_boot_id(void)
3862 {
3863 int ret;
3864 const char *boot_id_path = "/proc/sys/kernel/random/boot_id";
3865 const char *mock_boot_id_path = "/dev/.lxc-boot-id";
3866 lxc_id128_t n;
3867
3868 if (access(boot_id_path, F_OK))
3869 return 0;
3870
3871 memset(&n, 0, sizeof(n));
3872 if (lxc_id128_randomize(&n)) {
3873 SYSERROR("Failed to generate random data for uuid");
3874 return -1;
3875 }
3876
3877 ret = lxc_id128_write(mock_boot_id_path, n);
3878 if (ret < 0) {
3879 SYSERROR("Failed to write uuid to %s", mock_boot_id_path);
3880 return -1;
3881 }
3882
3883 ret = chmod(mock_boot_id_path, 0444);
3884 if (ret < 0) {
3885 SYSERROR("Failed to chown %s", mock_boot_id_path);
3886 (void)unlink(mock_boot_id_path);
3887 return -1;
3888 }
3889
3890 ret = mount(mock_boot_id_path, boot_id_path, NULL, MS_BIND, NULL);
3891 if (ret < 0) {
3892 SYSERROR("Failed to mount %s to %s", mock_boot_id_path,
3893 boot_id_path);
3894 (void)unlink(mock_boot_id_path);
3895 return -1;
3896 }
3897
3898 ret = mount(NULL, boot_id_path, NULL,
3899 (MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID | MS_NOEXEC |
3900 MS_NODEV),
3901 NULL);
3902 if (ret < 0) {
3903 SYSERROR("Failed to remount %s read-only", boot_id_path);
3904 (void)unlink(mock_boot_id_path);
3905 return -1;
3906 }
3907
3908 return 0;
3909 }
3910
3911 static int lxc_setup_keyring(struct lsm_ops *lsm_ops, const struct lxc_conf *conf)
3912 {
3913 key_serial_t keyring;
3914 int ret = 0;
3915
3916 if (conf->lsm_se_keyring_context)
3917 ret = lsm_ops->keyring_label_set(lsm_ops, conf->lsm_se_keyring_context);
3918 else if (conf->lsm_se_context)
3919 ret = lsm_ops->keyring_label_set(lsm_ops, conf->lsm_se_context);
3920 if (ret < 0)
3921 return log_error_errno(-1, errno, "Failed to set keyring context");
3922
3923 /*
3924 * Try to allocate a new session keyring for the container to prevent
3925 * information leaks.
3926 */
3927 keyring = keyctl(KEYCTL_JOIN_SESSION_KEYRING, prctl_arg(0),
3928 prctl_arg(0), prctl_arg(0), prctl_arg(0));
3929 if (keyring < 0) {
3930 switch (errno) {
3931 case ENOSYS:
3932 DEBUG("The keyctl() syscall is not supported or blocked");
3933 break;
3934 case EACCES:
3935 __fallthrough;
3936 case EPERM:
3937 DEBUG("Failed to access kernel keyring. Continuing...");
3938 break;
3939 default:
3940 SYSERROR("Failed to create kernel keyring");
3941 break;
3942 }
3943 }
3944
3945 return ret;
3946 }
3947
3948 static int lxc_rootfs_prepare_child(struct lxc_handler *handler)
3949 {
3950 struct lxc_rootfs *rootfs = &handler->conf->rootfs;
3951 int dfd_idmapped = -EBADF;
3952 int ret;
3953
3954 if (lxc_list_empty(&handler->conf->id_map))
3955 return 0;
3956
3957 if (is_empty_string(rootfs->mnt_opts.userns_path))
3958 return 0;
3959
3960 if (handler->conf->rootfs_setup)
3961 return 0;
3962
3963 ret = lxc_abstract_unix_recv_one_fd(handler->data_sock[1], &dfd_idmapped, NULL, 0);
3964 if (ret < 0)
3965 return syserror("Failed to receive idmapped mount fd");
3966
3967 rootfs->dfd_idmapped = dfd_idmapped;
3968 TRACE("Received detached idmapped mount %d", rootfs->dfd_idmapped);
3969 return 0;
3970 }
3971
3972 int lxc_idmapped_mounts_parent(struct lxc_handler *handler)
3973 {
3974 int mnt_seq = 0;
3975
3976 for (;;) {
3977 __do_close int fd_from = -EBADF, fd_userns = -EBADF;
3978 struct lxc_mount_attr attr = {};
3979 struct lxc_mount_options opts = {};
3980 ssize_t ret;
3981
3982 ret = __lxc_abstract_unix_recv_two_fds(handler->data_sock[1],
3983 &fd_from, &fd_userns,
3984 &opts, sizeof(opts));
3985 if (ret < 0)
3986 return syserror("Failed to receive idmapped mount file descriptors from child");
3987
3988 if (fd_from < 0 || fd_userns < 0)
3989 return log_trace(0, "Finished receiving idmapped mount file descriptors from child");
3990
3991 attr.attr_set = MOUNT_ATTR_IDMAP;
3992 attr.userns_fd = fd_userns;
3993 ret = mount_setattr(fd_from, "",
3994 AT_EMPTY_PATH |
3995 (opts.recursive ? AT_RECURSIVE : 0),
3996 &attr, sizeof(attr));
3997 if (ret)
3998 return syserror("Failed to idmap detached %smount %d to %d",
3999 opts.recursive ? "recursive " : "",
4000 fd_from, fd_userns);
4001
4002 ret = lxc_abstract_unix_send_credential(handler->data_sock[1],
4003 &mnt_seq,
4004 sizeof(mnt_seq));
4005 if (ret < 0)
4006 return syserror("Parent failed to notify child that detached %smount %d was idmapped to user namespace %d",
4007 opts.recursive ? "recursive " : "",
4008 fd_from, fd_userns);
4009
4010 TRACE("Parent idmapped detached %smount %d to user namespace %d",
4011 opts.recursive ? "recursive " : "", fd_from, fd_userns);
4012 mnt_seq++;
4013 }
4014 }
4015
4016 static int lxc_recv_ttys_from_child(struct lxc_handler *handler)
4017 {
4018 int i;
4019 struct lxc_terminal_info *tty;
4020 int ret = -1;
4021 int sock = handler->data_sock[1];
4022 struct lxc_conf *conf = handler->conf;
4023 struct lxc_tty_info *ttys = &conf->ttys;
4024
4025 if (!conf->ttys.max)
4026 return 0;
4027
4028 ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max);
4029 if (!ttys->tty)
4030 return -1;
4031
4032 for (i = 0; i < conf->ttys.max; i++) {
4033 int ttyx = -EBADF, ttyy = -EBADF;
4034
4035 ret = lxc_abstract_unix_recv_two_fds(sock, &ttyx, &ttyy);
4036 if (ret < 0)
4037 break;
4038
4039 tty = &ttys->tty[i];
4040 tty->busy = -1;
4041 tty->ptx = ttyx;
4042 tty->pty = ttyy;
4043 TRACE("Received pty with ptx fd %d and pty fd %d from child", tty->ptx, tty->pty);
4044 }
4045
4046 if (ret < 0)
4047 SYSERROR("Failed to receive %zu ttys from child", ttys->max);
4048 else
4049 TRACE("Received %zu ttys from child", ttys->max);
4050
4051 return ret;
4052 }
4053
4054 int lxc_sync_fds_parent(struct lxc_handler *handler)
4055 {
4056 int ret;
4057
4058 ret = lxc_seccomp_recv_notifier_fd(&handler->conf->seccomp, handler->data_sock[1]);
4059 if (ret < 0)
4060 return syserror_ret(ret, "Failed to receive seccomp notify fd from child");
4061
4062 ret = lxc_setup_devpts_parent(handler);
4063 if (ret < 0)
4064 return syserror_ret(ret, "Failed to receive devpts fd from child");
4065
4066 /* Read tty fds allocated by child. */
4067 ret = lxc_recv_ttys_from_child(handler);
4068 if (ret < 0)
4069 return syserror_ret(ret, "Failed to receive tty info from child process");
4070
4071 if (handler->ns_clone_flags & CLONE_NEWNET) {
4072 ret = lxc_network_recv_name_and_ifindex_from_child(handler);
4073 if (ret < 0)
4074 return syserror_ret(ret, "Failed to receive names and ifindices for network devices from child");
4075 }
4076
4077 TRACE("Finished syncing file descriptors with child");
4078 return 0;
4079 }
4080
4081 int lxc_sync_fds_child(struct lxc_handler *handler)
4082 {
4083 int ret;
4084
4085 ret = lxc_seccomp_send_notifier_fd(&handler->conf->seccomp, handler->data_sock[0]);
4086 if (ret < 0)
4087 return syserror_ret(ret, "Failed to send seccomp notify fd to parent");
4088
4089 ret = lxc_send_devpts_to_parent(handler);
4090 if (ret < 0)
4091 return syserror_ret(ret, "Failed to send seccomp devpts fd to parent");
4092
4093 ret = lxc_send_ttys_to_parent(handler);
4094 if (ret < 0)
4095 return syserror_ret(ret, "Failed to send tty file descriptors to parent");
4096
4097 if (handler->ns_clone_flags & CLONE_NEWNET) {
4098 ret = lxc_network_send_name_and_ifindex_to_parent(handler);
4099 if (ret < 0)
4100 return syserror_ret(ret, "Failed to send network device names and ifindices to parent");
4101 }
4102
4103 TRACE("Finished syncing file descriptors with parent");
4104 return 0;
4105 }
4106
4107 int lxc_setup(struct lxc_handler *handler)
4108 {
4109 int ret;
4110 const char *lxcpath = handler->lxcpath, *name = handler->name;
4111 struct lxc_conf *lxc_conf = handler->conf;
4112
4113 ret = lxc_rootfs_prepare_child(handler);
4114 if (ret < 0)
4115 return syserror("Failed to prepare rootfs");
4116
4117 ret = lxc_setup_rootfs_prepare_root(lxc_conf, name, lxcpath);
4118 if (ret < 0)
4119 return log_error(-1, "Failed to setup rootfs");
4120
4121 if (handler->nsfd[LXC_NS_UTS] == -EBADF) {
4122 ret = setup_utsname(lxc_conf->utsname);
4123 if (ret < 0)
4124 return log_error(-1, "Failed to setup the utsname %s", name);
4125 }
4126
4127 if (!lxc_conf->keyring_disable_session) {
4128 ret = lxc_setup_keyring(handler->lsm_ops, lxc_conf);
4129 if (ret < 0)
4130 return log_error(-1, "Failed to setup container keyring");
4131 }
4132
4133 if (handler->ns_clone_flags & CLONE_NEWNET) {
4134 ret = lxc_setup_network_in_child_namespaces(lxc_conf,
4135 &lxc_conf->network);
4136 if (ret < 0)
4137 return log_error(-1, "Failed to setup network");
4138 }
4139
4140 if (lxc_conf->autodev > 0) {
4141 ret = mount_autodev(name, &lxc_conf->rootfs, lxc_conf->autodevtmpfssize, lxcpath);
4142 if (ret < 0)
4143 return log_error(-1, "Failed to mount \"/dev\"");
4144 }
4145
4146 /* Do automatic mounts (mainly /proc and /sys), but exclude those that
4147 * need to wait until other stuff has finished.
4148 */
4149 ret = lxc_mount_auto_mounts(handler, lxc_conf->auto_mounts & ~LXC_AUTO_CGROUP_MASK);
4150 if (ret < 0)
4151 return log_error(-1, "Failed to setup first automatic mounts");
4152
4153 ret = setup_mount_fstab(&lxc_conf->rootfs, lxc_conf->fstab, name, lxcpath);
4154 if (ret < 0)
4155 return log_error(-1, "Failed to setup mounts");
4156
4157 if (!lxc_list_empty(&lxc_conf->mount_list)) {
4158 ret = setup_mount_entries(lxc_conf, &lxc_conf->rootfs,
4159 &lxc_conf->mount_list, name, lxcpath);
4160 if (ret < 0)
4161 return log_error(-1, "Failed to setup mount entries");
4162 }
4163
4164 if (!lxc_sync_wake_parent(handler, START_SYNC_IDMAPPED_MOUNTS))
4165 return -1;
4166
4167 ret = lxc_idmapped_mounts_child(handler);
4168 if (ret)
4169 return syserror("Failed to attached detached idmapped mounts");
4170
4171 lxc_conf->rootfs.dfd_dev = open_at(lxc_conf->rootfs.dfd_mnt, "dev",
4172 PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_BENEATH_XDEV, 0);
4173 if (lxc_conf->rootfs.dfd_dev < 0 && errno != ENOENT)
4174 return log_error_errno(-errno, errno, "Failed to open \"/dev\"");
4175
4176 if (lxc_conf->is_execute) {
4177 if (execveat_supported()) {
4178 int fd;
4179 char path[STRLITERALLEN(SBINDIR) + STRLITERALLEN("/init.lxc.static") + 1];
4180
4181 ret = strnprintf(path, sizeof(path), SBINDIR "/init.lxc.static");
4182 if (ret < 0)
4183 return log_error(-1, "Path to init.lxc.static too long");
4184
4185 fd = open(path, O_NOCTTY | O_NOFOLLOW | O_CLOEXEC | O_PATH);
4186 if (fd < 0)
4187 return log_error_errno(-1, errno, "Unable to open lxc.init.static");
4188
4189 ((struct execute_args *)handler->data)->init_fd = fd;
4190 ((struct execute_args *)handler->data)->init_path = NULL;
4191 } else {
4192 ret = lxc_execute_bind_init(handler);
4193 if (ret < 0)
4194 return log_error(-1, "Failed to bind-mount the lxc init system");
4195 }
4196 }
4197
4198 /* Now mount only cgroups, if wanted. Before, /sys could not have been
4199 * mounted. It is guaranteed to be mounted now either through
4200 * automatically or via fstab entries.
4201 */
4202 ret = lxc_mount_auto_mounts(handler, lxc_conf->auto_mounts & LXC_AUTO_CGROUP_MASK);
4203 if (ret < 0)
4204 return log_error(-1, "Failed to setup remaining automatic mounts");
4205
4206 ret = run_lxc_hooks(name, "mount", lxc_conf, NULL);
4207 if (ret < 0)
4208 return log_error(-1, "Failed to run mount hooks");
4209
4210 if (lxc_conf->autodev > 0) {
4211 ret = run_lxc_hooks(name, "autodev", lxc_conf, NULL);
4212 if (ret < 0)
4213 return log_error(-1, "Failed to run autodev hooks");
4214
4215 ret = lxc_fill_autodev(&lxc_conf->rootfs);
4216 if (ret < 0)
4217 return log_error(-1, "Failed to populate \"/dev\"");
4218 }
4219
4220 /* Make sure any start hooks are in the container */
4221 if (!verify_start_hooks(lxc_conf))
4222 return log_error(-1, "Failed to verify start hooks");
4223
4224 ret = lxc_create_tmp_proc_mount(lxc_conf);
4225 if (ret < 0)
4226 return log_error(-1, "Failed to mount transient procfs instance for LSMs");
4227
4228 ret = lxc_setup_console(handler, &lxc_conf->rootfs, &lxc_conf->console,
4229 lxc_conf->ttys.dir);
4230 if (ret < 0)
4231 return log_error(-1, "Failed to setup console");
4232
4233 ret = lxc_setup_dev_symlinks(&lxc_conf->rootfs);
4234 if (ret < 0)
4235 return log_error(-1, "Failed to setup \"/dev\" symlinks");
4236
4237 ret = lxc_setup_rootfs_switch_root(&lxc_conf->rootfs);
4238 if (ret < 0)
4239 return log_error(-1, "Failed to pivot root into rootfs");
4240
4241 /* Setting the boot-id is best-effort for now. */
4242 if (lxc_conf->autodev > 0)
4243 (void)lxc_setup_boot_id();
4244
4245 ret = lxc_setup_devpts_child(handler);
4246 if (ret < 0)
4247 return log_error(-1, "Failed to setup new devpts instance");
4248
4249 ret = lxc_create_ttys(handler);
4250 if (ret < 0)
4251 return -1;
4252
4253 ret = setup_personality(lxc_conf->personality);
4254 if (ret < 0)
4255 return syserror("Failed to set personality");
4256
4257 /* Set sysctl value to a path under /proc/sys as determined from the
4258 * key. For e.g. net.ipv4.ip_forward translated to
4259 * /proc/sys/net/ipv4/ip_forward.
4260 */
4261 if (!lxc_list_empty(&lxc_conf->sysctls)) {
4262 ret = setup_sysctl_parameters(&lxc_conf->sysctls);
4263 if (ret < 0)
4264 return log_error(-1, "Failed to setup sysctl parameters");
4265 }
4266
4267 if (!lxc_list_empty(&lxc_conf->keepcaps)) {
4268 if (!lxc_list_empty(&lxc_conf->caps))
4269 return log_error(-1, "Container requests lxc.cap.drop and lxc.cap.keep: either use lxc.cap.drop or lxc.cap.keep, not both");
4270
4271 if (dropcaps_except(&lxc_conf->keepcaps))
4272 return log_error(-1, "Failed to keep capabilities");
4273 } else if (setup_caps(&lxc_conf->caps)) {
4274 return log_error(-1, "Failed to drop capabilities");
4275 }
4276
4277 put_lxc_rootfs(&handler->conf->rootfs, true);
4278 NOTICE("The container \"%s\" is set up", name);
4279
4280 return 0;
4281 }
4282
4283 int run_lxc_hooks(const char *name, char *hookname, struct lxc_conf *conf,
4284 char *argv[])
4285 {
4286 struct lxc_list *it;
4287 int which;
4288
4289 for (which = 0; which < NUM_LXC_HOOKS; which ++) {
4290 if (strequal(hookname, lxchook_names[which]))
4291 break;
4292 }
4293
4294 if (which >= NUM_LXC_HOOKS)
4295 return -1;
4296
4297 lxc_list_for_each (it, &conf->hooks[which]) {
4298 int ret;
4299 char *hook = it->elem;
4300
4301 ret = run_script_argv(name, conf->hooks_version, "lxc", hook,
4302 hookname, argv);
4303 if (ret < 0)
4304 return -1;
4305 }
4306
4307 return 0;
4308 }
4309
4310 int lxc_clear_config_caps(struct lxc_conf *c)
4311 {
4312 struct lxc_list *it, *next;
4313
4314 lxc_list_for_each_safe (it, &c->caps, next) {
4315 lxc_list_del(it);
4316 free(it->elem);
4317 free(it);
4318 }
4319
4320 lxc_list_init(&c->caps);
4321 return 0;
4322 }
4323
4324 static int lxc_free_idmap(struct lxc_list *id_map)
4325 {
4326 struct lxc_list *it, *next;
4327
4328 lxc_list_for_each_safe(it, id_map, next) {
4329 lxc_list_del(it);
4330 free(it->elem);
4331 free(it);
4332 }
4333
4334 lxc_list_init(id_map);
4335 return 0;
4336 }
4337
4338 static int __lxc_free_idmap(struct lxc_list *id_map)
4339 {
4340 lxc_free_idmap(id_map);
4341 free(id_map);
4342 return 0;
4343 }
4344 define_cleanup_function(struct lxc_list *, __lxc_free_idmap);
4345
4346 int lxc_clear_idmaps(struct lxc_conf *c)
4347 {
4348 return lxc_free_idmap(&c->id_map);
4349 }
4350
4351 int lxc_clear_config_keepcaps(struct lxc_conf *c)
4352 {
4353 struct lxc_list *it, *next;
4354
4355 lxc_list_for_each_safe (it, &c->keepcaps, next) {
4356 lxc_list_del(it);
4357 free(it->elem);
4358 free(it);
4359 }
4360
4361 lxc_list_init(&c->keepcaps);
4362 return 0;
4363 }
4364
4365 int lxc_clear_namespace(struct lxc_conf *c)
4366 {
4367 for (int i = 0; i < LXC_NS_MAX; i++)
4368 free_disarm(c->ns_share[i]);
4369
4370 return 0;
4371 }
4372
4373 int lxc_clear_cgroups(struct lxc_conf *c, const char *key, int version)
4374 {
4375 char *global_token, *namespaced_token;
4376 size_t namespaced_token_len;
4377 struct lxc_list *it, *next, *list;
4378 const char *k = key;
4379 bool all = false;
4380
4381 if (version == CGROUP2_SUPER_MAGIC) {
4382 global_token = "lxc.cgroup2";
4383 namespaced_token = "lxc.cgroup2.";
4384 namespaced_token_len = STRLITERALLEN("lxc.cgroup2.");
4385 list = &c->cgroup2;
4386 } else if (version == CGROUP_SUPER_MAGIC) {
4387 global_token = "lxc.cgroup";
4388 namespaced_token = "lxc.cgroup.";
4389 namespaced_token_len = STRLITERALLEN("lxc.cgroup.");
4390 list = &c->cgroup;
4391 } else {
4392 return ret_errno(EINVAL);
4393 }
4394
4395 if (strequal(key, global_token))
4396 all = true;
4397 else if (strnequal(key, namespaced_token, namespaced_token_len))
4398 k += namespaced_token_len;
4399 else
4400 return ret_errno(EINVAL);
4401
4402 lxc_list_for_each_safe(it, list, next) {
4403 struct lxc_cgroup *cg = it->elem;
4404
4405 if (!all && !strequal(cg->subsystem, k))
4406 continue;
4407
4408 lxc_list_del(it);
4409 free(cg->subsystem);
4410 free(cg->value);
4411 free(cg);
4412 free(it);
4413 }
4414
4415 if (all)
4416 lxc_list_init(list);
4417
4418 return 0;
4419 }
4420
4421 static inline void lxc_clear_cgroups_devices(struct lxc_conf *conf)
4422 {
4423 lxc_clear_cgroup2_devices(&conf->bpf_devices);
4424 }
4425
4426 int lxc_clear_limits(struct lxc_conf *c, const char *key)
4427 {
4428 struct lxc_list *it, *next;
4429 const char *k = NULL;
4430 bool all = false;
4431
4432 if (strequal(key, "lxc.limit") || strequal(key, "lxc.prlimit"))
4433 all = true;
4434 else if (strnequal(key, "lxc.limit.", STRLITERALLEN("lxc.limit.")))
4435 k = key + STRLITERALLEN("lxc.limit.");
4436 else if (strnequal(key, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")))
4437 k = key + STRLITERALLEN("lxc.prlimit.");
4438 else
4439 return ret_errno(EINVAL);
4440
4441 lxc_list_for_each_safe (it, &c->limits, next) {
4442 struct lxc_limit *lim = it->elem;
4443
4444 if (!all && !strequal(lim->resource, k))
4445 continue;
4446
4447 lxc_list_del(it);
4448
4449 free_disarm(lim->resource);
4450 free(lim);
4451 free(it);
4452 }
4453
4454 if (all)
4455 lxc_list_init(&c->limits);
4456
4457 return 0;
4458 }
4459
4460 int lxc_clear_sysctls(struct lxc_conf *c, const char *key)
4461 {
4462 struct lxc_list *it, *next;
4463 const char *k = NULL;
4464 bool all = false;
4465
4466 if (strequal(key, "lxc.sysctl"))
4467 all = true;
4468 else if (strnequal(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
4469 k = key + STRLITERALLEN("lxc.sysctl.");
4470 else
4471 return -1;
4472
4473 lxc_list_for_each_safe(it, &c->sysctls, next) {
4474 struct lxc_sysctl *elem = it->elem;
4475
4476 if (!all && !strequal(elem->key, k))
4477 continue;
4478
4479 lxc_list_del(it);
4480 free(elem->key);
4481 free(elem->value);
4482 free(elem);
4483 free(it);
4484 }
4485
4486 if (all)
4487 lxc_list_init(&c->sysctls);
4488
4489 return 0;
4490 }
4491
4492 int lxc_clear_procs(struct lxc_conf *c, const char *key)
4493 {
4494 struct lxc_list *it, *next;
4495 const char *k = NULL;
4496 bool all = false;
4497
4498 if (strequal(key, "lxc.proc"))
4499 all = true;
4500 else if (strnequal(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
4501 k = key + STRLITERALLEN("lxc.proc.");
4502 else
4503 return -1;
4504
4505 lxc_list_for_each_safe(it, &c->procs, next) {
4506 struct lxc_proc *proc = it->elem;
4507
4508 if (!all && !strequal(proc->filename, k))
4509 continue;
4510
4511 lxc_list_del(it);
4512 free(proc->filename);
4513 free(proc->value);
4514 free(proc);
4515 free(it);
4516 }
4517
4518 if (all)
4519 lxc_list_init(&c->procs);
4520
4521 return 0;
4522 }
4523
4524 int lxc_clear_groups(struct lxc_conf *c)
4525 {
4526 struct lxc_list *it, *next;
4527
4528 lxc_list_for_each_safe (it, &c->groups, next) {
4529 lxc_list_del(it);
4530 free(it->elem);
4531 free(it);
4532 }
4533
4534 lxc_list_init(&c->groups);
4535 return 0;
4536 }
4537
4538 int lxc_clear_environment(struct lxc_conf *c)
4539 {
4540 struct lxc_list *it, *next;
4541
4542 lxc_list_for_each_safe (it, &c->environment, next) {
4543 lxc_list_del(it);
4544 free(it->elem);
4545 free(it);
4546 }
4547
4548 lxc_list_init(&c->environment);
4549 return 0;
4550 }
4551
4552 int lxc_clear_mount_entries(struct lxc_conf *c)
4553 {
4554 struct lxc_list *it, *next;
4555
4556 lxc_list_for_each_safe (it, &c->mount_list, next) {
4557 lxc_list_del(it);
4558 free(it->elem);
4559 free(it);
4560 }
4561
4562 lxc_list_init(&c->mount_list);
4563 return 0;
4564 }
4565
4566 int lxc_clear_automounts(struct lxc_conf *c)
4567 {
4568 c->auto_mounts = 0;
4569 return 0;
4570 }
4571
4572 int lxc_clear_hooks(struct lxc_conf *c, const char *key)
4573 {
4574 struct lxc_list *it, *next;
4575 const char *k = NULL;
4576 bool all = false, done = false;
4577
4578 if (strequal(key, "lxc.hook"))
4579 all = true;
4580 else if (strnequal(key, "lxc.hook.", STRLITERALLEN("lxc.hook.")))
4581 k = key + STRLITERALLEN("lxc.hook.");
4582 else
4583 return -1;
4584
4585 for (int i = 0; i < NUM_LXC_HOOKS; i++) {
4586 if (all || strequal(k, lxchook_names[i])) {
4587 lxc_list_for_each_safe (it, &c->hooks[i], next) {
4588 lxc_list_del(it);
4589 free(it->elem);
4590 free(it);
4591 }
4592 lxc_list_init(&c->hooks[i]);
4593
4594 done = true;
4595 }
4596 }
4597
4598 if (!done)
4599 return log_error(-1, "Invalid hook key: %s", key);
4600
4601 return 0;
4602 }
4603
4604 static inline void lxc_clear_aliens(struct lxc_conf *conf)
4605 {
4606 struct lxc_list *it, *next;
4607
4608 lxc_list_for_each_safe (it, &conf->aliens, next) {
4609 lxc_list_del(it);
4610 free(it->elem);
4611 free(it);
4612 }
4613
4614 lxc_list_init(&conf->aliens);
4615 }
4616
4617 void lxc_clear_includes(struct lxc_conf *conf)
4618 {
4619 struct lxc_list *it, *next;
4620
4621 lxc_list_for_each_safe(it, &conf->includes, next) {
4622 lxc_list_del(it);
4623 free(it->elem);
4624 free(it);
4625 }
4626
4627 lxc_list_init(&conf->includes);
4628 }
4629
4630 int lxc_clear_apparmor_raw(struct lxc_conf *c)
4631 {
4632 struct lxc_list *it, *next;
4633
4634 lxc_list_for_each_safe (it, &c->lsm_aa_raw, next) {
4635 lxc_list_del(it);
4636 free(it->elem);
4637 free(it);
4638 }
4639
4640 lxc_list_init(&c->lsm_aa_raw);
4641 return 0;
4642 }
4643
4644 void lxc_conf_free(struct lxc_conf *conf)
4645 {
4646 if (!conf)
4647 return;
4648
4649 if (current_config == conf)
4650 current_config = NULL;
4651 lxc_terminal_conf_free(&conf->console);
4652 free(conf->rootfs.mount);
4653 free(conf->rootfs.bdev_type);
4654 free(conf->rootfs.options);
4655 free(conf->rootfs.path);
4656 put_lxc_rootfs(&conf->rootfs, true);
4657 free(conf->logfile);
4658 if (conf->logfd != -1)
4659 close(conf->logfd);
4660 free(conf->utsname);
4661 free(conf->ttys.dir);
4662 free(conf->ttys.tty_names);
4663 free(conf->fstab);
4664 free(conf->rcfile);
4665 free(conf->execute_cmd);
4666 free(conf->init_cmd);
4667 free(conf->init_groups.list);
4668 free(conf->init_cwd);
4669 free(conf->unexpanded_config);
4670 free(conf->syslog);
4671 lxc_free_networks(&conf->network);
4672 free(conf->lsm_aa_profile);
4673 free(conf->lsm_aa_profile_computed);
4674 free(conf->lsm_se_context);
4675 free(conf->lsm_se_keyring_context);
4676 lxc_seccomp_free(&conf->seccomp);
4677 lxc_clear_config_caps(conf);
4678 lxc_clear_config_keepcaps(conf);
4679 lxc_clear_cgroups(conf, "lxc.cgroup", CGROUP_SUPER_MAGIC);
4680 lxc_clear_cgroups(conf, "lxc.cgroup2", CGROUP2_SUPER_MAGIC);
4681 lxc_clear_cgroups_devices(conf);
4682 lxc_clear_hooks(conf, "lxc.hook");
4683 lxc_clear_mount_entries(conf);
4684 lxc_clear_idmaps(conf);
4685 lxc_clear_groups(conf);
4686 lxc_clear_includes(conf);
4687 lxc_clear_aliens(conf);
4688 lxc_clear_environment(conf);
4689 lxc_clear_limits(conf, "lxc.prlimit");
4690 lxc_clear_sysctls(conf, "lxc.sysctl");
4691 lxc_clear_procs(conf, "lxc.proc");
4692 lxc_clear_apparmor_raw(conf);
4693 lxc_clear_namespace(conf);
4694 free(conf->cgroup_meta.dir);
4695 free(conf->cgroup_meta.monitor_dir);
4696 free(conf->cgroup_meta.monitor_pivot_dir);
4697 free(conf->cgroup_meta.container_dir);
4698 free(conf->cgroup_meta.namespace_dir);
4699 free(conf->cgroup_meta.controllers);
4700 free(conf->shmount.path_host);
4701 free(conf->shmount.path_cont);
4702 free(conf);
4703 }
4704
4705 struct userns_fn_data {
4706 int (*fn)(void *);
4707 const char *fn_name;
4708 void *arg;
4709 int p[2];
4710 };
4711
4712 static int run_userns_fn(void *data)
4713 {
4714 struct userns_fn_data *d = data;
4715 int ret;
4716 char c;
4717
4718 close_prot_errno_disarm(d->p[1]);
4719
4720 /*
4721 * Wait for parent to finish establishing a new mapping in the user
4722 * namespace we are executing in.
4723 */
4724 ret = lxc_read_nointr(d->p[0], &c, 1);
4725 close_prot_errno_disarm(d->p[0]);
4726 if (ret != 1)
4727 return -1;
4728
4729 if (d->fn_name)
4730 TRACE("Calling function \"%s\"", d->fn_name);
4731
4732 /* Call function to run. */
4733 return d->fn(d->arg);
4734 }
4735
4736 static struct id_map *mapped_nsid_add(const struct lxc_conf *conf, unsigned id,
4737 enum idtype idtype)
4738 {
4739 const struct id_map *map;
4740 struct id_map *retmap;
4741
4742 map = find_mapped_nsid_entry(conf, id, idtype);
4743 if (!map)
4744 return NULL;
4745
4746 retmap = zalloc(sizeof(*retmap));
4747 if (!retmap)
4748 return NULL;
4749
4750 memcpy(retmap, map, sizeof(*retmap));
4751 return retmap;
4752 }
4753
4754 static struct id_map *find_mapped_hostid_entry(const struct lxc_list *idmap,
4755 unsigned id, enum idtype idtype)
4756 {
4757 struct id_map *map;
4758 struct lxc_list *it;
4759 struct id_map *retmap = NULL;
4760
4761 lxc_list_for_each (it, idmap) {
4762 map = it->elem;
4763 if (map->idtype != idtype)
4764 continue;
4765
4766 if (id >= map->hostid && id < map->hostid + map->range) {
4767 retmap = map;
4768 break;
4769 }
4770 }
4771
4772 return retmap;
4773 }
4774
4775 /* Allocate a new {g,u}id mapping for the given {g,u}id. Re-use an already
4776 * existing one or establish a new one.
4777 */
4778 static struct id_map *mapped_hostid_add(const struct lxc_conf *conf, uid_t id,
4779 enum idtype type)
4780 {
4781 __do_free struct id_map *entry = NULL;
4782 int hostid_mapped;
4783 struct id_map *tmp = NULL;
4784
4785 entry = zalloc(sizeof(*entry));
4786 if (!entry)
4787 return NULL;
4788
4789 /* Reuse existing mapping. */
4790 tmp = find_mapped_hostid_entry(&conf->id_map, id, type);
4791 if (tmp) {
4792 memcpy(entry, tmp, sizeof(*entry));
4793 } else {
4794 /* Find new mapping. */
4795 hostid_mapped = find_unmapped_nsid(conf, type);
4796 if (hostid_mapped < 0)
4797 return log_debug(NULL, "Failed to find free mapping for id %d", id);
4798
4799 entry->idtype = type;
4800 entry->nsid = hostid_mapped;
4801 entry->hostid = (unsigned long)id;
4802 entry->range = 1;
4803 }
4804
4805 return move_ptr(entry);
4806 }
4807
4808 static struct lxc_list *get_minimal_idmap(const struct lxc_conf *conf,
4809 uid_t *resuid, gid_t *resgid)
4810 {
4811 __do_free struct id_map *container_root_uid = NULL,
4812 *container_root_gid = NULL,
4813 *host_uid_map = NULL, *host_gid_map = NULL;
4814 __do_free struct lxc_list *idmap = NULL;
4815 uid_t euid, egid;
4816 uid_t nsuid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid;
4817 gid_t nsgid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid;
4818 struct lxc_list *tmplist = NULL;
4819
4820 /* Find container root mappings. */
4821 container_root_uid = mapped_nsid_add(conf, nsuid, ID_TYPE_UID);
4822 if (!container_root_uid)
4823 return log_debug(NULL, "Failed to find mapping for namespace uid %d", 0);
4824 euid = geteuid();
4825 if (euid >= container_root_uid->hostid &&
4826 euid < (container_root_uid->hostid + container_root_uid->range))
4827 host_uid_map = move_ptr(container_root_uid);
4828
4829 container_root_gid = mapped_nsid_add(conf, nsgid, ID_TYPE_GID);
4830 if (!container_root_gid)
4831 return log_debug(NULL, "Failed to find mapping for namespace gid %d", 0);
4832 egid = getegid();
4833 if (egid >= container_root_gid->hostid &&
4834 egid < (container_root_gid->hostid + container_root_gid->range))
4835 host_gid_map = move_ptr(container_root_gid);
4836
4837 /* Check whether the {g,u}id of the user has a mapping. */
4838 if (!host_uid_map)
4839 host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID);
4840 if (!host_uid_map)
4841 return log_debug(NULL, "Failed to find mapping for uid %d", euid);
4842
4843 if (!host_gid_map)
4844 host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID);
4845 if (!host_gid_map)
4846 return log_debug(NULL, "Failed to find mapping for gid %d", egid);
4847
4848 /* Allocate new {g,u}id map list. */
4849 idmap = lxc_list_new();
4850 if (!idmap)
4851 return NULL;
4852
4853 /* Add container root to the map. */
4854 tmplist = lxc_list_new();
4855 if (!tmplist)
4856 return NULL;
4857 /* idmap will now keep track of that memory. */
4858 lxc_list_add_elem(tmplist, move_ptr(host_uid_map));
4859 lxc_list_add_tail(idmap, tmplist);
4860
4861 if (container_root_uid) {
4862 /* Add container root to the map. */
4863 tmplist = lxc_list_new();
4864 if (!tmplist)
4865 return NULL;
4866 /* idmap will now keep track of that memory. */
4867 lxc_list_add_elem(tmplist, move_ptr(container_root_uid));
4868 lxc_list_add_tail(idmap, tmplist);
4869 }
4870
4871 tmplist = lxc_list_new();
4872 if (!tmplist)
4873 return NULL;
4874 /* idmap will now keep track of that memory. */
4875 lxc_list_add_elem(tmplist, move_ptr(host_gid_map));
4876 lxc_list_add_tail(idmap, tmplist);
4877
4878 if (container_root_gid) {
4879 tmplist = lxc_list_new();
4880 if (!tmplist)
4881 return NULL;
4882 /* idmap will now keep track of that memory. */
4883 lxc_list_add_elem(tmplist, move_ptr(container_root_gid));
4884 lxc_list_add_tail(idmap, tmplist);
4885 }
4886
4887 TRACE("Allocated minimal idmapping for ns uid %d and ns gid %d", nsuid, nsgid);
4888
4889 if (resuid)
4890 *resuid = nsuid;
4891 if (resgid)
4892 *resgid = nsgid;
4893 return move_ptr(idmap);
4894 }
4895
4896 /*
4897 * Run a function in a new user namespace.
4898 * The caller's euid/egid will be mapped if it is not already.
4899 * Afaict, userns_exec_1() is only used to operate based on privileges for the
4900 * user's own {g,u}id on the host and for the container root's unmapped {g,u}id.
4901 * This means we require only to establish a mapping from:
4902 * - the container root {g,u}id as seen from the host > user's host {g,u}id
4903 * - the container root -> some sub{g,u}id
4904 * The former we add, if the user did not specify a mapping. The latter we
4905 * retrieve from the container's configured {g,u}id mappings as it must have been
4906 * there to start the container in the first place.
4907 */
4908 int userns_exec_1(const struct lxc_conf *conf, int (*fn)(void *), void *data,
4909 const char *fn_name)
4910 {
4911 call_cleaner(__lxc_free_idmap) struct lxc_list *idmap = NULL;
4912 int ret = -1, status = -1;
4913 char c = '1';
4914 struct userns_fn_data d = {
4915 .arg = data,
4916 .fn = fn,
4917 .fn_name = fn_name,
4918 };
4919 pid_t pid;
4920 int pipe_fds[2];
4921
4922 if (!conf)
4923 return -EINVAL;
4924
4925 idmap = get_minimal_idmap(conf, NULL, NULL);
4926 if (!idmap)
4927 return ret_errno(ENOENT);
4928
4929 ret = pipe2(pipe_fds, O_CLOEXEC);
4930 if (ret < 0)
4931 return -errno;
4932
4933 d.p[0] = pipe_fds[0];
4934 d.p[1] = pipe_fds[1];
4935
4936 /* Clone child in new user namespace. */
4937 pid = lxc_raw_clone_cb(run_userns_fn, &d, CLONE_NEWUSER, NULL);
4938 if (pid < 0) {
4939 ERROR("Failed to clone process in new user namespace");
4940 goto on_error;
4941 }
4942
4943 close_prot_errno_disarm(pipe_fds[0]);
4944
4945 if (lxc_log_trace()) {
4946 struct id_map *map;
4947 struct lxc_list *it;
4948
4949 lxc_list_for_each(it, idmap) {
4950 map = it->elem;
4951 TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
4952 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, map->nsid, map->hostid, map->range);
4953 }
4954 }
4955
4956 /* Set up {g,u}id mapping for user namespace of child process. */
4957 ret = lxc_map_ids(idmap, pid);
4958 if (ret < 0) {
4959 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid);
4960 goto on_error;
4961 }
4962
4963 /* Tell child to proceed. */
4964 if (lxc_write_nointr(pipe_fds[1], &c, 1) != 1) {
4965 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
4966 goto on_error;
4967 }
4968
4969 on_error:
4970 close_prot_errno_disarm(pipe_fds[0]);
4971 close_prot_errno_disarm(pipe_fds[1]);
4972
4973 /* Wait for child to finish. */
4974 if (pid > 0)
4975 status = wait_for_pid(pid);
4976
4977 if (status < 0)
4978 ret = -1;
4979
4980 return ret;
4981 }
4982
4983 int userns_exec_minimal(const struct lxc_conf *conf,
4984 int (*fn_parent)(void *), void *fn_parent_data,
4985 int (*fn_child)(void *), void *fn_child_data)
4986 {
4987 call_cleaner(__lxc_free_idmap) struct lxc_list *idmap = NULL;
4988 uid_t resuid = LXC_INVALID_UID;
4989 gid_t resgid = LXC_INVALID_GID;
4990 char c = '1';
4991 ssize_t ret;
4992 pid_t pid;
4993 int sock_fds[2];
4994
4995 if (!conf || !fn_child)
4996 return ret_errno(EINVAL);
4997
4998 idmap = get_minimal_idmap(conf, &resuid, &resgid);
4999 if (!idmap)
5000 return ret_errno(ENOENT);
5001
5002 ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sock_fds);
5003 if (ret < 0)
5004 return -errno;
5005
5006 pid = fork();
5007 if (pid < 0) {
5008 SYSERROR("Failed to create new process");
5009 goto on_error;
5010 }
5011
5012 if (pid == 0) {
5013 close_prot_errno_disarm(sock_fds[1]);
5014
5015 ret = unshare(CLONE_NEWUSER);
5016 if (ret < 0) {
5017 SYSERROR("Failed to unshare new user namespace");
5018 _exit(EXIT_FAILURE);
5019 }
5020
5021 ret = lxc_write_nointr(sock_fds[0], &c, 1);
5022 if (ret != 1)
5023 _exit(EXIT_FAILURE);
5024
5025 ret = lxc_read_nointr(sock_fds[0], &c, 1);
5026 if (ret != 1)
5027 _exit(EXIT_FAILURE);
5028
5029 close_prot_errno_disarm(sock_fds[0]);
5030
5031 if (!lxc_drop_groups() && errno != EPERM)
5032 _exit(EXIT_FAILURE);
5033
5034 ret = setresgid(resgid, resgid, resgid);
5035 if (ret < 0) {
5036 SYSERROR("Failed to setresgid(%d, %d, %d)",
5037 resgid, resgid, resgid);
5038 _exit(EXIT_FAILURE);
5039 }
5040
5041 ret = setresuid(resuid, resuid, resuid);
5042 if (ret < 0) {
5043 SYSERROR("Failed to setresuid(%d, %d, %d)",
5044 resuid, resuid, resuid);
5045 _exit(EXIT_FAILURE);
5046 }
5047
5048 ret = fn_child(fn_child_data);
5049 if (ret) {
5050 SYSERROR("Running function in new user namespace failed");
5051 _exit(EXIT_FAILURE);
5052 }
5053
5054 _exit(EXIT_SUCCESS);
5055 }
5056
5057 close_prot_errno_disarm(sock_fds[0]);
5058
5059 if (lxc_log_trace()) {
5060 struct id_map *map;
5061 struct lxc_list *it;
5062
5063 lxc_list_for_each(it, idmap) {
5064 map = it->elem;
5065 TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
5066 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, map->nsid, map->hostid, map->range);
5067 }
5068 }
5069
5070 ret = lxc_read_nointr(sock_fds[1], &c, 1);
5071 if (ret != 1) {
5072 SYSERROR("Failed waiting for child process %d\" to tell us to proceed", pid);
5073 goto on_error;
5074 }
5075
5076 /* Set up {g,u}id mapping for user namespace of child process. */
5077 ret = lxc_map_ids(idmap, pid);
5078 if (ret < 0) {
5079 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid);
5080 goto on_error;
5081 }
5082
5083 /* Tell child to proceed. */
5084 ret = lxc_write_nointr(sock_fds[1], &c, 1);
5085 if (ret != 1) {
5086 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
5087 goto on_error;
5088 }
5089
5090 if (fn_parent && fn_parent(fn_parent_data)) {
5091 SYSERROR("Running parent function failed");
5092 _exit(EXIT_FAILURE);
5093 }
5094
5095 on_error:
5096 close_prot_errno_disarm(sock_fds[0]);
5097 close_prot_errno_disarm(sock_fds[1]);
5098
5099 /* Wait for child to finish. */
5100 if (pid < 0)
5101 return -1;
5102
5103 return wait_for_pid(pid);
5104 }
5105
5106 int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data,
5107 const char *fn_name)
5108 {
5109 pid_t pid;
5110 uid_t euid, egid;
5111 int p[2];
5112 struct id_map *map;
5113 struct lxc_list *cur;
5114 struct userns_fn_data d;
5115 int ret = -1;
5116 char c = '1';
5117 struct lxc_list *idmap = NULL, *tmplist = NULL;
5118 struct id_map *container_root_uid = NULL, *container_root_gid = NULL,
5119 *host_uid_map = NULL, *host_gid_map = NULL;
5120
5121 if (!conf)
5122 return -EINVAL;
5123
5124 ret = pipe2(p, O_CLOEXEC);
5125 if (ret < 0) {
5126 SYSERROR("opening pipe");
5127 return -1;
5128 }
5129 d.fn = fn;
5130 d.fn_name = fn_name;
5131 d.arg = data;
5132 d.p[0] = p[0];
5133 d.p[1] = p[1];
5134
5135 /* Clone child in new user namespace. */
5136 pid = lxc_clone(run_userns_fn, &d, CLONE_NEWUSER, NULL);
5137 if (pid < 0) {
5138 ERROR("Failed to clone process in new user namespace");
5139 goto on_error;
5140 }
5141
5142 close(p[0]);
5143 p[0] = -1;
5144
5145 euid = geteuid();
5146 egid = getegid();
5147
5148 /* Allocate new {g,u}id map list. */
5149 idmap = lxc_list_new();
5150 if (!idmap)
5151 goto on_error;
5152
5153 /* Find container root. */
5154 lxc_list_for_each (cur, &conf->id_map) {
5155 struct id_map *tmpmap;
5156
5157 tmplist = lxc_list_new();
5158 if (!tmplist)
5159 goto on_error;
5160
5161 tmpmap = zalloc(sizeof(*tmpmap));
5162 if (!tmpmap) {
5163 free(tmplist);
5164 goto on_error;
5165 }
5166
5167 memset(tmpmap, 0, sizeof(*tmpmap));
5168 memcpy(tmpmap, cur->elem, sizeof(*tmpmap));
5169 tmplist->elem = tmpmap;
5170
5171 lxc_list_add_tail(idmap, tmplist);
5172
5173 map = cur->elem;
5174
5175 if (map->idtype == ID_TYPE_UID)
5176 if (euid >= map->hostid && euid < map->hostid + map->range)
5177 host_uid_map = map;
5178
5179 if (map->idtype == ID_TYPE_GID)
5180 if (egid >= map->hostid && egid < map->hostid + map->range)
5181 host_gid_map = map;
5182
5183 if (map->nsid != 0)
5184 continue;
5185
5186 if (map->idtype == ID_TYPE_UID)
5187 if (container_root_uid == NULL)
5188 container_root_uid = map;
5189
5190 if (map->idtype == ID_TYPE_GID)
5191 if (container_root_gid == NULL)
5192 container_root_gid = map;
5193 }
5194
5195 if (!container_root_uid || !container_root_gid) {
5196 ERROR("No mapping for container root found");
5197 goto on_error;
5198 }
5199
5200 /* Check whether the {g,u}id of the user has a mapping. */
5201 if (!host_uid_map)
5202 host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID);
5203 else
5204 host_uid_map = container_root_uid;
5205
5206 if (!host_gid_map)
5207 host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID);
5208 else
5209 host_gid_map = container_root_gid;
5210
5211 if (!host_uid_map) {
5212 DEBUG("Failed to find mapping for uid %d", euid);
5213 goto on_error;
5214 }
5215
5216 if (!host_gid_map) {
5217 DEBUG("Failed to find mapping for gid %d", egid);
5218 goto on_error;
5219 }
5220
5221 if (host_uid_map && (host_uid_map != container_root_uid)) {
5222 /* Add container root to the map. */
5223 tmplist = lxc_list_new();
5224 if (!tmplist)
5225 goto on_error;
5226 lxc_list_add_elem(tmplist, host_uid_map);
5227 lxc_list_add_tail(idmap, tmplist);
5228 }
5229 /* idmap will now keep track of that memory. */
5230 host_uid_map = NULL;
5231
5232 if (host_gid_map && (host_gid_map != container_root_gid)) {
5233 tmplist = lxc_list_new();
5234 if (!tmplist)
5235 goto on_error;
5236 lxc_list_add_elem(tmplist, host_gid_map);
5237 lxc_list_add_tail(idmap, tmplist);
5238 }
5239 /* idmap will now keep track of that memory. */
5240 host_gid_map = NULL;
5241
5242 if (lxc_log_trace()) {
5243 lxc_list_for_each (cur, idmap) {
5244 map = cur->elem;
5245 TRACE("establishing %cid mapping for \"%d\" in new "
5246 "user namespace: nsuid %lu - hostid %lu - range "
5247 "%lu",
5248 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid,
5249 map->nsid, map->hostid, map->range);
5250 }
5251 }
5252
5253 /* Set up {g,u}id mapping for user namespace of child process. */
5254 ret = lxc_map_ids(idmap, pid);
5255 if (ret < 0) {
5256 ERROR("error setting up {g,u}id mappings for child process \"%d\"", pid);
5257 goto on_error;
5258 }
5259
5260 /* Tell child to proceed. */
5261 if (lxc_write_nointr(p[1], &c, 1) != 1) {
5262 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
5263 goto on_error;
5264 }
5265
5266 on_error:
5267 if (p[0] != -1)
5268 close(p[0]);
5269 close(p[1]);
5270
5271 /* Wait for child to finish. */
5272 if (pid > 0)
5273 ret = wait_for_pid(pid);
5274
5275 if (idmap)
5276 __lxc_free_idmap(idmap);
5277
5278 if (host_uid_map && (host_uid_map != container_root_uid))
5279 free(host_uid_map);
5280 if (host_gid_map && (host_gid_map != container_root_gid))
5281 free(host_gid_map);
5282
5283 return ret;
5284 }
5285
5286 static int add_idmap_entry(struct lxc_list *idmap, enum idtype idtype,
5287 unsigned long nsid, unsigned long hostid,
5288 unsigned long range)
5289 {
5290 __do_free struct id_map *new_idmap = NULL;
5291 __do_free struct lxc_list *new_list = NULL;
5292
5293 new_idmap = zalloc(sizeof(*new_idmap));
5294 if (!new_idmap)
5295 return ret_errno(ENOMEM);
5296
5297 new_idmap->idtype = idtype;
5298 new_idmap->hostid = hostid;
5299 new_idmap->nsid = nsid;
5300 new_idmap->range = range;
5301
5302 new_list = zalloc(sizeof(*new_list));
5303 if (!new_list)
5304 return ret_errno(ENOMEM);
5305
5306 new_list->elem = move_ptr(new_idmap);
5307 lxc_list_add_tail(idmap, move_ptr(new_list));
5308
5309 INFO("Adding id map: type %c nsid %lu hostid %lu range %lu",
5310 idtype == ID_TYPE_UID ? 'u' : 'g', nsid, hostid, range);
5311 return 0;
5312 }
5313
5314 int userns_exec_mapped_root(const char *path, int path_fd,
5315 const struct lxc_conf *conf)
5316 {
5317 call_cleaner(__lxc_free_idmap) struct lxc_list *idmap = NULL;
5318 __do_close int fd = -EBADF;
5319 int target_fd = -EBADF;
5320 char c = '1';
5321 ssize_t ret;
5322 pid_t pid;
5323 int sock_fds[2];
5324 uid_t container_host_uid, hostuid;
5325 gid_t container_host_gid, hostgid;
5326 struct stat st;
5327
5328 if (!conf || (!path && path_fd < 0))
5329 return ret_errno(EINVAL);
5330
5331 if (!path)
5332 path = "(null)";
5333
5334 container_host_uid = get_mapped_rootid(conf, ID_TYPE_UID);
5335 if (!uid_valid(container_host_uid))
5336 return log_error(-1, "No uid mapping for container root");
5337
5338 container_host_gid = get_mapped_rootid(conf, ID_TYPE_GID);
5339 if (!gid_valid(container_host_gid))
5340 return log_error(-1, "No gid mapping for container root");
5341
5342 if (path_fd < 0) {
5343 fd = open(path, O_CLOEXEC | O_NOCTTY);
5344 if (fd < 0)
5345 return log_error_errno(-errno, errno, "Failed to open \"%s\"", path);
5346 target_fd = fd;
5347 } else {
5348 target_fd = path_fd;
5349 }
5350
5351 hostuid = geteuid();
5352 /* We are root so chown directly. */
5353 if (hostuid == 0) {
5354 ret = fchown(target_fd, container_host_uid, container_host_gid);
5355 if (ret)
5356 return log_error_errno(-errno, errno,
5357 "Failed to fchown(%d(%s), %d, %d)",
5358 target_fd, path, container_host_uid,
5359 container_host_gid);
5360 return log_trace(0, "Chowned %d(%s) to uid %d and %d", target_fd, path,
5361 container_host_uid, container_host_gid);
5362 }
5363
5364 /* The container's root host id matches */
5365 if (container_host_uid == hostuid)
5366 return log_info(0, "Container root id is mapped to our uid");
5367
5368 /* Get the current ids of our target. */
5369 ret = fstat(target_fd, &st);
5370 if (ret)
5371 return log_error_errno(-errno, errno, "Failed to stat \"%s\"", path);
5372
5373 hostgid = getegid();
5374 if (st.st_uid == hostuid && mapped_hostid(st.st_gid, conf, ID_TYPE_GID) < 0) {
5375 ret = fchown(target_fd, -1, hostgid);
5376 if (ret)
5377 return log_error_errno(-errno, errno,
5378 "Failed to fchown(%d(%s), -1, %d)",
5379 target_fd, path, hostgid);
5380 TRACE("Chowned %d(%s) to -1:%d", target_fd, path, hostgid);
5381 }
5382
5383 idmap = lxc_list_new();
5384 if (!idmap)
5385 return -ENOMEM;
5386
5387 /* "u:0:rootuid:1" */
5388 ret = add_idmap_entry(idmap, ID_TYPE_UID, 0, container_host_uid, 1);
5389 if (ret < 0)
5390 return log_error_errno(ret, -ret, "Failed to add idmap entry");
5391
5392 /* "u:hostuid:hostuid:1" */
5393 ret = add_idmap_entry(idmap, ID_TYPE_UID, hostuid, hostuid, 1);
5394 if (ret < 0)
5395 return log_error_errno(ret, -ret, "Failed to add idmap entry");
5396
5397 /* "g:0:rootgid:1" */
5398 ret = add_idmap_entry(idmap, ID_TYPE_GID, 0, container_host_gid, 1);
5399 if (ret < 0)
5400 return log_error_errno(ret, -ret, "Failed to add idmap entry");
5401
5402 /* "g:hostgid:hostgid:1" */
5403 ret = add_idmap_entry(idmap, ID_TYPE_GID, hostgid, hostgid, 1);
5404 if (ret < 0)
5405 return log_error_errno(ret, -ret, "Failed to add idmap entry");
5406
5407 if (hostgid != st.st_gid) {
5408 /* "g:pathgid:rootgid+pathgid:1" */
5409 ret = add_idmap_entry(idmap, ID_TYPE_GID, st.st_gid,
5410 container_host_gid + (gid_t)st.st_gid, 1);
5411 if (ret < 0)
5412 return log_error_errno(ret, -ret, "Failed to add idmap entry");
5413 }
5414
5415 ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sock_fds);
5416 if (ret < 0)
5417 return -errno;
5418
5419 pid = fork();
5420 if (pid < 0) {
5421 SYSERROR("Failed to create new process");
5422 goto on_error;
5423 }
5424
5425 if (pid == 0) {
5426 close_prot_errno_disarm(sock_fds[1]);
5427
5428 ret = unshare(CLONE_NEWUSER);
5429 if (ret < 0) {
5430 SYSERROR("Failed to unshare new user namespace");
5431 _exit(EXIT_FAILURE);
5432 }
5433
5434 ret = lxc_write_nointr(sock_fds[0], &c, 1);
5435 if (ret != 1)
5436 _exit(EXIT_FAILURE);
5437
5438 ret = lxc_read_nointr(sock_fds[0], &c, 1);
5439 if (ret != 1)
5440 _exit(EXIT_FAILURE);
5441
5442 close_prot_errno_disarm(sock_fds[0]);
5443
5444 if (!lxc_switch_uid_gid(0, 0))
5445 _exit(EXIT_FAILURE);
5446
5447 if (!lxc_drop_groups())
5448 _exit(EXIT_FAILURE);
5449
5450 ret = fchown(target_fd, 0, st.st_gid);
5451 if (ret) {
5452 SYSERROR("Failed to chown %d(%s) to 0:%d", target_fd, path, st.st_gid);
5453 _exit(EXIT_FAILURE);
5454 }
5455
5456 TRACE("Chowned %d(%s) to 0:%d", target_fd, path, st.st_gid);
5457 _exit(EXIT_SUCCESS);
5458 }
5459
5460 close_prot_errno_disarm(sock_fds[0]);
5461
5462 if (lxc_log_trace()) {
5463 struct id_map *map;
5464 struct lxc_list *it;
5465
5466 lxc_list_for_each(it, idmap) {
5467 map = it->elem;
5468 TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
5469 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, map->nsid, map->hostid, map->range);
5470 }
5471 }
5472
5473 ret = lxc_read_nointr(sock_fds[1], &c, 1);
5474 if (ret != 1) {
5475 SYSERROR("Failed waiting for child process %d\" to tell us to proceed", pid);
5476 goto on_error;
5477 }
5478
5479 /* Set up {g,u}id mapping for user namespace of child process. */
5480 ret = lxc_map_ids(idmap, pid);
5481 if (ret < 0) {
5482 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid);
5483 goto on_error;
5484 }
5485
5486 /* Tell child to proceed. */
5487 ret = lxc_write_nointr(sock_fds[1], &c, 1);
5488 if (ret != 1) {
5489 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
5490 goto on_error;
5491 }
5492
5493 on_error:
5494 close_prot_errno_disarm(sock_fds[0]);
5495 close_prot_errno_disarm(sock_fds[1]);
5496
5497 /* Wait for child to finish. */
5498 if (pid < 0)
5499 return -1;
5500
5501 return wait_for_pid(pid);
5502 }
5503
5504 /* not thread-safe, do not use from api without first forking */
5505 static char *getuname(void)
5506 {
5507 __do_free char *buf = NULL;
5508 struct passwd pwent;
5509 struct passwd *pwentp = NULL;
5510 size_t bufsize;
5511 int ret;
5512
5513 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
5514 if (bufsize == -1)
5515 bufsize = 1024;
5516
5517 buf = zalloc(bufsize);
5518 if (!buf)
5519 return NULL;
5520
5521 ret = getpwuid_r(geteuid(), &pwent, buf, bufsize, &pwentp);
5522 if (!pwentp) {
5523 if (ret == 0)
5524 WARN("Could not find matched password record.");
5525
5526 return log_error(NULL, "Failed to get password record - %u", geteuid());
5527 }
5528
5529 return strdup(pwent.pw_name);
5530 }
5531
5532 /* not thread-safe, do not use from api without first forking */
5533 static char *getgname(void)
5534 {
5535 __do_free char *buf = NULL;
5536 struct group grent;
5537 struct group *grentp = NULL;
5538 size_t bufsize;
5539 int ret;
5540
5541 bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
5542 if (bufsize == -1)
5543 bufsize = 1024;
5544
5545 buf = zalloc(bufsize);
5546 if (!buf)
5547 return NULL;
5548
5549 ret = getgrgid_r(getegid(), &grent, buf, bufsize, &grentp);
5550 if (!grentp) {
5551 if (ret == 0)
5552 WARN("Could not find matched group record");
5553
5554 return log_error(NULL, "Failed to get group record - %u", getegid());
5555 }
5556
5557 return strdup(grent.gr_name);
5558 }
5559
5560 /* not thread-safe, do not use from api without first forking */
5561 void suggest_default_idmap(void)
5562 {
5563 __do_free char *gname = NULL, *line = NULL, *uname = NULL;
5564 __do_fclose FILE *subuid_f = NULL, *subgid_f = NULL;
5565 unsigned int uid = 0, urange = 0, gid = 0, grange = 0;
5566 size_t len = 0;
5567
5568 uname = getuname();
5569 if (!uname)
5570 return;
5571
5572 gname = getgname();
5573 if (!gname)
5574 return;
5575
5576 subuid_f = fopen(subuidfile, "re");
5577 if (!subuid_f) {
5578 ERROR("Your system is not configured with subuids");
5579 return;
5580 }
5581
5582 while (getline(&line, &len, subuid_f) != -1) {
5583 char *p, *p2;
5584 size_t no_newline = 0;
5585
5586 p = strchr(line, ':');
5587 if (*line == '#')
5588 continue;
5589 if (!p)
5590 continue;
5591 *p = '\0';
5592 p++;
5593
5594 if (!strequal(line, uname))
5595 continue;
5596
5597 p2 = strchr(p, ':');
5598 if (!p2)
5599 continue;
5600 *p2 = '\0';
5601 p2++;
5602 if (!*p2)
5603 continue;
5604 no_newline = strcspn(p2, "\n");
5605 p2[no_newline] = '\0';
5606
5607 if (lxc_safe_uint(p, &uid) < 0)
5608 WARN("Could not parse UID");
5609 if (lxc_safe_uint(p2, &urange) < 0)
5610 WARN("Could not parse UID range");
5611 }
5612
5613 subgid_f = fopen(subgidfile, "re");
5614 if (!subgid_f) {
5615 ERROR("Your system is not configured with subgids");
5616 return;
5617 }
5618
5619 while (getline(&line, &len, subgid_f) != -1) {
5620 char *p, *p2;
5621 size_t no_newline = 0;
5622
5623 p = strchr(line, ':');
5624 if (*line == '#')
5625 continue;
5626 if (!p)
5627 continue;
5628 *p = '\0';
5629 p++;
5630
5631 if (!strequal(line, uname))
5632 continue;
5633
5634 p2 = strchr(p, ':');
5635 if (!p2)
5636 continue;
5637 *p2 = '\0';
5638 p2++;
5639 if (!*p2)
5640 continue;
5641 no_newline = strcspn(p2, "\n");
5642 p2[no_newline] = '\0';
5643
5644 if (lxc_safe_uint(p, &gid) < 0)
5645 WARN("Could not parse GID");
5646 if (lxc_safe_uint(p2, &grange) < 0)
5647 WARN("Could not parse GID range");
5648 }
5649
5650 if (!urange || !grange) {
5651 ERROR("You do not have subuids or subgids allocated");
5652 ERROR("Unprivileged containers require subuids and subgids");
5653 return;
5654 }
5655
5656 ERROR("You must either run as root, or define uid mappings");
5657 ERROR("To pass uid mappings to lxc-create, you could create");
5658 ERROR("~/.config/lxc/default.conf:");
5659 ERROR("lxc.include = %s", LXC_DEFAULT_CONFIG);
5660 ERROR("lxc.idmap = u 0 %u %u", uid, urange);
5661 ERROR("lxc.idmap = g 0 %u %u", gid, grange);
5662 }
5663
5664 static void free_cgroup_settings(struct lxc_list *result)
5665 {
5666 struct lxc_list *iterator, *next;
5667
5668 lxc_list_for_each_safe (iterator, result, next) {
5669 lxc_list_del(iterator);
5670 free_disarm(iterator);
5671 }
5672 free_disarm(result);
5673 }
5674
5675 /* Return the list of cgroup_settings sorted according to the following rules
5676 * 1. Put memory.limit_in_bytes before memory.memsw.limit_in_bytes
5677 */
5678 struct lxc_list *sort_cgroup_settings(struct lxc_list *cgroup_settings)
5679 {
5680 struct lxc_list *result;
5681 struct lxc_cgroup *cg = NULL;
5682 struct lxc_list *it = NULL, *item = NULL, *memsw_limit = NULL;
5683
5684 result = lxc_list_new();
5685 if (!result)
5686 return NULL;
5687 lxc_list_init(result);
5688
5689 /* Iterate over the cgroup settings and copy them to the output list. */
5690 lxc_list_for_each (it, cgroup_settings) {
5691 item = zalloc(sizeof(*item));
5692 if (!item) {
5693 free_cgroup_settings(result);
5694 return NULL;
5695 }
5696
5697 item->elem = it->elem;
5698 cg = it->elem;
5699 if (strequal(cg->subsystem, "memory.memsw.limit_in_bytes")) {
5700 /* Store the memsw_limit location */
5701 memsw_limit = item;
5702 } else if (strequal(cg->subsystem, "memory.limit_in_bytes") &&
5703 memsw_limit != NULL) {
5704 /* lxc.cgroup.memory.memsw.limit_in_bytes is found
5705 * before lxc.cgroup.memory.limit_in_bytes, swap these
5706 * two items */
5707 item->elem = memsw_limit->elem;
5708 memsw_limit->elem = it->elem;
5709 }
5710 lxc_list_add_tail(result, item);
5711 }
5712
5713 return result;
5714 }