]> git.proxmox.com Git - mirror_lxc.git/blame_incremental - src/lxc/conf.c
Merge pull request #3410 from brauner/2020-05-13/fixes
[mirror_lxc.git] / src / lxc / conf.c
... / ...
CommitLineData
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 <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <sys/mman.h>
22#include <sys/mount.h>
23#include <sys/param.h>
24#include <sys/prctl.h>
25#include <sys/sendfile.h>
26#include <sys/socket.h>
27#include <sys/stat.h>
28#include <sys/syscall.h>
29#include <sys/sysmacros.h>
30#include <sys/types.h>
31#include <sys/utsname.h>
32#include <sys/wait.h>
33#include <time.h>
34#include <unistd.h>
35
36#include "af_unix.h"
37#include "caps.h"
38#include "cgroup.h"
39#include "cgroup2_devices.h"
40#include "conf.h"
41#include "config.h"
42#include "confile.h"
43#include "confile_utils.h"
44#include "error.h"
45#include "log.h"
46#include "lsm/lsm.h"
47#include "lxclock.h"
48#include "lxcseccomp.h"
49#include "macro.h"
50#include "memory_utils.h"
51#include "namespace.h"
52#include "network.h"
53#include "parse.h"
54#include "raw_syscalls.h"
55#include "ringbuf.h"
56#include "start.h"
57#include "storage.h"
58#include "storage/overlay.h"
59#include "syscall_wrappers.h"
60#include "terminal.h"
61#include "utils.h"
62#include "uuid.h"
63
64#ifdef MAJOR_IN_MKDEV
65#include <sys/mkdev.h>
66#endif
67
68#ifdef HAVE_STATVFS
69#include <sys/statvfs.h>
70#endif
71
72#if HAVE_PTY_H
73#include <pty.h>
74#else
75#include <../include/openpty.h>
76#endif
77
78#if HAVE_LIBCAP
79#include <sys/capability.h>
80#endif
81
82#if HAVE_SYS_PERSONALITY_H
83#include <sys/personality.h>
84#endif
85
86#ifndef HAVE_STRLCAT
87#include "include/strlcat.h"
88#endif
89
90#if IS_BIONIC
91#include <../include/lxcmntent.h>
92#else
93#include <mntent.h>
94#endif
95
96#if !defined(HAVE_PRLIMIT) && defined(HAVE_PRLIMIT64)
97#include <../include/prlimit.h>
98#endif
99
100lxc_log_define(conf, lxc);
101
102/* The lxc_conf of the container currently being worked on in an API call.
103 * This is used in the error calls.
104 */
105#ifdef HAVE_TLS
106thread_local struct lxc_conf *current_config;
107#else
108struct lxc_conf *current_config;
109#endif
110
111char *lxchook_names[NUM_LXC_HOOKS] = {
112 "pre-start",
113 "pre-mount",
114 "mount",
115 "autodev",
116 "start",
117 "stop",
118 "post-stop",
119 "clone",
120 "destroy",
121 "start-host"
122};
123
124struct mount_opt {
125 char *name;
126 int clear;
127 int flag;
128};
129
130struct caps_opt {
131 char *name;
132 int value;
133};
134
135struct limit_opt {
136 char *name;
137 int value;
138};
139
140static struct mount_opt mount_opt[] = {
141 { "async", 1, MS_SYNCHRONOUS },
142 { "atime", 1, MS_NOATIME },
143 { "bind", 0, MS_BIND },
144 { "defaults", 0, 0 },
145 { "dev", 1, MS_NODEV },
146 { "diratime", 1, MS_NODIRATIME },
147 { "dirsync", 0, MS_DIRSYNC },
148 { "exec", 1, MS_NOEXEC },
149 { "lazytime", 0, MS_LAZYTIME },
150 { "mand", 0, MS_MANDLOCK },
151 { "noatime", 0, MS_NOATIME },
152 { "nodev", 0, MS_NODEV },
153 { "nodiratime", 0, MS_NODIRATIME },
154 { "noexec", 0, MS_NOEXEC },
155 { "nomand", 1, MS_MANDLOCK },
156 { "norelatime", 1, MS_RELATIME },
157 { "nostrictatime", 1, MS_STRICTATIME },
158 { "nosuid", 0, MS_NOSUID },
159 { "rbind", 0, MS_BIND|MS_REC },
160 { "relatime", 0, MS_RELATIME },
161 { "remount", 0, MS_REMOUNT },
162 { "ro", 0, MS_RDONLY },
163 { "rw", 1, MS_RDONLY },
164 { "strictatime", 0, MS_STRICTATIME },
165 { "suid", 1, MS_NOSUID },
166 { "sync", 0, MS_SYNCHRONOUS },
167 { NULL, 0, 0 },
168};
169
170static struct mount_opt propagation_opt[] = {
171 { "private", 0, MS_PRIVATE },
172 { "shared", 0, MS_SHARED },
173 { "slave", 0, MS_SLAVE },
174 { "unbindable", 0, MS_UNBINDABLE },
175 { "rprivate", 0, MS_PRIVATE|MS_REC },
176 { "rshared", 0, MS_SHARED|MS_REC },
177 { "rslave", 0, MS_SLAVE|MS_REC },
178 { "runbindable", 0, MS_UNBINDABLE|MS_REC },
179 { NULL, 0, 0 },
180};
181
182static struct caps_opt caps_opt[] = {
183#if HAVE_LIBCAP
184 { "chown", CAP_CHOWN },
185 { "dac_override", CAP_DAC_OVERRIDE },
186 { "dac_read_search", CAP_DAC_READ_SEARCH },
187 { "fowner", CAP_FOWNER },
188 { "fsetid", CAP_FSETID },
189 { "kill", CAP_KILL },
190 { "setgid", CAP_SETGID },
191 { "setuid", CAP_SETUID },
192 { "setpcap", CAP_SETPCAP },
193 { "linux_immutable", CAP_LINUX_IMMUTABLE },
194 { "net_bind_service", CAP_NET_BIND_SERVICE },
195 { "net_broadcast", CAP_NET_BROADCAST },
196 { "net_admin", CAP_NET_ADMIN },
197 { "net_raw", CAP_NET_RAW },
198 { "ipc_lock", CAP_IPC_LOCK },
199 { "ipc_owner", CAP_IPC_OWNER },
200 { "sys_module", CAP_SYS_MODULE },
201 { "sys_rawio", CAP_SYS_RAWIO },
202 { "sys_chroot", CAP_SYS_CHROOT },
203 { "sys_ptrace", CAP_SYS_PTRACE },
204 { "sys_pacct", CAP_SYS_PACCT },
205 { "sys_admin", CAP_SYS_ADMIN },
206 { "sys_boot", CAP_SYS_BOOT },
207 { "sys_nice", CAP_SYS_NICE },
208 { "sys_resource", CAP_SYS_RESOURCE },
209 { "sys_time", CAP_SYS_TIME },
210 { "sys_tty_config", CAP_SYS_TTY_CONFIG },
211 { "mknod", CAP_MKNOD },
212 { "lease", CAP_LEASE },
213#ifdef CAP_AUDIT_READ
214 { "audit_read", CAP_AUDIT_READ },
215#endif
216#ifdef CAP_AUDIT_WRITE
217 { "audit_write", CAP_AUDIT_WRITE },
218#endif
219#ifdef CAP_AUDIT_CONTROL
220 { "audit_control", CAP_AUDIT_CONTROL },
221#endif
222 { "setfcap", CAP_SETFCAP },
223 { "mac_override", CAP_MAC_OVERRIDE },
224 { "mac_admin", CAP_MAC_ADMIN },
225#ifdef CAP_SYSLOG
226 { "syslog", CAP_SYSLOG },
227#endif
228#ifdef CAP_WAKE_ALARM
229 { "wake_alarm", CAP_WAKE_ALARM },
230#endif
231#ifdef CAP_BLOCK_SUSPEND
232 { "block_suspend", CAP_BLOCK_SUSPEND },
233#endif
234#endif
235};
236
237static 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
288static 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 = malloc(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
330int 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 = malloc(size);
370 if (!buffer)
371 return -ENOMEM;
372
373 if (hook_version == 0)
374 buf_pos = snprintf(buffer, size, "exec %s %s %s %s", script, name, section, hookname);
375 else
376 buf_pos = snprintf(buffer, size, "exec %s", script);
377 if (buf_pos < 0 || (size_t)buf_pos >= size)
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 (strcmp(section, "net") == 0) {
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 (strcmp(argv[0], "macvlan") == 0) {
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 (strcmp(argv[0], "phys") == 0) {
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 (strcmp(argv[0], "veth") == 0) {
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 = snprintf(buffer + buf_pos, len, " %s", argv[i]);
435 if (ret < 0 || (size_t)ret >= len)
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
443int 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 = snprintf(buffer, size, "exec %s %s %s", script, name, section);
470 if (ret < 0 || ret >= size)
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 = snprintf(buffer + ret, len, " %s", p);
478 if (rc < 0 || rc >= len) {
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/* pin_rootfs
490 * if rootfs is a directory, then open ${rootfs}/.lxc-keep for writing for
491 * the duration of the container run, to prevent the container from marking
492 * the underlying fs readonly on shutdown. unlink the file immediately so
493 * no name pollution is happens.
494 * don't unlink on NFS to avoid random named stale handles.
495 * return -1 on error.
496 * return -2 if nothing needed to be pinned.
497 * return an open fd (>=0) if we pinned it.
498 */
499int pin_rootfs(const char *rootfs)
500{
501 __do_free char *absrootfs = NULL;
502 int fd, ret;
503 char absrootfspin[PATH_MAX];
504 struct stat s;
505 struct statfs sfs;
506
507 if (rootfs == NULL || strlen(rootfs) == 0)
508 return -2;
509
510 absrootfs = realpath(rootfs, NULL);
511 if (!absrootfs)
512 return -2;
513
514 ret = stat(absrootfs, &s);
515 if (ret < 0)
516 return -1;
517
518 if (!S_ISDIR(s.st_mode))
519 return -2;
520
521 ret = snprintf(absrootfspin, sizeof(absrootfspin), "%s/.lxc-keep", absrootfs);
522 if (ret < 0 || (size_t)ret >= sizeof(absrootfspin))
523 return -1;
524
525 fd = open(absrootfspin, O_CREAT | O_RDWR, S_IWUSR | S_IRUSR | O_CLOEXEC);
526 if (fd < 0)
527 return fd;
528
529 ret = fstatfs (fd, &sfs);
530 if (ret < 0)
531 return fd;
532
533 if (sfs.f_type == NFS_SUPER_MAGIC)
534 return log_debug(fd, "Rootfs on NFS, not unlinking pin file \"%s\"", absrootfspin);
535
536 (void)unlink(absrootfspin);
537
538 return fd;
539}
540
541/* If we are asking to remount something, make sure that any NOEXEC etc are
542 * honored.
543 */
544unsigned long add_required_remount_flags(const char *s, const char *d,
545 unsigned long flags)
546{
547#ifdef HAVE_STATVFS
548 int ret;
549 struct statvfs sb;
550 unsigned long required_flags = 0;
551
552 if (!s)
553 s = d;
554
555 if (!s)
556 return flags;
557
558 ret = statvfs(s, &sb);
559 if (ret < 0)
560 return flags;
561
562 if (flags & MS_REMOUNT) {
563 if (sb.f_flag & MS_NOSUID)
564 required_flags |= MS_NOSUID;
565 if (sb.f_flag & MS_NODEV)
566 required_flags |= MS_NODEV;
567 if (sb.f_flag & MS_RDONLY)
568 required_flags |= MS_RDONLY;
569 if (sb.f_flag & MS_NOEXEC)
570 required_flags |= MS_NOEXEC;
571 }
572
573 if (sb.f_flag & MS_NOATIME)
574 required_flags |= MS_NOATIME;
575 if (sb.f_flag & MS_NODIRATIME)
576 required_flags |= MS_NODIRATIME;
577 if (sb.f_flag & MS_LAZYTIME)
578 required_flags |= MS_LAZYTIME;
579 if (sb.f_flag & MS_RELATIME)
580 required_flags |= MS_RELATIME;
581 if (sb.f_flag & MS_STRICTATIME)
582 required_flags |= MS_STRICTATIME;
583
584 return flags | required_flags;
585#else
586 return flags;
587#endif
588}
589
590static int add_shmount_to_list(struct lxc_conf *conf)
591{
592 char new_mount[PATH_MAX];
593 /* Offset for the leading '/' since the path_cont
594 * is absolute inside the container.
595 */
596 int offset = 1, ret = -1;
597
598 ret = snprintf(new_mount, sizeof(new_mount),
599 "%s %s none bind,create=dir 0 0", conf->shmount.path_host,
600 conf->shmount.path_cont + offset);
601 if (ret < 0 || (size_t)ret >= sizeof(new_mount))
602 return -1;
603
604 return add_elem_to_mount_list(new_mount, conf);
605}
606
607static int lxc_mount_auto_mounts(struct lxc_conf *conf, int flags, struct lxc_handler *handler)
608{
609 int i, r;
610 static struct {
611 int match_mask;
612 int match_flag;
613 const char *source;
614 const char *destination;
615 const char *fstype;
616 unsigned long flags;
617 const char *options;
618 } default_mounts[] = {
619 /* Read-only bind-mounting... In older kernels, doing that
620 * required to do one MS_BIND mount and then
621 * MS_REMOUNT|MS_RDONLY the same one. According to mount(2)
622 * manpage, MS_BIND honors MS_RDONLY from kernel 2.6.26
623 * onwards. However, this apparently does not work on kernel
624 * 3.8. Unfortunately, on that very same kernel, doing the same
625 * trick as above doesn't seem to work either, there one needs
626 * to ALSO specify MS_BIND for the remount, otherwise the
627 * entire fs is remounted read-only or the mount fails because
628 * it's busy... MS_REMOUNT|MS_BIND|MS_RDONLY seems to work for
629 * kernels as low as 2.6.32...
630 */
631 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL },
632 /* proc/tty is used as a temporary placeholder for proc/sys/net which we'll move back in a few steps */
633 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys/net", "%r/proc/tty", NULL, MS_BIND, NULL },
634 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys", "%r/proc/sys", NULL, MS_BIND, NULL },
635 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL },
636 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/tty", "%r/proc/sys/net", NULL, MS_MOVE, NULL },
637 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sysrq-trigger", "%r/proc/sysrq-trigger", NULL, MS_BIND, NULL },
638 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sysrq-trigger", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL },
639 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL },
640 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW, "sysfs", "%r/sys", "sysfs", 0, NULL },
641 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO, "sysfs", "%r/sys", "sysfs", MS_RDONLY, NULL },
642 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys", "sysfs", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL },
643 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/sys", "%r/sys", NULL, MS_BIND, NULL },
644 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, NULL, "%r/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL },
645 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys/devices/virtual/net", "sysfs", 0, NULL },
646 { 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 },
647 { 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 },
648 { 0, 0, NULL, NULL, NULL, 0, NULL }
649 };
650
651 for (i = 0; default_mounts[i].match_mask; i++) {
652 __do_free char *destination = NULL, *source = NULL;
653 int saved_errno;
654 unsigned long mflags;
655 if ((flags & default_mounts[i].match_mask) != default_mounts[i].match_flag)
656 continue;
657
658 if (default_mounts[i].source) {
659 /* will act like strdup if %r is not present */
660 source = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].source);
661 if (!source)
662 return -1;
663 }
664
665 if (!default_mounts[i].destination)
666 return log_error(-1, "BUG: auto mounts destination %d was NULL", i);
667
668 /* will act like strdup if %r is not present */
669 destination = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].destination);
670 if (!destination)
671 return -1;
672
673 mflags = add_required_remount_flags(source, destination,
674 default_mounts[i].flags);
675 r = safe_mount(source, destination, default_mounts[i].fstype,
676 mflags, default_mounts[i].options,
677 conf->rootfs.path ? conf->rootfs.mount : NULL);
678 saved_errno = errno;
679 if (r < 0 && errno == ENOENT) {
680 INFO("Mount source or target for \"%s\" on \"%s\" does not exist. Skipping", source, destination);
681 r = 0;
682 } else if (r < 0) {
683 SYSERROR("Failed to mount \"%s\" on \"%s\" with flags %lu", source, destination, mflags);
684 }
685
686 if (r < 0) {
687 errno = saved_errno;
688 return -1;
689 }
690 }
691
692 if (flags & LXC_AUTO_CGROUP_MASK) {
693 int cg_flags;
694
695 cg_flags = flags & (LXC_AUTO_CGROUP_MASK & ~LXC_AUTO_CGROUP_FORCE);
696 /* If the type of cgroup mount was not specified, it depends on
697 * the container's capabilities as to what makes sense: if we
698 * have CAP_SYS_ADMIN, the read-only part can be remounted
699 * read-write anyway, so we may as well default to read-write;
700 * then the admin will not be given a false sense of security.
701 * (And if they really want mixed r/o r/w, then they can
702 * explicitly specify :mixed.) OTOH, if the container lacks
703 * CAP_SYS_ADMIN, do only default to :mixed, because then the
704 * container can't remount it read-write.
705 */
706 if (cg_flags == LXC_AUTO_CGROUP_NOSPEC || cg_flags == LXC_AUTO_CGROUP_FULL_NOSPEC) {
707 int has_sys_admin = 0;
708
709 if (!lxc_list_empty(&conf->keepcaps))
710 has_sys_admin = in_caplist(CAP_SYS_ADMIN, &conf->keepcaps);
711 else
712 has_sys_admin = !in_caplist(CAP_SYS_ADMIN, &conf->caps);
713
714 if (cg_flags == LXC_AUTO_CGROUP_NOSPEC)
715 cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_RW : LXC_AUTO_CGROUP_MIXED;
716 else
717 cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_FULL_RW : LXC_AUTO_CGROUP_FULL_MIXED;
718 }
719
720 if (flags & LXC_AUTO_CGROUP_FORCE)
721 cg_flags |= LXC_AUTO_CGROUP_FORCE;
722
723 if (!handler->cgroup_ops->mount(handler->cgroup_ops,
724 handler,
725 conf->rootfs.path ? conf->rootfs.mount : "",
726 cg_flags))
727 return log_error_errno(-1, errno, "Failed to mount \"/sys/fs/cgroup\"");
728 }
729
730 if (flags & LXC_AUTO_SHMOUNTS_MASK) {
731 int ret = add_shmount_to_list(conf);
732 if (ret < 0)
733 return log_error(-1, "Failed to add shmount entry to container config");
734 }
735
736 return 0;
737}
738
739static int setup_utsname(struct utsname *utsname)
740{
741 int ret;
742
743 if (!utsname)
744 return 0;
745
746 ret = sethostname(utsname->nodename, strlen(utsname->nodename));
747 if (ret < 0)
748 return log_error_errno(-1, errno, "Failed to set the hostname to \"%s\"",
749 utsname->nodename);
750
751 INFO("Set hostname to \"%s\"", utsname->nodename);
752
753 return 0;
754}
755
756struct dev_symlinks {
757 const char *oldpath;
758 const char *name;
759};
760
761static const struct dev_symlinks dev_symlinks[] = {
762 { "/proc/self/fd", "fd" },
763 { "/proc/self/fd/0", "stdin" },
764 { "/proc/self/fd/1", "stdout" },
765 { "/proc/self/fd/2", "stderr" },
766};
767
768static int lxc_setup_dev_symlinks(const struct lxc_rootfs *rootfs)
769{
770 int i, ret;
771 char path[PATH_MAX];
772 struct stat s;
773
774 for (i = 0; i < sizeof(dev_symlinks) / sizeof(dev_symlinks[0]); i++) {
775 const struct dev_symlinks *d = &dev_symlinks[i];
776
777 ret = snprintf(path, sizeof(path), "%s/dev/%s",
778 rootfs->path ? rootfs->mount : "", d->name);
779 if (ret < 0 || (size_t)ret >= sizeof(path))
780 return -1;
781
782 /* Stat the path first. If we don't get an error accept it as
783 * is and don't try to create it
784 */
785 ret = stat(path, &s);
786 if (ret == 0)
787 continue;
788
789 ret = symlink(d->oldpath, path);
790 if (ret && errno != EEXIST) {
791 if (errno == EROFS)
792 WARN("Failed to create \"%s\". Read-only filesystem", path);
793 else
794 return log_error_errno(-1, errno, "Failed to create \"%s\"", path);
795 }
796 }
797
798 return 0;
799}
800
801/* Build a space-separate list of ptys to pass to systemd. */
802static bool append_ttyname(char **pp, char *name)
803{
804 char *p;
805 size_t size;
806
807 if (!*pp) {
808 *pp = malloc(strlen(name) + strlen("container_ttys=") + 1);
809 if (!*pp)
810 return false;
811
812 sprintf(*pp, "container_ttys=%s", name);
813 return true;
814 }
815
816 size = strlen(*pp) + strlen(name) + 2;
817 p = realloc(*pp, size);
818 if (!p)
819 return false;
820
821 *pp = p;
822 (void)strlcat(p, " ", size);
823 (void)strlcat(p, name, size);
824
825 return true;
826}
827
828static int lxc_setup_ttys(struct lxc_conf *conf)
829{
830 int i, ret;
831 const struct lxc_tty_info *ttys = &conf->ttys;
832 char *ttydir = ttys->dir;
833 char path[PATH_MAX], lxcpath[PATH_MAX];
834
835 if (!conf->rootfs.path)
836 return 0;
837
838 for (i = 0; i < ttys->max; i++) {
839 struct lxc_terminal_info *tty = &ttys->tty[i];
840
841 ret = snprintf(path, sizeof(path), "/dev/tty%d", i + 1);
842 if (ret < 0 || (size_t)ret >= sizeof(path))
843 return -1;
844
845 if (ttydir) {
846 /* create dev/lxc/tty%d" */
847 ret = snprintf(lxcpath, sizeof(lxcpath),
848 "/dev/%s/tty%d", ttydir, i + 1);
849 if (ret < 0 || (size_t)ret >= sizeof(lxcpath))
850 return -1;
851
852 ret = mknod(lxcpath, S_IFREG | 0000, 0);
853 if (ret < 0 && errno != EEXIST) {
854 SYSERROR("Failed to create \"%s\"", lxcpath);
855 return -1;
856 }
857
858 ret = unlink(path);
859 if (ret < 0 && errno != ENOENT) {
860 SYSERROR("Failed to unlink \"%s\"", path);
861 return -1;
862 }
863
864 ret = mount(tty->name, lxcpath, "none", MS_BIND, 0);
865 if (ret < 0) {
866 SYSWARN("Failed to bind mount \"%s\" onto \"%s\"", tty->name, lxcpath);
867 continue;
868 }
869 DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name, lxcpath);
870
871 ret = snprintf(lxcpath, sizeof(lxcpath), "%s/tty%d",
872 ttydir, i + 1);
873 if (ret < 0 || (size_t)ret >= sizeof(lxcpath))
874 return -1;
875
876 ret = symlink(lxcpath, path);
877 if (ret < 0)
878 return log_error_errno(-1, errno, "Failed to create symlink \"%s\" -> \"%s\"", path, lxcpath);
879 } else {
880 /* If we populated /dev, then we need to create
881 * /dev/ttyN
882 */
883 ret = mknod(path, S_IFREG | 0000, 0);
884 if (ret < 0) /* this isn't fatal, continue */
885 SYSERROR("Failed to create \"%s\"", path);
886
887 ret = mount(tty->name, path, "none", MS_BIND, 0);
888 if (ret < 0) {
889 SYSERROR("Failed to mount '%s'->'%s'", tty->name, path);
890 continue;
891 }
892
893 DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name, path);
894 }
895
896 if (!append_ttyname(&conf->ttys.tty_names, tty->name))
897 return log_error(-1, "Error setting up container_ttys string");
898 }
899
900 INFO("Finished setting up %zu /dev/tty<N> device(s)", ttys->max);
901 return 0;
902}
903
904define_cleanup_function(struct lxc_tty_info *, lxc_delete_tty);
905
906int lxc_allocate_ttys(struct lxc_conf *conf)
907{
908 struct lxc_terminal_info *tty_new = NULL;
909 int ret;
910 call_cleaner(lxc_delete_tty) struct lxc_tty_info *ttys = &conf->ttys;
911
912 /* no tty in the configuration */
913 if (ttys->max == 0)
914 return 0;
915
916 tty_new = malloc(sizeof(struct lxc_terminal_info) * ttys->max);
917 if (!tty_new)
918 return -ENOMEM;
919 ttys->tty = tty_new;
920
921 for (size_t i = 0; i < ttys->max; i++) {
922 struct lxc_terminal_info *tty = &ttys->tty[i];
923
924 tty->master = -EBADF;
925 tty->slave = -EBADF;
926 ret = openpty(&tty->master, &tty->slave, NULL, NULL, NULL);
927 if (ret < 0) {
928 ttys->max = i;
929 return log_error_errno(-ENOTTY, ENOTTY, "Failed to create tty %zu", i);
930 }
931
932 ret = ttyname_r(tty->slave, tty->name, sizeof(tty->name));
933 if (ret < 0) {
934 ttys->max = i;
935 return log_error_errno(-ENOTTY, ENOTTY, "Failed to retrieve name of tty %zu slave", i);
936 }
937
938 DEBUG("Created tty \"%s\" with master fd %d and slave fd %d",
939 tty->name, tty->master, tty->slave);
940
941 /* Prevent leaking the file descriptors to the container */
942 ret = fd_cloexec(tty->master, true);
943 if (ret < 0)
944 SYSWARN("Failed to set FD_CLOEXEC flag on master fd %d of tty device \"%s\"",
945 tty->master, tty->name);
946
947 ret = fd_cloexec(tty->slave, true);
948 if (ret < 0)
949 SYSWARN("Failed to set FD_CLOEXEC flag on slave fd %d of tty device \"%s\"",
950 tty->slave, tty->name);
951
952 tty->busy = -1;
953 }
954
955 INFO("Finished creating %zu tty devices", ttys->max);
956 move_ptr(ttys);
957 return 0;
958}
959
960void lxc_delete_tty(struct lxc_tty_info *ttys)
961{
962 if (!ttys->tty)
963 return;
964
965 for (int i = 0; i < ttys->max; i++) {
966 struct lxc_terminal_info *tty = &ttys->tty[i];
967 close_prot_errno_disarm(tty->master);
968 close_prot_errno_disarm(tty->slave);
969 }
970
971 free_disarm(ttys->tty);
972}
973
974static int lxc_send_ttys_to_parent(struct lxc_handler *handler)
975{
976 int i;
977 int ret = -1;
978 struct lxc_conf *conf = handler->conf;
979 struct lxc_tty_info *ttys = &conf->ttys;
980 int sock = handler->data_sock[0];
981
982 if (ttys->max == 0)
983 return 0;
984
985 for (i = 0; i < ttys->max; i++) {
986 int ttyfds[2];
987 struct lxc_terminal_info *tty = &ttys->tty[i];
988
989 ttyfds[0] = tty->master;
990 ttyfds[1] = tty->slave;
991
992 ret = lxc_abstract_unix_send_fds(sock, ttyfds, 2, NULL, 0);
993 if (ret < 0)
994 break;
995
996 TRACE("Sent tty \"%s\" with master fd %d and slave fd %d to parent",
997 tty->name, tty->master, tty->slave);
998 }
999
1000 if (ret < 0)
1001 SYSERROR("Failed to send %zu ttys to parent", ttys->max);
1002 else
1003 TRACE("Sent %zu ttys to parent", ttys->max);
1004
1005 return ret;
1006}
1007
1008static int lxc_create_ttys(struct lxc_handler *handler)
1009{
1010 int ret = -1;
1011 struct lxc_conf *conf = handler->conf;
1012
1013 ret = lxc_allocate_ttys(conf);
1014 if (ret < 0) {
1015 ERROR("Failed to allocate ttys");
1016 goto on_error;
1017 }
1018
1019 ret = lxc_send_ttys_to_parent(handler);
1020 if (ret < 0) {
1021 ERROR("Failed to send ttys to parent");
1022 goto on_error;
1023 }
1024
1025 if (!conf->is_execute) {
1026 ret = lxc_setup_ttys(conf);
1027 if (ret < 0) {
1028 ERROR("Failed to setup ttys");
1029 goto on_error;
1030 }
1031 }
1032
1033 if (conf->ttys.tty_names) {
1034 ret = setenv("container_ttys", conf->ttys.tty_names, 1);
1035 if (ret < 0)
1036 SYSERROR("Failed to set \"container_ttys=%s\"", conf->ttys.tty_names);
1037 }
1038
1039 ret = 0;
1040
1041on_error:
1042 lxc_delete_tty(&conf->ttys);
1043
1044 return ret;
1045}
1046
1047/* Just create a path for /dev under $lxcpath/$name and in rootfs If we hit an
1048 * error, log it but don't fail yet.
1049 */
1050static int mount_autodev(const char *name, const struct lxc_rootfs *rootfs,
1051 int autodevtmpfssize, const char *lxcpath)
1052{
1053 __do_free char *path = NULL;
1054 int ret;
1055 size_t clen;
1056 mode_t cur_mask;
1057 char mount_options[128];
1058
1059 INFO("Preparing \"/dev\"");
1060
1061 /* $(rootfs->mount) + "/dev/pts" + '\0' */
1062 clen = (rootfs->path ? strlen(rootfs->mount) : 0) + 9;
1063 path = must_realloc(NULL, clen);
1064 sprintf(mount_options, "size=%d,mode=755", (autodevtmpfssize != 0) ? autodevtmpfssize : 500000);
1065 DEBUG("Using mount options: %s", mount_options);
1066
1067 ret = snprintf(path, clen, "%s/dev", rootfs->path ? rootfs->mount : "");
1068 if (ret < 0 || (size_t)ret >= clen)
1069 return -1;
1070
1071 cur_mask = umask(S_IXUSR | S_IXGRP | S_IXOTH);
1072 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1073 if (ret < 0 && errno != EEXIST) {
1074 SYSERROR("Failed to create \"/dev\" directory");
1075 ret = -errno;
1076 goto reset_umask;
1077 }
1078
1079 ret = safe_mount("none", path, "tmpfs", 0, mount_options,
1080 rootfs->path ? rootfs->mount : NULL );
1081 if (ret < 0) {
1082 SYSERROR("Failed to mount tmpfs on \"%s\"", path);
1083 goto reset_umask;
1084 }
1085 TRACE("Mounted tmpfs on \"%s\"", path);
1086
1087 ret = snprintf(path, clen, "%s/dev/pts", rootfs->path ? rootfs->mount : "");
1088 if (ret < 0 || (size_t)ret >= clen) {
1089 ret = -1;
1090 goto reset_umask;
1091 }
1092
1093 /* If we are running on a devtmpfs mapping, dev/pts may already exist.
1094 * If not, then create it and exit if that fails...
1095 */
1096 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1097 if (ret < 0 && errno != EEXIST) {
1098 SYSERROR("Failed to create directory \"%s\"", path);
1099 ret = -errno;
1100 goto reset_umask;
1101 }
1102
1103 ret = 0;
1104
1105reset_umask:
1106 (void)umask(cur_mask);
1107
1108 INFO("Prepared \"/dev\"");
1109 return ret;
1110}
1111
1112struct lxc_device_node {
1113 const char *name;
1114 const mode_t mode;
1115 const int maj;
1116 const int min;
1117};
1118
1119static const struct lxc_device_node lxc_devices[] = {
1120 { "full", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 7 },
1121 { "null", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 3 },
1122 { "random", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 8 },
1123 { "tty", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 5, 0 },
1124 { "urandom", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 9 },
1125 { "zero", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 5 },
1126};
1127
1128
1129enum {
1130 LXC_DEVNODE_BIND,
1131 LXC_DEVNODE_MKNOD,
1132 LXC_DEVNODE_PARTIAL,
1133 LXC_DEVNODE_OPEN,
1134};
1135
1136static int lxc_fill_autodev(const struct lxc_rootfs *rootfs)
1137{
1138 int i, ret;
1139 char path[PATH_MAX];
1140 mode_t cmask;
1141 int use_mknod = LXC_DEVNODE_MKNOD;
1142
1143 ret = snprintf(path, PATH_MAX, "%s/dev",
1144 rootfs->path ? rootfs->mount : "");
1145 if (ret < 0 || ret >= PATH_MAX)
1146 return -1;
1147
1148 /* ignore, just don't try to fill in */
1149 if (!dir_exists(path))
1150 return 0;
1151
1152 INFO("Populating \"/dev\"");
1153
1154 cmask = umask(S_IXUSR | S_IXGRP | S_IXOTH);
1155 for (i = 0; i < sizeof(lxc_devices) / sizeof(lxc_devices[0]); i++) {
1156 char hostpath[PATH_MAX];
1157 const struct lxc_device_node *device = &lxc_devices[i];
1158
1159 ret = snprintf(path, PATH_MAX, "%s/dev/%s",
1160 rootfs->path ? rootfs->mount : "", device->name);
1161 if (ret < 0 || ret >= PATH_MAX)
1162 return -1;
1163
1164 if (use_mknod >= LXC_DEVNODE_MKNOD) {
1165 ret = mknod(path, device->mode, makedev(device->maj, device->min));
1166 if (ret == 0 || (ret < 0 && errno == EEXIST)) {
1167 DEBUG("Created device node \"%s\"", path);
1168 } else if (ret < 0) {
1169 if (errno != EPERM)
1170 return log_error_errno(-1, errno, "Failed to create device node \"%s\"", path);
1171
1172 use_mknod = LXC_DEVNODE_BIND;
1173 }
1174
1175 /* Device nodes are fully useable. */
1176 if (use_mknod == LXC_DEVNODE_OPEN)
1177 continue;
1178
1179 if (use_mknod == LXC_DEVNODE_MKNOD) {
1180 /* See
1181 * - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=55956b59df336f6738da916dbb520b6e37df9fbd
1182 * - https://lists.linuxfoundation.org/pipermail/containers/2018-June/039176.html
1183 */
1184 ret = open(path, O_RDONLY | O_CLOEXEC);
1185 if (ret >= 0) {
1186 close_prot_errno_disarm(ret);
1187 /* Device nodes are fully useable. */
1188 use_mknod = LXC_DEVNODE_OPEN;
1189 continue;
1190 }
1191
1192 SYSTRACE("Failed to open \"%s\" device", path);
1193 /* Device nodes are only partially useable. */
1194 use_mknod = LXC_DEVNODE_PARTIAL;
1195 }
1196 }
1197
1198 if (use_mknod != LXC_DEVNODE_PARTIAL) {
1199 /* If we are dealing with partially functional device
1200 * nodes the prio mknod() call will have created the
1201 * device node so we can use it as a bind-mount target.
1202 */
1203 ret = mknod(path, S_IFREG | 0000, 0);
1204 if (ret < 0 && errno != EEXIST)
1205 return log_error_errno(-1, errno, "Failed to create file \"%s\"", path);
1206 }
1207
1208 /* Fallback to bind-mounting the device from the host. */
1209 ret = snprintf(hostpath, PATH_MAX, "/dev/%s", device->name);
1210 if (ret < 0 || ret >= PATH_MAX)
1211 return -1;
1212
1213 ret = safe_mount(hostpath, path, 0, MS_BIND, NULL,
1214 rootfs->path ? rootfs->mount : NULL);
1215 if (ret < 0)
1216 return log_error_errno(-1, errno, "Failed to bind mount host device node \"%s\" onto \"%s\"",
1217 hostpath, path);
1218 DEBUG("Bind mounted host device node \"%s\" onto \"%s\"", hostpath, path);
1219 }
1220 (void)umask(cmask);
1221
1222 INFO("Populated \"/dev\"");
1223 return 0;
1224}
1225
1226static int lxc_mount_rootfs(struct lxc_conf *conf)
1227{
1228 int ret;
1229 struct lxc_storage *bdev;
1230 const struct lxc_rootfs *rootfs = &conf->rootfs;
1231
1232 if (!rootfs->path) {
1233 ret = mount("", "/", NULL, MS_SLAVE | MS_REC, 0);
1234 if (ret < 0)
1235 return log_error_errno(-1, errno, "Failed to remount \"/\" MS_REC | MS_SLAVE");
1236
1237 return 0;
1238 }
1239
1240 ret = access(rootfs->mount, F_OK);
1241 if (ret != 0)
1242 return log_error_errno(-1, errno, "Failed to access to \"%s\". Check it is present",
1243 rootfs->mount);
1244
1245 bdev = storage_init(conf);
1246 if (!bdev)
1247 return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"",
1248 rootfs->path, rootfs->mount,
1249 rootfs->options ? rootfs->options : "(null)");
1250
1251 ret = bdev->ops->mount(bdev);
1252 storage_put(bdev);
1253 if (ret < 0)
1254 return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"",
1255 rootfs->path, rootfs->mount,
1256 rootfs->options ? rootfs->options : "(null)");
1257
1258 DEBUG("Mounted rootfs \"%s\" onto \"%s\" with options \"%s\"",
1259 rootfs->path, rootfs->mount,
1260 rootfs->options ? rootfs->options : "(null)");
1261
1262 return 0;
1263}
1264
1265int lxc_chroot(const struct lxc_rootfs *rootfs)
1266{
1267 __do_free char *nroot = NULL;
1268 int i, ret;
1269 char *root = rootfs->mount;
1270
1271 nroot = realpath(root, NULL);
1272 if (!nroot)
1273 return log_error_errno(-1, errno, "Failed to resolve \"%s\"", root);
1274
1275 ret = chdir("/");
1276 if (ret < 0)
1277 return -1;
1278
1279 /* We could use here MS_MOVE, but in userns this mount is locked and
1280 * can't be moved.
1281 */
1282 ret = mount(nroot, "/", NULL, MS_REC | MS_BIND, NULL);
1283 if (ret < 0)
1284 return log_error_errno(-1, errno, "Failed to mount \"%s\" onto \"/\" as MS_REC | MS_BIND", nroot);
1285
1286 ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL);
1287 if (ret < 0)
1288 return log_error_errno(-1, errno, "Failed to remount \"/\"");
1289
1290 /* The following code cleans up inherited mounts which are not required
1291 * for CT.
1292 *
1293 * The mountinfo file shows not all mounts, if a few points have been
1294 * unmounted between read operations from the mountinfo. So we need to
1295 * read mountinfo a few times.
1296 *
1297 * This loop can be skipped if a container uses userns, because all
1298 * inherited mounts are locked and we should live with all this trash.
1299 */
1300 for (;;) {
1301 __do_fclose FILE *f = NULL;
1302 __do_free char *line = NULL;
1303 char *slider1, *slider2;
1304 int progress = 0;
1305 size_t len = 0;
1306
1307 f = fopen("./proc/self/mountinfo", "re");
1308 if (!f)
1309 return log_error_errno(-1, errno, "Failed to open \"/proc/self/mountinfo\"");
1310
1311 while (getline(&line, &len, f) > 0) {
1312 for (slider1 = line, i = 0; slider1 && i < 4; i++)
1313 slider1 = strchr(slider1 + 1, ' ');
1314
1315 if (!slider1)
1316 continue;
1317
1318 slider2 = strchr(slider1 + 1, ' ');
1319 if (!slider2)
1320 continue;
1321
1322 *slider2 = '\0';
1323 *slider1 = '.';
1324
1325 if (strcmp(slider1 + 1, "/") == 0)
1326 continue;
1327
1328 if (strcmp(slider1 + 1, "/proc") == 0)
1329 continue;
1330
1331 ret = umount2(slider1, MNT_DETACH);
1332 if (ret == 0)
1333 progress++;
1334 }
1335
1336 if (!progress)
1337 break;
1338 }
1339
1340 /* This also can be skipped if a container uses userns. */
1341 (void)umount2("./proc", MNT_DETACH);
1342
1343 /* It is weird, but chdir("..") moves us in a new root */
1344 ret = chdir("..");
1345 if (ret < 0)
1346 return log_error_errno(-1, errno, "Failed to chdir(\"..\")");
1347
1348 ret = chroot(".");
1349 if (ret < 0)
1350 return log_error_errno(-1, errno, "Failed to chroot(\".\")");
1351
1352 return 0;
1353}
1354
1355/* (The following explanation is copied verbatim from the kernel.)
1356 *
1357 * pivot_root Semantics:
1358 * Moves the root file system of the current process to the directory put_old,
1359 * makes new_root as the new root file system of the current process, and sets
1360 * root/cwd of all processes which had them on the current root to new_root.
1361 *
1362 * Restrictions:
1363 * The new_root and put_old must be directories, and must not be on the
1364 * same file system as the current process root. The put_old must be
1365 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1366 * pointed to by put_old must yield the same directory as new_root. No other
1367 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1368 *
1369 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
1370 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
1371 * in this situation.
1372 *
1373 * Notes:
1374 * - we don't move root/cwd if they are not at the root (reason: if something
1375 * cared enough to change them, it's probably wrong to force them elsewhere)
1376 * - it's okay to pick a root that isn't the root of a file system, e.g.
1377 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1378 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1379 * first.
1380 */
1381static int lxc_pivot_root(const char *rootfs)
1382{
1383 __do_close int oldroot = -EBADF, newroot = -EBADF;
1384 int ret;
1385
1386 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
1387 if (oldroot < 0)
1388 return log_error_errno(-1, errno, "Failed to open old root directory");
1389
1390 newroot = open(rootfs, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
1391 if (newroot < 0)
1392 return log_error_errno(-1, errno, "Failed to open new root directory");
1393
1394 /* change into new root fs */
1395 ret = fchdir(newroot);
1396 if (ret < 0)
1397 return log_error_errno(-1, errno, "Failed to change to new rootfs \"%s\"", rootfs);
1398
1399 /* pivot_root into our new root fs */
1400 ret = pivot_root(".", ".");
1401 if (ret < 0)
1402 return log_error_errno(-1, errno, "Failed to pivot_root()");
1403
1404 /* At this point the old-root is mounted on top of our new-root. To
1405 * unmounted it we must not be chdir'd into it, so escape back to
1406 * old-root.
1407 */
1408 ret = fchdir(oldroot);
1409 if (ret < 0)
1410 return log_error_errno(-1, errno, "Failed to enter old root directory");
1411
1412 /* Make oldroot rslave to make sure our umounts don't propagate to the
1413 * host.
1414 */
1415 ret = mount("", ".", "", MS_SLAVE | MS_REC, NULL);
1416 if (ret < 0)
1417 return log_error_errno(-1, errno, "Failed to make oldroot rslave");
1418
1419 ret = umount2(".", MNT_DETACH);
1420 if (ret < 0)
1421 return log_error_errno(-1, errno, "Failed to detach old root directory");
1422
1423 ret = fchdir(newroot);
1424 if (ret < 0)
1425 return log_error_errno(-1, errno, "Failed to re-enter new root directory");
1426
1427 TRACE("pivot_root(\"%s\") successful", rootfs);
1428
1429 return 0;
1430}
1431
1432static int lxc_setup_rootfs_switch_root(const struct lxc_rootfs *rootfs)
1433{
1434 if (!rootfs->path)
1435 return log_debug(0, "Container does not have a rootfs");
1436
1437 if (detect_ramfs_rootfs())
1438 return lxc_chroot(rootfs);
1439
1440 return lxc_pivot_root(rootfs->mount);
1441}
1442
1443static const struct id_map *find_mapped_nsid_entry(const struct lxc_conf *conf,
1444 unsigned id,
1445 enum idtype idtype)
1446{
1447 struct lxc_list *it;
1448 struct id_map *map;
1449 struct id_map *retmap = NULL;
1450
1451 /* Shortcut for container's root mappings. */
1452 if (id == 0) {
1453 if (idtype == ID_TYPE_UID)
1454 return conf->root_nsuid_map;
1455
1456 if (idtype == ID_TYPE_GID)
1457 return conf->root_nsgid_map;
1458 }
1459
1460 lxc_list_for_each(it, &conf->id_map) {
1461 map = it->elem;
1462 if (map->idtype != idtype)
1463 continue;
1464
1465 if (id >= map->nsid && id < map->nsid + map->range) {
1466 retmap = map;
1467 break;
1468 }
1469 }
1470
1471 return retmap;
1472}
1473
1474static int lxc_setup_devpts(struct lxc_conf *conf)
1475{
1476 int ret;
1477 char **opts;
1478 char devpts_mntopts[256];
1479 char *mntopt_sets[5];
1480 char default_devpts_mntopts[256] = "gid=5,newinstance,ptmxmode=0666,mode=0620";
1481
1482 if (conf->pty_max <= 0)
1483 return log_debug(0, "No new devpts instance will be mounted since no pts devices are requested");
1484
1485 ret = snprintf(devpts_mntopts, sizeof(devpts_mntopts), "%s,max=%zu",
1486 default_devpts_mntopts, conf->pty_max);
1487 if (ret < 0 || (size_t)ret >= sizeof(devpts_mntopts))
1488 return -1;
1489
1490 (void)umount2("/dev/pts", MNT_DETACH);
1491
1492 /* Create mountpoint for devpts instance. */
1493 ret = mkdir("/dev/pts", 0755);
1494 if (ret < 0 && errno != EEXIST)
1495 return log_error_errno(-1, errno, "Failed to create \"/dev/pts\" directory");
1496
1497 /* gid=5 && max= */
1498 mntopt_sets[0] = devpts_mntopts;
1499
1500 /* !gid=5 && max= */
1501 mntopt_sets[1] = devpts_mntopts + STRLITERALLEN("gid=5") + 1;
1502
1503 /* gid=5 && !max= */
1504 mntopt_sets[2] = default_devpts_mntopts;
1505
1506 /* !gid=5 && !max= */
1507 mntopt_sets[3] = default_devpts_mntopts + STRLITERALLEN("gid=5") + 1;
1508
1509 /* end */
1510 mntopt_sets[4] = NULL;
1511
1512 for (ret = -1, opts = mntopt_sets; opts && *opts; opts++) {
1513 /* mount new devpts instance */
1514 ret = mount("devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC, *opts);
1515 if (ret == 0)
1516 break;
1517 }
1518
1519 if (ret < 0)
1520 return log_error_errno(-1, errno, "Failed to mount new devpts instance");
1521 DEBUG("Mount new devpts instance with options \"%s\"", *opts);
1522
1523 /* Remove any pre-existing /dev/ptmx file. */
1524 ret = remove("/dev/ptmx");
1525 if (ret < 0) {
1526 if (errno != ENOENT)
1527 return log_error_errno(-1, errno, "Failed to remove existing \"/dev/ptmx\" file");
1528 } else {
1529 DEBUG("Removed existing \"/dev/ptmx\" file");
1530 }
1531
1532 /* Create dummy /dev/ptmx file as bind mountpoint for /dev/pts/ptmx. */
1533 ret = mknod("/dev/ptmx", S_IFREG | 0000, 0);
1534 if (ret < 0 && errno != EEXIST)
1535 return log_error_errno(-1, errno, "Failed to create dummy \"/dev/ptmx\" file as bind mount target");
1536 DEBUG("Created dummy \"/dev/ptmx\" file as bind mount target");
1537
1538 /* Fallback option: create symlink /dev/ptmx -> /dev/pts/ptmx */
1539 ret = mount("/dev/pts/ptmx", "/dev/ptmx", NULL, MS_BIND, NULL);
1540 if (!ret)
1541 return log_debug(0, "Bind mounted \"/dev/pts/ptmx\" to \"/dev/ptmx\"");
1542 else
1543 /* Fallthrough and try to create a symlink. */
1544 ERROR("Failed to bind mount \"/dev/pts/ptmx\" to \"/dev/ptmx\"");
1545
1546 /* Remove the dummy /dev/ptmx file we created above. */
1547 ret = remove("/dev/ptmx");
1548 if (ret < 0)
1549 return log_error_errno(-1, errno, "Failed to remove existing \"/dev/ptmx\"");
1550
1551 /* Fallback option: Create symlink /dev/ptmx -> /dev/pts/ptmx. */
1552 ret = symlink("/dev/pts/ptmx", "/dev/ptmx");
1553 if (ret < 0)
1554 return log_error_errno(-1, errno, "Failed to create symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\"");
1555 DEBUG("Created symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\"");
1556
1557 return 0;
1558}
1559
1560static int setup_personality(int persona)
1561{
1562 int ret;
1563
1564#if HAVE_SYS_PERSONALITY_H
1565 if (persona == -1)
1566 return 0;
1567
1568 ret = personality(persona);
1569 if (ret < 0)
1570 return log_error_errno(-1, errno, "Failed to set personality to \"0x%x\"", persona);
1571
1572 INFO("Set personality to \"0x%x\"", persona);
1573#endif
1574
1575 return 0;
1576}
1577
1578static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs,
1579 const struct lxc_terminal *console,
1580 int pts_mnt_fd)
1581{
1582 int ret;
1583 char path[PATH_MAX];
1584 char *rootfs_path = rootfs->path ? rootfs->mount : "";
1585
1586 if (console->path && !strcmp(console->path, "none"))
1587 return 0;
1588
1589 ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path);
1590 if (ret < 0 || (size_t)ret >= sizeof(path))
1591 return -1;
1592
1593 /*
1594 * When we are asked to setup a console we remove any previous
1595 * /dev/console bind-mounts.
1596 */
1597 if (file_exists(path)) {
1598 ret = lxc_unstack_mountpoint(path, false);
1599 if (ret < 0)
1600 return log_error_errno(-ret, errno, "Failed to unmount \"%s\"", path);
1601 else
1602 DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path);
1603 }
1604
1605 /*
1606 * For unprivileged containers autodev or automounts will already have
1607 * taken care of creating /dev/console.
1608 */
1609 ret = mknod(path, S_IFREG | 0000, 0);
1610 if (ret < 0 && errno != EEXIST)
1611 return log_error_errno(-errno, errno, "Failed to create console");
1612
1613 ret = fchmod(console->slave, S_IXUSR | S_IXGRP);
1614 if (ret < 0)
1615 return log_error_errno(-errno, errno, "Failed to set mode \"0%o\" to \"%s\"", S_IXUSR | S_IXGRP, console->name);
1616
1617 if (pts_mnt_fd >= 0)
1618 ret = move_mount(pts_mnt_fd, "", -EBADF, path, MOVE_MOUNT_F_EMPTY_PATH);
1619 else
1620 ret = safe_mount(console->name, path, "none", MS_BIND, 0, rootfs_path);
1621 if (ret < 0)
1622 return log_error_errno(-1, errno, "Failed to mount %d(%s) on \"%s\"", pts_mnt_fd, console->name, path);
1623
1624 DEBUG("Mounted pts device %d(%s) onto \"%s\"", pts_mnt_fd, console->name, path);
1625 return 0;
1626}
1627
1628static int lxc_setup_ttydir_console(const struct lxc_rootfs *rootfs,
1629 const struct lxc_terminal *console,
1630 char *ttydir, int pts_mnt_fd)
1631{
1632 int ret;
1633 char path[PATH_MAX], lxcpath[PATH_MAX];
1634 char *rootfs_path = rootfs->path ? rootfs->mount : "";
1635
1636 if (console->path && !strcmp(console->path, "none"))
1637 return 0;
1638
1639 /* create rootfs/dev/<ttydir> directory */
1640 ret = snprintf(path, sizeof(path), "%s/dev/%s", rootfs_path, ttydir);
1641 if (ret < 0 || (size_t)ret >= sizeof(path))
1642 return -1;
1643
1644 ret = mkdir(path, 0755);
1645 if (ret && errno != EEXIST)
1646 return log_error_errno(-errno, errno, "Failed to create \"%s\"", path);
1647 DEBUG("Created directory for console and tty devices at \"%s\"", path);
1648
1649 ret = snprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/console", rootfs_path, ttydir);
1650 if (ret < 0 || (size_t)ret >= sizeof(lxcpath))
1651 return -1;
1652
1653 ret = mknod(lxcpath, S_IFREG | 0000, 0);
1654 if (ret < 0 && errno != EEXIST)
1655 return log_error_errno(-errno, errno, "Failed to create \"%s\"", lxcpath);
1656
1657 ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path);
1658 if (ret < 0 || (size_t)ret >= sizeof(path))
1659 return -1;
1660
1661 if (file_exists(path)) {
1662 ret = lxc_unstack_mountpoint(path, false);
1663 if (ret < 0)
1664 return log_error_errno(-ret, errno, "Failed to unmount \"%s\"", path);
1665 else
1666 DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path);
1667 }
1668
1669 ret = mknod(path, S_IFREG | 0000, 0);
1670 if (ret < 0 && errno != EEXIST)
1671 return log_error_errno(-errno, errno, "Failed to create console");
1672
1673 ret = fchmod(console->slave, S_IXUSR | S_IXGRP);
1674 if (ret < 0)
1675 return log_error_errno(-errno, errno, "Failed to set mode \"0%o\" to \"%s\"", S_IXUSR | S_IXGRP, console->name);
1676
1677 /* bind mount console->name to '/dev/<ttydir>/console' */
1678 if (pts_mnt_fd >= 0)
1679 ret = move_mount(pts_mnt_fd, "", -EBADF, path, MOVE_MOUNT_F_EMPTY_PATH);
1680 else
1681 ret = safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs_path);
1682 if (ret < 0)
1683 return log_error_errno(-1, errno, "Failed to mount %d(%s) on \"%s\"", pts_mnt_fd, console->name, lxcpath);
1684 DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath);
1685
1686 /* bind mount '/dev/<ttydir>/console' to '/dev/console' */
1687 ret = safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs_path);
1688 if (ret < 0)
1689 return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", console->name, lxcpath);
1690 DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath);
1691
1692 DEBUG("Console has been setup under \"%s\" and mounted to \"%s\"", lxcpath, path);
1693 return 0;
1694}
1695
1696static int lxc_setup_console(const struct lxc_rootfs *rootfs,
1697 const struct lxc_terminal *console, char *ttydir,
1698 int pts_mnt_fd)
1699{
1700
1701 if (!ttydir)
1702 return lxc_setup_dev_console(rootfs, console, pts_mnt_fd);
1703
1704 return lxc_setup_ttydir_console(rootfs, console, ttydir, pts_mnt_fd);
1705}
1706
1707static int parse_mntopt(char *opt, unsigned long *flags, char **data, size_t size)
1708{
1709 ssize_t ret;
1710
1711 /* If '=' is contained in opt, the option must go into data. */
1712 if (!strchr(opt, '=')) {
1713 /*
1714 * If opt is found in mount_opt, set or clear flags.
1715 * Otherwise append it to data.
1716 */
1717 size_t opt_len = strlen(opt);
1718 for (struct mount_opt *mo = &mount_opt[0]; mo->name != NULL; mo++) {
1719 size_t mo_name_len = strlen(mo->name);
1720
1721 if (opt_len == mo_name_len && strncmp(opt, mo->name, mo_name_len) == 0) {
1722 if (mo->clear)
1723 *flags &= ~mo->flag;
1724 else
1725 *flags |= mo->flag;
1726 return 0;
1727 }
1728 }
1729 }
1730
1731 if (strlen(*data)) {
1732 ret = strlcat(*data, ",", size);
1733 if (ret < 0)
1734 return log_error_errno(ret, errno, "Failed to append \",\" to %s", *data);
1735 }
1736
1737 ret = strlcat(*data, opt, size);
1738 if (ret < 0)
1739 return log_error_errno(ret, errno, "Failed to append \"%s\" to %s", opt, *data);
1740
1741 return 0;
1742}
1743
1744int parse_mntopts(const char *mntopts, unsigned long *mntflags, char **mntdata)
1745{
1746 __do_free char *mntopts_new = NULL, *mntopts_dup = NULL;
1747 char *mntopt_cur = NULL;
1748 size_t size;
1749
1750 if (*mntdata || *mntflags)
1751 return ret_errno(EINVAL);
1752
1753 if (!mntopts)
1754 return 0;
1755
1756 mntopts_dup = strdup(mntopts);
1757 if (!mntopts_dup)
1758 return ret_errno(ENOMEM);
1759
1760 size = strlen(mntopts_dup) + 1;
1761 mntopts_new = zalloc(size);
1762 if (!mntopts_new)
1763 return ret_errno(ENOMEM);
1764
1765 lxc_iterate_parts(mntopt_cur, mntopts_dup, ",")
1766 if (parse_mntopt(mntopt_cur, mntflags, &mntopts_new, size) < 0)
1767 return ret_errno(EINVAL);
1768
1769 if (*mntopts_new)
1770 *mntdata = move_ptr(mntopts_new);
1771
1772 return 0;
1773}
1774
1775static void parse_propagationopt(char *opt, unsigned long *flags)
1776{
1777 struct mount_opt *mo;
1778
1779 /* If opt is found in propagation_opt, set or clear flags. */
1780 for (mo = &propagation_opt[0]; mo->name != NULL; mo++) {
1781 if (strncmp(opt, mo->name, strlen(mo->name)) != 0)
1782 continue;
1783
1784 if (mo->clear)
1785 *flags &= ~mo->flag;
1786 else
1787 *flags |= mo->flag;
1788
1789 return;
1790 }
1791}
1792
1793int parse_propagationopts(const char *mntopts, unsigned long *pflags)
1794{
1795 __do_free char *s = NULL;
1796 char *p;
1797
1798 if (!mntopts)
1799 return 0;
1800
1801 s = strdup(mntopts);
1802 if (!s)
1803 return log_error_errno(-ENOMEM, errno, "Failed to allocate memory");
1804
1805 *pflags = 0L;
1806 lxc_iterate_parts(p, s, ",")
1807 parse_propagationopt(p, pflags);
1808
1809 return 0;
1810}
1811
1812static void null_endofword(char *word)
1813{
1814 while (*word && *word != ' ' && *word != '\t')
1815 word++;
1816 *word = '\0';
1817}
1818
1819/* skip @nfields spaces in @src */
1820static char *get_field(char *src, int nfields)
1821{
1822 int i;
1823 char *p = src;
1824
1825 for (i = 0; i < nfields; i++) {
1826 while (*p && *p != ' ' && *p != '\t')
1827 p++;
1828
1829 if (!*p)
1830 break;
1831
1832 p++;
1833 }
1834
1835 return p;
1836}
1837
1838static int mount_entry(const char *fsname, const char *target,
1839 const char *fstype, unsigned long mountflags,
1840 unsigned long pflags, const char *data, bool optional,
1841 bool dev, bool relative, const char *rootfs)
1842{
1843 int ret;
1844 char srcbuf[PATH_MAX];
1845 const char *srcpath = fsname;
1846#ifdef HAVE_STATVFS
1847 struct statvfs sb;
1848#endif
1849
1850 if (relative) {
1851 ret = snprintf(srcbuf, sizeof(srcbuf), "%s/%s", rootfs ? rootfs : "/", fsname ? fsname : "");
1852 if (ret < 0 || ret >= sizeof(srcbuf))
1853 return log_error_errno(-1, errno, "source path is too long");
1854 srcpath = srcbuf;
1855 }
1856
1857 ret = safe_mount(srcpath, target, fstype, mountflags & ~MS_REMOUNT, data,
1858 rootfs);
1859 if (ret < 0) {
1860 if (optional)
1861 return log_info_errno(0, errno, "Failed to mount \"%s\" on \"%s\" (optional)",
1862 srcpath ? srcpath : "(null)", target);
1863
1864 return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"",
1865 srcpath ? srcpath : "(null)", target);
1866 }
1867
1868 if ((mountflags & MS_REMOUNT) || (mountflags & MS_BIND)) {
1869
1870 DEBUG("Remounting \"%s\" on \"%s\" to respect bind or remount options",
1871 srcpath ? srcpath : "(none)", target ? target : "(none)");
1872
1873#ifdef HAVE_STATVFS
1874 if (srcpath && statvfs(srcpath, &sb) == 0) {
1875 unsigned long required_flags = 0;
1876
1877 if (sb.f_flag & MS_NOSUID)
1878 required_flags |= MS_NOSUID;
1879
1880 if (sb.f_flag & MS_NODEV && !dev)
1881 required_flags |= MS_NODEV;
1882
1883 if (sb.f_flag & MS_RDONLY)
1884 required_flags |= MS_RDONLY;
1885
1886 if (sb.f_flag & MS_NOEXEC)
1887 required_flags |= MS_NOEXEC;
1888
1889 DEBUG("Flags for \"%s\" were %lu, required extra flags are %lu",
1890 srcpath, sb.f_flag, required_flags);
1891
1892 /* If this was a bind mount request, and required_flags
1893 * does not have any flags which are not already in
1894 * mountflags, then skip the remount.
1895 */
1896 if (!(mountflags & MS_REMOUNT) &&
1897 (!(required_flags & ~mountflags) && !(mountflags & MS_RDONLY))) {
1898 DEBUG("Mountflags already were %lu, skipping remount", mountflags);
1899 goto skipremount;
1900 }
1901
1902 mountflags |= required_flags;
1903 }
1904#endif
1905
1906 ret = mount(srcpath, target, fstype, mountflags | MS_REMOUNT, data);
1907 if (ret < 0) {
1908 if (optional)
1909 return log_info_errno(0, errno, "Failed to mount \"%s\" on \"%s\" (optional)",
1910 srcpath ? srcpath : "(null)",
1911 target);
1912
1913 return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"",
1914 srcpath ? srcpath : "(null)",
1915 target);
1916 }
1917 }
1918
1919#ifdef HAVE_STATVFS
1920skipremount:
1921#endif
1922 if (pflags) {
1923 ret = mount(NULL, target, NULL, pflags, NULL);
1924 if (ret < 0) {
1925 if (optional)
1926 return log_info_errno(0, errno, "Failed to change mount propagation for \"%s\" (optional)", target);
1927 else
1928 return log_error_errno(-1, errno, "Failed to change mount propagation for \"%s\" (optional)", target);
1929 }
1930 DEBUG("Changed mount propagation for \"%s\"", target);
1931 }
1932
1933 DEBUG("Mounted \"%s\" on \"%s\" with filesystem type \"%s\"",
1934 srcpath ? srcpath : "(null)", target, fstype);
1935
1936 return 0;
1937}
1938
1939/* Remove "optional", "create=dir", and "create=file" from mntopt */
1940static void cull_mntent_opt(struct mntent *mntent)
1941{
1942 int i;
1943 char *list[] = {
1944 "create=dir",
1945 "create=file",
1946 "optional",
1947 "relative",
1948 NULL
1949 };
1950
1951 for (i = 0; list[i]; i++) {
1952 char *p, *p2;
1953
1954 p = strstr(mntent->mnt_opts, list[i]);
1955 if (!p)
1956 continue;
1957
1958 p2 = strchr(p, ',');
1959 if (!p2) {
1960 /* no more mntopts, so just chop it here */
1961 *p = '\0';
1962 continue;
1963 }
1964
1965 memmove(p, p2 + 1, strlen(p2 + 1) + 1);
1966 }
1967}
1968
1969static int mount_entry_create_dir_file(const struct mntent *mntent,
1970 const char *path,
1971 const struct lxc_rootfs *rootfs,
1972 const char *lxc_name, const char *lxc_path)
1973{
1974 __do_free char *p1 = NULL;
1975 int ret;
1976 char *p2;
1977
1978 if (strncmp(mntent->mnt_type, "overlay", 7) == 0) {
1979 ret = ovl_mkdir(mntent, rootfs, lxc_name, lxc_path);
1980 if (ret < 0)
1981 return -1;
1982 }
1983
1984 if (hasmntopt(mntent, "create=dir")) {
1985 ret = mkdir_p(path, 0755);
1986 if (ret < 0 && errno != EEXIST)
1987 return log_error_errno(-1, errno, "Failed to create directory \"%s\"", path);
1988 }
1989
1990 if (!hasmntopt(mntent, "create=file"))
1991 return 0;
1992
1993 ret = access(path, F_OK);
1994 if (ret == 0)
1995 return 0;
1996
1997 p1 = strdup(path);
1998 if (!p1)
1999 return -1;
2000
2001 p2 = dirname(p1);
2002
2003 ret = mkdir_p(p2, 0755);
2004 if (ret < 0 && errno != EEXIST)
2005 return log_error_errno(-1, errno, "Failed to create directory \"%s\"", path);
2006
2007 ret = mknod(path, S_IFREG | 0000, 0);
2008 if (ret < 0 && errno != EEXIST)
2009 return -errno;
2010
2011 return 0;
2012}
2013
2014/* rootfs, lxc_name, and lxc_path can be NULL when the container is created
2015 * without a rootfs. */
2016static inline int mount_entry_on_generic(struct mntent *mntent,
2017 const char *path,
2018 const struct lxc_rootfs *rootfs,
2019 const char *lxc_name,
2020 const char *lxc_path)
2021{
2022 __do_free char *mntdata = NULL;
2023 unsigned long mntflags = 0, pflags = 0;
2024 char *rootfs_path = NULL;
2025 int ret;
2026 bool dev, optional, relative;
2027
2028 optional = hasmntopt(mntent, "optional") != NULL;
2029 dev = hasmntopt(mntent, "dev") != NULL;
2030 relative = hasmntopt(mntent, "relative") != NULL;
2031
2032 if (rootfs && rootfs->path)
2033 rootfs_path = rootfs->mount;
2034
2035 ret = mount_entry_create_dir_file(mntent, path, rootfs, lxc_name,
2036 lxc_path);
2037 if (ret < 0) {
2038 if (optional)
2039 return 0;
2040
2041 return -1;
2042 }
2043 cull_mntent_opt(mntent);
2044
2045 ret = parse_propagationopts(mntent->mnt_opts, &pflags);
2046 if (ret < 0)
2047 return -1;
2048
2049 ret = parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata);
2050 if (ret < 0)
2051 return ret;
2052
2053 ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type, mntflags,
2054 pflags, mntdata, optional, dev, relative, rootfs_path);
2055
2056 return ret;
2057}
2058
2059static inline int mount_entry_on_systemfs(struct mntent *mntent)
2060{
2061 int ret;
2062 char path[PATH_MAX];
2063
2064 /* For containers created without a rootfs all mounts are treated as
2065 * absolute paths starting at / on the host.
2066 */
2067 if (mntent->mnt_dir[0] != '/')
2068 ret = snprintf(path, sizeof(path), "/%s", mntent->mnt_dir);
2069 else
2070 ret = snprintf(path, sizeof(path), "%s", mntent->mnt_dir);
2071 if (ret < 0 || ret >= sizeof(path))
2072 return -1;
2073
2074 return mount_entry_on_generic(mntent, path, NULL, NULL, NULL);
2075}
2076
2077static int mount_entry_on_absolute_rootfs(struct mntent *mntent,
2078 const struct lxc_rootfs *rootfs,
2079 const char *lxc_name,
2080 const char *lxc_path)
2081{
2082 int offset;
2083 char *aux;
2084 const char *lxcpath;
2085 char path[PATH_MAX];
2086 int ret = 0;
2087
2088 lxcpath = lxc_global_config_value("lxc.lxcpath");
2089 if (!lxcpath)
2090 return -1;
2091
2092 /* If rootfs->path is a blockdev path, allow container fstab to use
2093 * <lxcpath>/<name>/rootfs" as the target prefix.
2094 */
2095 ret = snprintf(path, PATH_MAX, "%s/%s/rootfs", lxcpath, lxc_name);
2096 if (ret < 0 || ret >= PATH_MAX)
2097 goto skipvarlib;
2098
2099 aux = strstr(mntent->mnt_dir, path);
2100 if (aux) {
2101 offset = strlen(path);
2102 goto skipabs;
2103 }
2104
2105skipvarlib:
2106 aux = strstr(mntent->mnt_dir, rootfs->path);
2107 if (!aux)
2108 return log_warn(ret, "Ignoring mount point \"%s\"", mntent->mnt_dir);
2109 offset = strlen(rootfs->path);
2110
2111skipabs:
2112 ret = snprintf(path, PATH_MAX, "%s/%s", rootfs->mount, aux + offset);
2113 if (ret < 0 || ret >= PATH_MAX)
2114 return -1;
2115
2116 return mount_entry_on_generic(mntent, path, rootfs, lxc_name, lxc_path);
2117}
2118
2119static int mount_entry_on_relative_rootfs(struct mntent *mntent,
2120 const struct lxc_rootfs *rootfs,
2121 const char *lxc_name,
2122 const char *lxc_path)
2123{
2124 int ret;
2125 char path[PATH_MAX];
2126
2127 /* relative to root mount point */
2128 ret = snprintf(path, sizeof(path), "%s/%s", rootfs->mount, mntent->mnt_dir);
2129 if (ret < 0 || (size_t)ret >= sizeof(path))
2130 return -1;
2131
2132 return mount_entry_on_generic(mntent, path, rootfs, lxc_name, lxc_path);
2133}
2134
2135static int mount_file_entries(const struct lxc_conf *conf,
2136 const struct lxc_rootfs *rootfs, FILE *file,
2137 const char *lxc_name, const char *lxc_path)
2138{
2139 char buf[PATH_MAX];
2140 struct mntent mntent;
2141
2142 while (getmntent_r(file, &mntent, buf, sizeof(buf))) {
2143 int ret;
2144
2145 if (!rootfs->path)
2146 ret = mount_entry_on_systemfs(&mntent);
2147 else if (mntent.mnt_dir[0] != '/')
2148 ret = mount_entry_on_relative_rootfs(&mntent, rootfs,
2149 lxc_name, lxc_path);
2150 else
2151 ret = mount_entry_on_absolute_rootfs(&mntent, rootfs,
2152 lxc_name, lxc_path);
2153 if (ret < 0)
2154 return -1;
2155 }
2156
2157 if (!feof(file) || ferror(file))
2158 return log_error(-1, "Failed to parse mount entries");
2159
2160 return 0;
2161}
2162
2163static inline void __auto_endmntent__(FILE **f)
2164{
2165 if (*f)
2166 endmntent(*f);
2167}
2168
2169#define __do_endmntent __attribute__((__cleanup__(__auto_endmntent__)))
2170
2171static int setup_mount(const struct lxc_conf *conf,
2172 const struct lxc_rootfs *rootfs, const char *fstab,
2173 const char *lxc_name, const char *lxc_path)
2174{
2175 __do_endmntent FILE *f = NULL;
2176 int ret;
2177
2178 if (!fstab)
2179 return 0;
2180
2181 f = setmntent(fstab, "re");
2182 if (!f)
2183 return log_error_errno(-1, errno, "Failed to open \"%s\"", fstab);
2184
2185 ret = mount_file_entries(conf, rootfs, f, lxc_name, lxc_path);
2186 if (ret < 0)
2187 ERROR("Failed to set up mount entries");
2188
2189 return ret;
2190}
2191
2192/*
2193 * In order for nested containers to be able to mount /proc and /sys they need
2194 * to see a "pure" proc and sysfs mount points with nothing mounted on top
2195 * (like lxcfs).
2196 * For this we provide proc and sysfs in /dev/.lxc/{proc,sys} while using an
2197 * apparmor rule to deny access to them. This is mostly for convenience: The
2198 * container's root user can mount them anyway and thus has access to the two
2199 * file systems. But a non-root user in the container should not be allowed to
2200 * access them as a side effect without explicitly allowing it.
2201 */
2202static const char nesting_helpers[] =
2203"proc dev/.lxc/proc proc create=dir,optional 0 0\n"
2204"sys dev/.lxc/sys sysfs create=dir,optional 0 0\n";
2205
2206FILE *make_anonymous_mount_file(struct lxc_list *mount,
2207 bool include_nesting_helpers)
2208{
2209 __do_close int fd = -EBADF;
2210 FILE *f;
2211 int ret;
2212 char *mount_entry;
2213 struct lxc_list *iterator;
2214
2215 fd = memfd_create(".lxc_mount_file", MFD_CLOEXEC);
2216 if (fd < 0) {
2217 char template[] = P_tmpdir "/.lxc_mount_file_XXXXXX";
2218
2219 if (errno != ENOSYS)
2220 return NULL;
2221
2222 fd = lxc_make_tmpfile(template, true);
2223 if (fd < 0)
2224 return log_error_errno(NULL, errno, "Could not create temporary mount file");
2225
2226 TRACE("Created temporary mount file");
2227 }
2228
2229 lxc_list_for_each (iterator, mount) {
2230 size_t len;
2231
2232 mount_entry = iterator->elem;
2233 len = strlen(mount_entry);
2234
2235 ret = lxc_write_nointr(fd, mount_entry, len);
2236 if (ret != len)
2237 return NULL;
2238
2239 ret = lxc_write_nointr(fd, "\n", 1);
2240 if (ret != 1)
2241 return NULL;
2242 }
2243
2244 if (include_nesting_helpers) {
2245 ret = lxc_write_nointr(fd, nesting_helpers,
2246 STRARRAYLEN(nesting_helpers));
2247 if (ret != STRARRAYLEN(nesting_helpers))
2248 return NULL;
2249 }
2250
2251 ret = lseek(fd, 0, SEEK_SET);
2252 if (ret < 0)
2253 return NULL;
2254
2255 f = fdopen(fd, "re+");
2256 if (f)
2257 move_fd(fd); /* Transfer ownership of fd. */
2258 return f;
2259}
2260
2261static int setup_mount_entries(const struct lxc_conf *conf,
2262 const struct lxc_rootfs *rootfs,
2263 struct lxc_list *mount, const char *lxc_name,
2264 const char *lxc_path)
2265{
2266 __do_fclose FILE *f = NULL;
2267
2268 f = make_anonymous_mount_file(mount, conf->lsm_aa_allow_nesting);
2269 if (!f)
2270 return -1;
2271
2272 return mount_file_entries(conf, rootfs, f, lxc_name, lxc_path);
2273}
2274
2275static int parse_cap(const char *cap)
2276{
2277 size_t i;
2278 int capid = -1;
2279 size_t end = sizeof(caps_opt) / sizeof(caps_opt[0]);
2280 char *ptr = NULL;
2281
2282 if (strcmp(cap, "none") == 0)
2283 return -2;
2284
2285 for (i = 0; i < end; i++) {
2286 if (strcmp(cap, caps_opt[i].name))
2287 continue;
2288
2289 capid = caps_opt[i].value;
2290 break;
2291 }
2292
2293 if (capid < 0) {
2294 /* Try to see if it's numeric, so the user may specify
2295 * capabilities that the running kernel knows about but we
2296 * don't
2297 */
2298 errno = 0;
2299 capid = strtol(cap, &ptr, 10);
2300 if (!ptr || *ptr != '\0' || errno != 0)
2301 /* not a valid number */
2302 capid = -1;
2303 else if (capid > lxc_caps_last_cap())
2304 /* we have a number but it's not a valid
2305 * capability */
2306 capid = -1;
2307 }
2308
2309 return capid;
2310}
2311
2312int in_caplist(int cap, struct lxc_list *caps)
2313{
2314 int capid;
2315 struct lxc_list *iterator;
2316
2317 lxc_list_for_each (iterator, caps) {
2318 capid = parse_cap(iterator->elem);
2319 if (capid == cap)
2320 return 1;
2321 }
2322
2323 return 0;
2324}
2325
2326static int setup_caps(struct lxc_list *caps)
2327{
2328 int capid;
2329 char *drop_entry;
2330 struct lxc_list *iterator;
2331
2332 lxc_list_for_each (iterator, caps) {
2333 int ret;
2334
2335 drop_entry = iterator->elem;
2336
2337 capid = parse_cap(drop_entry);
2338 if (capid < 0)
2339 return log_error(-1, "unknown capability %s", drop_entry);
2340
2341 ret = prctl(PR_CAPBSET_DROP, prctl_arg(capid), prctl_arg(0),
2342 prctl_arg(0), prctl_arg(0));
2343 if (ret < 0)
2344 return log_error_errno(-1, errno, "Failed to remove %s capability", drop_entry);
2345 DEBUG("Dropped %s (%d) capability", drop_entry, capid);
2346 }
2347
2348 DEBUG("Capabilities have been setup");
2349 return 0;
2350}
2351
2352static int dropcaps_except(struct lxc_list *caps)
2353{
2354 __do_free int *caplist = NULL;
2355 int i, capid, numcaps;
2356 char *keep_entry;
2357 struct lxc_list *iterator;
2358
2359 numcaps = lxc_caps_last_cap() + 1;
2360 if (numcaps <= 0 || numcaps > 200)
2361 return -1;
2362 TRACE("Found %d capabilities", numcaps);
2363
2364 /* caplist[i] is 1 if we keep capability i */
2365 caplist = must_realloc(NULL, numcaps * sizeof(int));
2366 memset(caplist, 0, numcaps * sizeof(int));
2367
2368 lxc_list_for_each (iterator, caps) {
2369 keep_entry = iterator->elem;
2370
2371 capid = parse_cap(keep_entry);
2372 if (capid == -2)
2373 continue;
2374
2375 if (capid < 0)
2376 return log_error(-1, "Unknown capability %s", keep_entry);
2377
2378 DEBUG("Keep capability %s (%d)", keep_entry, capid);
2379 caplist[capid] = 1;
2380 }
2381
2382 for (i = 0; i < numcaps; i++) {
2383 int ret;
2384
2385 if (caplist[i])
2386 continue;
2387
2388 ret = prctl(PR_CAPBSET_DROP, prctl_arg(i), prctl_arg(0),
2389 prctl_arg(0), prctl_arg(0));
2390 if (ret < 0)
2391 return log_error_errno(-1, errno, "Failed to remove capability %d", i);
2392 }
2393
2394 DEBUG("Capabilities have been setup");
2395 return 0;
2396}
2397
2398static int parse_resource(const char *res)
2399{
2400 int ret;
2401 size_t i;
2402 int resid = -1;
2403
2404 for (i = 0; i < sizeof(limit_opt) / sizeof(limit_opt[0]); ++i)
2405 if (strcmp(res, limit_opt[i].name) == 0)
2406 return limit_opt[i].value;
2407
2408 /* Try to see if it's numeric, so the user may specify
2409 * resources that the running kernel knows about but
2410 * we don't.
2411 */
2412 ret = lxc_safe_int(res, &resid);
2413 if (ret < 0)
2414 return -1;
2415
2416 return resid;
2417}
2418
2419int setup_resource_limits(struct lxc_list *limits, pid_t pid)
2420{
2421 int resid;
2422 struct lxc_list *it;
2423 struct lxc_limit *lim;
2424
2425 lxc_list_for_each (it, limits) {
2426 lim = it->elem;
2427
2428 resid = parse_resource(lim->resource);
2429 if (resid < 0)
2430 return log_error(-1, "Unknown resource %s", lim->resource);
2431
2432#if HAVE_PRLIMIT || HAVE_PRLIMIT64
2433 if (prlimit(pid, resid, &lim->limit, NULL) != 0)
2434 return log_error_errno(-1, errno, "Failed to set limit %s", lim->resource);
2435
2436 TRACE("Setup \"%s\" limit", lim->resource);
2437#else
2438 return log_error(-1, "Cannot set limit \"%s\" as prlimit is missing", lim->resource);
2439#endif
2440 }
2441
2442 return 0;
2443}
2444
2445int setup_sysctl_parameters(struct lxc_list *sysctls)
2446{
2447 __do_free char *tmp = NULL;
2448 struct lxc_list *it;
2449 struct lxc_sysctl *elem;
2450 int ret = 0;
2451 char filename[PATH_MAX] = {0};
2452
2453 lxc_list_for_each (it, sysctls) {
2454 elem = it->elem;
2455 tmp = lxc_string_replace(".", "/", elem->key);
2456 if (!tmp)
2457 return log_error(-1, "Failed to replace key %s", elem->key);
2458
2459 ret = snprintf(filename, sizeof(filename), "/proc/sys/%s", tmp);
2460 if (ret < 0 || (size_t)ret >= sizeof(filename))
2461 return log_error(-1, "Error setting up sysctl parameters path");
2462
2463 ret = lxc_write_to_file(filename, elem->value,
2464 strlen(elem->value), false, 0666);
2465 if (ret < 0)
2466 return log_error_errno(-1, errno, "Failed to setup sysctl parameters %s to %s",
2467 elem->key, elem->value);
2468 }
2469
2470 return 0;
2471}
2472
2473int setup_proc_filesystem(struct lxc_list *procs, pid_t pid)
2474{
2475 __do_free char *tmp = NULL;
2476 struct lxc_list *it;
2477 struct lxc_proc *elem;
2478 int ret = 0;
2479 char filename[PATH_MAX] = {0};
2480
2481 lxc_list_for_each (it, procs) {
2482 elem = it->elem;
2483 tmp = lxc_string_replace(".", "/", elem->filename);
2484 if (!tmp)
2485 return log_error(-1, "Failed to replace key %s", elem->filename);
2486
2487 ret = snprintf(filename, sizeof(filename), "/proc/%d/%s", pid, tmp);
2488 if (ret < 0 || (size_t)ret >= sizeof(filename))
2489 return log_error(-1, "Error setting up proc filesystem path");
2490
2491 ret = lxc_write_to_file(filename, elem->value,
2492 strlen(elem->value), false, 0666);
2493 if (ret < 0)
2494 return log_error_errno(-1, errno, "Failed to setup proc filesystem %s to %s", elem->filename, elem->value);
2495 }
2496
2497 return 0;
2498}
2499
2500static char *default_rootfs_mount = LXCROOTFSMOUNT;
2501
2502struct lxc_conf *lxc_conf_init(void)
2503{
2504 int i;
2505 struct lxc_conf *new;
2506
2507 new = malloc(sizeof(*new));
2508 if (!new)
2509 return NULL;
2510 memset(new, 0, sizeof(*new));
2511
2512 new->loglevel = LXC_LOG_LEVEL_NOTSET;
2513 new->personality = -1;
2514 new->autodev = 1;
2515 new->console.buffer_size = 0;
2516 new->console.log_path = NULL;
2517 new->console.log_fd = -1;
2518 new->console.log_size = 0;
2519 new->console.path = NULL;
2520 new->console.peer = -1;
2521 new->console.proxy.busy = -1;
2522 new->console.proxy.master = -1;
2523 new->console.proxy.slave = -1;
2524 new->console.master = -1;
2525 new->console.slave = -1;
2526 new->console.name[0] = '\0';
2527 memset(&new->console.ringbuf, 0, sizeof(struct lxc_ringbuf));
2528 new->maincmd_fd = -1;
2529 new->monitor_signal_pdeath = SIGKILL;
2530 new->nbd_idx = -1;
2531 new->rootfs.mount = strdup(default_rootfs_mount);
2532 if (!new->rootfs.mount) {
2533 free(new);
2534 return NULL;
2535 }
2536 new->rootfs.managed = true;
2537 new->logfd = -1;
2538 lxc_list_init(&new->cgroup);
2539 lxc_list_init(&new->cgroup2);
2540 lxc_list_init(&new->devices);
2541 lxc_list_init(&new->network);
2542 lxc_list_init(&new->mount_list);
2543 lxc_list_init(&new->caps);
2544 lxc_list_init(&new->keepcaps);
2545 lxc_list_init(&new->id_map);
2546 new->root_nsuid_map = NULL;
2547 new->root_nsgid_map = NULL;
2548 lxc_list_init(&new->includes);
2549 lxc_list_init(&new->aliens);
2550 lxc_list_init(&new->environment);
2551 lxc_list_init(&new->limits);
2552 lxc_list_init(&new->sysctls);
2553 lxc_list_init(&new->procs);
2554 new->hooks_version = 0;
2555 for (i = 0; i < NUM_LXC_HOOKS; i++)
2556 lxc_list_init(&new->hooks[i]);
2557 lxc_list_init(&new->groups);
2558 lxc_list_init(&new->state_clients);
2559 new->lsm_aa_profile = NULL;
2560 lxc_list_init(&new->lsm_aa_raw);
2561 new->lsm_se_context = NULL;
2562 new->lsm_se_keyring_context = NULL;
2563 new->keyring_disable_session = false;
2564 new->tmp_umount_proc = false;
2565 new->tmp_umount_proc = 0;
2566 new->shmount.path_host = NULL;
2567 new->shmount.path_cont = NULL;
2568
2569 /* if running in a new user namespace, init and COMMAND
2570 * default to running as UID/GID 0 when using lxc-execute */
2571 new->init_uid = 0;
2572 new->init_gid = 0;
2573 memset(&new->cgroup_meta, 0, sizeof(struct lxc_cgroup));
2574 memset(&new->ns_share, 0, sizeof(char *) * LXC_NS_MAX);
2575 seccomp_conf_init(new);
2576
2577 return new;
2578}
2579
2580int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf,
2581 size_t buf_size)
2582{
2583 __do_close int fd = -EBADF;
2584 int ret;
2585 char path[PATH_MAX];
2586
2587 if (geteuid() != 0 && idtype == ID_TYPE_GID) {
2588 __do_close int setgroups_fd = -EBADF;
2589
2590 ret = snprintf(path, PATH_MAX, "/proc/%d/setgroups", pid);
2591 if (ret < 0 || ret >= PATH_MAX)
2592 return -E2BIG;
2593
2594 setgroups_fd = open(path, O_WRONLY);
2595 if (setgroups_fd < 0 && errno != ENOENT)
2596 return log_error_errno(-1, errno, "Failed to open \"%s\"", path);
2597
2598 if (setgroups_fd >= 0) {
2599 ret = lxc_write_nointr(setgroups_fd, "deny\n",
2600 STRLITERALLEN("deny\n"));
2601 if (ret != STRLITERALLEN("deny\n"))
2602 return log_error_errno(-1, errno, "Failed to write \"deny\" to \"/proc/%d/setgroups\"", pid);
2603 TRACE("Wrote \"deny\" to \"/proc/%d/setgroups\"", pid);
2604 }
2605 }
2606
2607 ret = snprintf(path, PATH_MAX, "/proc/%d/%cid_map", pid,
2608 idtype == ID_TYPE_UID ? 'u' : 'g');
2609 if (ret < 0 || ret >= PATH_MAX)
2610 return -E2BIG;
2611
2612 fd = open(path, O_WRONLY | O_CLOEXEC);
2613 if (fd < 0)
2614 return log_error_errno(-1, errno, "Failed to open \"%s\"", path);
2615
2616 ret = lxc_write_nointr(fd, buf, buf_size);
2617 if (ret != buf_size)
2618 return log_error_errno(-1, errno, "Failed to write %cid mapping to \"%s\"",
2619 idtype == ID_TYPE_UID ? 'u' : 'g', path);
2620
2621 return 0;
2622}
2623
2624/* Check whether a binary exist and has either CAP_SETUID, CAP_SETGID or both.
2625 *
2626 * @return 1 if functional binary was found
2627 * @return 0 if binary exists but is lacking privilege
2628 * @return -ENOENT if binary does not exist
2629 * @return -EINVAL if cap to check is neither CAP_SETUID nor CAP_SETGID
2630 */
2631static int idmaptool_on_path_and_privileged(const char *binary, cap_value_t cap)
2632{
2633 __do_free char *path = NULL;
2634 int ret;
2635 struct stat st;
2636
2637 errno = EINVAL;
2638 if (cap != CAP_SETUID && cap != CAP_SETGID)
2639 return -1;
2640
2641 errno = ENOENT;
2642 path = on_path(binary, NULL);
2643 if (!path)
2644 return -1;
2645
2646 ret = stat(path, &st);
2647 if (ret < 0)
2648 return -1;
2649
2650 /* Check if the binary is setuid. */
2651 if (st.st_mode & S_ISUID)
2652 return log_debug(1, "The binary \"%s\" does have the setuid bit set", path);
2653
2654#if HAVE_LIBCAP && LIBCAP_SUPPORTS_FILE_CAPABILITIES
2655 /* Check if it has the CAP_SETUID capability. */
2656 if ((cap & CAP_SETUID) &&
2657 lxc_file_cap_is_set(path, CAP_SETUID, CAP_EFFECTIVE) &&
2658 lxc_file_cap_is_set(path, CAP_SETUID, CAP_PERMITTED))
2659 return log_debug(1, "The binary \"%s\" has CAP_SETUID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path);
2660
2661 /* Check if it has the CAP_SETGID capability. */
2662 if ((cap & CAP_SETGID) &&
2663 lxc_file_cap_is_set(path, CAP_SETGID, CAP_EFFECTIVE) &&
2664 lxc_file_cap_is_set(path, CAP_SETGID, CAP_PERMITTED))
2665 return log_debug(1, "The binary \"%s\" has CAP_SETGID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path);
2666#else
2667 /* If we cannot check for file capabilities we need to give the benefit
2668 * of the doubt. Otherwise we might fail even though all the necessary
2669 * file capabilities are set.
2670 */
2671 DEBUG("Cannot check for file capabilities as full capability support is missing. Manual intervention needed");
2672#endif
2673
2674 return 1;
2675}
2676
2677int lxc_map_ids_exec_wrapper(void *args)
2678{
2679 execl("/bin/sh", "sh", "-c", (char *)args, (char *)NULL);
2680 return -1;
2681}
2682
2683int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
2684{
2685 int fill, left;
2686 char u_or_g;
2687 char *pos;
2688 char cmd_output[PATH_MAX];
2689 struct id_map *map;
2690 struct lxc_list *iterator;
2691 enum idtype type;
2692 int ret = 0, gidmap = 0, uidmap = 0;
2693 char mapbuf[STRLITERALLEN("new@idmap") + STRLITERALLEN(" ") +
2694 INTTYPE_TO_STRLEN(pid_t) + STRLITERALLEN(" ") +
2695 LXC_IDMAPLEN] = {0};
2696 bool had_entry = false, use_shadow = false;
2697 int hostuid, hostgid;
2698
2699 hostuid = geteuid();
2700 hostgid = getegid();
2701
2702 /* If new{g,u}idmap exists, that is, if shadow is handing out subuid
2703 * ranges, then insist that root also reserve ranges in subuid. This
2704 * will protected it by preventing another user from being handed the
2705 * range by shadow.
2706 */
2707 uidmap = idmaptool_on_path_and_privileged("newuidmap", CAP_SETUID);
2708 if (uidmap == -ENOENT)
2709 WARN("newuidmap binary is missing");
2710 else if (!uidmap)
2711 WARN("newuidmap is lacking necessary privileges");
2712
2713 gidmap = idmaptool_on_path_and_privileged("newgidmap", CAP_SETGID);
2714 if (gidmap == -ENOENT)
2715 WARN("newgidmap binary is missing");
2716 else if (!gidmap)
2717 WARN("newgidmap is lacking necessary privileges");
2718
2719 if (uidmap > 0 && gidmap > 0) {
2720 DEBUG("Functional newuidmap and newgidmap binary found");
2721 use_shadow = true;
2722 } else {
2723 /* In case unprivileged users run application containers via
2724 * execute() or a start*() there are valid cases where they may
2725 * only want to map their own {g,u}id. Let's not block them from
2726 * doing so by requiring geteuid() == 0.
2727 */
2728 DEBUG("No newuidmap and newgidmap binary found. Trying to "
2729 "write directly with euid %d", hostuid);
2730 }
2731
2732 /* Check if we really need to use newuidmap and newgidmap.
2733 * If the user is only remapping his own {g,u}id, we don't need it.
2734 */
2735 if (use_shadow && lxc_list_len(idmap) == 2) {
2736 use_shadow = false;
2737 lxc_list_for_each(iterator, idmap) {
2738 map = iterator->elem;
2739 if (map->idtype == ID_TYPE_UID && map->range == 1 &&
2740 map->nsid == hostuid && map->hostid == hostuid)
2741 continue;
2742 if (map->idtype == ID_TYPE_GID && map->range == 1 &&
2743 map->nsid == hostgid && map->hostid == hostgid)
2744 continue;
2745 use_shadow = true;
2746 break;
2747 }
2748 }
2749
2750 for (type = ID_TYPE_UID, u_or_g = 'u'; type <= ID_TYPE_GID;
2751 type++, u_or_g = 'g') {
2752 pos = mapbuf;
2753
2754 if (use_shadow)
2755 pos += sprintf(mapbuf, "new%cidmap %d", u_or_g, pid);
2756
2757 lxc_list_for_each(iterator, idmap) {
2758 map = iterator->elem;
2759 if (map->idtype != type)
2760 continue;
2761
2762 had_entry = true;
2763
2764 left = LXC_IDMAPLEN - (pos - mapbuf);
2765 fill = snprintf(pos, left, "%s%lu %lu %lu%s",
2766 use_shadow ? " " : "", map->nsid,
2767 map->hostid, map->range,
2768 use_shadow ? "" : "\n");
2769 /*
2770 * The kernel only takes <= 4k for writes to
2771 * /proc/<pid>/{g,u}id_map
2772 */
2773 if (fill <= 0 || fill >= left)
2774 return log_error_errno(-1, errno, "Too many %cid mappings defined", u_or_g);
2775
2776 pos += fill;
2777 }
2778 if (!had_entry)
2779 continue;
2780
2781 /* Try to catch the output of new{g,u}idmap to make debugging
2782 * easier.
2783 */
2784 if (use_shadow) {
2785 ret = run_command(cmd_output, sizeof(cmd_output),
2786 lxc_map_ids_exec_wrapper,
2787 (void *)mapbuf);
2788 if (ret < 0)
2789 return log_error(-1, "new%cidmap failed to write mapping \"%s\": %s", u_or_g, cmd_output, mapbuf);
2790 TRACE("new%cidmap wrote mapping \"%s\"", u_or_g, mapbuf);
2791 } else {
2792 ret = write_id_mapping(type, pid, mapbuf, pos - mapbuf);
2793 if (ret < 0)
2794 return log_error(-1, "Failed to write mapping: %s", mapbuf);
2795 TRACE("Wrote mapping \"%s\"", mapbuf);
2796 }
2797
2798 memset(mapbuf, 0, sizeof(mapbuf));
2799 }
2800
2801 return 0;
2802}
2803
2804/*
2805 * Return the host uid/gid to which the container root is mapped in val.
2806 * Return true if id was found, false otherwise.
2807 */
2808static id_t get_mapped_rootid(const struct lxc_conf *conf, enum idtype idtype)
2809{
2810 unsigned nsid;
2811 struct id_map *map;
2812 struct lxc_list *it;
2813
2814 if (idtype == ID_TYPE_UID)
2815 nsid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid;
2816 else
2817 nsid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid;
2818
2819 lxc_list_for_each (it, &conf->id_map) {
2820 map = it->elem;
2821 if (map->idtype != idtype)
2822 continue;
2823 if (map->nsid != nsid)
2824 continue;
2825 return map->hostid;
2826 }
2827
2828 if (idtype == ID_TYPE_UID)
2829 return LXC_INVALID_UID;
2830
2831 return LXC_INVALID_GID;
2832}
2833
2834int mapped_hostid(unsigned id, const struct lxc_conf *conf, enum idtype idtype)
2835{
2836 struct id_map *map;
2837 struct lxc_list *it;
2838
2839 lxc_list_for_each (it, &conf->id_map) {
2840 map = it->elem;
2841 if (map->idtype != idtype)
2842 continue;
2843
2844 if (id >= map->hostid && id < map->hostid + map->range)
2845 return (id - map->hostid) + map->nsid;
2846 }
2847
2848 return -1;
2849}
2850
2851int find_unmapped_nsid(const struct lxc_conf *conf, enum idtype idtype)
2852{
2853 struct id_map *map;
2854 struct lxc_list *it;
2855 unsigned int freeid = 0;
2856
2857again:
2858 lxc_list_for_each (it, &conf->id_map) {
2859 map = it->elem;
2860 if (map->idtype != idtype)
2861 continue;
2862
2863 if (freeid >= map->nsid && freeid < map->nsid + map->range) {
2864 freeid = map->nsid + map->range;
2865 goto again;
2866 }
2867 }
2868
2869 return freeid;
2870}
2871
2872int chown_mapped_root_exec_wrapper(void *args)
2873{
2874 execvp("lxc-usernsexec", args);
2875 return -1;
2876}
2877
2878/* chown_mapped_root: for an unprivileged user with uid/gid X to
2879 * chown a dir to subuid/subgid Y, he needs to run chown as root
2880 * in a userns where nsid 0 is mapped to hostuid/hostgid Y, and
2881 * nsid Y is mapped to hostuid/hostgid X. That way, the container
2882 * root is privileged with respect to hostuid/hostgid X, allowing
2883 * him to do the chown.
2884 */
2885int chown_mapped_root(const char *path, const struct lxc_conf *conf)
2886{
2887 uid_t rootuid, rootgid;
2888 int hostuid, hostgid, ret;
2889 struct stat sb;
2890 char map1[100], map2[100], map3[100], map4[100], map5[100];
2891 char ugid[100];
2892 const char *args1[] = {"lxc-usernsexec",
2893 "-m", map1,
2894 "-m", map2,
2895 "-m", map3,
2896 "-m", map5,
2897 "--", "chown", ugid, path,
2898 NULL};
2899 const char *args2[] = {"lxc-usernsexec",
2900 "-m", map1,
2901 "-m", map2,
2902 "-m", map3,
2903 "-m", map4,
2904 "-m", map5,
2905 "--", "chown", ugid, path,
2906 NULL};
2907 char cmd_output[PATH_MAX];
2908
2909 rootuid = get_mapped_rootid(conf, ID_TYPE_UID);
2910 if (!uid_valid(rootuid))
2911 return log_error(-1, "No uid mapping for container root");
2912
2913 rootgid = get_mapped_rootid(conf, ID_TYPE_GID);
2914 if (!gid_valid(rootgid))
2915 return log_error(-1, "No gid mapping for container root");
2916
2917 hostuid = geteuid();
2918 if (hostuid == 0) {
2919 if (chown(path, rootuid, rootgid) < 0)
2920 return log_error(-1, "Error chowning %s", path);
2921
2922 return 0;
2923 }
2924
2925 /* nothing to do */
2926 if (rootuid == hostuid)
2927 return log_info(0, "Container root is our uid; no need to chown");
2928
2929 /* save the current gid of "path" */
2930 if (stat(path, &sb) < 0)
2931 return log_error(-1, "Error stat %s", path);
2932
2933 /* Update the path argument in case this was overlayfs. */
2934 args1[sizeof(args1) / sizeof(args1[0]) - 2] = path;
2935 args2[sizeof(args2) / sizeof(args2[0]) - 2] = path;
2936
2937 /*
2938 * A file has to be group-owned by a gid mapped into the
2939 * container, or the container won't be privileged over it.
2940 */
2941 hostgid = getegid();
2942 DEBUG("trying to chown \"%s\" to %d", path, hostgid);
2943 if (sb.st_uid == hostuid &&
2944 mapped_hostid(sb.st_gid, conf, ID_TYPE_GID) < 0 &&
2945 chown(path, -1, hostgid) < 0)
2946 return log_error(-1, "Failed chgrping %s", path);
2947
2948 /* "u:0:rootuid:1" */
2949 ret = snprintf(map1, 100, "u:0:%d:1", rootuid);
2950 if (ret < 0 || ret >= 100)
2951 return log_error(-1, "Error uid printing map string");
2952
2953 /* "u:hostuid:hostuid:1" */
2954 ret = snprintf(map2, 100, "u:%d:%d:1", hostuid, hostuid);
2955 if (ret < 0 || ret >= 100)
2956 return log_error(-1, "Error uid printing map string");
2957
2958 /* "g:0:rootgid:1" */
2959 ret = snprintf(map3, 100, "g:0:%d:1", rootgid);
2960 if (ret < 0 || ret >= 100)
2961 return log_error(-1, "Error gid printing map string");
2962
2963 /* "g:pathgid:rootgid+pathgid:1" */
2964 ret = snprintf(map4, 100, "g:%d:%d:1", (gid_t)sb.st_gid,
2965 rootgid + (gid_t)sb.st_gid);
2966 if (ret < 0 || ret >= 100)
2967 return log_error(-1, "Error gid printing map string");
2968
2969 /* "g:hostgid:hostgid:1" */
2970 ret = snprintf(map5, 100, "g:%d:%d:1", hostgid, hostgid);
2971 if (ret < 0 || ret >= 100)
2972 return log_error(-1, "Error gid printing map string");
2973
2974 /* "0:pathgid" (chown) */
2975 ret = snprintf(ugid, 100, "0:%d", (gid_t)sb.st_gid);
2976 if (ret < 0 || ret >= 100)
2977 return log_error(-1, "Error owner printing format string for chown");
2978
2979 if (hostgid == sb.st_gid)
2980 ret = run_command(cmd_output, sizeof(cmd_output),
2981 chown_mapped_root_exec_wrapper,
2982 (void *)args1);
2983 else
2984 ret = run_command(cmd_output, sizeof(cmd_output),
2985 chown_mapped_root_exec_wrapper,
2986 (void *)args2);
2987 if (ret < 0)
2988 ERROR("lxc-usernsexec failed: %s", cmd_output);
2989
2990 return ret;
2991}
2992
2993/* NOTE: Must not be called from inside the container namespace! */
2994int lxc_create_tmp_proc_mount(struct lxc_conf *conf)
2995{
2996 int mounted;
2997
2998 mounted = lxc_mount_proc_if_needed(conf->rootfs.path ? conf->rootfs.mount : "");
2999 if (mounted == -1) {
3000 SYSERROR("Failed to mount proc in the container");
3001 /* continue only if there is no rootfs */
3002 if (conf->rootfs.path)
3003 return -1;
3004 } else if (mounted == 1) {
3005 conf->tmp_umount_proc = true;
3006 }
3007
3008 return 0;
3009}
3010
3011void tmp_proc_unmount(struct lxc_conf *lxc_conf)
3012{
3013 if (!lxc_conf->tmp_umount_proc)
3014 return;
3015
3016 (void)umount2("/proc", MNT_DETACH);
3017 lxc_conf->tmp_umount_proc = false;
3018}
3019
3020/* Walk /proc/mounts and change any shared entries to slave. */
3021void remount_all_slave(void)
3022{
3023 __do_free char *line = NULL;
3024 __do_fclose FILE *f = NULL;
3025 __do_close int memfd = -EBADF, mntinfo_fd = -EBADF;
3026 int ret;
3027 ssize_t copied;
3028 size_t len = 0;
3029
3030 mntinfo_fd = open("/proc/self/mountinfo", O_RDONLY | O_CLOEXEC);
3031 if (mntinfo_fd < 0) {
3032 SYSERROR("Failed to open \"/proc/self/mountinfo\"");
3033 return;
3034 }
3035
3036 memfd = memfd_create(".lxc_mountinfo", MFD_CLOEXEC);
3037 if (memfd < 0) {
3038 char template[] = P_tmpdir "/.lxc_mountinfo_XXXXXX";
3039
3040 if (errno != ENOSYS) {
3041 SYSERROR("Failed to create temporary in-memory file");
3042 return;
3043 }
3044
3045 memfd = lxc_make_tmpfile(template, true);
3046 if (memfd < 0) {
3047 WARN("Failed to create temporary file");
3048 return;
3049 }
3050 }
3051
3052again:
3053 copied = lxc_sendfile_nointr(memfd, mntinfo_fd, NULL, LXC_SENDFILE_MAX);
3054 if (copied < 0) {
3055 if (errno == EINTR)
3056 goto again;
3057
3058 SYSERROR("Failed to copy \"/proc/self/mountinfo\"");
3059 return;
3060 }
3061
3062 ret = lseek(memfd, 0, SEEK_SET);
3063 if (ret < 0) {
3064 SYSERROR("Failed to reset file descriptor offset");
3065 return;
3066 }
3067
3068 f = fdopen(memfd, "re");
3069 if (!f) {
3070 SYSERROR("Failed to open copy of \"/proc/self/mountinfo\" to mark all shared. Continuing");
3071 return;
3072 }
3073
3074 /*
3075 * After a successful fdopen() memfd will be closed when calling
3076 * fclose(f). Calling close(memfd) afterwards is undefined.
3077 */
3078 move_fd(memfd);
3079
3080 while (getline(&line, &len, f) != -1) {
3081 char *opts, *target;
3082
3083 target = get_field(line, 4);
3084 if (!target)
3085 continue;
3086
3087 opts = get_field(target, 2);
3088 if (!opts)
3089 continue;
3090
3091 null_endofword(opts);
3092 if (!strstr(opts, "shared"))
3093 continue;
3094
3095 null_endofword(target);
3096 ret = mount(NULL, target, NULL, MS_SLAVE, NULL);
3097 if (ret < 0) {
3098 SYSERROR("Failed to make \"%s\" MS_SLAVE", target);
3099 ERROR("Continuing...");
3100 continue;
3101 }
3102 TRACE("Remounted \"%s\" as MS_SLAVE", target);
3103 }
3104 TRACE("Remounted all mount table entries as MS_SLAVE");
3105}
3106
3107static int lxc_execute_bind_init(struct lxc_handler *handler)
3108{
3109 int ret;
3110 char *p;
3111 char path[PATH_MAX], destpath[PATH_MAX];
3112 struct lxc_conf *conf = handler->conf;
3113
3114 /* If init exists in the container, don't bind mount a static one */
3115 p = choose_init(conf->rootfs.mount);
3116 if (p) {
3117 __do_free char *old = p;
3118
3119 p = strdup(old + strlen(conf->rootfs.mount));
3120 if (!p)
3121 return -ENOMEM;
3122
3123 INFO("Found existing init at \"%s\"", p);
3124 goto out;
3125 }
3126
3127 ret = snprintf(path, PATH_MAX, SBINDIR "/init.lxc.static");
3128 if (ret < 0 || ret >= PATH_MAX)
3129 return -1;
3130
3131 if (!file_exists(path))
3132 return log_error_errno(-1, errno, "The file \"%s\" does not exist on host", path);
3133
3134 ret = snprintf(destpath, PATH_MAX, "%s" P_tmpdir "%s", conf->rootfs.mount, "/.lxc-init");
3135 if (ret < 0 || ret >= PATH_MAX)
3136 return -1;
3137
3138 if (!file_exists(destpath)) {
3139 ret = mknod(destpath, S_IFREG | 0000, 0);
3140 if (ret < 0 && errno != EEXIST)
3141 return log_error_errno(-1, errno, "Failed to create dummy \"%s\" file as bind mount target", destpath);
3142 }
3143
3144 ret = safe_mount(path, destpath, "none", MS_BIND, NULL, conf->rootfs.mount);
3145 if (ret < 0)
3146 return log_error_errno(-1, errno, "Failed to bind mount lxc.init.static into container");
3147
3148 p = strdup(destpath + strlen(conf->rootfs.mount));
3149 if (!p)
3150 return -ENOMEM;
3151
3152 INFO("Bind mounted lxc.init.static into container at \"%s\"", path);
3153out:
3154 ((struct execute_args *)handler->data)->init_fd = -1;
3155 ((struct execute_args *)handler->data)->init_path = p;
3156 return 0;
3157}
3158
3159/* This does the work of remounting / if it is shared, calling the container
3160 * pre-mount hooks, and mounting the rootfs.
3161 */
3162int lxc_setup_rootfs_prepare_root(struct lxc_conf *conf, const char *name,
3163 const char *lxcpath)
3164{
3165 int ret;
3166
3167 if (conf->rootfs_setup) {
3168 const char *path = conf->rootfs.mount;
3169
3170 /* The rootfs was set up in another namespace. bind-mount it to
3171 * give us a mount in our own ns so we can pivot_root to it
3172 */
3173 ret = mount(path, path, "rootfs", MS_BIND, NULL);
3174 if (ret < 0)
3175 return log_error(-1, "Failed to bind mount container / onto itself");
3176
3177 return log_trace(0, "Bind mounted container / onto itself");
3178 }
3179
3180 remount_all_slave();
3181
3182 ret = run_lxc_hooks(name, "pre-mount", conf, NULL);
3183 if (ret < 0)
3184 return log_error(-1, "Failed to run pre-mount hooks");
3185
3186 ret = lxc_mount_rootfs(conf);
3187 if (ret < 0)
3188 return log_error(-1, "Failed to setup rootfs for");
3189
3190 conf->rootfs_setup = true;
3191 return 0;
3192}
3193
3194static bool verify_start_hooks(struct lxc_conf *conf)
3195{
3196 char path[PATH_MAX];
3197 struct lxc_list *it;
3198
3199 lxc_list_for_each (it, &conf->hooks[LXCHOOK_START]) {
3200 int ret;
3201 char *hookname = it->elem;
3202
3203 ret = snprintf(path, PATH_MAX, "%s%s",
3204 conf->rootfs.path ? conf->rootfs.mount : "",
3205 hookname);
3206 if (ret < 0 || ret >= PATH_MAX)
3207 return false;
3208
3209 ret = access(path, X_OK);
3210 if (ret < 0)
3211 return log_error_errno(false, errno, "Start hook \"%s\" not found in container", hookname);
3212
3213 return true;
3214 }
3215
3216 return true;
3217}
3218
3219static bool execveat_supported(void)
3220{
3221 lxc_raw_execveat(-1, "", NULL, NULL, AT_EMPTY_PATH);
3222 if (errno == ENOSYS)
3223 return false;
3224
3225 return true;
3226}
3227
3228static int lxc_setup_boot_id(void)
3229{
3230 int ret;
3231 const char *boot_id_path = "/proc/sys/kernel/random/boot_id";
3232 const char *mock_boot_id_path = "/dev/.lxc-boot-id";
3233 lxc_id128_t n;
3234
3235 if (access(boot_id_path, F_OK))
3236 return 0;
3237
3238 memset(&n, 0, sizeof(n));
3239 if (lxc_id128_randomize(&n)) {
3240 SYSERROR("Failed to generate random data for uuid");
3241 return -1;
3242 }
3243
3244 ret = lxc_id128_write(mock_boot_id_path, n);
3245 if (ret < 0) {
3246 SYSERROR("Failed to write uuid to %s", mock_boot_id_path);
3247 return -1;
3248 }
3249
3250 ret = chmod(mock_boot_id_path, 0444);
3251 if (ret < 0) {
3252 SYSERROR("Failed to chown %s", mock_boot_id_path);
3253 (void)unlink(mock_boot_id_path);
3254 return -1;
3255 }
3256
3257 ret = mount(mock_boot_id_path, boot_id_path, NULL, MS_BIND, NULL);
3258 if (ret < 0) {
3259 SYSERROR("Failed to mount %s to %s", mock_boot_id_path,
3260 boot_id_path);
3261 (void)unlink(mock_boot_id_path);
3262 return -1;
3263 }
3264
3265 ret = mount(NULL, boot_id_path, NULL,
3266 (MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID | MS_NOEXEC |
3267 MS_NODEV),
3268 NULL);
3269 if (ret < 0) {
3270 SYSERROR("Failed to remount %s read-only", boot_id_path);
3271 (void)unlink(mock_boot_id_path);
3272 return -1;
3273 }
3274
3275 return 0;
3276}
3277
3278int lxc_setup(struct lxc_handler *handler)
3279{
3280 __do_close int pts_mnt_fd = -EBADF;
3281 int ret;
3282 const char *lxcpath = handler->lxcpath, *name = handler->name;
3283 struct lxc_conf *lxc_conf = handler->conf;
3284 char *keyring_context = NULL;
3285
3286 ret = lxc_setup_rootfs_prepare_root(lxc_conf, name, lxcpath);
3287 if (ret < 0)
3288 return log_error(-1, "Failed to setup rootfs");
3289
3290 if (handler->nsfd[LXC_NS_UTS] == -EBADF) {
3291 ret = setup_utsname(lxc_conf->utsname);
3292 if (ret < 0)
3293 return log_error(-1, "Failed to setup the utsname %s", name);
3294 }
3295
3296 if (!lxc_conf->keyring_disable_session) {
3297 if (lxc_conf->lsm_se_keyring_context) {
3298 keyring_context = lxc_conf->lsm_se_keyring_context;
3299 } else if (lxc_conf->lsm_se_context) {
3300 keyring_context = lxc_conf->lsm_se_context;
3301 }
3302
3303 ret = lxc_setup_keyring(keyring_context);
3304 if (ret < 0)
3305 return -1;
3306 }
3307
3308 if (handler->ns_clone_flags & CLONE_NEWNET) {
3309 ret = lxc_setup_network_in_child_namespaces(lxc_conf,
3310 &lxc_conf->network);
3311 if (ret < 0)
3312 return log_error(-1, "Failed to setup network");
3313
3314 ret = lxc_network_send_name_and_ifindex_to_parent(handler);
3315 if (ret < 0)
3316 return log_error(-1, "Failed to send network device names and ifindices to parent");
3317 }
3318
3319 pts_mnt_fd = open_tree(-EBADF, lxc_conf->console.name,
3320 OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_EMPTY_PATH);
3321 if (pts_mnt_fd < 0)
3322 SYSTRACE("Failed to create detached mount for container's console \"%s\"",
3323 lxc_conf->console.name);
3324
3325 if (lxc_conf->autodev > 0) {
3326 ret = mount_autodev(name, &lxc_conf->rootfs, lxc_conf->autodevtmpfssize, lxcpath);
3327 if (ret < 0)
3328 return log_error(-1, "Failed to mount \"/dev\"");
3329 }
3330
3331 /* Do automatic mounts (mainly /proc and /sys), but exclude those that
3332 * need to wait until other stuff has finished.
3333 */
3334 ret = lxc_mount_auto_mounts(lxc_conf, lxc_conf->auto_mounts & ~LXC_AUTO_CGROUP_MASK, handler);
3335 if (ret < 0)
3336 return log_error(-1, "Failed to setup first automatic mounts");
3337
3338 ret = setup_mount(lxc_conf, &lxc_conf->rootfs, lxc_conf->fstab, name, lxcpath);
3339 if (ret < 0)
3340 return log_error(-1, "Failed to setup mounts");
3341
3342 if (!lxc_list_empty(&lxc_conf->mount_list)) {
3343 ret = setup_mount_entries(lxc_conf, &lxc_conf->rootfs,
3344 &lxc_conf->mount_list, name, lxcpath);
3345 if (ret < 0)
3346 return log_error(-1, "Failed to setup mount entries");
3347 }
3348
3349 if (lxc_conf->is_execute) {
3350 if (execveat_supported()) {
3351 int fd;
3352 char path[PATH_MAX];
3353
3354 ret = snprintf(path, PATH_MAX, SBINDIR "/init.lxc.static");
3355 if (ret < 0 || ret >= PATH_MAX)
3356 return log_error(-1, "Path to init.lxc.static too long");
3357
3358 fd = open(path, O_PATH | O_CLOEXEC);
3359 if (fd < 0)
3360 return log_error_errno(-1, errno, "Unable to open lxc.init.static");
3361
3362 ((struct execute_args *)handler->data)->init_fd = fd;
3363 ((struct execute_args *)handler->data)->init_path = NULL;
3364 } else {
3365 ret = lxc_execute_bind_init(handler);
3366 if (ret < 0)
3367 return log_error(-1, "Failed to bind-mount the lxc init system");
3368 }
3369 }
3370
3371 /* Now mount only cgroups, if wanted. Before, /sys could not have been
3372 * mounted. It is guaranteed to be mounted now either through
3373 * automatically or via fstab entries.
3374 */
3375 ret = lxc_mount_auto_mounts(lxc_conf, lxc_conf->auto_mounts & LXC_AUTO_CGROUP_MASK, handler);
3376 if (ret < 0)
3377 return log_error(-1, "Failed to setup remaining automatic mounts");
3378
3379 ret = run_lxc_hooks(name, "mount", lxc_conf, NULL);
3380 if (ret < 0)
3381 return log_error(-1, "Failed to run mount hooks");
3382
3383 if (lxc_conf->autodev > 0) {
3384 ret = run_lxc_hooks(name, "autodev", lxc_conf, NULL);
3385 if (ret < 0)
3386 return log_error(-1, "Failed to run autodev hooks");
3387
3388 ret = lxc_fill_autodev(&lxc_conf->rootfs);
3389 if (ret < 0)
3390 return log_error(-1, "Failed to populate \"/dev\"");
3391 }
3392
3393 /* Make sure any start hooks are in the container */
3394 if (!verify_start_hooks(lxc_conf))
3395 return log_error(-1, "Failed to verify start hooks");
3396
3397 ret = lxc_create_tmp_proc_mount(lxc_conf);
3398 if (ret < 0)
3399 return log_error(-1, "Failed to \"/proc\" LSMs");
3400
3401 ret = lxc_setup_console(&lxc_conf->rootfs, &lxc_conf->console,
3402 lxc_conf->ttys.dir, pts_mnt_fd);
3403 if (ret < 0)
3404 return log_error(-1, "Failed to setup console");
3405
3406 ret = lxc_setup_dev_symlinks(&lxc_conf->rootfs);
3407 if (ret < 0)
3408 return log_error(-1, "Failed to setup \"/dev\" symlinks");
3409
3410 ret = lxc_setup_rootfs_switch_root(&lxc_conf->rootfs);
3411 if (ret < 0)
3412 return log_error(-1, "Failed to pivot root into rootfs");
3413
3414 /* Setting the boot-id is best-effort for now. */
3415 if (lxc_conf->autodev > 0)
3416 (void)lxc_setup_boot_id();
3417
3418 ret = lxc_setup_devpts(lxc_conf);
3419 if (ret < 0)
3420 return log_error(-1, "Failed to setup new devpts instance");
3421
3422 ret = lxc_create_ttys(handler);
3423 if (ret < 0)
3424 return -1;
3425
3426 ret = setup_personality(lxc_conf->personality);
3427 if (ret < 0)
3428 return log_error(-1, "Failed to set personality");
3429
3430 /* Set sysctl value to a path under /proc/sys as determined from the
3431 * key. For e.g. net.ipv4.ip_forward translated to
3432 * /proc/sys/net/ipv4/ip_forward.
3433 */
3434 if (!lxc_list_empty(&lxc_conf->sysctls)) {
3435 ret = setup_sysctl_parameters(&lxc_conf->sysctls);
3436 if (ret < 0)
3437 return log_error(-1, "Failed to setup sysctl parameters");
3438 }
3439
3440 if (!lxc_list_empty(&lxc_conf->keepcaps)) {
3441 if (!lxc_list_empty(&lxc_conf->caps))
3442 return log_error(-1, "Container requests lxc.cap.drop and lxc.cap.keep: either use lxc.cap.drop or lxc.cap.keep, not both");
3443
3444 if (dropcaps_except(&lxc_conf->keepcaps))
3445 return log_error(-1, "Failed to keep capabilities");
3446 } else if (setup_caps(&lxc_conf->caps)) {
3447 return log_error(-1, "Failed to drop capabilities");
3448 }
3449
3450 NOTICE("The container \"%s\" is set up", name);
3451
3452 return 0;
3453}
3454
3455int run_lxc_hooks(const char *name, char *hookname, struct lxc_conf *conf,
3456 char *argv[])
3457{
3458 struct lxc_list *it;
3459 int which;
3460
3461 for (which = 0; which < NUM_LXC_HOOKS; which ++) {
3462 if (strcmp(hookname, lxchook_names[which]) == 0)
3463 break;
3464 }
3465
3466 if (which >= NUM_LXC_HOOKS)
3467 return -1;
3468
3469 lxc_list_for_each (it, &conf->hooks[which]) {
3470 int ret;
3471 char *hook = it->elem;
3472
3473 ret = run_script_argv(name, conf->hooks_version, "lxc", hook,
3474 hookname, argv);
3475 if (ret < 0)
3476 return -1;
3477 }
3478
3479 return 0;
3480}
3481
3482int lxc_clear_config_caps(struct lxc_conf *c)
3483{
3484 struct lxc_list *it, *next;
3485
3486 lxc_list_for_each_safe (it, &c->caps, next) {
3487 lxc_list_del(it);
3488 free(it->elem);
3489 free(it);
3490 }
3491
3492 return 0;
3493}
3494
3495static int lxc_free_idmap(struct lxc_list *id_map)
3496{
3497 struct lxc_list *it, *next;
3498
3499 lxc_list_for_each_safe(it, id_map, next) {
3500 lxc_list_del(it);
3501 free(it->elem);
3502 free(it);
3503 }
3504
3505 return 0;
3506}
3507define_cleanup_function(struct lxc_list *, lxc_free_idmap);
3508
3509int lxc_clear_idmaps(struct lxc_conf *c)
3510{
3511 return lxc_free_idmap(&c->id_map);
3512}
3513
3514int lxc_clear_config_keepcaps(struct lxc_conf *c)
3515{
3516 struct lxc_list *it, *next;
3517
3518 lxc_list_for_each_safe (it, &c->keepcaps, next) {
3519 lxc_list_del(it);
3520 free(it->elem);
3521 free(it);
3522 }
3523
3524 return 0;
3525}
3526
3527int lxc_clear_namespace(struct lxc_conf *c)
3528{
3529 int i;
3530 for (i = 0; i < LXC_NS_MAX; i++) {
3531 free(c->ns_share[i]);
3532 c->ns_share[i] = NULL;
3533 }
3534 return 0;
3535}
3536
3537int lxc_clear_cgroups(struct lxc_conf *c, const char *key, int version)
3538{
3539 char *global_token, *namespaced_token;
3540 size_t namespaced_token_len;
3541 struct lxc_list *it, *next, *list;
3542 const char *k = key;
3543 bool all = false;
3544
3545 if (version == CGROUP2_SUPER_MAGIC) {
3546 global_token = "lxc.cgroup2";
3547 namespaced_token = "lxc.cgroup2.";
3548 namespaced_token_len = STRLITERALLEN("lxc.cgroup2.");
3549 list = &c->cgroup2;
3550 } else if (version == CGROUP_SUPER_MAGIC) {
3551 global_token = "lxc.cgroup";
3552 namespaced_token = "lxc.cgroup.";
3553 namespaced_token_len = STRLITERALLEN("lxc.cgroup.");
3554 list = &c->cgroup;
3555 } else {
3556 return -EINVAL;
3557 }
3558
3559 if (strcmp(key, global_token) == 0)
3560 all = true;
3561 else if (strncmp(key, namespaced_token, namespaced_token_len) == 0)
3562 k += namespaced_token_len;
3563 else
3564 return -EINVAL;
3565
3566 lxc_list_for_each_safe (it, list, next) {
3567 struct lxc_cgroup *cg = it->elem;
3568
3569 if (!all && strcmp(cg->subsystem, k) != 0)
3570 continue;
3571
3572 lxc_list_del(it);
3573 free(cg->subsystem);
3574 free(cg->value);
3575 free(cg);
3576 free(it);
3577 }
3578
3579 return 0;
3580}
3581
3582static void lxc_clear_devices(struct lxc_conf *conf)
3583{
3584 struct lxc_list *list = &conf->devices;
3585 struct lxc_list *it, *next;
3586
3587 lxc_list_for_each_safe(it, list, next) {
3588 lxc_list_del(it);
3589 free(it);
3590 }
3591}
3592
3593int lxc_clear_limits(struct lxc_conf *c, const char *key)
3594{
3595 struct lxc_list *it, *next;
3596 const char *k = NULL;
3597 bool all = false;
3598
3599 if (strcmp(key, "lxc.limit") == 0 || strcmp(key, "lxc.prlimit") == 0)
3600 all = true;
3601 else if (strncmp(key, "lxc.limit.", STRLITERALLEN("lxc.limit.")) == 0)
3602 k = key + STRLITERALLEN("lxc.limit.");
3603 else if (strncmp(key, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) == 0)
3604 k = key + STRLITERALLEN("lxc.prlimit.");
3605 else
3606 return -1;
3607
3608 lxc_list_for_each_safe (it, &c->limits, next) {
3609 struct lxc_limit *lim = it->elem;
3610
3611 if (!all && strcmp(lim->resource, k) != 0)
3612 continue;
3613
3614 lxc_list_del(it);
3615 free(lim->resource);
3616 free(lim);
3617 free(it);
3618 }
3619
3620 return 0;
3621}
3622
3623int lxc_clear_sysctls(struct lxc_conf *c, const char *key)
3624{
3625 struct lxc_list *it, *next;
3626 const char *k = NULL;
3627 bool all = false;
3628
3629 if (strcmp(key, "lxc.sysctl") == 0)
3630 all = true;
3631 else if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
3632 k = key + STRLITERALLEN("lxc.sysctl.");
3633 else
3634 return -1;
3635
3636 lxc_list_for_each_safe (it, &c->sysctls, next) {
3637 struct lxc_sysctl *elem = it->elem;
3638
3639 if (!all && strcmp(elem->key, k) != 0)
3640 continue;
3641
3642 lxc_list_del(it);
3643 free(elem->key);
3644 free(elem->value);
3645 free(elem);
3646 free(it);
3647 }
3648
3649 return 0;
3650}
3651
3652int lxc_clear_procs(struct lxc_conf *c, const char *key)
3653{
3654 struct lxc_list *it, *next;
3655 const char *k = NULL;
3656 bool all = false;
3657
3658 if (strcmp(key, "lxc.proc") == 0)
3659 all = true;
3660 else if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
3661 k = key + STRLITERALLEN("lxc.proc.");
3662 else
3663 return -1;
3664
3665 lxc_list_for_each_safe (it, &c->procs, next) {
3666 struct lxc_proc *proc = it->elem;
3667
3668 if (!all && strcmp(proc->filename, k) != 0)
3669 continue;
3670
3671 lxc_list_del(it);
3672 free(proc->filename);
3673 free(proc->value);
3674 free(proc);
3675 free(it);
3676 }
3677
3678 return 0;
3679}
3680
3681int lxc_clear_groups(struct lxc_conf *c)
3682{
3683 struct lxc_list *it, *next;
3684
3685 lxc_list_for_each_safe (it, &c->groups, next) {
3686 lxc_list_del(it);
3687 free(it->elem);
3688 free(it);
3689 }
3690
3691 return 0;
3692}
3693
3694int lxc_clear_environment(struct lxc_conf *c)
3695{
3696 struct lxc_list *it, *next;
3697
3698 lxc_list_for_each_safe (it, &c->environment, next) {
3699 lxc_list_del(it);
3700 free(it->elem);
3701 free(it);
3702 }
3703
3704 return 0;
3705}
3706
3707int lxc_clear_mount_entries(struct lxc_conf *c)
3708{
3709 struct lxc_list *it, *next;
3710
3711 lxc_list_for_each_safe (it, &c->mount_list, next) {
3712 lxc_list_del(it);
3713 free(it->elem);
3714 free(it);
3715 }
3716
3717 return 0;
3718}
3719
3720int lxc_clear_automounts(struct lxc_conf *c)
3721{
3722 c->auto_mounts = 0;
3723 return 0;
3724}
3725
3726int lxc_clear_hooks(struct lxc_conf *c, const char *key)
3727{
3728 int i;
3729 struct lxc_list *it, *next;
3730 const char *k = NULL;
3731 bool all = false, done = false;
3732
3733 if (strcmp(key, "lxc.hook") == 0)
3734 all = true;
3735 else if (strncmp(key, "lxc.hook.", STRLITERALLEN("lxc.hook.")) == 0)
3736 k = key + STRLITERALLEN("lxc.hook.");
3737 else
3738 return -1;
3739
3740 for (i = 0; i < NUM_LXC_HOOKS; i++) {
3741 if (all || strcmp(k, lxchook_names[i]) == 0) {
3742 lxc_list_for_each_safe (it, &c->hooks[i], next) {
3743 lxc_list_del(it);
3744 free(it->elem);
3745 free(it);
3746 }
3747
3748 done = true;
3749 }
3750 }
3751
3752 if (!done)
3753 return log_error(-1, "Invalid hook key: %s", key);
3754
3755 return 0;
3756}
3757
3758static inline void lxc_clear_aliens(struct lxc_conf *conf)
3759{
3760 struct lxc_list *it, *next;
3761
3762 lxc_list_for_each_safe (it, &conf->aliens, next) {
3763 lxc_list_del(it);
3764 free(it->elem);
3765 free(it);
3766 }
3767}
3768
3769void lxc_clear_includes(struct lxc_conf *conf)
3770{
3771 struct lxc_list *it, *next;
3772
3773 lxc_list_for_each_safe (it, &conf->includes, next) {
3774 lxc_list_del(it);
3775 free(it->elem);
3776 free(it);
3777 }
3778}
3779
3780int lxc_clear_apparmor_raw(struct lxc_conf *c)
3781{
3782 struct lxc_list *it, *next;
3783
3784 lxc_list_for_each_safe (it, &c->lsm_aa_raw, next) {
3785 lxc_list_del(it);
3786 free(it->elem);
3787 free(it);
3788 }
3789
3790 return 0;
3791}
3792
3793void lxc_conf_free(struct lxc_conf *conf)
3794{
3795 if (!conf)
3796 return;
3797
3798 if (current_config == conf)
3799 current_config = NULL;
3800 lxc_terminal_conf_free(&conf->console);
3801 free(conf->rootfs.mount);
3802 free(conf->rootfs.bdev_type);
3803 free(conf->rootfs.options);
3804 free(conf->rootfs.path);
3805 free(conf->rootfs.data);
3806 free(conf->logfile);
3807 if (conf->logfd != -1)
3808 close(conf->logfd);
3809 free(conf->utsname);
3810 free(conf->ttys.dir);
3811 free(conf->ttys.tty_names);
3812 free(conf->fstab);
3813 free(conf->rcfile);
3814 free(conf->execute_cmd);
3815 free(conf->init_cmd);
3816 free(conf->init_cwd);
3817 free(conf->unexpanded_config);
3818 free(conf->syslog);
3819 lxc_free_networks(&conf->network);
3820 free(conf->lsm_aa_profile);
3821 free(conf->lsm_aa_profile_computed);
3822 free(conf->lsm_se_context);
3823 lxc_seccomp_free(&conf->seccomp);
3824 lxc_clear_config_caps(conf);
3825 lxc_clear_config_keepcaps(conf);
3826 lxc_clear_cgroups(conf, "lxc.cgroup", CGROUP_SUPER_MAGIC);
3827 lxc_clear_cgroups(conf, "lxc.cgroup2", CGROUP2_SUPER_MAGIC);
3828 lxc_clear_devices(conf);
3829 lxc_clear_cgroup2_devices(conf);
3830 lxc_clear_hooks(conf, "lxc.hook");
3831 lxc_clear_mount_entries(conf);
3832 lxc_clear_idmaps(conf);
3833 lxc_clear_groups(conf);
3834 lxc_clear_includes(conf);
3835 lxc_clear_aliens(conf);
3836 lxc_clear_environment(conf);
3837 lxc_clear_limits(conf, "lxc.prlimit");
3838 lxc_clear_sysctls(conf, "lxc.sysctl");
3839 lxc_clear_procs(conf, "lxc.proc");
3840 lxc_clear_apparmor_raw(conf);
3841 lxc_clear_namespace(conf);
3842 free(conf->cgroup_meta.dir);
3843 free(conf->cgroup_meta.monitor_dir);
3844 free(conf->cgroup_meta.container_dir);
3845 free(conf->cgroup_meta.namespace_dir);
3846 free(conf->cgroup_meta.controllers);
3847 free(conf->shmount.path_host);
3848 free(conf->shmount.path_cont);
3849 free(conf);
3850}
3851
3852struct userns_fn_data {
3853 int (*fn)(void *);
3854 const char *fn_name;
3855 void *arg;
3856 int p[2];
3857};
3858
3859static int run_userns_fn(void *data)
3860{
3861 struct userns_fn_data *d = data;
3862 int ret;
3863 char c;
3864
3865 close_prot_errno_disarm(d->p[1]);
3866
3867 /*
3868 * Wait for parent to finish establishing a new mapping in the user
3869 * namespace we are executing in.
3870 */
3871 ret = lxc_read_nointr(d->p[0], &c, 1);
3872 close_prot_errno_disarm(d->p[0]);
3873 if (ret != 1)
3874 return -1;
3875
3876 if (d->fn_name)
3877 TRACE("Calling function \"%s\"", d->fn_name);
3878
3879 /* Call function to run. */
3880 return d->fn(d->arg);
3881}
3882
3883static struct id_map *mapped_nsid_add(const struct lxc_conf *conf, unsigned id,
3884 enum idtype idtype)
3885{
3886 const struct id_map *map;
3887 struct id_map *retmap;
3888
3889 map = find_mapped_nsid_entry(conf, id, idtype);
3890 if (!map)
3891 return NULL;
3892
3893 retmap = malloc(sizeof(*retmap));
3894 if (!retmap)
3895 return NULL;
3896
3897 memcpy(retmap, map, sizeof(*retmap));
3898 return retmap;
3899}
3900
3901static struct id_map *find_mapped_hostid_entry(const struct lxc_conf *conf,
3902 unsigned id, enum idtype idtype)
3903{
3904 struct id_map *map;
3905 struct lxc_list *it;
3906 struct id_map *retmap = NULL;
3907
3908 lxc_list_for_each (it, &conf->id_map) {
3909 map = it->elem;
3910 if (map->idtype != idtype)
3911 continue;
3912
3913 if (id >= map->hostid && id < map->hostid + map->range) {
3914 retmap = map;
3915 break;
3916 }
3917 }
3918
3919 return retmap;
3920}
3921
3922/* Allocate a new {g,u}id mapping for the given {g,u}id. Re-use an already
3923 * existing one or establish a new one.
3924 */
3925static struct id_map *mapped_hostid_add(const struct lxc_conf *conf, uid_t id,
3926 enum idtype type)
3927{
3928 __do_free struct id_map *entry = NULL;
3929 int hostid_mapped;
3930 struct id_map *tmp = NULL;
3931
3932 entry = malloc(sizeof(*entry));
3933 if (!entry)
3934 return NULL;
3935
3936 /* Reuse existing mapping. */
3937 tmp = find_mapped_hostid_entry(conf, id, type);
3938 if (tmp) {
3939 memcpy(entry, tmp, sizeof(*entry));
3940 } else {
3941 /* Find new mapping. */
3942 hostid_mapped = find_unmapped_nsid(conf, type);
3943 if (hostid_mapped < 0)
3944 return log_debug(NULL, "Failed to find free mapping for id %d", id);
3945
3946 entry->idtype = type;
3947 entry->nsid = hostid_mapped;
3948 entry->hostid = (unsigned long)id;
3949 entry->range = 1;
3950 }
3951
3952 return move_ptr(entry);
3953}
3954
3955static struct lxc_list *get_minimal_idmap(const struct lxc_conf *conf,
3956 uid_t *resuid, gid_t *resgid)
3957{
3958 __do_free struct id_map *container_root_uid = NULL,
3959 *container_root_gid = NULL,
3960 *host_uid_map = NULL, *host_gid_map = NULL;
3961 __do_free struct lxc_list *idmap = NULL;
3962 uid_t euid, egid;
3963 uid_t nsuid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid;
3964 gid_t nsgid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid;
3965 struct lxc_list *tmplist = NULL;
3966
3967 /* Find container root mappings. */
3968 container_root_uid = mapped_nsid_add(conf, nsuid, ID_TYPE_UID);
3969 if (!container_root_uid)
3970 return log_debug(NULL, "Failed to find mapping for namespace uid %d", 0);
3971 euid = geteuid();
3972 if (euid >= container_root_uid->hostid &&
3973 euid < (container_root_uid->hostid + container_root_uid->range))
3974 host_uid_map = move_ptr(container_root_uid);
3975
3976 container_root_gid = mapped_nsid_add(conf, nsgid, ID_TYPE_GID);
3977 if (!container_root_gid)
3978 return log_debug(NULL, "Failed to find mapping for namespace gid %d", 0);
3979 egid = getegid();
3980 if (egid >= container_root_gid->hostid &&
3981 egid < (container_root_gid->hostid + container_root_gid->range))
3982 host_gid_map = move_ptr(container_root_gid);
3983
3984 /* Check whether the {g,u}id of the user has a mapping. */
3985 if (!host_uid_map)
3986 host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID);
3987 if (!host_uid_map)
3988 return log_debug(NULL, "Failed to find mapping for uid %d", euid);
3989
3990 if (!host_gid_map)
3991 host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID);
3992 if (!host_gid_map)
3993 return log_debug(NULL, "Failed to find mapping for gid %d", egid);
3994
3995 /* Allocate new {g,u}id map list. */
3996 idmap = malloc(sizeof(*idmap));
3997 if (!idmap)
3998 return NULL;
3999 lxc_list_init(idmap);
4000
4001 /* Add container root to the map. */
4002 tmplist = malloc(sizeof(*tmplist));
4003 if (!tmplist)
4004 return NULL;
4005 /* idmap will now keep track of that memory. */
4006 lxc_list_add_elem(tmplist, move_ptr(host_uid_map));
4007 lxc_list_add_tail(idmap, tmplist);
4008
4009 if (container_root_uid) {
4010 /* Add container root to the map. */
4011 tmplist = malloc(sizeof(*tmplist));
4012 if (!tmplist)
4013 return NULL;
4014 /* idmap will now keep track of that memory. */
4015 lxc_list_add_elem(tmplist, move_ptr(container_root_uid));
4016 lxc_list_add_tail(idmap, tmplist);
4017 }
4018
4019 tmplist = malloc(sizeof(*tmplist));
4020 if (!tmplist)
4021 return NULL;
4022 /* idmap will now keep track of that memory. */
4023 lxc_list_add_elem(tmplist, move_ptr(host_gid_map));
4024 lxc_list_add_tail(idmap, tmplist);
4025
4026 if (container_root_gid) {
4027 tmplist = malloc(sizeof(*tmplist));
4028 if (!tmplist)
4029 return NULL;
4030 /* idmap will now keep track of that memory. */
4031 lxc_list_add_elem(tmplist, move_ptr(container_root_gid));
4032 lxc_list_add_tail(idmap, tmplist);
4033 }
4034
4035 TRACE("Allocated minimal idmapping for ns uid %d and ns gid %d", nsuid, nsgid);
4036
4037 if (resuid)
4038 *resuid = nsuid;
4039 if (resgid)
4040 *resgid = nsgid;
4041 return move_ptr(idmap);
4042}
4043
4044/*
4045 * Run a function in a new user namespace.
4046 * The caller's euid/egid will be mapped if it is not already.
4047 * Afaict, userns_exec_1() is only used to operate based on privileges for the
4048 * user's own {g,u}id on the host and for the container root's unmapped {g,u}id.
4049 * This means we require only to establish a mapping from:
4050 * - the container root {g,u}id as seen from the host > user's host {g,u}id
4051 * - the container root -> some sub{g,u}id
4052 * The former we add, if the user did not specify a mapping. The latter we
4053 * retrieve from the container's configured {g,u}id mappings as it must have been
4054 * there to start the container in the first place.
4055 */
4056int userns_exec_1(const struct lxc_conf *conf, int (*fn)(void *), void *data,
4057 const char *fn_name)
4058{
4059 call_cleaner(lxc_free_idmap) struct lxc_list *idmap = NULL;
4060 int ret = -1, status = -1;
4061 char c = '1';
4062 struct userns_fn_data d = {
4063 .arg = data,
4064 .fn = fn,
4065 .fn_name = fn_name,
4066 };
4067 pid_t pid;
4068 int pipe_fds[2];
4069
4070 if (!conf)
4071 return -EINVAL;
4072
4073 idmap = get_minimal_idmap(conf, NULL, NULL);
4074 if (!idmap)
4075 return ret_errno(ENOENT);
4076
4077 ret = pipe2(pipe_fds, O_CLOEXEC);
4078 if (ret < 0)
4079 return -errno;
4080
4081 d.p[0] = pipe_fds[0];
4082 d.p[1] = pipe_fds[1];
4083
4084 /* Clone child in new user namespace. */
4085 pid = lxc_raw_clone_cb(run_userns_fn, &d, CLONE_NEWUSER, NULL);
4086 if (pid < 0) {
4087 ERROR("Failed to clone process in new user namespace");
4088 goto on_error;
4089 }
4090
4091 close_prot_errno_disarm(pipe_fds[0]);
4092
4093 if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE ||
4094 conf->loglevel == LXC_LOG_LEVEL_TRACE) {
4095 struct id_map *map;
4096 struct lxc_list *it;
4097
4098 lxc_list_for_each(it, idmap) {
4099 map = it->elem;
4100 TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
4101 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, map->nsid, map->hostid, map->range);
4102 }
4103 }
4104
4105 /* Set up {g,u}id mapping for user namespace of child process. */
4106 ret = lxc_map_ids(idmap, pid);
4107 if (ret < 0) {
4108 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid);
4109 goto on_error;
4110 }
4111
4112 /* Tell child to proceed. */
4113 if (lxc_write_nointr(pipe_fds[1], &c, 1) != 1) {
4114 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
4115 goto on_error;
4116 }
4117
4118on_error:
4119 close_prot_errno_disarm(pipe_fds[0]);
4120 close_prot_errno_disarm(pipe_fds[1]);
4121
4122 /* Wait for child to finish. */
4123 if (pid > 0)
4124 status = wait_for_pid(pid);
4125
4126 if (status < 0)
4127 ret = -1;
4128
4129 return ret;
4130}
4131
4132int userns_exec_minimal(const struct lxc_conf *conf,
4133 int (*fn_parent)(void *), void *fn_parent_data,
4134 int (*fn_child)(void *), void *fn_child_data)
4135{
4136 call_cleaner(lxc_free_idmap) struct lxc_list *idmap = NULL;
4137 uid_t resuid = LXC_INVALID_UID;
4138 gid_t resgid = LXC_INVALID_GID;
4139 char c = '1';
4140 ssize_t ret;
4141 pid_t pid;
4142 int sock_fds[2];
4143
4144 if (!conf || !fn_child)
4145 return ret_errno(EINVAL);
4146
4147 idmap = get_minimal_idmap(conf, &resuid, &resgid);
4148 if (!idmap)
4149 return ret_errno(ENOENT);
4150
4151 ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sock_fds);
4152 if (ret < 0)
4153 return -errno;
4154
4155 pid = fork();
4156 if (pid < 0) {
4157 SYSERROR("Failed to create new process");
4158 goto on_error;
4159 }
4160
4161 if (pid == 0) {
4162 close_prot_errno_disarm(sock_fds[1]);
4163
4164 ret = unshare(CLONE_NEWUSER);
4165 if (ret < 0) {
4166 SYSERROR("Failed to unshare new user namespace");
4167 _exit(EXIT_FAILURE);
4168 }
4169
4170 ret = lxc_write_nointr(sock_fds[0], &c, 1);
4171 if (ret != 1)
4172 _exit(EXIT_FAILURE);
4173
4174 ret = lxc_read_nointr(sock_fds[0], &c, 1);
4175 if (ret != 1)
4176 _exit(EXIT_FAILURE);
4177
4178 close_prot_errno_disarm(sock_fds[0]);
4179
4180 if (!lxc_setgroups(0, NULL) && errno != EPERM)
4181 _exit(EXIT_FAILURE);
4182
4183 ret = setresgid(resgid, resgid, resgid);
4184 if (ret < 0) {
4185 SYSERROR("Failed to setresgid(%d, %d, %d)",
4186 resgid, resgid, resgid);
4187 _exit(EXIT_FAILURE);
4188 }
4189
4190 ret = setresuid(resuid, resuid, resuid);
4191 if (ret < 0) {
4192 SYSERROR("Failed to setresuid(%d, %d, %d)",
4193 resuid, resuid, resuid);
4194 _exit(EXIT_FAILURE);
4195 }
4196
4197 ret = fn_child(fn_child_data);
4198 if (ret) {
4199 SYSERROR("Running function in new user namespace failed");
4200 _exit(EXIT_FAILURE);
4201 }
4202
4203 _exit(EXIT_SUCCESS);
4204 }
4205
4206 close_prot_errno_disarm(sock_fds[0]);
4207
4208 if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE ||
4209 conf->loglevel == LXC_LOG_LEVEL_TRACE) {
4210 struct id_map *map;
4211 struct lxc_list *it;
4212
4213 lxc_list_for_each(it, idmap) {
4214 map = it->elem;
4215 TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
4216 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, map->nsid, map->hostid, map->range);
4217 }
4218 }
4219
4220 ret = lxc_read_nointr(sock_fds[1], &c, 1);
4221 if (ret != 1) {
4222 SYSERROR("Failed waiting for child process %d\" to tell us to proceed", pid);
4223 goto on_error;
4224 }
4225
4226 /* Set up {g,u}id mapping for user namespace of child process. */
4227 ret = lxc_map_ids(idmap, pid);
4228 if (ret < 0) {
4229 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid);
4230 goto on_error;
4231 }
4232
4233 /* Tell child to proceed. */
4234 ret = lxc_write_nointr(sock_fds[1], &c, 1);
4235 if (ret != 1) {
4236 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
4237 goto on_error;
4238 }
4239
4240 if (fn_parent && fn_parent(fn_parent_data)) {
4241 SYSERROR("Running parent function failed");
4242 _exit(EXIT_FAILURE);
4243 }
4244
4245on_error:
4246 close_prot_errno_disarm(sock_fds[0]);
4247 close_prot_errno_disarm(sock_fds[1]);
4248
4249 /* Wait for child to finish. */
4250 if (pid < 0)
4251 return -1;
4252
4253 return wait_for_pid(pid);
4254}
4255
4256int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data,
4257 const char *fn_name)
4258{
4259 pid_t pid;
4260 uid_t euid, egid;
4261 int p[2];
4262 struct id_map *map;
4263 struct lxc_list *cur;
4264 struct userns_fn_data d;
4265 int ret = -1;
4266 char c = '1';
4267 struct lxc_list *idmap = NULL, *tmplist = NULL;
4268 struct id_map *container_root_uid = NULL, *container_root_gid = NULL,
4269 *host_uid_map = NULL, *host_gid_map = NULL;
4270
4271 if (!conf)
4272 return -EINVAL;
4273
4274 ret = pipe2(p, O_CLOEXEC);
4275 if (ret < 0) {
4276 SYSERROR("opening pipe");
4277 return -1;
4278 }
4279 d.fn = fn;
4280 d.fn_name = fn_name;
4281 d.arg = data;
4282 d.p[0] = p[0];
4283 d.p[1] = p[1];
4284
4285 /* Clone child in new user namespace. */
4286 pid = lxc_clone(run_userns_fn, &d, CLONE_NEWUSER, NULL);
4287 if (pid < 0) {
4288 ERROR("Failed to clone process in new user namespace");
4289 goto on_error;
4290 }
4291
4292 close(p[0]);
4293 p[0] = -1;
4294
4295 euid = geteuid();
4296 egid = getegid();
4297
4298 /* Allocate new {g,u}id map list. */
4299 idmap = malloc(sizeof(*idmap));
4300 if (!idmap)
4301 goto on_error;
4302 lxc_list_init(idmap);
4303
4304 /* Find container root. */
4305 lxc_list_for_each (cur, &conf->id_map) {
4306 struct id_map *tmpmap;
4307
4308 tmplist = malloc(sizeof(*tmplist));
4309 if (!tmplist)
4310 goto on_error;
4311
4312 tmpmap = malloc(sizeof(*tmpmap));
4313 if (!tmpmap) {
4314 free(tmplist);
4315 goto on_error;
4316 }
4317
4318 memset(tmpmap, 0, sizeof(*tmpmap));
4319 memcpy(tmpmap, cur->elem, sizeof(*tmpmap));
4320 tmplist->elem = tmpmap;
4321
4322 lxc_list_add_tail(idmap, tmplist);
4323
4324 map = cur->elem;
4325
4326 if (map->idtype == ID_TYPE_UID)
4327 if (euid >= map->hostid && euid < map->hostid + map->range)
4328 host_uid_map = map;
4329
4330 if (map->idtype == ID_TYPE_GID)
4331 if (egid >= map->hostid && egid < map->hostid + map->range)
4332 host_gid_map = map;
4333
4334 if (map->nsid != 0)
4335 continue;
4336
4337 if (map->idtype == ID_TYPE_UID)
4338 if (container_root_uid == NULL)
4339 container_root_uid = map;
4340
4341 if (map->idtype == ID_TYPE_GID)
4342 if (container_root_gid == NULL)
4343 container_root_gid = map;
4344 }
4345
4346 if (!container_root_uid || !container_root_gid) {
4347 ERROR("No mapping for container root found");
4348 goto on_error;
4349 }
4350
4351 /* Check whether the {g,u}id of the user has a mapping. */
4352 if (!host_uid_map)
4353 host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID);
4354 else
4355 host_uid_map = container_root_uid;
4356
4357 if (!host_gid_map)
4358 host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID);
4359 else
4360 host_gid_map = container_root_gid;
4361
4362 if (!host_uid_map) {
4363 DEBUG("Failed to find mapping for uid %d", euid);
4364 goto on_error;
4365 }
4366
4367 if (!host_gid_map) {
4368 DEBUG("Failed to find mapping for gid %d", egid);
4369 goto on_error;
4370 }
4371
4372 if (host_uid_map && (host_uid_map != container_root_uid)) {
4373 /* Add container root to the map. */
4374 tmplist = malloc(sizeof(*tmplist));
4375 if (!tmplist)
4376 goto on_error;
4377 lxc_list_add_elem(tmplist, host_uid_map);
4378 lxc_list_add_tail(idmap, tmplist);
4379 }
4380 /* idmap will now keep track of that memory. */
4381 host_uid_map = NULL;
4382
4383 if (host_gid_map && (host_gid_map != container_root_gid)) {
4384 tmplist = malloc(sizeof(*tmplist));
4385 if (!tmplist)
4386 goto on_error;
4387 lxc_list_add_elem(tmplist, host_gid_map);
4388 lxc_list_add_tail(idmap, tmplist);
4389 }
4390 /* idmap will now keep track of that memory. */
4391 host_gid_map = NULL;
4392
4393 if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE ||
4394 conf->loglevel == LXC_LOG_LEVEL_TRACE) {
4395 lxc_list_for_each (cur, idmap) {
4396 map = cur->elem;
4397 TRACE("establishing %cid mapping for \"%d\" in new "
4398 "user namespace: nsuid %lu - hostid %lu - range "
4399 "%lu",
4400 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid,
4401 map->nsid, map->hostid, map->range);
4402 }
4403 }
4404
4405 /* Set up {g,u}id mapping for user namespace of child process. */
4406 ret = lxc_map_ids(idmap, pid);
4407 if (ret < 0) {
4408 ERROR("error setting up {g,u}id mappings for child process \"%d\"", pid);
4409 goto on_error;
4410 }
4411
4412 /* Tell child to proceed. */
4413 if (lxc_write_nointr(p[1], &c, 1) != 1) {
4414 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
4415 goto on_error;
4416 }
4417
4418on_error:
4419 if (p[0] != -1)
4420 close(p[0]);
4421 close(p[1]);
4422
4423 /* Wait for child to finish. */
4424 if (pid > 0)
4425 ret = wait_for_pid(pid);
4426
4427 if (idmap) {
4428 lxc_free_idmap(idmap);
4429 free(idmap);
4430 }
4431
4432 if (host_uid_map && (host_uid_map != container_root_uid))
4433 free(host_uid_map);
4434 if (host_gid_map && (host_gid_map != container_root_gid))
4435 free(host_gid_map);
4436
4437 return ret;
4438}
4439
4440static int add_idmap_entry(struct lxc_list *idmap, enum idtype idtype,
4441 unsigned long nsid, unsigned long hostid,
4442 unsigned long range)
4443{
4444 __do_free struct id_map *new_idmap = NULL;
4445 __do_free struct lxc_list *new_list = NULL;
4446
4447 new_idmap = zalloc(sizeof(*new_idmap));
4448 if (!new_idmap)
4449 return ret_errno(ENOMEM);
4450
4451 new_idmap->idtype = idtype;
4452 new_idmap->hostid = hostid;
4453 new_idmap->nsid = nsid;
4454 new_idmap->range = range;
4455
4456 new_list = zalloc(sizeof(*new_list));
4457 if (!new_list)
4458 return ret_errno(ENOMEM);
4459
4460 new_list->elem = move_ptr(new_idmap);
4461 lxc_list_add_tail(idmap, move_ptr(new_list));
4462
4463 INFO("Adding id map: type %c nsid %lu hostid %lu range %lu",
4464 idtype == ID_TYPE_UID ? 'u' : 'g', nsid, hostid, range);
4465 return 0;
4466}
4467
4468int userns_exec_mapped_root(const char *path, int path_fd,
4469 const struct lxc_conf *conf)
4470{
4471 call_cleaner(lxc_free_idmap) struct lxc_list *idmap = NULL;
4472 __do_close int fd = -EBADF;
4473 int target_fd = -EBADF;
4474 char c = '1';
4475 ssize_t ret;
4476 pid_t pid;
4477 int sock_fds[2];
4478 uid_t container_host_uid, hostuid;
4479 gid_t container_host_gid, hostgid;
4480 struct stat st;
4481
4482 if (!conf || (!path && path_fd < 0))
4483 return ret_errno(EINVAL);
4484
4485 if (!path)
4486 path = "(null)";
4487
4488 container_host_uid = get_mapped_rootid(conf, ID_TYPE_UID);
4489 if (!uid_valid(container_host_uid))
4490 return log_error(-1, "No uid mapping for container root");
4491
4492 container_host_gid = get_mapped_rootid(conf, ID_TYPE_GID);
4493 if (!gid_valid(container_host_gid))
4494 return log_error(-1, "No gid mapping for container root");
4495
4496 if (path_fd < 0) {
4497 fd = open(path, O_RDWR | O_CLOEXEC | O_NOCTTY | O_PATH);
4498 if (fd < 0)
4499 return log_error_errno(-errno, errno, "Failed to open \"%s\"", path);
4500 target_fd = fd;
4501 } else {
4502 target_fd = path_fd;
4503 }
4504
4505 hostuid = geteuid();
4506 /* We are root so chown directly. */
4507 if (hostuid == 0) {
4508 ret = fchown(target_fd, container_host_uid, container_host_gid);
4509 if (ret)
4510 return log_error_errno(-errno, errno,
4511 "Failed to fchown(%d(%s), %d, %d)",
4512 target_fd, path, container_host_uid,
4513 container_host_gid);
4514 return log_trace(0, "Chowned %d(%s) to uid %d and %d", target_fd, path,
4515 container_host_uid, container_host_gid);
4516 }
4517
4518 /* The container's root host id matches */
4519 if (container_host_uid == hostuid)
4520 return log_info(0, "Container root id is mapped to our uid");
4521
4522 /* Get the current ids of our target. */
4523 ret = fstat(target_fd, &st);
4524 if (ret)
4525 return log_error_errno(-errno, errno, "Failed to stat \"%s\"", path);
4526
4527 hostgid = getegid();
4528 if (st.st_uid == hostuid && mapped_hostid(st.st_gid, conf, ID_TYPE_GID) < 0) {
4529 ret = fchown(target_fd, -1, hostgid);
4530 if (ret)
4531 return log_error_errno(-errno, errno,
4532 "Failed to fchown(%d(%s), -1, %d)",
4533 target_fd, path, hostgid);
4534 }
4535
4536 idmap = malloc(sizeof(*idmap));
4537 if (!idmap)
4538 return -ENOMEM;
4539 lxc_list_init(idmap);
4540
4541 /* "u:0:rootuid:1" */
4542 ret = add_idmap_entry(idmap, ID_TYPE_UID, 0, container_host_uid, 1);
4543 if (ret < 0)
4544 return log_error_errno(ret, -ret, "Failed to add idmap entry");
4545
4546 /* "u:hostuid:hostuid:1" */
4547 ret = add_idmap_entry(idmap, ID_TYPE_UID, hostuid, hostuid, 1);
4548 if (ret < 0)
4549 return log_error_errno(ret, -ret, "Failed to add idmap entry");
4550
4551 /* "g:0:rootgid:1" */
4552 ret = add_idmap_entry(idmap, ID_TYPE_GID, 0, container_host_gid, 1);
4553 if (ret < 0)
4554 return log_error_errno(ret, -ret, "Failed to add idmap entry");
4555
4556 /* "g:hostgid:hostgid:1" */
4557 ret = add_idmap_entry(idmap, ID_TYPE_GID, hostgid, hostgid, 1);
4558 if (ret < 0)
4559 return log_error_errno(ret, -ret, "Failed to add idmap entry");
4560
4561 if (hostgid != st.st_gid) {
4562 /* "g:pathgid:rootgid+pathgid:1" */
4563 ret = add_idmap_entry(idmap, ID_TYPE_GID, st.st_gid,
4564 container_host_gid + (gid_t)st.st_gid, 1);
4565 if (ret < 0)
4566 return log_error_errno(ret, -ret, "Failed to add idmap entry");
4567 }
4568
4569 ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sock_fds);
4570 if (ret < 0)
4571 return -errno;
4572
4573 pid = fork();
4574 if (pid < 0) {
4575 SYSERROR("Failed to create new process");
4576 goto on_error;
4577 }
4578
4579 if (pid == 0) {
4580 close_prot_errno_disarm(sock_fds[1]);
4581
4582 ret = unshare(CLONE_NEWUSER);
4583 if (ret < 0) {
4584 SYSERROR("Failed to unshare new user namespace");
4585 _exit(EXIT_FAILURE);
4586 }
4587
4588 ret = lxc_write_nointr(sock_fds[0], &c, 1);
4589 if (ret != 1)
4590 _exit(EXIT_FAILURE);
4591
4592 ret = lxc_read_nointr(sock_fds[0], &c, 1);
4593 if (ret != 1)
4594 _exit(EXIT_FAILURE);
4595
4596 close_prot_errno_disarm(sock_fds[0]);
4597
4598 if (!lxc_switch_uid_gid(0, 0))
4599 _exit(EXIT_FAILURE);
4600
4601 if (!lxc_setgroups(0, NULL))
4602 _exit(EXIT_FAILURE);
4603
4604 ret = chown(path, 0, st.st_gid);
4605 if (ret) {
4606 SYSERROR("Failed to chown \"%s\"", path);
4607 _exit(EXIT_FAILURE);
4608 }
4609
4610 _exit(EXIT_SUCCESS);
4611 }
4612
4613 close_prot_errno_disarm(sock_fds[0]);
4614
4615 if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE ||
4616 conf->loglevel == LXC_LOG_LEVEL_TRACE) {
4617 struct id_map *map;
4618 struct lxc_list *it;
4619
4620 lxc_list_for_each(it, idmap) {
4621 map = it->elem;
4622 TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
4623 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, map->nsid, map->hostid, map->range);
4624 }
4625 }
4626
4627 ret = lxc_read_nointr(sock_fds[1], &c, 1);
4628 if (ret != 1) {
4629 SYSERROR("Failed waiting for child process %d\" to tell us to proceed", pid);
4630 goto on_error;
4631 }
4632
4633 /* Set up {g,u}id mapping for user namespace of child process. */
4634 ret = lxc_map_ids(idmap, pid);
4635 if (ret < 0) {
4636 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid);
4637 goto on_error;
4638 }
4639
4640 /* Tell child to proceed. */
4641 ret = lxc_write_nointr(sock_fds[1], &c, 1);
4642 if (ret != 1) {
4643 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
4644 goto on_error;
4645 }
4646
4647on_error:
4648 close_prot_errno_disarm(sock_fds[0]);
4649 close_prot_errno_disarm(sock_fds[1]);
4650
4651 /* Wait for child to finish. */
4652 if (pid < 0)
4653 return -1;
4654
4655 return wait_for_pid(pid);
4656}
4657
4658/* not thread-safe, do not use from api without first forking */
4659static char *getuname(void)
4660{
4661 __do_free char *buf = NULL;
4662 struct passwd pwent;
4663 struct passwd *pwentp = NULL;
4664 size_t bufsize;
4665 int ret;
4666
4667 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
4668 if (bufsize == -1)
4669 bufsize = 1024;
4670
4671 buf = malloc(bufsize);
4672 if (!buf)
4673 return NULL;
4674
4675 ret = getpwuid_r(geteuid(), &pwent, buf, bufsize, &pwentp);
4676 if (!pwentp) {
4677 if (ret == 0)
4678 WARN("Could not find matched password record.");
4679
4680 return log_error(NULL, "Failed to get password record - %u", geteuid());
4681 }
4682
4683 return strdup(pwent.pw_name);
4684}
4685
4686/* not thread-safe, do not use from api without first forking */
4687static char *getgname(void)
4688{
4689 __do_free char *buf = NULL;
4690 struct group grent;
4691 struct group *grentp = NULL;
4692 size_t bufsize;
4693 int ret;
4694
4695 bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
4696 if (bufsize == -1)
4697 bufsize = 1024;
4698
4699 buf = malloc(bufsize);
4700 if (!buf)
4701 return NULL;
4702
4703 ret = getgrgid_r(getegid(), &grent, buf, bufsize, &grentp);
4704 if (!grentp) {
4705 if (ret == 0)
4706 WARN("Could not find matched group record");
4707
4708 return log_error(NULL, "Failed to get group record - %u", getegid());
4709 }
4710
4711 return strdup(grent.gr_name);
4712}
4713
4714/* not thread-safe, do not use from api without first forking */
4715void suggest_default_idmap(void)
4716{
4717 __do_free char *gname = NULL, *line = NULL, *uname = NULL;
4718 __do_fclose FILE *subuid_f = NULL, *subgid_f = NULL;
4719 unsigned int uid = 0, urange = 0, gid = 0, grange = 0;
4720 size_t len = 0;
4721
4722 uname = getuname();
4723 if (!uname)
4724 return;
4725
4726 gname = getgname();
4727 if (!gname)
4728 return;
4729
4730 subuid_f = fopen(subuidfile, "re");
4731 if (!subuid_f) {
4732 ERROR("Your system is not configured with subuids");
4733 return;
4734 }
4735
4736 while (getline(&line, &len, subuid_f) != -1) {
4737 char *p, *p2;
4738 size_t no_newline = 0;
4739
4740 p = strchr(line, ':');
4741 if (*line == '#')
4742 continue;
4743 if (!p)
4744 continue;
4745 *p = '\0';
4746 p++;
4747
4748 if (strcmp(line, uname))
4749 continue;
4750
4751 p2 = strchr(p, ':');
4752 if (!p2)
4753 continue;
4754 *p2 = '\0';
4755 p2++;
4756 if (!*p2)
4757 continue;
4758 no_newline = strcspn(p2, "\n");
4759 p2[no_newline] = '\0';
4760
4761 if (lxc_safe_uint(p, &uid) < 0)
4762 WARN("Could not parse UID");
4763 if (lxc_safe_uint(p2, &urange) < 0)
4764 WARN("Could not parse UID range");
4765 }
4766
4767 subgid_f = fopen(subgidfile, "re");
4768 if (!subgid_f) {
4769 ERROR("Your system is not configured with subgids");
4770 return;
4771 }
4772
4773 while (getline(&line, &len, subgid_f) != -1) {
4774 char *p, *p2;
4775 size_t no_newline = 0;
4776
4777 p = strchr(line, ':');
4778 if (*line == '#')
4779 continue;
4780 if (!p)
4781 continue;
4782 *p = '\0';
4783 p++;
4784
4785 if (strcmp(line, uname))
4786 continue;
4787
4788 p2 = strchr(p, ':');
4789 if (!p2)
4790 continue;
4791 *p2 = '\0';
4792 p2++;
4793 if (!*p2)
4794 continue;
4795 no_newline = strcspn(p2, "\n");
4796 p2[no_newline] = '\0';
4797
4798 if (lxc_safe_uint(p, &gid) < 0)
4799 WARN("Could not parse GID");
4800 if (lxc_safe_uint(p2, &grange) < 0)
4801 WARN("Could not parse GID range");
4802 }
4803
4804 if (!urange || !grange) {
4805 ERROR("You do not have subuids or subgids allocated");
4806 ERROR("Unprivileged containers require subuids and subgids");
4807 return;
4808 }
4809
4810 ERROR("You must either run as root, or define uid mappings");
4811 ERROR("To pass uid mappings to lxc-create, you could create");
4812 ERROR("~/.config/lxc/default.conf:");
4813 ERROR("lxc.include = %s", LXC_DEFAULT_CONFIG);
4814 ERROR("lxc.idmap = u 0 %u %u", uid, urange);
4815 ERROR("lxc.idmap = g 0 %u %u", gid, grange);
4816}
4817
4818static void free_cgroup_settings(struct lxc_list *result)
4819{
4820 struct lxc_list *iterator, *next;
4821
4822 lxc_list_for_each_safe (iterator, result, next) {
4823 lxc_list_del(iterator);
4824 free_disarm(iterator);
4825 }
4826 free_disarm(result);
4827}
4828
4829/* Return the list of cgroup_settings sorted according to the following rules
4830 * 1. Put memory.limit_in_bytes before memory.memsw.limit_in_bytes
4831 */
4832struct lxc_list *sort_cgroup_settings(struct lxc_list *cgroup_settings)
4833{
4834 struct lxc_list *result;
4835 struct lxc_cgroup *cg = NULL;
4836 struct lxc_list *it = NULL, *item = NULL, *memsw_limit = NULL;
4837
4838 result = malloc(sizeof(*result));
4839 if (!result)
4840 return NULL;
4841 lxc_list_init(result);
4842
4843 /* Iterate over the cgroup settings and copy them to the output list. */
4844 lxc_list_for_each (it, cgroup_settings) {
4845 item = malloc(sizeof(*item));
4846 if (!item) {
4847 free_cgroup_settings(result);
4848 return NULL;
4849 }
4850
4851 item->elem = it->elem;
4852 cg = it->elem;
4853 if (strcmp(cg->subsystem, "memory.memsw.limit_in_bytes") == 0) {
4854 /* Store the memsw_limit location */
4855 memsw_limit = item;
4856 } else if (strcmp(cg->subsystem, "memory.limit_in_bytes") == 0 &&
4857 memsw_limit != NULL) {
4858 /* lxc.cgroup.memory.memsw.limit_in_bytes is found
4859 * before lxc.cgroup.memory.limit_in_bytes, swap these
4860 * two items */
4861 item->elem = memsw_limit->elem;
4862 memsw_limit->elem = it->elem;
4863 }
4864 lxc_list_add_tail(result, item);
4865 }
4866
4867 return result;
4868}