]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/conf.c
Merge pull request #2270 from brauner/2018-04-11/attach_try_dropping_supplementary_groups
[mirror_lxc.git] / src / lxc / conf.c
CommitLineData
0ad19a3f 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
0ad19a3f 8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ad19a3f 22 */
1d52bdf7
CB
23
24#define _GNU_SOURCE
d06245b8
NC
25#include "config.h"
26
9d257a2a 27#include <arpa/inet.h>
8f3e280e
CB
28#include <dirent.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <grp.h>
32#include <inttypes.h>
33#include <libgen.h>
9d257a2a
CB
34#include <linux/loop.h>
35#include <net/if.h>
36#include <netinet/in.h>
8f3e280e
CB
37#include <pwd.h>
38#include <stdarg.h>
0ad19a3f 39#include <stdio.h>
0ad19a3f 40#include <stdlib.h>
0ad19a3f 41#include <string.h>
8f3e280e
CB
42#include <sys/mman.h>
43#include <sys/mount.h>
44#include <sys/param.h>
45#include <sys/prctl.h>
8f3e280e 46#include <sys/socket.h>
9d257a2a 47#include <sys/stat.h>
2d76d1d7 48#include <sys/syscall.h>
9d257a2a 49#include <sys/sysmacros.h>
97e9cfa0 50#include <sys/types.h>
8f3e280e
CB
51#include <sys/utsname.h>
52#include <sys/wait.h>
9d257a2a
CB
53#include <time.h>
54#include <unistd.h>
1d52bdf7 55
af6824fc 56#ifdef MAJOR_IN_MKDEV
9d257a2a 57#include <sys/mkdev.h>
af6824fc 58#endif
af6824fc 59
614305f3 60#ifdef HAVE_STATVFS
2938f7c8 61#include <sys/statvfs.h>
614305f3 62#endif
e827ff7e
SG
63
64#if HAVE_PTY_H
b0a33c1e 65#include <pty.h>
e827ff7e
SG
66#else
67#include <../include/openpty.h>
68#endif
0ad19a3f 69
9d257a2a
CB
70#if HAVE_LIBCAP
71#include <sys/capability.h>
72#endif
73
74#if HAVE_SYS_PERSONALITY_H
75#include <sys/personality.h>
76#endif
77
78#if IS_BIONIC
79#include <../include/lxcmntent.h>
80#else
81#include <mntent.h>
82#endif
83
84#if !defined(HAVE_PRLIMIT) && defined(HAVE_PRLIMIT64)
85#include <../include/prlimit.h>
86#endif
87
e8bd4e43 88#include "af_unix.h"
9d257a2a 89#include "caps.h"
8f3e280e 90#include "cgroup.h"
1b09f2c0 91#include "conf.h"
1ed6ba91 92#include "confile_utils.h"
8f3e280e 93#include "error.h"
1b09f2c0 94#include "log.h"
0ed9b1bc 95#include "lsm/lsm.h"
025ed0f3 96#include "lxclock.h"
8f3e280e 97#include "lxcseccomp.h"
4355ab5f 98#include "namespace.h"
8f3e280e
CB
99#include "network.h"
100#include "parse.h"
732375f5 101#include "ringbuf.h"
28d832c4 102#include "storage.h"
28d832c4 103#include "storage/overlay.h"
0ed9b1bc 104#include "terminal.h"
8f3e280e 105#include "utils.h"
d0a36f2c 106
9d257a2a
CB
107#ifndef MS_PRIVATE
108#define MS_PRIVATE (1<<18)
edaf8b1b
SG
109#endif
110
9d257a2a
CB
111#ifndef MS_LAZYTIME
112#define MS_LAZYTIME (1<<25)
f48b5fd8
FF
113#endif
114
36eb9bde 115lxc_log_define(lxc_conf, lxc);
e5bda9ee 116
0fd73091
CB
117/* The lxc_conf of the container currently being worked on in an API call.
118 * This is used in the error calls.
119 */
120#ifdef HAVE_TLS
121__thread struct lxc_conf *current_config;
122#else
123struct lxc_conf *current_config;
124#endif
125
2d76d1d7
SG
126/* Define pivot_root() if missing from the C library */
127#ifndef HAVE_PIVOT_ROOT
9d257a2a 128static int pivot_root(const char *new_root, const char *put_old)
2d76d1d7
SG
129{
130#ifdef __NR_pivot_root
8f3e280e 131 return syscall(__NR_pivot_root, new_root, put_old);
2d76d1d7 132#else
8f3e280e
CB
133 errno = ENOSYS;
134 return -1;
2d76d1d7
SG
135#endif
136}
137#else
9d257a2a 138extern int pivot_root(const char *new_root, const char *put_old);
8912711c
CB
139#endif
140
0fd73091
CB
141char *lxchook_names[NUM_LXC_HOOKS] = {
142 "pre-start",
143 "pre-mount",
144 "mount",
145 "autodev",
146 "start",
147 "stop",
148 "post-stop",
149 "clone",
150 "destroy",
151 "start-host"
152};
72d0e1cb 153
998ac676
RT
154struct mount_opt {
155 char *name;
156 int clear;
157 int flag;
158};
159
81810dd1
DL
160struct caps_opt {
161 char *name;
162 int value;
163};
164
c6d09e15
WB
165struct limit_opt {
166 char *name;
167 int value;
168};
169
998ac676 170static struct mount_opt mount_opt[] = {
470b359b
CB
171 { "async", 1, MS_SYNCHRONOUS },
172 { "atime", 1, MS_NOATIME },
173 { "bind", 0, MS_BIND },
88d413d5 174 { "defaults", 0, 0 },
88d413d5 175 { "dev", 1, MS_NODEV },
470b359b 176 { "diratime", 1, MS_NODIRATIME },
88d413d5 177 { "dirsync", 0, MS_DIRSYNC },
470b359b 178 { "exec", 1, MS_NOEXEC },
8912711c 179 { "lazytime", 0, MS_LAZYTIME },
88d413d5 180 { "mand", 0, MS_MANDLOCK },
88d413d5 181 { "noatime", 0, MS_NOATIME },
470b359b 182 { "nodev", 0, MS_NODEV },
88d413d5 183 { "nodiratime", 0, MS_NODIRATIME },
470b359b
CB
184 { "noexec", 0, MS_NOEXEC },
185 { "nomand", 1, MS_MANDLOCK },
186 { "norelatime", 1, MS_RELATIME },
187 { "nostrictatime", 1, MS_STRICTATIME },
188 { "nosuid", 0, MS_NOSUID },
88d413d5
SW
189 { "rbind", 0, MS_BIND|MS_REC },
190 { "relatime", 0, MS_RELATIME },
470b359b
CB
191 { "remount", 0, MS_REMOUNT },
192 { "ro", 0, MS_RDONLY },
193 { "rw", 1, MS_RDONLY },
88d413d5 194 { "strictatime", 0, MS_STRICTATIME },
470b359b
CB
195 { "suid", 1, MS_NOSUID },
196 { "sync", 0, MS_SYNCHRONOUS },
88d413d5 197 { NULL, 0, 0 },
998ac676
RT
198};
199
d840039e 200static struct mount_opt propagation_opt[] = {
0fd73091
CB
201 { "private", 0, MS_PRIVATE },
202 { "shared", 0, MS_SHARED },
203 { "slave", 0, MS_SLAVE },
204 { "unbindable", 0, MS_UNBINDABLE },
205 { "rprivate", 0, MS_PRIVATE|MS_REC },
206 { "rshared", 0, MS_SHARED|MS_REC },
207 { "rslave", 0, MS_SLAVE|MS_REC },
208 { "runbindable", 0, MS_UNBINDABLE|MS_REC },
209 { NULL, 0, 0 },
d840039e
YT
210};
211
81810dd1 212static struct caps_opt caps_opt[] = {
8560cd36 213#if HAVE_LIBCAP
0fd73091
CB
214 { "chown", CAP_CHOWN },
215 { "dac_override", CAP_DAC_OVERRIDE },
216 { "dac_read_search", CAP_DAC_READ_SEARCH },
217 { "fowner", CAP_FOWNER },
218 { "fsetid", CAP_FSETID },
219 { "kill", CAP_KILL },
220 { "setgid", CAP_SETGID },
221 { "setuid", CAP_SETUID },
222 { "setpcap", CAP_SETPCAP },
223 { "linux_immutable", CAP_LINUX_IMMUTABLE },
224 { "net_bind_service", CAP_NET_BIND_SERVICE },
225 { "net_broadcast", CAP_NET_BROADCAST },
226 { "net_admin", CAP_NET_ADMIN },
227 { "net_raw", CAP_NET_RAW },
228 { "ipc_lock", CAP_IPC_LOCK },
229 { "ipc_owner", CAP_IPC_OWNER },
230 { "sys_module", CAP_SYS_MODULE },
231 { "sys_rawio", CAP_SYS_RAWIO },
232 { "sys_chroot", CAP_SYS_CHROOT },
233 { "sys_ptrace", CAP_SYS_PTRACE },
234 { "sys_pacct", CAP_SYS_PACCT },
235 { "sys_admin", CAP_SYS_ADMIN },
236 { "sys_boot", CAP_SYS_BOOT },
237 { "sys_nice", CAP_SYS_NICE },
238 { "sys_resource", CAP_SYS_RESOURCE },
239 { "sys_time", CAP_SYS_TIME },
240 { "sys_tty_config", CAP_SYS_TTY_CONFIG },
241 { "mknod", CAP_MKNOD },
242 { "lease", CAP_LEASE },
57b837e2 243#ifdef CAP_AUDIT_READ
0fd73091 244 { "audit_read", CAP_AUDIT_READ },
57b837e2 245#endif
9527e566 246#ifdef CAP_AUDIT_WRITE
0fd73091 247 { "audit_write", CAP_AUDIT_WRITE },
9527e566
FW
248#endif
249#ifdef CAP_AUDIT_CONTROL
0fd73091 250 { "audit_control", CAP_AUDIT_CONTROL },
9527e566 251#endif
0fd73091
CB
252 { "setfcap", CAP_SETFCAP },
253 { "mac_override", CAP_MAC_OVERRIDE },
254 { "mac_admin", CAP_MAC_ADMIN },
5170c716 255#ifdef CAP_SYSLOG
0fd73091 256 { "syslog", CAP_SYSLOG },
5170c716
CS
257#endif
258#ifdef CAP_WAKE_ALARM
0fd73091 259 { "wake_alarm", CAP_WAKE_ALARM },
5170c716 260#endif
2b54359b 261#ifdef CAP_BLOCK_SUSPEND
0fd73091 262 { "block_suspend", CAP_BLOCK_SUSPEND },
2b54359b 263#endif
495d2046 264#endif
8560cd36 265};
81810dd1 266
c6d09e15
WB
267static struct limit_opt limit_opt[] = {
268#ifdef RLIMIT_AS
269 { "as", RLIMIT_AS },
270#endif
271#ifdef RLIMIT_CORE
272 { "core", RLIMIT_CORE },
273#endif
274#ifdef RLIMIT_CPU
275 { "cpu", RLIMIT_CPU },
276#endif
277#ifdef RLIMIT_DATA
278 { "data", RLIMIT_DATA },
279#endif
280#ifdef RLIMIT_FSIZE
281 { "fsize", RLIMIT_FSIZE },
282#endif
283#ifdef RLIMIT_LOCKS
284 { "locks", RLIMIT_LOCKS },
285#endif
286#ifdef RLIMIT_MEMLOCK
287 { "memlock", RLIMIT_MEMLOCK },
288#endif
289#ifdef RLIMIT_MSGQUEUE
290 { "msgqueue", RLIMIT_MSGQUEUE },
291#endif
292#ifdef RLIMIT_NICE
293 { "nice", RLIMIT_NICE },
294#endif
295#ifdef RLIMIT_NOFILE
296 { "nofile", RLIMIT_NOFILE },
297#endif
298#ifdef RLIMIT_NPROC
299 { "nproc", RLIMIT_NPROC },
300#endif
301#ifdef RLIMIT_RSS
302 { "rss", RLIMIT_RSS },
303#endif
304#ifdef RLIMIT_RTPRIO
305 { "rtprio", RLIMIT_RTPRIO },
306#endif
307#ifdef RLIMIT_RTTIME
308 { "rttime", RLIMIT_RTTIME },
309#endif
310#ifdef RLIMIT_SIGPENDING
311 { "sigpending", RLIMIT_SIGPENDING },
312#endif
313#ifdef RLIMIT_STACK
314 { "stack", RLIMIT_STACK },
315#endif
316};
317
91c3830e
SH
318static int run_buffer(char *buffer)
319{
8e7da691 320 int ret;
0fd73091
CB
321 char *output;
322 struct lxc_popen_FILE *f;
91c3830e 323
ebec9176 324 f = lxc_popen(buffer);
91c3830e 325 if (!f) {
3f60c2f7 326 SYSERROR("Failed to popen() %s", buffer);
91c3830e
SH
327 return -1;
328 }
329
330 output = malloc(LXC_LOG_BUFFER_SIZE);
331 if (!output) {
3f60c2f7 332 ERROR("Failed to allocate memory for %s", buffer);
ebec9176 333 lxc_pclose(f);
91c3830e
SH
334 return -1;
335 }
336
062b72c6 337 while (fgets(output, LXC_LOG_BUFFER_SIZE, f->f))
3f60c2f7 338 DEBUG("Script %s with output: %s", buffer, output);
91c3830e
SH
339
340 free(output);
341
ebec9176 342 ret = lxc_pclose(f);
8e7da691 343 if (ret == -1) {
3f60c2f7 344 SYSERROR("Script exited with error");
91c3830e 345 return -1;
8e7da691 346 } else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) {
3f60c2f7 347 ERROR("Script exited with status %d", WEXITSTATUS(ret));
8e7da691
DE
348 return -1;
349 } else if (WIFSIGNALED(ret)) {
3f60c2f7 350 ERROR("Script terminated by signal %d", WTERMSIG(ret));
8e7da691 351 return -1;
91c3830e
SH
352 }
353
354 return 0;
355}
356
14a7b0f9
CB
357int run_script_argv(const char *name, unsigned int hook_version,
358 const char *section, const char *script,
586b1ce7 359 const char *hookname, char **argv)
148e91f5 360{
3f60c2f7 361 int buf_pos, i, ret;
148e91f5 362 char *buffer;
6f8d00d2 363 int fret = -1;
d08e5708 364 size_t size = 0;
148e91f5 365
3f60c2f7
CB
366 if (hook_version == 0)
367 INFO("Executing script \"%s\" for container \"%s\", config "
368 "section \"%s\"", script, name, section);
369 else
370 INFO("Executing script \"%s\" for container \"%s\"", script, name);
148e91f5 371
586b1ce7
CB
372 for (i = 0; argv && argv[i]; i++)
373 size += strlen(argv[i]) + 1;
148e91f5 374
3f60c2f7 375 size += sizeof("exec");
148e91f5 376 size += strlen(script);
3f60c2f7
CB
377 size++;
378
148e91f5 379 if (size > INT_MAX)
3f60c2f7 380 return -EFBIG;
148e91f5 381
3f60c2f7 382 if (hook_version == 0) {
d08e5708
CB
383 size += strlen(hookname);
384 size++;
385
386 size += strlen(name);
387 size++;
388
389 size += strlen(section);
390 size++;
391
392 if (size > INT_MAX)
393 return -EFBIG;
327cce76 394 }
3f60c2f7 395
6f8d00d2
CB
396 buffer = malloc(size);
397 if (!buffer)
398 return -ENOMEM;
399
327cce76 400 if (hook_version == 0)
3f60c2f7 401 buf_pos = snprintf(buffer, size, "exec %s %s %s %s", script, name, section, hookname);
327cce76 402 else
3f60c2f7 403 buf_pos = snprintf(buffer, size, "exec %s", script);
327cce76
CB
404 if (buf_pos < 0 || (size_t)buf_pos >= size) {
405 ERROR("Failed to create command line for script \"%s\"", script);
6f8d00d2 406 goto on_error;
327cce76 407 }
3f60c2f7 408
327cce76 409 if (hook_version == 1) {
3f60c2f7
CB
410 ret = setenv("LXC_HOOK_TYPE", hookname, 1);
411 if (ret < 0) {
412 SYSERROR("Failed to set environment variable: "
413 "LXC_HOOK_TYPE=%s", hookname);
6f8d00d2 414 goto on_error;
3f60c2f7 415 }
90f20466 416 TRACE("Set environment variable: LXC_HOOK_TYPE=%s", hookname);
3f60c2f7
CB
417
418 ret = setenv("LXC_HOOK_SECTION", section, 1);
419 if (ret < 0) {
420 SYSERROR("Failed to set environment variable: "
421 "LXC_HOOK_SECTION=%s", section);
6f8d00d2 422 goto on_error;
3f60c2f7
CB
423 }
424 TRACE("Set environment variable: LXC_HOOK_SECTION=%s", section);
14a7b0f9
CB
425
426 if (strcmp(section, "net") == 0) {
427 char *parent;
428
586b1ce7 429 if (!argv || !argv[0])
6f8d00d2 430 goto on_error;
14a7b0f9 431
586b1ce7 432 ret = setenv("LXC_NET_TYPE", argv[0], 1);
14a7b0f9
CB
433 if (ret < 0) {
434 SYSERROR("Failed to set environment variable: "
586b1ce7 435 "LXC_NET_TYPE=%s", argv[0]);
6f8d00d2 436 goto on_error;
14a7b0f9 437 }
586b1ce7 438 TRACE("Set environment variable: LXC_NET_TYPE=%s", argv[0]);
14a7b0f9 439
586b1ce7 440 parent = argv[1] ? argv[1] : "";
14a7b0f9 441
586b1ce7 442 if (strcmp(argv[0], "macvlan")) {
14a7b0f9
CB
443 ret = setenv("LXC_NET_PARENT", parent, 1);
444 if (ret < 0) {
445 SYSERROR("Failed to set environment "
446 "variable: LXC_NET_PARENT=%s", parent);
6f8d00d2 447 goto on_error;
14a7b0f9
CB
448 }
449 TRACE("Set environment variable: LXC_NET_PARENT=%s", parent);
586b1ce7 450 } else if (strcmp(argv[0], "phys")) {
14a7b0f9
CB
451 ret = setenv("LXC_NET_PARENT", parent, 1);
452 if (ret < 0) {
453 SYSERROR("Failed to set environment "
454 "variable: LXC_NET_PARENT=%s", parent);
6f8d00d2 455 goto on_error;
14a7b0f9
CB
456 }
457 TRACE("Set environment variable: LXC_NET_PARENT=%s", parent);
586b1ce7
CB
458 } else if (strcmp(argv[0], "veth")) {
459 char *peer = argv[2] ? argv[2] : "";
14a7b0f9
CB
460
461 ret = setenv("LXC_NET_PEER", peer, 1);
462 if (ret < 0) {
463 SYSERROR("Failed to set environment "
464 "variable: LXC_NET_PEER=%s", peer);
6f8d00d2 465 goto on_error;
14a7b0f9
CB
466 }
467 TRACE("Set environment variable: LXC_NET_PEER=%s", peer);
468
469 ret = setenv("LXC_NET_PARENT", parent, 1);
470 if (ret < 0) {
471 SYSERROR("Failed to set environment "
472 "variable: LXC_NET_PARENT=%s", parent);
6f8d00d2 473 goto on_error;
14a7b0f9
CB
474 }
475 TRACE("Set environment variable: LXC_NET_PARENT=%s", parent);
476 }
477 }
148e91f5
SH
478 }
479
586b1ce7 480 for (i = 0; argv && argv[i]; i++) {
3f60c2f7
CB
481 size_t len = size - buf_pos;
482
586b1ce7 483 ret = snprintf(buffer + buf_pos, len, " %s", argv[i]);
3f60c2f7
CB
484 if (ret < 0 || (size_t)ret >= len) {
485 ERROR("Failed to create command line for script \"%s\"", script);
6f8d00d2 486 goto on_error;
148e91f5 487 }
3f60c2f7 488 buf_pos += ret;
148e91f5
SH
489 }
490
6f8d00d2
CB
491 fret = run_buffer(buffer);
492
493on_error:
494 free(buffer);
495 return fret;
148e91f5
SH
496}
497
811ef482 498int run_script(const char *name, const char *section, const char *script, ...)
e3b4c4c4 499{
abbfd20b 500 int ret;
91c3830e 501 char *buffer, *p;
abbfd20b 502 va_list ap;
0fd73091 503 size_t size = 0;
751d9dcd 504
0fd73091 505 INFO("Executing script \"%s\" for container \"%s\", config section \"%s\"",
751d9dcd 506 script, name, section);
e3b4c4c4 507
abbfd20b
DL
508 va_start(ap, script);
509 while ((p = va_arg(ap, char *)))
95642a10 510 size += strlen(p) + 1;
abbfd20b
DL
511 va_end(ap);
512
6d1a5f93 513 size += strlen("exec");
abbfd20b
DL
514 size += strlen(script);
515 size += strlen(name);
516 size += strlen(section);
6d1a5f93 517 size += 4;
abbfd20b 518
95642a10
MS
519 if (size > INT_MAX)
520 return -1;
521
522 buffer = alloca(size);
6d1a5f93 523 ret = snprintf(buffer, size, "exec %s %s %s", script, name, section);
0fd73091 524 if (ret < 0 || ret >= size)
9ba8130c 525 return -1;
751d9dcd 526
abbfd20b 527 va_start(ap, script);
9ba8130c 528 while ((p = va_arg(ap, char *))) {
062b72c6 529 int len = size - ret;
9ba8130c
SH
530 int rc;
531 rc = snprintf(buffer + ret, len, " %s", p);
0fd73091 532 if (rc < 0 || rc >= len)
9ba8130c 533 return -1;
9ba8130c
SH
534 ret += rc;
535 }
abbfd20b 536 va_end(ap);
751d9dcd 537
91c3830e 538 return run_buffer(buffer);
e3b4c4c4
ST
539}
540
0fd73091 541/* pin_rootfs
63fc76c3 542 * if rootfs is a directory, then open ${rootfs}/.lxc-keep for writing for
b7ed4bf0
CS
543 * the duration of the container run, to prevent the container from marking
544 * the underlying fs readonly on shutdown. unlink the file immediately so
63fc76c3
GJ
545 * no name pollution is happens.
546 * don't unlink on NFS to avoid random named stale handles.
0c547523
SH
547 * return -1 on error.
548 * return -2 if nothing needed to be pinned.
549 * return an open fd (>=0) if we pinned it.
550 */
551int pin_rootfs(const char *rootfs)
552{
0fd73091
CB
553 int fd, ret;
554 char absrootfs[MAXPATHLEN], absrootfspin[MAXPATHLEN];
0c547523 555 struct stat s;
63fc76c3 556 struct statfs sfs;
0c547523 557
e99ee0de 558 if (rootfs == NULL || strlen(rootfs) == 0)
0d03360a 559 return -2;
e99ee0de 560
00ec333b 561 if (!realpath(rootfs, absrootfs))
9be53773 562 return -2;
0c547523 563
0fd73091
CB
564 ret = access(absrootfs, F_OK);
565 if (ret != 0)
0c547523 566 return -1;
0c547523 567
0fd73091
CB
568 ret = stat(absrootfs, &s);
569 if (ret < 0)
0c547523 570 return -1;
0c547523 571
72f919c4 572 if (!S_ISDIR(s.st_mode))
0c547523
SH
573 return -2;
574
63fc76c3 575 ret = snprintf(absrootfspin, MAXPATHLEN, "%s/.lxc-keep", absrootfs);
00ec333b 576 if (ret >= MAXPATHLEN)
0c547523 577 return -1;
0c547523 578
0fd73091 579 fd = open(absrootfspin, O_CREAT | O_RDWR, S_IWUSR | S_IRUSR);
b7ed4bf0
CS
580 if (fd < 0)
581 return fd;
0fd73091 582
63fc76c3
GJ
583 if (fstatfs (fd, &sfs)) {
584 return -1;
585 }
586
587 if (sfs.f_type == NFS_SUPER_MAGIC) {
588 DEBUG("rootfs on NFS, not unlinking pin file \"%s\".", absrootfspin);
589 return fd;
590 }
591
b7ed4bf0 592 (void)unlink(absrootfspin);
0fd73091 593
0c547523
SH
594 return fd;
595}
596
0fd73091
CB
597/* If we are asking to remount something, make sure that any NOEXEC etc are
598 * honored.
e2a7e8dc 599 */
5ae72b98 600unsigned long add_required_remount_flags(const char *s, const char *d,
5285689c 601 unsigned long flags)
e2a7e8dc 602{
614305f3 603#ifdef HAVE_STATVFS
0fd73091 604 int ret;
e2a7e8dc
SH
605 struct statvfs sb;
606 unsigned long required_flags = 0;
607
608 if (!(flags & MS_REMOUNT))
609 return flags;
610
611 if (!s)
612 s = d;
613
614 if (!s)
615 return flags;
0fd73091
CB
616
617 ret = statvfs(s, &sb);
618 if (ret < 0)
e2a7e8dc
SH
619 return flags;
620
621 if (sb.f_flag & MS_NOSUID)
622 required_flags |= MS_NOSUID;
623 if (sb.f_flag & MS_NODEV)
624 required_flags |= MS_NODEV;
625 if (sb.f_flag & MS_RDONLY)
626 required_flags |= MS_RDONLY;
627 if (sb.f_flag & MS_NOEXEC)
628 required_flags |= MS_NOEXEC;
629
630 return flags | required_flags;
614305f3
SH
631#else
632 return flags;
633#endif
e2a7e8dc
SH
634}
635
4fb3cba5 636static int lxc_mount_auto_mounts(struct lxc_conf *conf, int flags, struct lxc_handler *handler)
368bbc02 637{
0fd73091 638 int i, r;
b06b8511
CS
639 static struct {
640 int match_mask;
641 int match_flag;
642 const char *source;
643 const char *destination;
644 const char *fstype;
645 unsigned long flags;
646 const char *options;
647 } default_mounts[] = {
0fd73091
CB
648 /* Read-only bind-mounting... In older kernels, doing that
649 * required to do one MS_BIND mount and then
650 * MS_REMOUNT|MS_RDONLY the same one. According to mount(2)
651 * manpage, MS_BIND honors MS_RDONLY from kernel 2.6.26
652 * onwards. However, this apparently does not work on kernel
653 * 3.8. Unfortunately, on that very same kernel, doing the same
654 * trick as above doesn't seem to work either, there one needs
655 * to ALSO specify MS_BIND for the remount, otherwise the
656 * entire fs is remounted read-only or the mount fails because
657 * it's busy... MS_REMOUNT|MS_BIND|MS_RDONLY seems to work for
658 * kernels as low as 2.6.32...
368bbc02 659 */
0fd73091 660 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL },
592fd47a 661 /* proc/tty is used as a temporary placeholder for proc/sys/net which we'll move back in a few steps */
0fd73091
CB
662 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys/net", "%r/proc/tty", NULL, MS_BIND, NULL },
663 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys", "%r/proc/sys", NULL, MS_BIND, NULL },
664 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL },
665 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/tty", "%r/proc/sys/net", NULL, MS_MOVE, NULL },
666 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sysrq-trigger", "%r/proc/sysrq-trigger", NULL, MS_BIND, NULL },
667 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sysrq-trigger", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL },
668 { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL },
669 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW, "sysfs", "%r/sys", "sysfs", 0, NULL },
670 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO, "sysfs", "%r/sys", "sysfs", MS_RDONLY, NULL },
671 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys", "sysfs", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL },
672 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/sys", "%r/sys", NULL, MS_BIND, NULL },
673 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, NULL, "%r/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL },
674 { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys/devices/virtual/net", "sysfs", 0, NULL },
675 { 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 },
676 { 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 },
677 { 0, 0, NULL, NULL, NULL, 0, NULL }
b06b8511 678 };
368bbc02 679
b06b8511 680 for (i = 0; default_mounts[i].match_mask; i++) {
0fd73091
CB
681 int saved_errno;
682 unsigned long mflags;
683 char *destination = NULL;
684 char *source = NULL;
685 if ((flags & default_mounts[i].match_mask) != default_mounts[i].match_flag)
686 continue;
687
688 if (default_mounts[i].source) {
cc4fd506 689 /* will act like strdup if %r is not present */
0fd73091
CB
690 source = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].source);
691 if (!source)
cc4fd506 692 return -1;
0fd73091 693 }
f24a52d5 694
0fd73091
CB
695 if (!default_mounts[i].destination) {
696 ERROR("BUG: auto mounts destination %d was NULL", i);
b06b8511 697 free(source);
0fd73091
CB
698 return -1;
699 }
700
701 /* will act like strdup if %r is not present */
702 destination = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].destination);
703 if (!destination) {
704 saved_errno = errno;
705 free(source);
706 errno = saved_errno;
707 return -1;
708 }
709
710 mflags = add_required_remount_flags(source, destination,
711 default_mounts[i].flags);
712 r = safe_mount(source, destination, default_mounts[i].fstype,
713 mflags, default_mounts[i].options,
714 conf->rootfs.path ? conf->rootfs.mount : NULL);
715 saved_errno = errno;
716 if (r < 0 && errno == ENOENT) {
717 INFO("Mount source or target for \"%s\" on \"%s\" does "
718 "not exist. Skipping", source, destination);
719 r = 0;
720 } else if (r < 0) {
721 SYSERROR("Failed to mount \"%s\" on \"%s\" with flags %lu", source, destination, mflags);
722 }
723
724 free(source);
725 free(destination);
726 if (r < 0) {
727 errno = saved_errno;
728 return -1;
368bbc02 729 }
368bbc02
CS
730 }
731
b06b8511 732 if (flags & LXC_AUTO_CGROUP_MASK) {
0769b82a
CS
733 int cg_flags;
734
3f69fb12 735 cg_flags = flags & (LXC_AUTO_CGROUP_MASK & ~LXC_AUTO_CGROUP_FORCE);
0fd73091
CB
736 /* If the type of cgroup mount was not specified, it depends on
737 * the container's capabilities as to what makes sense: if we
738 * have CAP_SYS_ADMIN, the read-only part can be remounted
739 * read-write anyway, so we may as well default to read-write;
740 * then the admin will not be given a false sense of security.
741 * (And if they really want mixed r/o r/w, then they can
742 * explicitly specify :mixed.) OTOH, if the container lacks
743 * CAP_SYS_ADMIN, do only default to :mixed, because then the
744 * container can't remount it read-write.
745 */
0769b82a
CS
746 if (cg_flags == LXC_AUTO_CGROUP_NOSPEC || cg_flags == LXC_AUTO_CGROUP_FULL_NOSPEC) {
747 int has_sys_admin = 0;
b0ee5983
CB
748
749 if (!lxc_list_empty(&conf->keepcaps))
0769b82a 750 has_sys_admin = in_caplist(CAP_SYS_ADMIN, &conf->keepcaps);
b0ee5983 751 else
0769b82a 752 has_sys_admin = !in_caplist(CAP_SYS_ADMIN, &conf->caps);
b0ee5983
CB
753
754 if (cg_flags == LXC_AUTO_CGROUP_NOSPEC)
0769b82a 755 cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_RW : LXC_AUTO_CGROUP_MIXED;
b0ee5983 756 else
0769b82a 757 cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_FULL_RW : LXC_AUTO_CGROUP_FULL_MIXED;
0769b82a 758 }
0fd73091 759
3f69fb12 760 if (flags & LXC_AUTO_CGROUP_FORCE)
0fd73091
CB
761 cg_flags |= LXC_AUTO_CGROUP_FORCE;
762
8ede5f4c 763 if (!cgroup_mount(conf->rootfs.path ? conf->rootfs.mount : "", handler, cg_flags)) {
0fd73091 764 SYSERROR("Failed to mount \"/sys/fs/cgroup\"");
b06b8511 765 return -1;
368bbc02
CS
766 }
767 }
768
368bbc02 769 return 0;
368bbc02
CS
770}
771
4e5440c6 772static int setup_utsname(struct utsname *utsname)
0ad19a3f 773{
0fd73091
CB
774 int ret;
775
4e5440c6
DL
776 if (!utsname)
777 return 0;
0ad19a3f 778
0fd73091
CB
779 ret = sethostname(utsname->nodename, strlen(utsname->nodename));
780 if (ret < 0) {
781 SYSERROR("Failed to set the hostname to \"%s\"", utsname->nodename);
0ad19a3f 782 return -1;
783 }
784
0fd73091 785 INFO("Set hostname to \"%s\"", utsname->nodename);
cd54d859 786
0ad19a3f 787 return 0;
788}
789
69aa6655
DE
790struct dev_symlinks {
791 const char *oldpath;
792 const char *name;
793};
794
795static const struct dev_symlinks dev_symlinks[] = {
0fd73091
CB
796 { "/proc/self/fd", "fd" },
797 { "/proc/self/fd/0", "stdin" },
798 { "/proc/self/fd/1", "stdout" },
799 { "/proc/self/fd/2", "stderr" },
69aa6655
DE
800};
801
ed8704d0 802static int lxc_setup_dev_symlinks(const struct lxc_rootfs *rootfs)
69aa6655 803{
0fd73091 804 int i, ret;
69aa6655 805 char path[MAXPATHLEN];
09227be2 806 struct stat s;
69aa6655 807
69aa6655
DE
808 for (i = 0; i < sizeof(dev_symlinks) / sizeof(dev_symlinks[0]); i++) {
809 const struct dev_symlinks *d = &dev_symlinks[i];
0fd73091
CB
810
811 ret = snprintf(path, sizeof(path), "%s/dev/%s",
812 rootfs->path ? rootfs->mount : "", d->name);
69aa6655
DE
813 if (ret < 0 || ret >= MAXPATHLEN)
814 return -1;
09227be2 815
0fd73091
CB
816 /* Stat the path first. If we don't get an error accept it as
817 * is and don't try to create it
09227be2 818 */
0fd73091
CB
819 ret = stat(path, &s);
820 if (ret == 0)
09227be2 821 continue;
09227be2 822
69aa6655
DE
823 ret = symlink(d->oldpath, path);
824 if (ret && errno != EEXIST) {
0fd73091
CB
825 if (errno == EROFS) {
826 WARN("Failed to create \"%s\". Read-only filesystem", path);
09227be2 827 } else {
0fd73091 828 SYSERROR("Failed to create \"%s\"", path);
09227be2
MW
829 return -1;
830 }
69aa6655
DE
831 }
832 }
0fd73091 833
69aa6655
DE
834 return 0;
835}
836
2187efd3 837/* Build a space-separate list of ptys to pass to systemd. */
393903d1 838static bool append_ptyname(char **pp, char *name)
b0a33c1e 839{
393903d1
SH
840 char *p;
841
842 if (!*pp) {
843 *pp = malloc(strlen(name) + strlen("container_ttys=") + 1);
844 if (!*pp)
845 return false;
0fd73091 846
393903d1
SH
847 sprintf(*pp, "container_ttys=%s", name);
848 return true;
849 }
0fd73091 850
393903d1
SH
851 p = realloc(*pp, strlen(*pp) + strlen(name) + 2);
852 if (!p)
853 return false;
0fd73091 854
393903d1
SH
855 *pp = p;
856 strcat(p, " ");
857 strcat(p, name);
0fd73091 858
393903d1
SH
859 return true;
860}
861
2187efd3 862static int lxc_setup_ttys(struct lxc_conf *conf)
393903d1 863{
9e1045e3 864 int i, ret;
0e4be3cf 865 const struct lxc_tty_info *ttys = &conf->ttys;
393903d1 866 char *ttydir = conf->ttydir;
7c6ef2a2 867 char path[MAXPATHLEN], lxcpath[MAXPATHLEN];
b0a33c1e 868
e8bd4e43 869 if (!conf->rootfs.path)
bc9bd0e3
DL
870 return 0;
871
0e4be3cf
CB
872 for (i = 0; i < ttys->nbtty; i++) {
873 struct lxc_terminal_info *tty = &ttys->tty[i];
b0a33c1e 874
e8bd4e43 875 ret = snprintf(path, sizeof(path), "/dev/tty%d", i + 1);
73363c61 876 if (ret < 0 || (size_t)ret >= sizeof(path))
7c6ef2a2 877 return -1;
9e1045e3 878
7c6ef2a2
SH
879 if (ttydir) {
880 /* create dev/lxc/tty%d" */
9e1045e3
CB
881 ret = snprintf(lxcpath, sizeof(lxcpath),
882 "/dev/%s/tty%d", ttydir, i + 1);
73363c61 883 if (ret < 0 || (size_t)ret >= sizeof(lxcpath))
7c6ef2a2 884 return -1;
9e1045e3 885
7c6ef2a2 886 ret = creat(lxcpath, 0660);
9e1045e3 887 if (ret < 0 && errno != EEXIST) {
73363c61 888 SYSERROR("Failed to create \"%s\"", lxcpath);
7c6ef2a2
SH
889 return -1;
890 }
4d44e274
SH
891 if (ret >= 0)
892 close(ret);
9e1045e3 893
7c6ef2a2 894 ret = unlink(path);
9e1045e3 895 if (ret < 0 && errno != ENOENT) {
73363c61 896 SYSERROR("Failed to unlink \"%s\"", path);
7c6ef2a2
SH
897 return -1;
898 }
b0a33c1e 899
2520facd 900 ret = mount(tty->name, lxcpath, "none", MS_BIND, 0);
9e1045e3 901 if (ret < 0) {
73363c61 902 WARN("Failed to bind mount \"%s\" onto \"%s\"",
2520facd 903 tty->name, path);
7c6ef2a2
SH
904 continue;
905 }
0fd73091 906 DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name,
9e1045e3 907 path);
13954cce 908
9e1045e3
CB
909 ret = snprintf(lxcpath, sizeof(lxcpath), "%s/tty%d",
910 ttydir, i + 1);
73363c61 911 if (ret < 0 || (size_t)ret >= sizeof(lxcpath))
9ba8130c 912 return -1;
9e1045e3 913
7c6ef2a2 914 ret = symlink(lxcpath, path);
9e1045e3 915 if (ret < 0) {
73363c61 916 SYSERROR("Failed to create symlink \"%s\" -> \"%s\"",
9e1045e3 917 path, lxcpath);
7c6ef2a2
SH
918 return -1;
919 }
920 } else {
9e1045e3
CB
921 /* If we populated /dev, then we need to create
922 * /dev/ttyN
923 */
924 ret = access(path, F_OK);
925 if (ret < 0) {
c6883f38 926 ret = creat(path, 0660);
9e1045e3 927 if (ret < 0) {
73363c61 928 SYSERROR("Failed to create \"%s\"", path);
c6883f38 929 /* this isn't fatal, continue */
025ed0f3 930 } else {
c6883f38 931 close(ret);
025ed0f3 932 }
c6883f38 933 }
9e1045e3 934
2520facd 935 ret = mount(tty->name, path, "none", MS_BIND, 0);
9e1045e3 936 if (ret < 0) {
2520facd 937 SYSERROR("Failed to mount '%s'->'%s'", tty->name, path);
7c6ef2a2
SH
938 continue;
939 }
9e1045e3 940
2520facd 941 DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name,
9e1045e3 942 path);
393903d1 943 }
9e1045e3 944
2520facd 945 if (!append_ptyname(&conf->pty_names, tty->name)) {
393903d1
SH
946 ERROR("Error setting up container_ttys string");
947 return -1;
b0a33c1e 948 }
949 }
950
0e4be3cf 951 INFO("Finished setting up %d /dev/tty<N> device(s)", ttys->nbtty);
b0a33c1e 952 return 0;
953}
954
2187efd3
CB
955int lxc_allocate_ttys(const char *name, struct lxc_conf *conf)
956{
2187efd3 957 int i, ret;
0fd73091 958 struct lxc_tty_info *ttys = &conf->ttys;
2187efd3
CB
959
960 /* no tty in the configuration */
961 if (!conf->tty)
962 return 0;
963
0e4be3cf
CB
964 ttys->tty = malloc(sizeof(*ttys->tty) * conf->tty);
965 if (!ttys->tty)
2187efd3 966 return -ENOMEM;
2187efd3
CB
967
968 for (i = 0; i < conf->tty; i++) {
0e4be3cf 969 struct lxc_terminal_info *tty = &ttys->tty[i];
2187efd3 970
2520facd
CB
971 ret = openpty(&tty->master, &tty->slave,
972 tty->name, NULL, NULL);
2187efd3 973 if (ret) {
0fd73091 974 SYSERROR("Failed to create tty %d", i);
0e4be3cf
CB
975 ttys->nbtty = i;
976 lxc_delete_tty(ttys);
2187efd3
CB
977 return -ENOTTY;
978 }
979
0fd73091 980 DEBUG("Created tty \"%s\" with master fd %d and slave fd %d",
2520facd 981 tty->name, tty->master, tty->slave);
2187efd3
CB
982
983 /* Prevent leaking the file descriptors to the container */
2520facd 984 ret = fcntl(tty->master, F_SETFD, FD_CLOEXEC);
2187efd3 985 if (ret < 0)
0fd73091
CB
986 WARN("Failed to set FD_CLOEXEC flag on master fd %d of "
987 "tty device \"%s\": %s",
2520facd 988 tty->master, tty->name, strerror(errno));
2187efd3 989
2520facd 990 ret = fcntl(tty->slave, F_SETFD, FD_CLOEXEC);
2187efd3 991 if (ret < 0)
0fd73091
CB
992 WARN("Failed to set FD_CLOEXEC flag on slave fd %d of "
993 "tty device \"%s\": %s",
2520facd 994 tty->slave, tty->name, strerror(errno));
2187efd3 995
2520facd 996 tty->busy = 0;
2187efd3
CB
997 }
998
0e4be3cf 999 ttys->nbtty = conf->tty;
2187efd3 1000
0fd73091 1001 INFO("Finished creating %d tty devices", conf->tty);
2187efd3
CB
1002 return 0;
1003}
1004
0e4be3cf 1005void lxc_delete_tty(struct lxc_tty_info *ttys)
2187efd3
CB
1006{
1007 int i;
1008
0e4be3cf
CB
1009 for (i = 0; i < ttys->nbtty; i++) {
1010 struct lxc_terminal_info *tty = &ttys->tty[i];
2187efd3 1011
2520facd
CB
1012 close(tty->master);
1013 close(tty->slave);
2187efd3
CB
1014 }
1015
0e4be3cf
CB
1016 free(ttys->tty);
1017 ttys->tty = NULL;
1018 ttys->nbtty = 0;
2187efd3
CB
1019}
1020
1021static int lxc_send_ttys_to_parent(struct lxc_handler *handler)
1022{
1023 int i;
0fd73091 1024 int ret = -1;
2187efd3 1025 struct lxc_conf *conf = handler->conf;
0e4be3cf 1026 struct lxc_tty_info *ttys = &conf->ttys;
2187efd3 1027 int sock = handler->data_sock[0];
2187efd3 1028
0fd73091 1029 if (conf->tty == 0)
2187efd3
CB
1030 return 0;
1031
1032 for (i = 0; i < conf->tty; i++) {
1033 int ttyfds[2];
0e4be3cf 1034 struct lxc_terminal_info *tty = &ttys->tty[i];
2187efd3 1035
2520facd
CB
1036 ttyfds[0] = tty->master;
1037 ttyfds[1] = tty->slave;
2187efd3
CB
1038
1039 ret = lxc_abstract_unix_send_fds(sock, ttyfds, 2, NULL, 0);
1040 if (ret < 0)
1041 break;
1042
0fd73091 1043 TRACE("Sent ty \"%s\" with master fd %d and slave fd %d to "
2520facd 1044 "parent", tty->name, tty->master, tty->slave);
2187efd3
CB
1045 }
1046
1047 if (ret < 0)
1048 ERROR("Failed to send %d ttys to parent: %s", conf->tty,
1049 strerror(errno));
1050 else
1051 TRACE("Sent %d ttys to parent", conf->tty);
1052
1053 return ret;
1054}
1055
1056static int lxc_create_ttys(struct lxc_handler *handler)
1057{
1058 int ret = -1;
1059 struct lxc_conf *conf = handler->conf;
1060
1061 ret = lxc_allocate_ttys(handler->name, conf);
1062 if (ret < 0) {
1063 ERROR("Failed to allocate ttys");
1064 goto on_error;
1065 }
1066
1067 ret = lxc_send_ttys_to_parent(handler);
1068 if (ret < 0) {
1069 ERROR("Failed to send ttys to parent");
1070 goto on_error;
1071 }
1072
1073 if (!conf->is_execute) {
1074 ret = lxc_setup_ttys(conf);
1075 if (ret < 0) {
1076 ERROR("Failed to setup ttys");
1077 goto on_error;
1078 }
1079 }
1080
1081 if (conf->pty_names) {
1082 ret = setenv("container_ttys", conf->pty_names, 1);
1083 if (ret < 0)
1084 SYSERROR("Failed to set \"container_ttys=%s\"", conf->pty_names);
1085 }
1086
1087 ret = 0;
1088
1089on_error:
0e4be3cf 1090 lxc_delete_tty(&conf->ttys);
2187efd3
CB
1091
1092 return ret;
1093}
1094
59bb8698 1095static int setup_rootfs_pivot_root(const char *rootfs)
bf601689 1096{
0fd73091
CB
1097 int ret;
1098 int newroot = -1, oldroot = -1;
bf601689 1099
2d489f9e
SH
1100 oldroot = open("/", O_DIRECTORY | O_RDONLY);
1101 if (oldroot < 0) {
0fd73091 1102 SYSERROR("Failed to open old root directory");
9ba8130c
SH
1103 return -1;
1104 }
0fd73091 1105
2d489f9e
SH
1106 newroot = open(rootfs, O_DIRECTORY | O_RDONLY);
1107 if (newroot < 0) {
0fd73091
CB
1108 SYSERROR("Failed to open new root directory");
1109 goto on_error;
c08556c6 1110 }
bf601689 1111
cc6f6dd7 1112 /* change into new root fs */
0fd73091
CB
1113 ret = fchdir(newroot);
1114 if (ret < 0) {
1115 SYSERROR("Failed to change to new rootfs \"%s\"", rootfs);
1116 goto on_error;
cc6f6dd7
DL
1117 }
1118
cc6f6dd7 1119 /* pivot_root into our new root fs */
0fd73091
CB
1120 ret = pivot_root(".", ".");
1121 if (ret < 0) {
1122 SYSERROR("Failed to pivot_root()");
1123 goto on_error;
bf601689 1124 }
cc6f6dd7 1125
e599717b 1126 /* At this point the old-root is mounted on top of our new-root. To
0fd73091
CB
1127 * unmounted it we must not be chdir'd into it, so escape back to
1128 * old-root.
2d489f9e 1129 */
0fd73091
CB
1130 ret = fchdir(oldroot);
1131 if (ret < 0) {
1132 SYSERROR("Failed to enter old root directory");
1133 goto on_error;
2d489f9e 1134 }
0fd73091 1135
e599717b
FW
1136 /* Make oldroot rslave to make sure our umounts don't propagate to the
1137 * host.
1138 */
1139 ret = mount("", ".", "", MS_SLAVE | MS_REC, NULL);
1140 if (ret < 0) {
1141 SYSERROR("Failed to make oldroot rslave");
1142 goto on_error;
1143 }
1144
0fd73091
CB
1145 ret = umount2(".", MNT_DETACH);
1146 if (ret < 0) {
1147 SYSERROR("Failed to detach old root directory");
1148 goto on_error;
cc6f6dd7
DL
1149 }
1150
0fd73091
CB
1151 ret = fchdir(newroot);
1152 if (ret < 0) {
1153 SYSERROR("Failed to re-enter new root directory");
1154 goto on_error;
2d489f9e 1155 }
cc6f6dd7 1156
2d489f9e
SH
1157 close(oldroot);
1158 close(newroot);
bf601689 1159
0fd73091 1160 DEBUG("pivot_root(\"%s\") successful", rootfs);
bf601689 1161
bf601689 1162 return 0;
2d489f9e 1163
0fd73091 1164on_error:
2d489f9e
SH
1165 if (oldroot != -1)
1166 close(oldroot);
1167 if (newroot != -1)
1168 close(newroot);
0fd73091 1169
2d489f9e 1170 return -1;
bf601689
MH
1171}
1172
7133b912
CB
1173/* Just create a path for /dev under $lxcpath/$name and in rootfs If we hit an
1174 * error, log it but don't fail yet.
91c3830e 1175 */
7133b912
CB
1176static int mount_autodev(const char *name, const struct lxc_rootfs *rootfs,
1177 const char *lxcpath)
91c3830e
SH
1178{
1179 int ret;
87da4ec3
SH
1180 size_t clen;
1181 char *path;
91c3830e 1182
7133b912 1183 INFO("Preparing \"/dev\"");
bc6928ff 1184
14221cbb 1185 /* $(rootfs->mount) + "/dev/pts" + '\0' */
ec50007f 1186 clen = (rootfs->path ? strlen(rootfs->mount) : 0) + 9;
87da4ec3 1187 path = alloca(clen);
bc6928ff 1188
ec50007f 1189 ret = snprintf(path, clen, "%s/dev", rootfs->path ? rootfs->mount : "");
7133b912 1190 if (ret < 0 || (size_t)ret >= clen)
91c3830e 1191 return -1;
bc6928ff 1192
87da4ec3 1193 if (!dir_exists(path)) {
7133b912
CB
1194 WARN("\"/dev\" directory does not exist. Proceeding without "
1195 "autodev being set up");
87da4ec3 1196 return 0;
bc6928ff 1197 }
87da4ec3 1198
1ec0e8e3 1199 ret = safe_mount("none", path, "tmpfs", 0, "size=500000,mode=755",
7133b912
CB
1200 rootfs->path ? rootfs->mount : NULL);
1201 if (ret < 0) {
1202 SYSERROR("Failed to mount tmpfs on \"%s\"", path);
1ec0e8e3 1203 return -1;
91c3830e 1204 }
7133b912 1205 INFO("Mounted tmpfs on \"%s\"", path);
87da4ec3 1206
ec50007f 1207 ret = snprintf(path, clen, "%s/dev/pts", rootfs->path ? rootfs->mount : "");
7133b912 1208 if (ret < 0 || (size_t)ret >= clen)
91c3830e 1209 return -1;
87da4ec3 1210
7133b912 1211 /* If we are running on a devtmpfs mapping, dev/pts may already exist.
bc6928ff
MW
1212 * If not, then create it and exit if that fails...
1213 */
87da4ec3 1214 if (!dir_exists(path)) {
bc6928ff 1215 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
7133b912
CB
1216 if (ret < 0) {
1217 SYSERROR("Failed to create directory \"%s\"", path);
bc6928ff
MW
1218 return -1;
1219 }
91c3830e
SH
1220 }
1221
7133b912 1222 INFO("Prepared \"/dev\"");
91c3830e
SH
1223 return 0;
1224}
1225
5e73416f 1226struct lxc_device_node {
74a3920a 1227 const char *name;
5e73416f
CB
1228 const mode_t mode;
1229 const int maj;
1230 const int min;
c6883f38
SH
1231};
1232
5e73416f 1233static const struct lxc_device_node lxc_devices[] = {
06749971 1234 { "full", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 7 },
5e73416f 1235 { "null", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 3 },
06749971
CB
1236 { "random", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 8 },
1237 { "tty", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 5, 0 },
5e73416f
CB
1238 { "urandom", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 9 },
1239 { "zero", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 5 },
c6883f38
SH
1240};
1241
27245ff7 1242static int lxc_fill_autodev(const struct lxc_rootfs *rootfs)
c6883f38 1243{
5e73416f 1244 int i, ret;
c6883f38 1245 char path[MAXPATHLEN];
3a32201c 1246 mode_t cmask;
5e73416f 1247 bool can_mknod = true;
c6883f38 1248
3999be0a
CB
1249 ret = snprintf(path, MAXPATHLEN, "%s/dev",
1250 rootfs->path ? rootfs->mount : "");
1251 if (ret < 0 || ret >= MAXPATHLEN)
c6883f38 1252 return -1;
91c3830e 1253
0bbf8572
CB
1254 /* ignore, just don't try to fill in */
1255 if (!dir_exists(path))
9cb4d183
SH
1256 return 0;
1257
3999be0a
CB
1258 INFO("Populating \"/dev\"");
1259
3a32201c 1260 cmask = umask(S_IXUSR | S_IXGRP | S_IXOTH);
5e73416f
CB
1261 for (i = 0; i < sizeof(lxc_devices) / sizeof(lxc_devices[0]); i++) {
1262 char hostpath[MAXPATHLEN];
1263 const struct lxc_device_node *device = &lxc_devices[i];
0728ebf4 1264
3999be0a 1265 ret = snprintf(path, MAXPATHLEN, "%s/dev/%s",
5e73416f 1266 rootfs->path ? rootfs->mount : "", device->name);
c6883f38
SH
1267 if (ret < 0 || ret >= MAXPATHLEN)
1268 return -1;
0bbf8572 1269
5e73416f
CB
1270 if (can_mknod) {
1271 ret = mknod(path, device->mode, makedev(device->maj, device->min));
1272 if (ret == 0 || (ret < 0 && errno == EEXIST)) {
1273 DEBUG("Created device node \"%s\"", path);
0bbf8572
CB
1274 continue;
1275 }
1276
5e73416f
CB
1277 if (errno != EPERM) {
1278 SYSERROR("Failed to create device node \"%s\"", path);
9cb4d183
SH
1279 return -1;
1280 }
3999be0a 1281
5e73416f
CB
1282 /* This can e.g. happen when the container is
1283 * unprivileged or CAP_MKNOD has been dropped.
1284 */
1285 can_mknod = false;
1286 }
1287
1288 ret = mknod(path, S_IFREG, 0);
1289 if (ret < 0 && errno != EEXIST) {
1290 SYSERROR("Failed to create file \"%s\"", path);
1291 return -1;
1292 }
1293
1294 /* Fallback to bind-mounting the device from the host. */
1295 ret = snprintf(hostpath, MAXPATHLEN, "/dev/%s", device->name);
1296 if (ret < 0 || ret >= MAXPATHLEN)
1297 return -1;
1298
1299 ret = safe_mount(hostpath, path, 0, MS_BIND, NULL,
1300 rootfs->path ? rootfs->mount : NULL);
1301 if (ret < 0) {
1302 SYSERROR("Failed to bind mount host device node \"%s\" "
1303 "onto \"%s\"", hostpath, path);
1304 return -1;
c6883f38 1305 }
5e73416f
CB
1306 DEBUG("Bind mounted host device node \"%s\" onto \"%s\"",
1307 hostpath, path);
c6883f38 1308 }
5e73416f 1309 (void)umask(cmask);
c6883f38 1310
3999be0a 1311 INFO("Populated \"/dev\"");
c6883f38
SH
1312 return 0;
1313}
1314
9aa76a17 1315static int lxc_setup_rootfs(struct lxc_conf *conf)
0ad19a3f 1316{
9aa76a17 1317 int ret;
10bc1861 1318 struct lxc_storage *bdev;
91c3e281 1319 const struct lxc_rootfs *rootfs;
cc28d0b0 1320
91c3e281 1321 rootfs = &conf->rootfs;
a0f379bf 1322 if (!rootfs->path) {
0fd73091
CB
1323 ret = mount("", "/", NULL, MS_SLAVE | MS_REC, 0);
1324 if (ret < 0) {
1325 SYSERROR("Failed to make / rslave");
a0f379bf
DW
1326 return -1;
1327 }
0fd73091 1328
c69bd12f 1329 return 0;
a0f379bf 1330 }
0ad19a3f 1331
0fd73091
CB
1332 ret = access(rootfs->mount, F_OK);
1333 if (ret != 0) {
1334 SYSERROR("Failed to access to \"%s\". Check it is present",
12297168 1335 rootfs->mount);
b1789442
DL
1336 return -1;
1337 }
1338
8a388ed4 1339 bdev = storage_init(conf);
9aa76a17 1340 if (!bdev) {
0fd73091 1341 ERROR("Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"",
91c3e281
CB
1342 rootfs->path, rootfs->mount,
1343 rootfs->options ? rootfs->options : "(null)");
9aa76a17 1344 return -1;
9be53773 1345 }
9aa76a17
CB
1346
1347 ret = bdev->ops->mount(bdev);
10bc1861 1348 storage_put(bdev);
9aa76a17 1349 if (ret < 0) {
0fd73091 1350 ERROR("Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"",
91c3e281
CB
1351 rootfs->path, rootfs->mount,
1352 rootfs->options ? rootfs->options : "(null)");
c3f0a28c 1353 return -1;
1354 }
0ad19a3f 1355
0fd73091 1356 DEBUG("Mounted rootfs \"%s\" onto \"%s\" with options \"%s\"",
91c3e281
CB
1357 rootfs->path, rootfs->mount,
1358 rootfs->options ? rootfs->options : "(null)");
9aa76a17 1359
ac778708
DL
1360 return 0;
1361}
1362
91e93c71
AV
1363int prepare_ramfs_root(char *root)
1364{
0fd73091
CB
1365 int i, ret;
1366 char *p, *p2;
1367 char buf[LXC_LINELEN], nroot[PATH_MAX];
91e93c71 1368 FILE *f;
91e93c71 1369
0fd73091
CB
1370 if (!realpath(root, nroot))
1371 return -1;
91e93c71 1372
0fd73091
CB
1373 ret = chdir("/");
1374 if (ret < 0)
1375 return -1;
91e93c71 1376
0fd73091
CB
1377 /* We could use here MS_MOVE, but in userns this mount is locked and
1378 * can't be moved.
91e93c71 1379 */
0fd73091
CB
1380 ret = mount(root, "/", NULL, MS_REC | MS_BIND, NULL);
1381 if (ret < 0) {
1382 SYSERROR("Failed to move \"%s\" into \"/\"", root);
1383 return -1;
91e93c71
AV
1384 }
1385
0fd73091
CB
1386 ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL);
1387 if (ret < 0) {
1388 SYSERROR("Failed to make \"/\" rprivate");
1389 return -1;
91e93c71
AV
1390 }
1391
0fd73091
CB
1392 /* The following code cleans up inhereted mounts which are not required
1393 * for CT.
91e93c71
AV
1394 *
1395 * The mountinfo file shows not all mounts, if a few points have been
1396 * unmounted between read operations from the mountinfo. So we need to
1397 * read mountinfo a few times.
1398 *
1399 * This loop can be skipped if a container uses unserns, because all
1400 * inherited mounts are locked and we should live with all this trash.
1401 */
0fd73091 1402 for (;;) {
91e93c71
AV
1403 int progress = 0;
1404
1405 f = fopen("./proc/self/mountinfo", "r");
1406 if (!f) {
1407 SYSERROR("Unable to open /proc/self/mountinfo");
1408 return -1;
1409 }
0fd73091 1410
eab15c1e 1411 while (fgets(buf, LXC_LINELEN, f)) {
91e93c71
AV
1412 for (p = buf, i=0; p && i < 4; i++)
1413 p = strchr(p+1, ' ');
0fd73091 1414
91e93c71
AV
1415 if (!p)
1416 continue;
0fd73091 1417
91e93c71
AV
1418 p2 = strchr(p+1, ' ');
1419 if (!p2)
1420 continue;
1421
1422 *p2 = '\0';
1423 *p = '.';
1424
1425 if (strcmp(p + 1, "/") == 0)
1426 continue;
0fd73091 1427
91e93c71
AV
1428 if (strcmp(p + 1, "/proc") == 0)
1429 continue;
1430
0fd73091
CB
1431 ret = umount2(p, MNT_DETACH);
1432 if (ret == 0)
91e93c71
AV
1433 progress++;
1434 }
0fd73091 1435
91e93c71 1436 fclose(f);
0fd73091 1437
91e93c71
AV
1438 if (!progress)
1439 break;
1440 }
1441
0fd73091
CB
1442 /* This also can be skipped if a container uses unserns. */
1443 (void)umount2("./proc", MNT_DETACH);
91e93c71
AV
1444
1445 /* It is weird, but chdir("..") moves us in a new root */
0fd73091
CB
1446 ret = chdir("..");
1447 if (ret < 0) {
91e93c71
AV
1448 SYSERROR("Unable to change working directory");
1449 return -1;
1450 }
1451
0fd73091
CB
1452 ret = chroot(".");
1453 if (ret < 0) {
91e93c71
AV
1454 SYSERROR("Unable to chroot");
1455 return -1;
1456 }
1457
1458 return 0;
1459}
1460
74a3920a 1461static int setup_pivot_root(const struct lxc_rootfs *rootfs)
ac778708 1462{
0fd73091
CB
1463 int ret;
1464
39c7b795 1465 if (!rootfs->path) {
0fd73091 1466 DEBUG("Container does not have a rootfs");
ac778708 1467 return 0;
39c7b795 1468 }
ac778708 1469
91e93c71 1470 if (detect_ramfs_rootfs()) {
0fd73091
CB
1471 DEBUG("Detected that container is on ramfs");
1472
1473 ret = prepare_ramfs_root(rootfs->mount);
1474 if (ret < 0) {
1475 ERROR("Failed to prepare minimal ramfs root");
91e93c71 1476 return -1;
39c7b795
CB
1477 }
1478
0fd73091 1479 DEBUG("Prepared ramfs root for container");
39c7b795
CB
1480 return 0;
1481 }
1482
0fd73091
CB
1483 ret = setup_rootfs_pivot_root(rootfs->mount);
1484 if (ret < 0) {
1485 ERROR("Failed to pivot_root()");
25368b52 1486 return -1;
c69bd12f
DL
1487 }
1488
0fd73091 1489 DEBUG("Finished pivot_root()");
25368b52 1490 return 0;
0ad19a3f 1491}
1492
f4900711
CB
1493static struct id_map *find_mapped_nsid_entry(struct lxc_conf *conf, unsigned id,
1494 enum idtype idtype)
1495{
1496 struct lxc_list *it;
1497 struct id_map *map;
1498 struct id_map *retmap = NULL;
1499
dcf0ffdf
CB
1500 /* Shortcut for container's root mappings. */
1501 if (id == 0) {
1502 if (idtype == ID_TYPE_UID)
1503 return conf->root_nsuid_map;
1504
1505 if (idtype == ID_TYPE_GID)
1506 return conf->root_nsgid_map;
1507 }
1508
f4900711
CB
1509 lxc_list_for_each(it, &conf->id_map) {
1510 map = it->elem;
1511 if (map->idtype != idtype)
1512 continue;
1513
1514 if (id >= map->nsid && id < map->nsid + map->range) {
1515 retmap = map;
1516 break;
1517 }
1518 }
1519
1520 return retmap;
1521}
1522
1523static int lxc_setup_devpts(struct lxc_conf *conf)
3c26f34e 1524{
70761e5e 1525 int ret;
f4900711 1526 const char *default_devpts_mntopts;
9d28c4f9 1527 char devpts_mntopts[256];
77890c6d 1528
f4900711 1529 if (conf->pts <= 0) {
0fd73091 1530 DEBUG("No new devpts instance will be mounted since no pts "
70761e5e 1531 "devices are requested");
d852c78c 1532 return 0;
3c26f34e 1533 }
1534
f4900711
CB
1535 if (!find_mapped_nsid_entry(conf, 5, ID_TYPE_GID))
1536 default_devpts_mntopts = "newinstance,ptmxmode=0666,mode=0620";
1537 else
1538 default_devpts_mntopts = "newinstance,ptmxmode=0666,mode=0620,gid=5";
1539
9d28c4f9 1540 ret = snprintf(devpts_mntopts, sizeof(devpts_mntopts), "%s,max=%d",
f4900711 1541 default_devpts_mntopts, conf->pts);
9d28c4f9
CB
1542 if (ret < 0 || (size_t)ret >= sizeof(devpts_mntopts))
1543 return -1;
1544
d5cb35d6 1545 /* Unmount old devpts instance. */
70761e5e
CB
1546 ret = access("/dev/pts/ptmx", F_OK);
1547 if (!ret) {
70761e5e
CB
1548 ret = umount("/dev/pts");
1549 if (ret < 0) {
0fd73091 1550 SYSERROR("Failed to unmount old devpts instance");
70761e5e 1551 return -1;
7e40254a 1552 }
0fd73091 1553 DEBUG("Unmounted old devpts instance");
7e40254a
JTLB
1554 }
1555
70761e5e
CB
1556 /* Create mountpoint for devpts instance. */
1557 ret = mkdir("/dev/pts", 0755);
1558 if (ret < 0 && errno != EEXIST) {
0fd73091 1559 SYSERROR("Failed to create \"/dev/pts\" directory");
3c26f34e 1560 return -1;
1561 }
1562
70761e5e 1563 /* Mount new devpts instance. */
f4900711 1564 ret = mount("devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC, devpts_mntopts);
70761e5e 1565 if (ret < 0) {
0fd73091 1566 SYSERROR("Failed to mount new devpts instance");
70761e5e
CB
1567 return -1;
1568 }
0fd73091 1569 DEBUG("Mount new devpts instance with options \"%s\"", devpts_mntopts);
70761e5e 1570
d5cb35d6 1571 /* Remove any pre-existing /dev/ptmx file. */
70761e5e 1572 ret = access("/dev/ptmx", F_OK);
d5cb35d6
CB
1573 if (!ret) {
1574 ret = remove("/dev/ptmx");
1575 if (ret < 0) {
0fd73091 1576 SYSERROR("Failed to remove existing \"/dev/ptmx\" file");
d5cb35d6 1577 return -1;
70761e5e 1578 }
0fd73091 1579 DEBUG("Removed existing \"/dev/ptmx\" file");
3c26f34e 1580 }
1581
d5cb35d6
CB
1582 /* Create dummy /dev/ptmx file as bind mountpoint for /dev/pts/ptmx. */
1583 ret = open("/dev/ptmx", O_CREAT, 0666);
1584 if (ret < 0) {
0fd73091 1585 SYSERROR("Failed to create dummy \"/dev/ptmx\" file as bind mount target");
d5cb35d6
CB
1586 return -1;
1587 }
e87bd19c 1588 close(ret);
0fd73091 1589 DEBUG("Created dummy \"/dev/ptmx\" file as bind mount target");
77890c6d 1590
d5cb35d6 1591 /* Fallback option: create symlink /dev/ptmx -> /dev/pts/ptmx */
e87bd19c 1592 ret = mount("/dev/pts/ptmx", "/dev/ptmx", NULL, MS_BIND, NULL);
d5cb35d6 1593 if (!ret) {
0fd73091 1594 DEBUG("Bind mounted \"/dev/pts/ptmx\" to \"/dev/ptmx\"");
d5cb35d6
CB
1595 return 0;
1596 } else {
1597 /* Fallthrough and try to create a symlink. */
0fd73091 1598 ERROR("Failed to bind mount \"/dev/pts/ptmx\" to \"/dev/ptmx\"");
d5cb35d6
CB
1599 }
1600
1601 /* Remove the dummy /dev/ptmx file we created above. */
1602 ret = remove("/dev/ptmx");
70761e5e 1603 if (ret < 0) {
0fd73091 1604 SYSERROR("Failed to remove existing \"/dev/ptmx\"");
d5cb35d6
CB
1605 return -1;
1606 }
1607
1608 /* Fallback option: Create symlink /dev/ptmx -> /dev/pts/ptmx. */
1609 ret = symlink("/dev/pts/ptmx", "/dev/ptmx");
1610 if (ret < 0) {
0fd73091 1611 SYSERROR("Failed to create symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\"");
3c26f34e 1612 return -1;
1613 }
0fd73091 1614 DEBUG("Created symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\"");
cd54d859 1615
3c26f34e 1616 return 0;
1617}
1618
cccc74b5
DL
1619static int setup_personality(int persona)
1620{
0fd73091
CB
1621 int ret;
1622
1623#if HAVE_SYS_PERSONALITY_H
cccc74b5
DL
1624 if (persona == -1)
1625 return 0;
1626
0fd73091
CB
1627 ret = personality(persona);
1628 if (ret < 0) {
1629 SYSERROR("Failed to set personality to \"0x%x\"", persona);
cccc74b5
DL
1630 return -1;
1631 }
1632
0fd73091
CB
1633 INFO("Set personality to \"0x%x\"", persona);
1634#endif
cccc74b5
DL
1635
1636 return 0;
1637}
1638
3d7d929a 1639static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs,
dcad02f8 1640 const struct lxc_terminal *console)
6e590161 1641{
0fd73091 1642 int fd, ret;
63376d7d 1643 char path[MAXPATHLEN];
86530b0a 1644 char *rootfs_path = rootfs->path ? rootfs->mount : "";
52e35957 1645
8b1b1210
CB
1646 if (console->path && !strcmp(console->path, "none"))
1647 return 0;
1648
86530b0a 1649 ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path);
3d7d929a 1650 if (ret < 0 || (size_t)ret >= sizeof(path))
7c6ef2a2 1651 return -1;
52e35957 1652
8b1b1210
CB
1653 /* When we are asked to setup a console we remove any previous
1654 * /dev/console bind-mounts.
1655 */
a7ba3c7f
CB
1656 if (file_exists(path)) {
1657 ret = lxc_unstack_mountpoint(path, false);
1658 if (ret < 0) {
86530b0a 1659 ERROR("Failed to unmount \"%s\": %s", path, strerror(errno));
a7ba3c7f
CB
1660 return -ret;
1661 } else {
86530b0a 1662 DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path);
a7ba3c7f 1663 }
8b1b1210
CB
1664 }
1665
1666 /* For unprivileged containers autodev or automounts will already have
1667 * taken care of creating /dev/console.
1668 */
0728ebf4
TA
1669 fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH);
1670 if (fd < 0) {
1671 if (errno != EEXIST) {
86530b0a 1672 SYSERROR("Failed to create console");
3d7d929a 1673 return -errno;
0728ebf4
TA
1674 }
1675 } else {
1676 close(fd);
52e35957
DL
1677 }
1678
86530b0a
L
1679 ret = chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH);
1680 if (ret < 0) {
0fd73091
CB
1681 SYSERROR("Failed to set mode \"0%o\" to \"%s\"",
1682 S_IXUSR | S_IXGRP | S_IXOTH, console->name);
3d7d929a 1683 return -errno;
63376d7d 1684 }
13954cce 1685
86530b0a
L
1686 ret = safe_mount(console->name, path, "none", MS_BIND, 0, rootfs_path);
1687 if (ret < 0) {
0fd73091 1688 ERROR("Failed to mount \"%s\" on \"%s\"", console->name, path);
6e590161 1689 return -1;
1690 }
1691
86530b0a 1692 DEBUG("Mounted pts device \"%s\" onto \"%s\"", console->name, path);
7c6ef2a2
SH
1693 return 0;
1694}
1695
3d7d929a 1696static int lxc_setup_ttydir_console(const struct lxc_rootfs *rootfs,
dcad02f8 1697 const struct lxc_terminal *console,
3d7d929a 1698 char *ttydir)
7c6ef2a2 1699{
3dc035f1 1700 int ret, fd;
3d7d929a 1701 char path[MAXPATHLEN], lxcpath[MAXPATHLEN];
86530b0a 1702 char *rootfs_path = rootfs->path ? rootfs->mount : "";
7c6ef2a2 1703
3dc035f1
L
1704 if (console->path && !strcmp(console->path, "none"))
1705 return 0;
1706
7c6ef2a2 1707 /* create rootfs/dev/<ttydir> directory */
86530b0a 1708 ret = snprintf(path, sizeof(path), "%s/dev/%s", rootfs_path, ttydir);
3d7d929a 1709 if (ret < 0 || (size_t)ret >= sizeof(path))
7c6ef2a2 1710 return -1;
3d7d929a 1711
7c6ef2a2
SH
1712 ret = mkdir(path, 0755);
1713 if (ret && errno != EEXIST) {
0fd73091 1714 SYSERROR("Failed to create \"%s\"", path);
3d7d929a 1715 return -errno;
7c6ef2a2 1716 }
4742cd9a 1717 DEBUG("Created directory for console and tty devices at \"%s\"", path);
7c6ef2a2 1718
86530b0a 1719 ret = snprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/console", rootfs_path, ttydir);
3d7d929a
CB
1720 if (ret < 0 || (size_t)ret >= sizeof(lxcpath))
1721 return -1;
1722
7c6ef2a2 1723 ret = creat(lxcpath, 0660);
3d7d929a 1724 if (ret == -1 && errno != EEXIST) {
0fd73091 1725 SYSERROR("Failed to create \"%s\"", lxcpath);
3d7d929a 1726 return -errno;
7c6ef2a2 1727 }
4d44e274
SH
1728 if (ret >= 0)
1729 close(ret);
7c6ef2a2 1730
86530b0a 1731 ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path);
3dc035f1 1732 if (ret < 0 || (size_t)ret >= sizeof(path))
7c6ef2a2 1733 return -1;
2a12fefd 1734
3dc035f1 1735 if (file_exists(path)) {
a7ba3c7f 1736 ret = lxc_unstack_mountpoint(path, false);
2a12fefd 1737 if (ret < 0) {
0fd73091 1738 ERROR("%s - Failed to unmount \"%s\"", strerror(errno), path);
a7ba3c7f
CB
1739 return -ret;
1740 } else {
86530b0a 1741 DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path);
a7ba3c7f 1742 }
3dc035f1 1743 }
2a12fefd 1744
3dc035f1
L
1745 fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH);
1746 if (fd < 0) {
1747 if (errno != EEXIST) {
86530b0a 1748 SYSERROR("Failed to create console");
3dc035f1 1749 return -errno;
2a12fefd 1750 }
3dc035f1
L
1751 } else {
1752 close(fd);
7c6ef2a2
SH
1753 }
1754
86530b0a
L
1755 ret = chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH);
1756 if (ret < 0) {
0fd73091
CB
1757 SYSERROR("Failed to set mode \"0%o\" to \"%s\"",
1758 S_IXUSR | S_IXGRP | S_IXOTH, console->name);
2a12fefd
CB
1759 return -errno;
1760 }
1761
3dc035f1 1762 /* bind mount console->name to '/dev/<ttydir>/console' */
86530b0a
L
1763 ret = safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs_path);
1764 if (ret < 0) {
0fd73091 1765 ERROR("Failed to mount \"%s\" on \"%s\"", console->name, lxcpath);
7c6ef2a2
SH
1766 return -1;
1767 }
86530b0a 1768 DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath);
3dc035f1
L
1769
1770 /* bind mount '/dev/<ttydir>/console' to '/dev/console' */
86530b0a
L
1771 ret = safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs_path);
1772 if (ret < 0) {
0fd73091 1773 ERROR("Failed to mount \"%s\" on \"%s\"", console->name, lxcpath);
3dc035f1
L
1774 return -1;
1775 }
86530b0a 1776 DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath);
3dc035f1 1777
86530b0a 1778 DEBUG("Console has been setup under \"%s\" and mounted to \"%s\"", lxcpath, path);
6e590161 1779 return 0;
1780}
1781
3d7d929a 1782static int lxc_setup_console(const struct lxc_rootfs *rootfs,
dcad02f8 1783 const struct lxc_terminal *console, char *ttydir)
7c6ef2a2 1784{
3d7d929a 1785
7c6ef2a2 1786 if (!ttydir)
3d7d929a 1787 return lxc_setup_dev_console(rootfs, console);
7c6ef2a2 1788
3d7d929a 1789 return lxc_setup_ttydir_console(rootfs, console, ttydir);
7c6ef2a2
SH
1790}
1791
998ac676
RT
1792static void parse_mntopt(char *opt, unsigned long *flags, char **data)
1793{
1794 struct mount_opt *mo;
1795
1796 /* If opt is found in mount_opt, set or clear flags.
1797 * Otherwise append it to data. */
1798
1799 for (mo = &mount_opt[0]; mo->name != NULL; mo++) {
0fd73091 1800 if (strncmp(opt, mo->name, strlen(mo->name)) == 0) {
998ac676
RT
1801 if (mo->clear)
1802 *flags &= ~mo->flag;
1803 else
1804 *flags |= mo->flag;
1805 return;
1806 }
1807 }
1808
1809 if (strlen(*data))
1810 strcat(*data, ",");
1811 strcat(*data, opt);
1812}
1813
0fd73091 1814int parse_mntopts(const char *mntopts, unsigned long *mntflags, char **mntdata)
998ac676 1815{
0fd73091
CB
1816 char *data, *p, *s;
1817 char *saveptr = NULL;
998ac676 1818
911324ef 1819 *mntdata = NULL;
91656ce5 1820 *mntflags = 0L;
911324ef
DL
1821
1822 if (!mntopts)
998ac676
RT
1823 return 0;
1824
911324ef 1825 s = strdup(mntopts);
0fd73091 1826 if (!s)
998ac676 1827 return -1;
998ac676
RT
1828
1829 data = malloc(strlen(s) + 1);
1830 if (!data) {
998ac676
RT
1831 free(s);
1832 return -1;
1833 }
1834 *data = 0;
1835
0fd73091 1836 for (; (p = strtok_r(s, ",", &saveptr)); s = NULL)
998ac676
RT
1837 parse_mntopt(p, mntflags, &data);
1838
1839 if (*data)
1840 *mntdata = data;
1841 else
1842 free(data);
1843 free(s);
1844
1845 return 0;
1846}
1847
d840039e
YT
1848static void parse_propagationopt(char *opt, unsigned long *flags)
1849{
1850 struct mount_opt *mo;
1851
1852 /* If opt is found in propagation_opt, set or clear flags. */
d840039e 1853 for (mo = &propagation_opt[0]; mo->name != NULL; mo++) {
0fd73091
CB
1854 if (strncmp(opt, mo->name, strlen(mo->name)) != 0)
1855 continue;
1856
1857 if (mo->clear)
1858 *flags &= ~mo->flag;
1859 else
1860 *flags |= mo->flag;
1861
1862 return;
d840039e
YT
1863 }
1864}
1865
1866static int parse_propagationopts(const char *mntopts, unsigned long *pflags)
1867{
0fd73091
CB
1868 char *p, *s;
1869 char *saveptr = NULL;
d840039e
YT
1870
1871 if (!mntopts)
1872 return 0;
1873
1874 s = strdup(mntopts);
1875 if (!s) {
1876 SYSERROR("Failed to allocate memory");
1877 return -ENOMEM;
1878 }
1879
0fd73091
CB
1880 *pflags = 0L;
1881 for (; (p = strtok_r(s, ",", &saveptr)); s = NULL)
d840039e 1882 parse_propagationopt(p, pflags);
d840039e 1883 free(s);
0fd73091 1884
d840039e
YT
1885 return 0;
1886}
1887
6fd5e769
SH
1888static void null_endofword(char *word)
1889{
1890 while (*word && *word != ' ' && *word != '\t')
1891 word++;
1892 *word = '\0';
1893}
1894
0fd73091 1895/* skip @nfields spaces in @src */
6fd5e769
SH
1896static char *get_field(char *src, int nfields)
1897{
6fd5e769 1898 int i;
0fd73091 1899 char *p = src;
6fd5e769
SH
1900
1901 for (i = 0; i < nfields; i++) {
1902 while (*p && *p != ' ' && *p != '\t')
1903 p++;
0fd73091 1904
6fd5e769
SH
1905 if (!*p)
1906 break;
0fd73091 1907
6fd5e769
SH
1908 p++;
1909 }
0fd73091 1910
6fd5e769
SH
1911 return p;
1912}
1913
911324ef
DL
1914static int mount_entry(const char *fsname, const char *target,
1915 const char *fstype, unsigned long mountflags,
d840039e
YT
1916 unsigned long pflags, const char *data, bool optional,
1917 bool dev, bool relative, const char *rootfs)
911324ef 1918{
0ac4b28a 1919 int ret;
181437fd
YT
1920 char srcbuf[MAXPATHLEN];
1921 const char *srcpath = fsname;
614305f3 1922#ifdef HAVE_STATVFS
2938f7c8 1923 struct statvfs sb;
614305f3 1924#endif
2938f7c8 1925
181437fd
YT
1926 if (relative) {
1927 ret = snprintf(srcbuf, MAXPATHLEN, "%s/%s", rootfs ? rootfs : "/", fsname ? fsname : "");
1928 if (ret < 0 || ret >= MAXPATHLEN) {
1929 ERROR("source path is too long");
1930 return -1;
1931 }
1932 srcpath = srcbuf;
1933 }
1934
1935 ret = safe_mount(srcpath, target, fstype, mountflags & ~MS_REMOUNT, data,
0ac4b28a
CB
1936 rootfs);
1937 if (ret < 0) {
1fc64d22 1938 if (optional) {
0fd73091
CB
1939 INFO("%s - Failed to mount \"%s\" on \"%s\" "
1940 "(optional)", strerror(errno),
1941 srcpath ? srcpath : "(null)", target);
1fc64d22
SG
1942 return 0;
1943 }
0ac4b28a 1944
0103eb53 1945 SYSERROR("Failed to mount \"%s\" on \"%s\"",
181437fd 1946 srcpath ? srcpath : "(null)", target);
0ac4b28a 1947 return -1;
911324ef
DL
1948 }
1949
1950 if ((mountflags & MS_REMOUNT) || (mountflags & MS_BIND)) {
7c5b6e7c 1951 unsigned long rqd_flags = 0;
0ac4b28a
CB
1952
1953 DEBUG("Remounting \"%s\" on \"%s\" to respect bind or remount "
181437fd 1954 "options", srcpath ? srcpath : "(none)", target ? target : "(none)");
0ac4b28a 1955
7c5b6e7c
AS
1956 if (mountflags & MS_RDONLY)
1957 rqd_flags |= MS_RDONLY;
614305f3 1958#ifdef HAVE_STATVFS
181437fd 1959 if (srcpath && statvfs(srcpath, &sb) == 0) {
7c5b6e7c 1960 unsigned long required_flags = rqd_flags;
0ac4b28a 1961
2938f7c8
SH
1962 if (sb.f_flag & MS_NOSUID)
1963 required_flags |= MS_NOSUID;
0ac4b28a 1964
ae7a770e 1965 if (sb.f_flag & MS_NODEV && !dev)
2938f7c8 1966 required_flags |= MS_NODEV;
0ac4b28a 1967
2938f7c8
SH
1968 if (sb.f_flag & MS_RDONLY)
1969 required_flags |= MS_RDONLY;
0ac4b28a 1970
2938f7c8
SH
1971 if (sb.f_flag & MS_NOEXEC)
1972 required_flags |= MS_NOEXEC;
0ac4b28a
CB
1973
1974 DEBUG("Flags for \"%s\" were %lu, required extra flags "
181437fd 1975 "are %lu", srcpath, sb.f_flag, required_flags);
0ac4b28a
CB
1976
1977 /* If this was a bind mount request, and required_flags
2938f7c8 1978 * does not have any flags which are not already in
0ac4b28a 1979 * mountflags, then skip the remount.
2938f7c8
SH
1980 */
1981 if (!(mountflags & MS_REMOUNT)) {
0ac4b28a
CB
1982 if (!(required_flags & ~mountflags) &&
1983 rqd_flags == 0) {
1984 DEBUG("Mountflags already were %lu, "
1985 "skipping remount", mountflags);
2938f7c8
SH
1986 goto skipremount;
1987 }
1988 }
0ac4b28a 1989
2938f7c8 1990 mountflags |= required_flags;
6fd5e769 1991 }
614305f3 1992#endif
911324ef 1993
181437fd 1994 ret = mount(srcpath, target, fstype, mountflags | MS_REMOUNT, data);
0ac4b28a 1995 if (ret < 0) {
1fc64d22 1996 if (optional) {
0ac4b28a 1997 INFO("Failed to mount \"%s\" on \"%s\" "
0103eb53 1998 "(optional): %s",
181437fd 1999 srcpath ? srcpath : "(null)", target,
0ac4b28a 2000 strerror(errno));
1fc64d22
SG
2001 return 0;
2002 }
0ac4b28a 2003
0103eb53 2004 SYSERROR("Failed to mount \"%s\" on \"%s\"",
181437fd 2005 srcpath ? srcpath : "(null)", target);
0ac4b28a 2006 return -1;
911324ef
DL
2007 }
2008 }
2009
d840039e
YT
2010 if (pflags) {
2011 ret = mount(NULL, target, NULL, pflags, NULL);
2012 if (ret < 0) {
2013 if (optional) {
2014 INFO("%s - Failed to change mount propagation "
2015 "for \"%s\" (optional)", strerror(errno), target);
2016 return 0;
2017 } else {
2018 SYSERROR("Failed to change mount propagation "
2019 "for \"%s\" (optional)", target);
2020 return -1;
2021 }
2022 }
2023 DEBUG("Changed mount propagation for \"%s\"", target);
2024 }
2025
2026
614305f3 2027#ifdef HAVE_STATVFS
6fd5e769 2028skipremount:
614305f3 2029#endif
0103eb53 2030 DEBUG("Mounted \"%s\" on \"%s\" with filesystem type \"%s\"",
181437fd 2031 srcpath ? srcpath : "(null)", target, fstype);
911324ef
DL
2032
2033 return 0;
2034}
2035
c5e30de4 2036/* Remove "optional", "create=dir", and "create=file" from mntopt */
4e4ca161
SH
2037static void cull_mntent_opt(struct mntent *mntent)
2038{
2039 int i;
0fd73091
CB
2040 char *list[] = {
2041 "create=dir",
2042 "create=file",
2043 "optional",
2044 "relative",
2045 NULL
2046 };
c5e30de4
CB
2047
2048 for (i = 0; list[i]; i++) {
2049 char *p, *p2;
2050
2051 p = strstr(mntent->mnt_opts, list[i]);
2052 if (!p)
4e4ca161 2053 continue;
c5e30de4 2054
4e4ca161
SH
2055 p2 = strchr(p, ',');
2056 if (!p2) {
2057 /* no more mntopts, so just chop it here */
2058 *p = '\0';
2059 continue;
2060 }
c5e30de4
CB
2061
2062 memmove(p, p2 + 1, strlen(p2 + 1) + 1);
4e4ca161
SH
2063 }
2064}
2065
4d5b72a1 2066static int mount_entry_create_dir_file(const struct mntent *mntent,
749f98d9
CB
2067 const char *path,
2068 const struct lxc_rootfs *rootfs,
0fd73091 2069 const char *lxc_name, const char *lxc_path)
0ad19a3f 2070{
12e6ab5d
CB
2071 int fd, ret;
2072 char *p1, *p2;
911324ef 2073
12e6ab5d 2074 if (strncmp(mntent->mnt_type, "overlay", 7) == 0) {
749f98d9 2075 ret = ovl_mkdir(mntent, rootfs, lxc_name, lxc_path);
12e6ab5d
CB
2076 if (ret < 0)
2077 return -1;
2078 }
6e46cc0d 2079
34cfffb3 2080 if (hasmntopt(mntent, "create=dir")) {
749f98d9
CB
2081 ret = mkdir_p(path, 0755);
2082 if (ret < 0 && errno != EEXIST) {
2083 SYSERROR("Failed to create directory \"%s\"", path);
2084 return -1;
34cfffb3
SG
2085 }
2086 }
2087
0fd73091
CB
2088 if (!hasmntopt(mntent, "create=file"))
2089 return 0;
749f98d9 2090
0fd73091
CB
2091 ret = access(path, F_OK);
2092 if (ret == 0)
2093 return 0;
749f98d9 2094
0fd73091
CB
2095 p1 = strdup(path);
2096 if (!p1)
2097 return -1;
749f98d9 2098
0fd73091 2099 p2 = dirname(p1);
749f98d9 2100
0fd73091
CB
2101 ret = mkdir_p(p2, 0755);
2102 free(p1);
2103 if (ret < 0 && errno != EEXIST) {
2104 SYSERROR("Failed to create directory \"%s\"", path);
2105 return -1;
34cfffb3 2106 }
749f98d9 2107
0fd73091
CB
2108 fd = open(path, O_CREAT, 0644);
2109 if (fd < 0)
2110 return -1;
2111 close(fd);
2112
749f98d9 2113 return 0;
4d5b72a1
NC
2114}
2115
ec50007f
CB
2116/* rootfs, lxc_name, and lxc_path can be NULL when the container is created
2117 * without a rootfs. */
db4aba38 2118static inline int mount_entry_on_generic(struct mntent *mntent,
d8b712bc
CB
2119 const char *path,
2120 const struct lxc_rootfs *rootfs,
2121 const char *lxc_name,
2122 const char *lxc_path)
4d5b72a1 2123{
d8b712bc 2124 int ret;
949d0338 2125 unsigned long mntflags;
4d5b72a1 2126 char *mntdata;
181437fd 2127 bool dev, optional, relative;
949d0338 2128 unsigned long pflags = 0;
ec50007f 2129 char *rootfs_path = NULL;
d8b712bc
CB
2130
2131 optional = hasmntopt(mntent, "optional") != NULL;
2132 dev = hasmntopt(mntent, "dev") != NULL;
181437fd 2133 relative = hasmntopt(mntent, "relative") != NULL;
d8b712bc 2134
ec50007f
CB
2135 if (rootfs && rootfs->path)
2136 rootfs_path = rootfs->mount;
2137
d8b712bc
CB
2138 ret = mount_entry_create_dir_file(mntent, path, rootfs, lxc_name,
2139 lxc_path);
2140 if (ret < 0) {
2141 if (optional)
2142 return 0;
608e3567 2143
d8b712bc
CB
2144 return -1;
2145 }
4e4ca161
SH
2146 cull_mntent_opt(mntent);
2147
d840039e
YT
2148 ret = parse_propagationopts(mntent->mnt_opts, &pflags);
2149 if (ret < 0)
2150 return -1;
2151
d8b712bc
CB
2152 ret = parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata);
2153 if (ret < 0)
a17b1e65 2154 return -1;
a17b1e65 2155
6e46cc0d 2156 ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type, mntflags,
d840039e 2157 pflags, mntdata, optional, dev, relative, rootfs_path);
68c152ef 2158
911324ef 2159 free(mntdata);
911324ef
DL
2160 return ret;
2161}
2162
db4aba38
NC
2163static inline int mount_entry_on_systemfs(struct mntent *mntent)
2164{
1433c9f9 2165 int ret;
07667a6a 2166 char path[MAXPATHLEN];
1433c9f9
CB
2167
2168 /* For containers created without a rootfs all mounts are treated as
07667a6a
CB
2169 * absolute paths starting at / on the host.
2170 */
1433c9f9
CB
2171 if (mntent->mnt_dir[0] != '/')
2172 ret = snprintf(path, sizeof(path), "/%s", mntent->mnt_dir);
2173 else
2174 ret = snprintf(path, sizeof(path), "%s", mntent->mnt_dir);
07667a6a 2175 if (ret < 0 || ret >= sizeof(path))
1433c9f9 2176 return -1;
1433c9f9
CB
2177
2178 return mount_entry_on_generic(mntent, path, NULL, NULL, NULL);
db4aba38
NC
2179}
2180
4e4ca161 2181static int mount_entry_on_absolute_rootfs(struct mntent *mntent,
80a881b2 2182 const struct lxc_rootfs *rootfs,
0a2dddd4
CB
2183 const char *lxc_name,
2184 const char *lxc_path)
911324ef 2185{
bdd2b34c 2186 int offset;
013bd428 2187 char *aux;
67e571de 2188 const char *lxcpath;
bdd2b34c
CB
2189 char path[MAXPATHLEN];
2190 int ret = 0;
0ad19a3f 2191
593e8478 2192 lxcpath = lxc_global_config_value("lxc.lxcpath");
bdd2b34c 2193 if (!lxcpath)
2a59a681 2194 return -1;
2a59a681 2195
bdd2b34c
CB
2196 /* If rootfs->path is a blockdev path, allow container fstab to use
2197 * <lxcpath>/<name>/rootfs" as the target prefix.
2198 */
2199 ret = snprintf(path, MAXPATHLEN, "%s/%s/rootfs", lxcpath, lxc_name);
2200 if (ret < 0 || ret >= MAXPATHLEN)
80a881b2
SH
2201 goto skipvarlib;
2202
2203 aux = strstr(mntent->mnt_dir, path);
2204 if (aux) {
2205 offset = strlen(path);
2206 goto skipabs;
2207 }
2208
2209skipvarlib:
013bd428
DL
2210 aux = strstr(mntent->mnt_dir, rootfs->path);
2211 if (!aux) {
bdd2b34c 2212 WARN("Ignoring mount point \"%s\"", mntent->mnt_dir);
db4aba38 2213 return ret;
013bd428 2214 }
80a881b2
SH
2215 offset = strlen(rootfs->path);
2216
2217skipabs:
bdd2b34c
CB
2218 ret = snprintf(path, MAXPATHLEN, "%s/%s", rootfs->mount, aux + offset);
2219 if (ret < 0 || ret >= MAXPATHLEN)
a17b1e65 2220 return -1;
a17b1e65 2221
0a2dddd4 2222 return mount_entry_on_generic(mntent, path, rootfs, lxc_name, lxc_path);
911324ef 2223}
d330fe7b 2224
4e4ca161 2225static int mount_entry_on_relative_rootfs(struct mntent *mntent,
0a2dddd4
CB
2226 const struct lxc_rootfs *rootfs,
2227 const char *lxc_name,
2228 const char *lxc_path)
911324ef 2229{
911324ef 2230 int ret;
0fd73091 2231 char path[MAXPATHLEN];
d330fe7b 2232
34cfffb3 2233 /* relative to root mount point */
6e46cc0d 2234 ret = snprintf(path, sizeof(path), "%s/%s", rootfs->mount, mntent->mnt_dir);
0fd73091 2235 if (ret < 0 || (size_t)ret >= sizeof(path))
9ba8130c 2236 return -1;
911324ef 2237
0a2dddd4 2238 return mount_entry_on_generic(mntent, path, rootfs, lxc_name, lxc_path);
911324ef
DL
2239}
2240
06749971
CB
2241static int mount_file_entries(const struct lxc_conf *conf,
2242 const struct lxc_rootfs *rootfs, FILE *file,
1ae3c19f 2243 const char *lxc_name, const char *lxc_path)
911324ef 2244{
aaf901be 2245 char buf[4096];
0fd73091 2246 struct mntent mntent;
911324ef 2247 int ret = -1;
e76b8764 2248
aaf901be 2249 while (getmntent_r(file, &mntent, buf, sizeof(buf))) {
1ae3c19f
CB
2250 if (!rootfs->path)
2251 ret = mount_entry_on_systemfs(&mntent);
2252 else if (mntent.mnt_dir[0] != '/')
2253 ret = mount_entry_on_relative_rootfs(&mntent, rootfs,
2254 lxc_name, lxc_path);
2255 else
2256 ret = mount_entry_on_absolute_rootfs(&mntent, rootfs,
2257 lxc_name, lxc_path);
2258 if (ret < 0)
2259 return -1;
0ad19a3f 2260 }
2261 ret = 0;
cd54d859 2262
0fd73091 2263 INFO("Finished setting up mounts");
e7938e9e
MN
2264 return ret;
2265}
2266
06749971
CB
2267static int setup_mount(const struct lxc_conf *conf,
2268 const struct lxc_rootfs *rootfs, const char *fstab,
42dff448 2269 const char *lxc_name, const char *lxc_path)
e7938e9e 2270{
42dff448 2271 FILE *f;
e7938e9e
MN
2272 int ret;
2273
2274 if (!fstab)
2275 return 0;
2276
42dff448
CB
2277 f = setmntent(fstab, "r");
2278 if (!f) {
2279 SYSERROR("Failed to open \"%s\"", fstab);
e7938e9e
MN
2280 return -1;
2281 }
2282
06749971 2283 ret = mount_file_entries(conf, rootfs, f, lxc_name, lxc_path);
42dff448
CB
2284 if (ret < 0)
2285 ERROR("Failed to set up mount entries");
e7938e9e 2286
42dff448 2287 endmntent(f);
0ad19a3f 2288 return ret;
2289}
2290
5ef5c9a3 2291FILE *make_anonymous_mount_file(struct lxc_list *mount)
e7938e9e 2292{
5ef5c9a3 2293 int ret;
e7938e9e 2294 char *mount_entry;
5ef5c9a3 2295 struct lxc_list *iterator;
5ef5c9a3
CB
2296 int fd = -1;
2297
0fd73091 2298 fd = memfd_create(".lxc_mount_file", MFD_CLOEXEC);
5ef5c9a3 2299 if (fd < 0) {
a324e7eb
CB
2300 char template[] = P_tmpdir "/.lxc_mount_file_XXXXXX";
2301
5ef5c9a3
CB
2302 if (errno != ENOSYS)
2303 return NULL;
a324e7eb
CB
2304
2305 fd = lxc_make_tmpfile(template, true);
0fd73091
CB
2306 if (fd < 0) {
2307 SYSERROR("Could not create temporary mount file");
2308 return NULL;
2309 }
2310
6bd04140 2311 TRACE("Created temporary mount file");
5ef5c9a3 2312 }
0fd73091
CB
2313 if (fd < 0) {
2314 SYSERROR("Could not create temporary mount file");
9fc7f8c0 2315 return NULL;
e7938e9e
MN
2316 }
2317
0fd73091
CB
2318 lxc_list_for_each (iterator, mount) {
2319 size_t len;
2320
e7938e9e 2321 mount_entry = iterator->elem;
0fd73091 2322 len = strlen(mount_entry);
5ef5c9a3 2323
0fd73091
CB
2324 ret = write(fd, mount_entry, len);
2325 if (ret != len)
2326 goto on_error;
2327
2328 ret = write(fd, "\n", 1);
2329 if (ret != 1)
2330 goto on_error;
e7938e9e
MN
2331 }
2332
0fd73091
CB
2333 ret = lseek(fd, 0, SEEK_SET);
2334 if (ret < 0)
2335 goto on_error;
2336
2337 return fdopen(fd, "r+");
2338
2339on_error:
2340 SYSERROR("Failed to write mount entry to temporary mount file");
2341 close(fd);
2342 return NULL;
9fc7f8c0
TA
2343}
2344
06749971
CB
2345static int setup_mount_entries(const struct lxc_conf *conf,
2346 const struct lxc_rootfs *rootfs,
5ef5c9a3
CB
2347 struct lxc_list *mount, const char *lxc_name,
2348 const char *lxc_path)
9fc7f8c0 2349{
9fc7f8c0 2350 int ret;
0fd73091 2351 FILE *f;
9fc7f8c0 2352
19b5d755
CB
2353 f = make_anonymous_mount_file(mount);
2354 if (!f)
9fc7f8c0 2355 return -1;
e7938e9e 2356
06749971 2357 ret = mount_file_entries(conf, rootfs, f, lxc_name, lxc_path);
19b5d755 2358 fclose(f);
0fd73091 2359
e7938e9e
MN
2360 return ret;
2361}
2362
bab88e68
CS
2363static int parse_cap(const char *cap)
2364{
84760c11 2365 size_t i;
2366 int capid = -1;
0fd73091
CB
2367 size_t end = sizeof(caps_opt) / sizeof(caps_opt[0]);
2368 char *ptr = NULL;
bab88e68 2369
0fd73091 2370 if (strcmp(cap, "none") == 0)
7035407c
DE
2371 return -2;
2372
8560cd36 2373 for (i = 0; i < end; i++) {
bab88e68
CS
2374 if (strcmp(cap, caps_opt[i].name))
2375 continue;
2376
2377 capid = caps_opt[i].value;
2378 break;
2379 }
2380
2381 if (capid < 0) {
0fd73091
CB
2382 /* Try to see if it's numeric, so the user may specify
2383 * capabilities that the running kernel knows about but we
2384 * don't
2385 */
bab88e68
CS
2386 errno = 0;
2387 capid = strtol(cap, &ptr, 10);
2388 if (!ptr || *ptr != '\0' || errno != 0)
2389 /* not a valid number */
2390 capid = -1;
2391 else if (capid > lxc_caps_last_cap())
2392 /* we have a number but it's not a valid
2393 * capability */
2394 capid = -1;
2395 }
2396
2397 return capid;
2398}
2399
0769b82a
CS
2400int in_caplist(int cap, struct lxc_list *caps)
2401{
0769b82a 2402 int capid;
0fd73091 2403 struct lxc_list *iterator;
0769b82a 2404
0fd73091 2405 lxc_list_for_each (iterator, caps) {
0769b82a
CS
2406 capid = parse_cap(iterator->elem);
2407 if (capid == cap)
2408 return 1;
2409 }
2410
2411 return 0;
2412}
2413
81810dd1
DL
2414static int setup_caps(struct lxc_list *caps)
2415{
bab88e68 2416 int capid;
0fd73091
CB
2417 char *drop_entry;
2418 struct lxc_list *iterator;
81810dd1 2419
0fd73091
CB
2420 lxc_list_for_each (iterator, caps) {
2421 int ret;
81810dd1
DL
2422
2423 drop_entry = iterator->elem;
2424
bab88e68 2425 capid = parse_cap(drop_entry);
0fd73091 2426 if (capid < 0) {
1e11be34
DL
2427 ERROR("unknown capability %s", drop_entry);
2428 return -1;
81810dd1
DL
2429 }
2430
0fd73091
CB
2431 ret = prctl(PR_CAPBSET_DROP, capid, 0, 0, 0);
2432 if (ret < 0) {
2433 SYSERROR("Failed to remove %s capability", drop_entry);
3ec1648d
SH
2434 return -1;
2435 }
0fd73091 2436 DEBUG("Dropped %s (%d) capability", drop_entry, capid);
81810dd1
DL
2437 }
2438
0fd73091 2439 DEBUG("Capabilities have been setup");
1fb86a7c
SH
2440 return 0;
2441}
2442
2443static int dropcaps_except(struct lxc_list *caps)
2444{
0fd73091 2445 int i, capid, numcaps;
1fb86a7c 2446 char *keep_entry;
0fd73091 2447 struct lxc_list *iterator;
1fb86a7c 2448
0fd73091 2449 numcaps = lxc_caps_last_cap() + 1;
2caf9a97
SH
2450 if (numcaps <= 0 || numcaps > 200)
2451 return -1;
0fd73091 2452 TRACE("Found %d capabilities", numcaps);
2caf9a97 2453
1a0e70ac 2454 /* caplist[i] is 1 if we keep capability i */
1fb86a7c
SH
2455 int *caplist = alloca(numcaps * sizeof(int));
2456 memset(caplist, 0, numcaps * sizeof(int));
2457
0fd73091 2458 lxc_list_for_each (iterator, caps) {
1fb86a7c
SH
2459 keep_entry = iterator->elem;
2460
bab88e68 2461 capid = parse_cap(keep_entry);
7035407c
DE
2462 if (capid == -2)
2463 continue;
2464
0fd73091
CB
2465 if (capid < 0) {
2466 ERROR("Unknown capability %s", keep_entry);
1fb86a7c
SH
2467 return -1;
2468 }
2469
0fd73091 2470 DEBUG("Keep capability %s (%d)", keep_entry, capid);
1fb86a7c
SH
2471 caplist[capid] = 1;
2472 }
0fd73091
CB
2473
2474 for (i = 0; i < numcaps; i++) {
2475 int ret;
2476
1fb86a7c
SH
2477 if (caplist[i])
2478 continue;
0fd73091
CB
2479
2480 ret = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
2481 if (ret < 0) {
2482 SYSERROR("Failed to remove capability %d", i);
3ec1648d
SH
2483 return -1;
2484 }
1fb86a7c
SH
2485 }
2486
0fd73091 2487 DEBUG("Capabilities have been setup");
81810dd1
DL
2488 return 0;
2489}
2490
0fd73091
CB
2491static int parse_resource(const char *res)
2492{
2493 int ret;
c6d09e15
WB
2494 size_t i;
2495 int resid = -1;
2496
0fd73091 2497 for (i = 0; i < sizeof(limit_opt) / sizeof(limit_opt[0]); ++i)
c6d09e15
WB
2498 if (strcmp(res, limit_opt[i].name) == 0)
2499 return limit_opt[i].value;
c6d09e15 2500
0fd73091 2501 /* Try to see if it's numeric, so the user may specify
c6d09e15 2502 * resources that the running kernel knows about but
0fd73091
CB
2503 * we don't.
2504 */
2505 ret = lxc_safe_int(res, &resid);
2506 if (ret < 0)
2507 return -1;
2508
2509 return resid;
c6d09e15
WB
2510}
2511
0fd73091
CB
2512int setup_resource_limits(struct lxc_list *limits, pid_t pid)
2513{
2514 int resid;
c6d09e15
WB
2515 struct lxc_list *it;
2516 struct lxc_limit *lim;
c6d09e15 2517
0fd73091 2518 lxc_list_for_each (it, limits) {
c6d09e15
WB
2519 lim = it->elem;
2520
2521 resid = parse_resource(lim->resource);
2522 if (resid < 0) {
0fd73091 2523 ERROR("Unknown resource %s", lim->resource);
c6d09e15
WB
2524 return -1;
2525 }
2526
f48b5fd8 2527#if HAVE_PRLIMIT || HAVE_PRLIMIT64
c6d09e15 2528 if (prlimit(pid, resid, &lim->limit, NULL) != 0) {
0fd73091
CB
2529 ERROR("Failed to set limit %s: %s", lim->resource,
2530 strerror(errno));
c6d09e15
WB
2531 return -1;
2532 }
f48b5fd8
FF
2533#else
2534 ERROR("Cannot set limit %s as prlimit is missing", lim->resource);
2535 return -1;
2536#endif
c6d09e15 2537 }
0fd73091 2538
c6d09e15
WB
2539 return 0;
2540}
2541
7edd0540
L
2542int setup_sysctl_parameters(struct lxc_list *sysctls)
2543{
2544 struct lxc_list *it;
2545 struct lxc_sysctl *elem;
0fd73091 2546 int ret = 0;
7edd0540
L
2547 char *tmp = NULL;
2548 char filename[MAXPATHLEN] = {0};
7edd0540 2549
0fd73091 2550 lxc_list_for_each (it, sysctls) {
7edd0540
L
2551 elem = it->elem;
2552 tmp = lxc_string_replace(".", "/", elem->key);
2553 if (!tmp) {
2554 ERROR("Failed to replace key %s", elem->key);
2555 return -1;
2556 }
2557
2558 ret = snprintf(filename, sizeof(filename), "/proc/sys/%s", tmp);
2559 free(tmp);
2560 if (ret < 0 || (size_t)ret >= sizeof(filename)) {
2561 ERROR("Error setting up sysctl parameters path");
2562 return -1;
2563 }
2564
0fd73091
CB
2565 ret = lxc_write_to_file(filename, elem->value,
2566 strlen(elem->value), false);
7edd0540 2567 if (ret < 0) {
0fd73091
CB
2568 ERROR("Failed to setup sysctl parameters %s to %s",
2569 elem->key, elem->value);
7edd0540
L
2570 return -1;
2571 }
2572 }
0fd73091 2573
7edd0540
L
2574 return 0;
2575}
2576
61d7a733
YT
2577int setup_proc_filesystem(struct lxc_list *procs, pid_t pid)
2578{
2579 struct lxc_list *it;
2580 struct lxc_proc *elem;
0fd73091 2581 int ret = 0;
61d7a733
YT
2582 char *tmp = NULL;
2583 char filename[MAXPATHLEN] = {0};
61d7a733 2584
0fd73091 2585 lxc_list_for_each (it, procs) {
61d7a733
YT
2586 elem = it->elem;
2587 tmp = lxc_string_replace(".", "/", elem->filename);
2588 if (!tmp) {
2589 ERROR("Failed to replace key %s", elem->filename);
2590 return -1;
2591 }
2592
2593 ret = snprintf(filename, sizeof(filename), "/proc/%d/%s", pid, tmp);
2594 free(tmp);
2595 if (ret < 0 || (size_t)ret >= sizeof(filename)) {
2596 ERROR("Error setting up proc filesystem path");
2597 return -1;
2598 }
2599
0fd73091
CB
2600 ret = lxc_write_to_file(filename, elem->value,
2601 strlen(elem->value), false);
61d7a733 2602 if (ret < 0) {
0fd73091
CB
2603 ERROR("Failed to setup proc filesystem %s to %s",
2604 elem->filename, elem->value);
61d7a733
YT
2605 return -1;
2606 }
2607 }
0fd73091 2608
61d7a733
YT
2609 return 0;
2610}
2611
ae9242c8
SH
2612static char *default_rootfs_mount = LXCROOTFSMOUNT;
2613
7b379ab3 2614struct lxc_conf *lxc_conf_init(void)
089cd8b8 2615{
26ddeedd 2616 int i;
0fd73091 2617 struct lxc_conf *new;
7b379ab3 2618
13277ec4 2619 new = malloc(sizeof(*new));
0fd73091 2620 if (!new)
7b379ab3 2621 return NULL;
7b379ab3
MN
2622 memset(new, 0, sizeof(*new));
2623
4b73005c 2624 new->loglevel = LXC_LOG_LEVEL_NOTSET;
cccc74b5 2625 new->personality = -1;
124fa0a8 2626 new->autodev = 1;
3a784510 2627 new->console.buffer_size = 0;
596a818d
DE
2628 new->console.log_path = NULL;
2629 new->console.log_fd = -1;
861813e5 2630 new->console.log_size = 0;
28a4b0e5 2631 new->console.path = NULL;
63376d7d 2632 new->console.peer = -1;
fb87aa6a
CB
2633 new->console.proxy.busy = -1;
2634 new->console.proxy.master = -1;
2635 new->console.proxy.slave = -1;
63376d7d
DL
2636 new->console.master = -1;
2637 new->console.slave = -1;
2638 new->console.name[0] = '\0';
732375f5 2639 memset(&new->console.ringbuf, 0, sizeof(struct lxc_ringbuf));
d2e30e99 2640 new->maincmd_fd = -1;
76a26f55 2641 new->nbd_idx = -1;
54c30e29 2642 new->rootfs.mount = strdup(default_rootfs_mount);
53f3f048 2643 if (!new->rootfs.mount) {
53f3f048
SH
2644 free(new);
2645 return NULL;
2646 }
858377e4 2647 new->logfd = -1;
7b379ab3 2648 lxc_list_init(&new->cgroup);
54860ed0 2649 lxc_list_init(&new->cgroup2);
7b379ab3
MN
2650 lxc_list_init(&new->network);
2651 lxc_list_init(&new->mount_list);
81810dd1 2652 lxc_list_init(&new->caps);
1fb86a7c 2653 lxc_list_init(&new->keepcaps);
f6d3e3e4 2654 lxc_list_init(&new->id_map);
46ad64ab
CB
2655 new->root_nsuid_map = NULL;
2656 new->root_nsgid_map = NULL;
f979ac15 2657 lxc_list_init(&new->includes);
4184c3e1 2658 lxc_list_init(&new->aliens);
7c661726 2659 lxc_list_init(&new->environment);
c6d09e15 2660 lxc_list_init(&new->limits);
7edd0540 2661 lxc_list_init(&new->sysctls);
61d7a733 2662 lxc_list_init(&new->procs);
44ae0fb6 2663 new->hooks_version = 0;
28d9e29e 2664 for (i = 0; i < NUM_LXC_HOOKS; i++)
26ddeedd 2665 lxc_list_init(&new->hooks[i]);
ee1e7aa0 2666 lxc_list_init(&new->groups);
d39b10eb 2667 lxc_list_init(&new->state_clients);
fe4de9a6
DE
2668 new->lsm_aa_profile = NULL;
2669 new->lsm_se_context = NULL;
5112cd70 2670 new->tmp_umount_proc = 0;
7b379ab3 2671
72bb04e4
PT
2672 /* if running in a new user namespace, init and COMMAND
2673 * default to running as UID/GID 0 when using lxc-execute */
2674 new->init_uid = 0;
2675 new->init_gid = 0;
43654d34 2676 memset(&new->cgroup_meta, 0, sizeof(struct lxc_cgroup));
b074bbf1 2677 memset(&new->ns_share, 0, sizeof(char *) * LXC_NS_MAX);
72bb04e4 2678
7b379ab3 2679 return new;
089cd8b8
DL
2680}
2681
344c9d81 2682int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf,
a19b974f 2683 size_t buf_size)
f6d3e3e4 2684{
29053180 2685 int fd, ret;
0fd73091 2686 char path[MAXPATHLEN];
f6d3e3e4 2687
a19b974f
CB
2688 if (geteuid() != 0 && idtype == ID_TYPE_GID) {
2689 size_t buflen;
2690
2691 ret = snprintf(path, MAXPATHLEN, "/proc/%d/setgroups", pid);
0fd73091 2692 if (ret < 0 || ret >= MAXPATHLEN)
a19b974f 2693 return -E2BIG;
a19b974f
CB
2694
2695 fd = open(path, O_WRONLY);
2696 if (fd < 0 && errno != ENOENT) {
2697 SYSERROR("Failed to open \"%s\"", path);
2698 return -1;
2699 }
2700
2388737b
CB
2701 if (fd >= 0) {
2702 buflen = sizeof("deny\n") - 1;
2703 errno = 0;
2704 ret = lxc_write_nointr(fd, "deny\n", buflen);
2705 if (ret != buflen) {
0fd73091
CB
2706 SYSERROR("Failed to write \"deny\" to "
2707 "\"/proc/%d/setgroups\"", pid);
2388737b
CB
2708 close(fd);
2709 return -1;
2710 }
a19b974f 2711 close(fd);
a19b974f 2712 }
a19b974f
CB
2713 }
2714
29053180
CB
2715 ret = snprintf(path, MAXPATHLEN, "/proc/%d/%cid_map", pid,
2716 idtype == ID_TYPE_UID ? 'u' : 'g');
0fd73091 2717 if (ret < 0 || ret >= MAXPATHLEN)
f6d3e3e4 2718 return -E2BIG;
29053180
CB
2719
2720 fd = open(path, O_WRONLY);
2721 if (fd < 0) {
a19b974f 2722 SYSERROR("Failed to open \"%s\"", path);
29053180 2723 return -1;
f6d3e3e4 2724 }
29053180
CB
2725
2726 errno = 0;
2727 ret = lxc_write_nointr(fd, buf, buf_size);
2728 if (ret != buf_size) {
a19b974f 2729 SYSERROR("Failed to write %cid mapping to \"%s\"",
29053180
CB
2730 idtype == ID_TYPE_UID ? 'u' : 'g', path);
2731 close(fd);
2732 return -1;
2733 }
2734 close(fd);
2735
2736 return 0;
f6d3e3e4
SH
2737}
2738
6e50e704
CB
2739/* Check whether a binary exist and has either CAP_SETUID, CAP_SETGID or both.
2740 *
2741 * @return 1 if functional binary was found
2742 * @return 0 if binary exists but is lacking privilege
2743 * @return -ENOENT if binary does not exist
2744 * @return -EINVAL if cap to check is neither CAP_SETUID nor CAP_SETGID
6e50e704 2745 */
df6a2945
CB
2746static int idmaptool_on_path_and_privileged(const char *binary, cap_value_t cap)
2747{
2748 char *path;
2749 int ret;
2750 struct stat st;
2751 int fret = 0;
2752
6e50e704
CB
2753 if (cap != CAP_SETUID && cap != CAP_SETGID)
2754 return -EINVAL;
2755
df6a2945
CB
2756 path = on_path(binary, NULL);
2757 if (!path)
2758 return -ENOENT;
2759
2760 ret = stat(path, &st);
2761 if (ret < 0) {
2762 fret = -errno;
2763 goto cleanup;
2764 }
2765
2766 /* Check if the binary is setuid. */
2767 if (st.st_mode & S_ISUID) {
0fd73091 2768 DEBUG("The binary \"%s\" does have the setuid bit set", path);
df6a2945
CB
2769 fret = 1;
2770 goto cleanup;
2771 }
2772
0fd73091 2773#if HAVE_LIBCAP && LIBCAP_SUPPORTS_FILE_CAPABILITIES
df6a2945
CB
2774 /* Check if it has the CAP_SETUID capability. */
2775 if ((cap & CAP_SETUID) &&
2776 lxc_file_cap_is_set(path, CAP_SETUID, CAP_EFFECTIVE) &&
2777 lxc_file_cap_is_set(path, CAP_SETUID, CAP_PERMITTED)) {
2778 DEBUG("The binary \"%s\" has CAP_SETUID in its CAP_EFFECTIVE "
0fd73091 2779 "and CAP_PERMITTED sets", path);
df6a2945
CB
2780 fret = 1;
2781 goto cleanup;
2782 }
2783
2784 /* Check if it has the CAP_SETGID capability. */
2785 if ((cap & CAP_SETGID) &&
2786 lxc_file_cap_is_set(path, CAP_SETGID, CAP_EFFECTIVE) &&
2787 lxc_file_cap_is_set(path, CAP_SETGID, CAP_PERMITTED)) {
2788 DEBUG("The binary \"%s\" has CAP_SETGID in its CAP_EFFECTIVE "
0fd73091 2789 "and CAP_PERMITTED sets", path);
df6a2945
CB
2790 fret = 1;
2791 goto cleanup;
2792 }
0fd73091 2793#else
69924fff
CB
2794 /* If we cannot check for file capabilities we need to give the benefit
2795 * of the doubt. Otherwise we might fail even though all the necessary
2796 * file capabilities are set.
2797 */
d6018f88 2798 DEBUG("Cannot check for file capabilites as full capability support is "
0fd73091 2799 "missing. Manual intervention needed");
d6018f88 2800 fret = 1;
0fd73091 2801#endif
df6a2945
CB
2802
2803cleanup:
2804 free(path);
2805 return fret;
2806}
2807
986ef930
CB
2808int lxc_map_ids_exec_wrapper(void *args)
2809{
2810 execl("/bin/sh", "sh", "-c", (char *)args, (char *)NULL);
2811 return -1;
2812}
2813
f6d3e3e4
SH
2814int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
2815{
0fd73091 2816 int fill, left;
986ef930 2817 char u_or_g;
4bc3b759 2818 char *pos;
986ef930 2819 char cmd_output[MAXPATHLEN];
0fd73091
CB
2820 struct id_map *map;
2821 struct lxc_list *iterator;
2822 enum idtype type;
986ef930
CB
2823 /* strlen("new@idmap") = 9
2824 * +
2825 * strlen(" ") = 1
2826 * +
2827 * LXC_NUMSTRLEN64
2828 * +
2829 * strlen(" ") = 1
2830 *
2831 * We add some additional space to make sure that we really have
2832 * LXC_IDMAPLEN bytes available for our the {g,u]id mapping.
2833 */
0fd73091 2834 int ret = 0, gidmap = 0, uidmap = 0;
986ef930 2835 char mapbuf[9 + 1 + LXC_NUMSTRLEN64 + 1 + LXC_IDMAPLEN] = {0};
0fd73091 2836 bool had_entry = false, use_shadow = false;
df6a2945
CB
2837
2838 /* If new{g,u}idmap exists, that is, if shadow is handing out subuid
2839 * ranges, then insist that root also reserve ranges in subuid. This
22038de5
SH
2840 * will protected it by preventing another user from being handed the
2841 * range by shadow.
2842 */
df6a2945 2843 uidmap = idmaptool_on_path_and_privileged("newuidmap", CAP_SETUID);
6e50e704
CB
2844 if (uidmap == -ENOENT)
2845 WARN("newuidmap binary is missing");
2846 else if (!uidmap)
2847 WARN("newuidmap is lacking necessary privileges");
2848
df6a2945 2849 gidmap = idmaptool_on_path_and_privileged("newgidmap", CAP_SETGID);
6e50e704
CB
2850 if (gidmap == -ENOENT)
2851 WARN("newgidmap binary is missing");
2852 else if (!gidmap)
2853 WARN("newgidmap is lacking necessary privileges");
2854
df6a2945 2855 if (uidmap > 0 && gidmap > 0) {
0fd73091 2856 DEBUG("Functional newuidmap and newgidmap binary found");
4bc3b759 2857 use_shadow = true;
df6a2945 2858 } else {
99d43365
CB
2859 /* In case unprivileged users run application containers via
2860 * execute() or a start*() there are valid cases where they may
2861 * only want to map their own {g,u}id. Let's not block them from
2862 * doing so by requiring geteuid() == 0.
2863 */
2864 DEBUG("No newuidmap and newgidmap binary found. Trying to "
0fd73091 2865 "write directly with euid %d", geteuid());
0e6e3a41 2866 }
251d0d2a 2867
986ef930
CB
2868 for (type = ID_TYPE_UID, u_or_g = 'u'; type <= ID_TYPE_GID;
2869 type++, u_or_g = 'g') {
2870 pos = mapbuf;
2871
0e6e3a41 2872 if (use_shadow)
986ef930 2873 pos += sprintf(mapbuf, "new%cidmap %d", u_or_g, pid);
4f7521b4 2874
cf3ef16d 2875 lxc_list_for_each(iterator, idmap) {
251d0d2a 2876 map = iterator->elem;
cf3ef16d
SH
2877 if (map->idtype != type)
2878 continue;
2879
4bc3b759
CB
2880 had_entry = true;
2881
986ef930 2882 left = LXC_IDMAPLEN - (pos - mapbuf);
d1838f34 2883 fill = snprintf(pos, left, "%s%lu %lu %lu%s",
4bc3b759
CB
2884 use_shadow ? " " : "", map->nsid,
2885 map->hostid, map->range,
0e6e3a41 2886 use_shadow ? "" : "\n");
a427e268
CB
2887 if (fill <= 0 || fill >= left) {
2888 /* The kernel only takes <= 4k for writes to
2889 * /proc/<pid>/{g,u}id_map
2890 */
2891 SYSERROR("Too many %cid mappings defined", u_or_g);
2892 return -1;
2893 }
4bc3b759 2894
cf3ef16d 2895 pos += fill;
251d0d2a 2896 }
cf3ef16d 2897 if (!had_entry)
4f7521b4 2898 continue;
cf3ef16d 2899
986ef930
CB
2900 /* Try to catch the ouput of new{g,u}idmap to make debugging
2901 * easier.
2902 */
2903 if (use_shadow) {
2904 ret = run_command(cmd_output, sizeof(cmd_output),
2905 lxc_map_ids_exec_wrapper,
2906 (void *)mapbuf);
2907 if (ret < 0) {
54fbbeb5
CB
2908 ERROR("new%cidmap failed to write mapping \"%s\": %s",
2909 u_or_g, cmd_output, mapbuf);
986ef930
CB
2910 return -1;
2911 }
54fbbeb5 2912 TRACE("new%cidmap wrote mapping \"%s\"", u_or_g, mapbuf);
d1838f34 2913 } else {
986ef930 2914 ret = write_id_mapping(type, pid, mapbuf, pos - mapbuf);
54fbbeb5 2915 if (ret < 0) {
da0f9977 2916 ERROR("Failed to write mapping: %s", mapbuf);
986ef930 2917 return -1;
54fbbeb5
CB
2918 }
2919 TRACE("Wrote mapping \"%s\"", mapbuf);
d1838f34 2920 }
986ef930
CB
2921
2922 memset(mapbuf, 0, sizeof(mapbuf));
f6d3e3e4 2923 }
251d0d2a 2924
986ef930 2925 return 0;
f6d3e3e4
SH
2926}
2927
0fd73091 2928/* Return the host uid/gid to which the container root is mapped in val.
0b3a6504 2929 * Return true if id was found, false otherwise.
cf3ef16d 2930 */
2a9a80cb 2931bool get_mapped_rootid(struct lxc_conf *conf, enum idtype idtype,
4160c3a0 2932 unsigned long *val)
cf3ef16d 2933{
4160c3a0 2934 unsigned nsid;
0fd73091
CB
2935 struct id_map *map;
2936 struct lxc_list *it;
4160c3a0
CB
2937
2938 if (idtype == ID_TYPE_UID)
2939 nsid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid;
2940 else
2941 nsid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid;
cf3ef16d 2942
0fd73091 2943 lxc_list_for_each (it, &conf->id_map) {
cf3ef16d 2944 map = it->elem;
7b50c609 2945 if (map->idtype != idtype)
cf3ef16d 2946 continue;
4160c3a0 2947 if (map->nsid != nsid)
cf3ef16d 2948 continue;
2a9a80cb
SH
2949 *val = map->hostid;
2950 return true;
cf3ef16d 2951 }
4160c3a0 2952
2a9a80cb 2953 return false;
cf3ef16d
SH
2954}
2955
2133f58c 2956int mapped_hostid(unsigned id, struct lxc_conf *conf, enum idtype idtype)
cf3ef16d 2957{
cf3ef16d 2958 struct id_map *map;
0fd73091
CB
2959 struct lxc_list *it;
2960
2961 lxc_list_for_each (it, &conf->id_map) {
cf3ef16d 2962 map = it->elem;
2133f58c 2963 if (map->idtype != idtype)
cf3ef16d 2964 continue;
0fd73091 2965
cf3ef16d 2966 if (id >= map->hostid && id < map->hostid + map->range)
57d116ab 2967 return (id - map->hostid) + map->nsid;
cf3ef16d 2968 }
0fd73091 2969
57d116ab 2970 return -1;
cf3ef16d
SH
2971}
2972
339efad9 2973int find_unmapped_nsid(struct lxc_conf *conf, enum idtype idtype)
cf3ef16d 2974{
cf3ef16d 2975 struct id_map *map;
0fd73091 2976 struct lxc_list *it;
2133f58c 2977 unsigned int freeid = 0;
0fd73091 2978
cf3ef16d 2979again:
0fd73091 2980 lxc_list_for_each (it, &conf->id_map) {
cf3ef16d 2981 map = it->elem;
2133f58c 2982 if (map->idtype != idtype)
cf3ef16d 2983 continue;
0fd73091 2984
cf3ef16d
SH
2985 if (freeid >= map->nsid && freeid < map->nsid + map->range) {
2986 freeid = map->nsid + map->range;
2987 goto again;
2988 }
2989 }
0fd73091 2990
cf3ef16d
SH
2991 return freeid;
2992}
2993
f4f52cb5
CB
2994int chown_mapped_root_exec_wrapper(void *args)
2995{
2996 execvp("lxc-usernsexec", args);
2997 return -1;
2998}
2999
0fd73091 3000/* chown_mapped_root: for an unprivileged user with uid/gid X to
7b50c609
TS
3001 * chown a dir to subuid/subgid Y, he needs to run chown as root
3002 * in a userns where nsid 0 is mapped to hostuid/hostgid Y, and
3003 * nsid Y is mapped to hostuid/hostgid X. That way, the container
3004 * root is privileged with respect to hostuid/hostgid X, allowing
3005 * him to do the chown.
f6d3e3e4 3006 */
41dc7155 3007int chown_mapped_root(const char *path, struct lxc_conf *conf)
f6d3e3e4 3008{
f4f52cb5 3009 uid_t rootuid, rootgid;
2a9a80cb 3010 unsigned long val;
f4f52cb5
CB
3011 int hostuid, hostgid, ret;
3012 struct stat sb;
3013 char map1[100], map2[100], map3[100], map4[100], map5[100];
3014 char ugid[100];
41dc7155 3015 const char *args1[] = {"lxc-usernsexec",
f4f52cb5
CB
3016 "-m", map1,
3017 "-m", map2,
3018 "-m", map3,
3019 "-m", map5,
3020 "--", "chown", ugid, path,
3021 NULL};
41dc7155 3022 const char *args2[] = {"lxc-usernsexec",
f4f52cb5
CB
3023 "-m", map1,
3024 "-m", map2,
3025 "-m", map3,
3026 "-m", map4,
3027 "-m", map5,
3028 "--", "chown", ugid, path,
3029 NULL};
3030 char cmd_output[MAXPATHLEN];
3031
3032 hostuid = geteuid();
3033 hostgid = getegid();
f6d3e3e4 3034
2a9a80cb 3035 if (!get_mapped_rootid(conf, ID_TYPE_UID, &val)) {
bc80f098 3036 ERROR("No uid mapping for container root");
c4d10a05 3037 return -1;
f6d3e3e4 3038 }
f4f52cb5 3039 rootuid = (uid_t)val;
0fd73091 3040
7b50c609 3041 if (!get_mapped_rootid(conf, ID_TYPE_GID, &val)) {
bc80f098 3042 ERROR("No gid mapping for container root");
7b50c609
TS
3043 return -1;
3044 }
f4f52cb5 3045 rootgid = (gid_t)val;
2a9a80cb 3046
f4f52cb5 3047 if (hostuid == 0) {
7b50c609 3048 if (chown(path, rootuid, rootgid) < 0) {
c4d10a05
SH
3049 ERROR("Error chowning %s", path);
3050 return -1;
3051 }
0fd73091 3052
c4d10a05
SH
3053 return 0;
3054 }
f3d7e4ca 3055
f4f52cb5 3056 if (rootuid == hostuid) {
1a0e70ac 3057 /* nothing to do */
b103ceac 3058 INFO("Container root is our uid; no need to chown");
f3d7e4ca
SH
3059 return 0;
3060 }
3061
bbdbf8f0 3062 /* save the current gid of "path" */
f4f52cb5
CB
3063 if (stat(path, &sb) < 0) {
3064 ERROR("Error stat %s", path);
f6d3e3e4
SH
3065 return -1;
3066 }
7b50c609 3067
bbdbf8f0
CB
3068 /* Update the path argument in case this was overlayfs. */
3069 args1[sizeof(args1) / sizeof(args1[0]) - 2] = path;
3070 args2[sizeof(args2) / sizeof(args2[0]) - 2] = path;
3071
f4f52cb5
CB
3072 /*
3073 * A file has to be group-owned by a gid mapped into the
3074 * container, or the container won't be privileged over it.
3075 */
3076 DEBUG("trying to chown \"%s\" to %d", path, hostgid);
3077 if (sb.st_uid == hostuid &&
3078 mapped_hostid(sb.st_gid, conf, ID_TYPE_GID) < 0 &&
3079 chown(path, -1, hostgid) < 0) {
3080 ERROR("Failed chgrping %s", path);
3081 return -1;
3082 }
f6d3e3e4 3083
1a0e70ac 3084 /* "u:0:rootuid:1" */
f4f52cb5
CB
3085 ret = snprintf(map1, 100, "u:0:%d:1", rootuid);
3086 if (ret < 0 || ret >= 100) {
3087 ERROR("Error uid printing map string");
3088 return -1;
3089 }
7b50c609 3090
1a0e70ac 3091 /* "u:hostuid:hostuid:1" */
f4f52cb5
CB
3092 ret = snprintf(map2, 100, "u:%d:%d:1", hostuid, hostuid);
3093 if (ret < 0 || ret >= 100) {
3094 ERROR("Error uid printing map string");
3095 return -1;
3096 }
c4d10a05 3097
1a0e70ac 3098 /* "g:0:rootgid:1" */
f4f52cb5
CB
3099 ret = snprintf(map3, 100, "g:0:%d:1", rootgid);
3100 if (ret < 0 || ret >= 100) {
3101 ERROR("Error gid printing map string");
3102 return -1;
3103 }
98e5ba51 3104
1a0e70ac 3105 /* "g:pathgid:rootgid+pathgid:1" */
f4f52cb5
CB
3106 ret = snprintf(map4, 100, "g:%d:%d:1", (gid_t)sb.st_gid,
3107 rootgid + (gid_t)sb.st_gid);
3108 if (ret < 0 || ret >= 100) {
3109 ERROR("Error gid printing map string");
3110 return -1;
3111 }
c4d10a05 3112
1a0e70ac 3113 /* "g:hostgid:hostgid:1" */
f4f52cb5
CB
3114 ret = snprintf(map5, 100, "g:%d:%d:1", hostgid, hostgid);
3115 if (ret < 0 || ret >= 100) {
3116 ERROR("Error gid printing map string");
3117 return -1;
3118 }
7b50c609 3119
1a0e70ac 3120 /* "0:pathgid" (chown) */
f4f52cb5
CB
3121 ret = snprintf(ugid, 100, "0:%d", (gid_t)sb.st_gid);
3122 if (ret < 0 || ret >= 100) {
3123 ERROR("Error owner printing format string for chown");
3124 return -1;
3125 }
7b50c609 3126
f4f52cb5
CB
3127 if (hostgid == sb.st_gid)
3128 ret = run_command(cmd_output, sizeof(cmd_output),
3129 chown_mapped_root_exec_wrapper,
3130 (void *)args1);
3131 else
3132 ret = run_command(cmd_output, sizeof(cmd_output),
3133 chown_mapped_root_exec_wrapper,
3134 (void *)args2);
3135 if (ret < 0)
3136 ERROR("lxc-usernsexec failed: %s", cmd_output);
7b50c609 3137
f4f52cb5 3138 return ret;
f6d3e3e4
SH
3139}
3140
943144d9
CB
3141/* NOTE: Must not be called from inside the container namespace! */
3142int lxc_create_tmp_proc_mount(struct lxc_conf *conf)
5112cd70
SH
3143{
3144 int mounted;
3145
943144d9 3146 mounted = lxc_mount_proc_if_needed(conf->rootfs.path ? conf->rootfs.mount : "");
5112cd70 3147 if (mounted == -1) {
0fd73091 3148 SYSERROR("Failed to mount proc in the container");
01958b1f 3149 /* continue only if there is no rootfs */
943144d9 3150 if (conf->rootfs.path)
01958b1f 3151 return -1;
5112cd70 3152 } else if (mounted == 1) {
943144d9 3153 conf->tmp_umount_proc = 1;
5112cd70 3154 }
943144d9 3155
5112cd70
SH
3156 return 0;
3157}
3158
3159void tmp_proc_unmount(struct lxc_conf *lxc_conf)
3160{
0fd73091
CB
3161 if (lxc_conf->tmp_umount_proc != 1)
3162 return;
3163
3164 umount("/proc");
3165 lxc_conf->tmp_umount_proc = 0;
5112cd70
SH
3166}
3167
0fd73091 3168/* Walk /proc/mounts and change any shared entries to slave. */
6a0c909a 3169void remount_all_slave(void)
e995d7a2 3170{
0fd73091 3171 FILE *f;
e995d7a2 3172 size_t len = 0;
0fd73091 3173 char *line = NULL;
e995d7a2 3174
0fd73091 3175 f = fopen("/proc/self/mountinfo", "r");
e995d7a2 3176 if (!f) {
0fd73091 3177 SYSERROR("Failed to open \"/proc/self/mountinfo\" to mark all shared");
e995d7a2
SH
3178 ERROR("Continuing container startup...");
3179 return;
3180 }
3181
3182 while (getline(&line, &len, f) != -1) {
0fd73091
CB
3183 int ret;
3184 char *opts, *target;
3185
e995d7a2
SH
3186 target = get_field(line, 4);
3187 if (!target)
3188 continue;
0fd73091 3189
e995d7a2
SH
3190 opts = get_field(target, 2);
3191 if (!opts)
3192 continue;
0fd73091 3193
e995d7a2
SH
3194 null_endofword(opts);
3195 if (!strstr(opts, "shared"))
3196 continue;
0fd73091 3197
e995d7a2 3198 null_endofword(target);
0fd73091
CB
3199 ret = mount(NULL, target, NULL, MS_SLAVE, NULL);
3200 if (ret < 0) {
3201 SYSERROR("Failed to make \"%s\" MS_SLAVE", target);
e995d7a2
SH
3202 ERROR("Continuing...");
3203 }
3204 }
3205 fclose(f);
f10fad2f 3206 free(line);
e995d7a2
SH
3207}
3208
8353b4c9 3209static int lxc_execute_bind_init(struct lxc_conf *conf)
2322903b
SH
3210{
3211 int ret;
9d9c111c
SH
3212 char path[PATH_MAX], destpath[PATH_MAX], *p;
3213
3214 /* If init exists in the container, don't bind mount a static one */
3215 p = choose_init(conf->rootfs.mount);
3216 if (p) {
3217 free(p);
8353b4c9 3218 return 0;
9d9c111c 3219 }
2322903b
SH
3220
3221 ret = snprintf(path, PATH_MAX, SBINDIR "/init.lxc.static");
0fd73091 3222 if (ret < 0 || ret >= PATH_MAX)
8353b4c9 3223 return -1;
2322903b
SH
3224
3225 if (!file_exists(path)) {
0fd73091 3226 ERROR("The file \"%s\" does not exist on host", path);
8353b4c9 3227 return -1;
2322903b
SH
3228 }
3229
3230 ret = snprintf(destpath, PATH_MAX, "%s%s", conf->rootfs.mount, "/init.lxc.static");
0fd73091 3231 if (ret < 0 || ret >= PATH_MAX)
8353b4c9 3232 return -1;
2322903b
SH
3233
3234 if (!file_exists(destpath)) {
0fd73091
CB
3235 FILE *pathfile;
3236
3237 pathfile = fopen(destpath, "wb");
2322903b 3238 if (!pathfile) {
8353b4c9
CB
3239 SYSERROR("Failed to create mount target \"%s\"", destpath);
3240 return -1;
2322903b 3241 }
8353b4c9 3242
2322903b
SH
3243 fclose(pathfile);
3244 }
3245
592fd47a 3246 ret = safe_mount(path, destpath, "none", MS_BIND, NULL, conf->rootfs.mount);
8353b4c9 3247 if (ret < 0) {
0fd73091 3248 SYSERROR("Failed to bind mount lxc.init.static into container");
8353b4c9
CB
3249 return -1;
3250 }
3251
3252 INFO("Bind mounted lxc.init.static into container at \"%s\"", path);
3253 return 0;
2322903b
SH
3254}
3255
0fd73091
CB
3256/* This does the work of remounting / if it is shared, calling the container
3257 * pre-mount hooks, and mounting the rootfs.
35120d9c
SH
3258 */
3259int do_rootfs_setup(struct lxc_conf *conf, const char *name, const char *lxcpath)
0ad19a3f 3260{
0fd73091
CB
3261 int ret;
3262
35120d9c 3263 if (conf->rootfs_setup) {
35120d9c 3264 const char *path = conf->rootfs.mount;
0fd73091
CB
3265
3266 /* The rootfs was set up in another namespace. bind-mount it to
3267 * give us a mount in our own ns so we can pivot_root to it
3268 */
3269 ret = mount(path, path, "rootfs", MS_BIND, NULL);
3270 if (ret < 0) {
3271 ERROR("Failed to bind mount container / onto itself");
145832ba 3272 return -1;
35120d9c 3273 }
0fd73091
CB
3274
3275 TRACE("Bind mounted container / onto itself");
145832ba 3276 return 0;
35120d9c 3277 }
d4ef7c50 3278
e995d7a2
SH
3279 remount_all_slave();
3280
0fd73091
CB
3281 ret = run_lxc_hooks(name, "pre-mount", conf, NULL);
3282 if (ret < 0) {
3283 ERROR("Failed to run pre-mount hooks");
35120d9c
SH
3284 return -1;
3285 }
3286
0fd73091
CB
3287 ret = lxc_setup_rootfs(conf);
3288 if (ret < 0) {
3289 ERROR("Failed to setup rootfs for");
35120d9c
SH
3290 return -1;
3291 }
3292
3293 conf->rootfs_setup = true;
3294 return 0;
3295}
3296
1c1c7051
SH
3297static bool verify_start_hooks(struct lxc_conf *conf)
3298{
1c1c7051 3299 char path[MAXPATHLEN];
0fd73091
CB
3300 struct lxc_list *it;
3301
3302 lxc_list_for_each (it, &conf->hooks[LXCHOOK_START]) {
1c1c7051 3303 int ret;
0fd73091
CB
3304 struct stat st;
3305 char *hookname = it->elem;
1c1c7051
SH
3306
3307 ret = snprintf(path, MAXPATHLEN, "%s%s",
0fd73091
CB
3308 conf->rootfs.path ? conf->rootfs.mount : "",
3309 hookname);
1c1c7051
SH
3310 if (ret < 0 || ret >= MAXPATHLEN)
3311 return false;
0fd73091 3312
1c1c7051 3313 ret = stat(path, &st);
0fd73091 3314 if (ret < 0) {
7b6753e7 3315 SYSERROR("Start hook %s not found in container",
0fd73091 3316 hookname);
1c1c7051
SH
3317 return false;
3318 }
0fd73091 3319
6a0c909a 3320 return true;
1c1c7051
SH
3321 }
3322
3323 return true;
3324}
3325
3b988b33 3326int lxc_setup(struct lxc_handler *handler)
35120d9c 3327{
2187efd3 3328 int ret;
0fd73091 3329 const char *lxcpath = handler->lxcpath, *name = handler->name;
35120d9c 3330 struct lxc_conf *lxc_conf = handler->conf;
35120d9c 3331
8353b4c9
CB
3332 ret = do_rootfs_setup(lxc_conf, name, lxcpath);
3333 if (ret < 0) {
3334 ERROR("Failed to setup rootfs");
35120d9c
SH
3335 return -1;
3336 }
3337
28d9e29e 3338 if (handler->nsfd[LXC_NS_UTS] == -1) {
8353b4c9
CB
3339 ret = setup_utsname(lxc_conf->utsname);
3340 if (ret < 0) {
0fd73091 3341 ERROR("Failed to setup the utsname %s", name);
6c544cb3
MM
3342 return -1;
3343 }
0ad19a3f 3344 }
3345
8353b4c9
CB
3346 ret = lxc_setup_network_in_child_namespaces(lxc_conf, &lxc_conf->network);
3347 if (ret < 0) {
3348 ERROR("Failed to setup network");
95b5ffaf 3349 return -1;
0ad19a3f 3350 }
3351
8353b4c9
CB
3352 ret = lxc_network_send_name_and_ifindex_to_parent(handler);
3353 if (ret < 0) {
3354 ERROR("Failed to send network device names and ifindices to parent");
790255cf
CB
3355 return -1;
3356 }
3357
bc6928ff 3358 if (lxc_conf->autodev > 0) {
8353b4c9
CB
3359 ret = mount_autodev(name, &lxc_conf->rootfs, lxcpath);
3360 if (ret < 0) {
3361 ERROR("Failed to mount \"/dev\"");
c6883f38
SH
3362 return -1;
3363 }
3364 }
3365
8353b4c9
CB
3366 /* Do automatic mounts (mainly /proc and /sys), but exclude those that
3367 * need to wait until other stuff has finished.
368bbc02 3368 */
8353b4c9
CB
3369 ret = lxc_mount_auto_mounts(lxc_conf, lxc_conf->auto_mounts & ~LXC_AUTO_CGROUP_MASK, handler);
3370 if (ret < 0) {
3371 ERROR("Failed to setup first automatic mounts");
368bbc02
CS
3372 return -1;
3373 }
3374
8353b4c9
CB
3375 ret = setup_mount(lxc_conf, &lxc_conf->rootfs, lxc_conf->fstab, name, lxcpath);
3376 if (ret < 0) {
3377 ERROR("Failed to setup mounts");
95b5ffaf 3378 return -1;
576f946d 3379 }
3380
7b6753e7 3381 /* Make sure any start hooks are in the container */
1c1c7051
SH
3382 if (!verify_start_hooks(lxc_conf))
3383 return -1;
3384
8353b4c9
CB
3385 if (lxc_conf->is_execute) {
3386 ret = lxc_execute_bind_init(lxc_conf);
3387 if (ret < 0) {
3388 ERROR("Failed to bind-mount the lxc init system");
3389 return -1;
3390 }
3391 }
2322903b 3392
8353b4c9
CB
3393 /* Now mount only cgroups, if wanted. Before, /sys could not have been
3394 * mounted. It is guaranteed to be mounted now either through
3395 * automatically or via fstab entries.
368bbc02 3396 */
8353b4c9
CB
3397 ret = lxc_mount_auto_mounts(lxc_conf, lxc_conf->auto_mounts & LXC_AUTO_CGROUP_MASK, handler);
3398 if (ret < 0) {
3399 ERROR("Failed to setup remaining automatic mounts");
368bbc02
CS
3400 return -1;
3401 }
3402
8353b4c9 3403 ret = run_lxc_hooks(name, "mount", lxc_conf, NULL);
1a2cf89d 3404 if (ret < 0) {
8353b4c9 3405 ERROR("Failed to run mount hooks");
773fb9ca
SH
3406 return -1;
3407 }
3408
bc6928ff 3409 if (lxc_conf->autodev > 0) {
8353b4c9
CB
3410 ret = run_lxc_hooks(name, "autodev", lxc_conf, NULL);
3411 if (ret < 0) {
3412 ERROR("Failed to run autodev hooks");
f7bee6c6
MW
3413 return -1;
3414 }
06749971 3415
8353b4c9
CB
3416 ret = lxc_fill_autodev(&lxc_conf->rootfs);
3417 if (ret < 0) {
3418 ERROR("Failed to populate \"/dev\"");
91c3830e
SH
3419 return -1;
3420 }
3421 }
368bbc02 3422
8353b4c9
CB
3423 if (!lxc_list_empty(&lxc_conf->mount_list)) {
3424 ret = setup_mount_entries(lxc_conf, &lxc_conf->rootfs,
3425 &lxc_conf->mount_list, name, lxcpath);
3426 if (ret < 0) {
3427 ERROR("Failed to setup mount entries");
3428 return -1;
3429 }
181437fd
YT
3430 }
3431
ed8704d0
CB
3432 ret = lxc_setup_console(&lxc_conf->rootfs, &lxc_conf->console,
3433 lxc_conf->ttydir);
3434 if (ret < 0) {
3435 ERROR("Failed to setup console");
95b5ffaf 3436 return -1;
6e590161 3437 }
3438
ed8704d0
CB
3439 ret = lxc_setup_dev_symlinks(&lxc_conf->rootfs);
3440 if (ret < 0) {
8353b4c9 3441 ERROR("Failed to setup \"/dev\" symlinks");
69aa6655
DE
3442 return -1;
3443 }
3444
8353b4c9
CB
3445 ret = lxc_create_tmp_proc_mount(lxc_conf);
3446 if (ret < 0) {
3447 ERROR("Failed to \"/proc\" LSMs");
e075f5d9 3448 return -1;
e075f5d9 3449 }
e075f5d9 3450
8353b4c9
CB
3451 ret = setup_pivot_root(&lxc_conf->rootfs);
3452 if (ret < 0) {
3453 ERROR("Failed to pivot root into rootfs");
95b5ffaf 3454 return -1;
ed502555 3455 }
3456
8353b4c9
CB
3457 ret = lxc_setup_devpts(lxc_conf);
3458 if (ret < 0) {
3459 ERROR("Failed to setup new devpts instance");
95b5ffaf 3460 return -1;
3c26f34e 3461 }
3462
2187efd3
CB
3463 ret = lxc_create_ttys(handler);
3464 if (ret < 0)
e8bd4e43 3465 return -1;
e8bd4e43 3466
8353b4c9
CB
3467 ret = setup_personality(lxc_conf->personality);
3468 if (ret < 0) {
3469 ERROR("Failed to set personality");
cccc74b5
DL
3470 return -1;
3471 }
3472
8353b4c9
CB
3473 /* Set sysctl value to a path under /proc/sys as determined from the
3474 * key. For e.g. net.ipv4.ip_forward translated to
3475 * /proc/sys/net/ipv4/ip_forward.
7edd0540
L
3476 */
3477 if (!lxc_list_empty(&lxc_conf->sysctls)) {
3478 ret = setup_sysctl_parameters(&lxc_conf->sysctls);
8353b4c9
CB
3479 if (ret < 0) {
3480 ERROR("Failed to setup sysctl parameters");
7edd0540 3481 return -1;
8353b4c9 3482 }
7edd0540
L
3483 }
3484
97a8f74f
SG
3485 if (!lxc_list_empty(&lxc_conf->keepcaps)) {
3486 if (!lxc_list_empty(&lxc_conf->caps)) {
8353b4c9
CB
3487 ERROR("Container requests lxc.cap.drop and "
3488 "lxc.cap.keep: either use lxc.cap.drop or "
3489 "lxc.cap.keep, not both");
f6d3e3e4
SH
3490 return -1;
3491 }
8353b4c9 3492
97a8f74f 3493 if (dropcaps_except(&lxc_conf->keepcaps)) {
8353b4c9 3494 ERROR("Failed to keep capabilities");
97a8f74f
SG
3495 return -1;
3496 }
3497 } else if (setup_caps(&lxc_conf->caps)) {
8353b4c9 3498 ERROR("Failed to drop capabilities");
97a8f74f 3499 return -1;
81810dd1
DL
3500 }
3501
8353b4c9 3502 NOTICE("The container \"%s\" is set up", name);
cd54d859 3503
0ad19a3f 3504 return 0;
3505}
26ddeedd 3506
3f60c2f7 3507int run_lxc_hooks(const char *name, char *hookname, struct lxc_conf *conf,
14a7b0f9 3508 char *argv[])
26ddeedd 3509{
26ddeedd 3510 struct lxc_list *it;
3f60c2f7 3511 int which = -1;
26ddeedd 3512
3f60c2f7 3513 if (strcmp(hookname, "pre-start") == 0)
26ddeedd 3514 which = LXCHOOK_PRESTART;
3f60c2f7 3515 else if (strcmp(hookname, "start-host") == 0)
08dd2805 3516 which = LXCHOOK_START_HOST;
3f60c2f7 3517 else if (strcmp(hookname, "pre-mount") == 0)
5ea6163a 3518 which = LXCHOOK_PREMOUNT;
3f60c2f7 3519 else if (strcmp(hookname, "mount") == 0)
26ddeedd 3520 which = LXCHOOK_MOUNT;
3f60c2f7 3521 else if (strcmp(hookname, "autodev") == 0)
f7bee6c6 3522 which = LXCHOOK_AUTODEV;
3f60c2f7 3523 else if (strcmp(hookname, "start") == 0)
26ddeedd 3524 which = LXCHOOK_START;
3f60c2f7 3525 else if (strcmp(hookname, "stop") == 0)
52492063 3526 which = LXCHOOK_STOP;
3f60c2f7 3527 else if (strcmp(hookname, "post-stop") == 0)
26ddeedd 3528 which = LXCHOOK_POSTSTOP;
3f60c2f7 3529 else if (strcmp(hookname, "clone") == 0)
148e91f5 3530 which = LXCHOOK_CLONE;
3f60c2f7 3531 else if (strcmp(hookname, "destroy") == 0)
37cf711b 3532 which = LXCHOOK_DESTROY;
26ddeedd
SH
3533 else
3534 return -1;
3f60c2f7 3535
0fd73091 3536 lxc_list_for_each (it, &conf->hooks[which]) {
26ddeedd 3537 int ret;
3f60c2f7
CB
3538 char *hook = it->elem;
3539
3540 ret = run_script_argv(name, conf->hooks_version, "lxc", hook,
14a7b0f9 3541 hookname, argv);
3f60c2f7
CB
3542 if (ret < 0)
3543 return -1;
26ddeedd 3544 }
3f60c2f7 3545
26ddeedd
SH
3546 return 0;
3547}
72d0e1cb 3548
72d0e1cb
SG
3549int lxc_clear_config_caps(struct lxc_conf *c)
3550{
1a0e70ac 3551 struct lxc_list *it, *next;
72d0e1cb 3552
0fd73091 3553 lxc_list_for_each_safe (it, &c->caps, next) {
72d0e1cb
SG
3554 lxc_list_del(it);
3555 free(it->elem);
3556 free(it);
3557 }
0fd73091 3558
72d0e1cb
SG
3559 return 0;
3560}
3561
c7e345ae
CB
3562static int lxc_free_idmap(struct lxc_list *id_map)
3563{
27c27d73
SH
3564 struct lxc_list *it, *next;
3565
0fd73091 3566 lxc_list_for_each_safe (it, id_map, next) {
27c27d73
SH
3567 lxc_list_del(it);
3568 free(it->elem);
3569 free(it);
3570 }
c7e345ae 3571
27c27d73
SH
3572 return 0;
3573}
3574
4355ab5f
SH
3575int lxc_clear_idmaps(struct lxc_conf *c)
3576{
3577 return lxc_free_idmap(&c->id_map);
3578}
3579
1fb86a7c
SH
3580int lxc_clear_config_keepcaps(struct lxc_conf *c)
3581{
0fd73091 3582 struct lxc_list *it, *next;
1fb86a7c 3583
0fd73091 3584 lxc_list_for_each_safe (it, &c->keepcaps, next) {
1fb86a7c
SH
3585 lxc_list_del(it);
3586 free(it->elem);
3587 free(it);
3588 }
0fd73091 3589
1fb86a7c
SH
3590 return 0;
3591}
3592
54860ed0 3593int lxc_clear_cgroups(struct lxc_conf *c, const char *key, int version)
72d0e1cb 3594{
54860ed0 3595 char *global_token, *namespaced_token;
ab1a6cac 3596 size_t namespaced_token_len;
54860ed0 3597 struct lxc_list *it, *next, *list;
ab1a6cac 3598 const char *k = key;
54860ed0 3599 bool all = false;
72d0e1cb 3600
54860ed0
CB
3601 if (version == CGROUP2_SUPER_MAGIC) {
3602 global_token = "lxc.cgroup2";
3603 namespaced_token = "lxc.cgroup2.";
0fd73091 3604 namespaced_token_len = sizeof("lxc.cgroup2.") - 1;
54860ed0
CB
3605 list = &c->cgroup2;
3606 } else if (version == CGROUP_SUPER_MAGIC) {
3607 global_token = "lxc.cgroup";
3608 namespaced_token = "lxc.cgroup.";
0fd73091 3609 namespaced_token_len = sizeof("lxc.cgroup.") - 1;
54860ed0
CB
3610 list = &c->cgroup;
3611 } else {
ab1a6cac 3612 return -EINVAL;
54860ed0
CB
3613 }
3614
3615 if (strcmp(key, global_token) == 0)
72d0e1cb 3616 all = true;
54860ed0 3617 else if (strncmp(key, namespaced_token, sizeof(namespaced_token) - 1) == 0)
ab1a6cac 3618 k += namespaced_token_len;
a6390f01 3619 else
ab1a6cac 3620 return -EINVAL;
72d0e1cb 3621
0fd73091 3622 lxc_list_for_each_safe (it, list, next) {
72d0e1cb 3623 struct lxc_cgroup *cg = it->elem;
54860ed0 3624
72d0e1cb
SG
3625 if (!all && strcmp(cg->subsystem, k) != 0)
3626 continue;
54860ed0 3627
72d0e1cb
SG
3628 lxc_list_del(it);
3629 free(cg->subsystem);
3630 free(cg->value);
3631 free(cg);
3632 free(it);
3633 }
e409b214 3634
72d0e1cb
SG
3635 return 0;
3636}
3637
c6d09e15
WB
3638int lxc_clear_limits(struct lxc_conf *c, const char *key)
3639{
3640 struct lxc_list *it, *next;
c6d09e15 3641 const char *k = NULL;
0fd73091 3642 bool all = false;
c6d09e15 3643
b668653c 3644 if (strcmp(key, "lxc.limit") == 0 || strcmp(key, "lxc.prlimit") == 0)
c6d09e15 3645 all = true;
b668653c
CB
3646 else if (strncmp(key, "lxc.limit.", sizeof("lxc.limit.") - 1) == 0)
3647 k = key + sizeof("lxc.limit.") - 1;
3648 else if (strncmp(key, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) == 0)
3649 k = key + sizeof("lxc.prlimit.") - 1;
c6d09e15
WB
3650 else
3651 return -1;
3652
0fd73091 3653 lxc_list_for_each_safe (it, &c->limits, next) {
c6d09e15 3654 struct lxc_limit *lim = it->elem;
0fd73091 3655
c6d09e15
WB
3656 if (!all && strcmp(lim->resource, k) != 0)
3657 continue;
0fd73091 3658
c6d09e15
WB
3659 lxc_list_del(it);
3660 free(lim->resource);
3661 free(lim);
3662 free(it);
3663 }
b668653c 3664
c6d09e15
WB
3665 return 0;
3666}
3667
7edd0540
L
3668int lxc_clear_sysctls(struct lxc_conf *c, const char *key)
3669{
3670 struct lxc_list *it, *next;
7edd0540 3671 const char *k = NULL;
0fd73091 3672 bool all = false;
7edd0540
L
3673
3674 if (strcmp(key, "lxc.sysctl") == 0)
3675 all = true;
3676 else if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3677 k = key + sizeof("lxc.sysctl.") - 1;
3678 else
3679 return -1;
3680
0fd73091 3681 lxc_list_for_each_safe (it, &c->sysctls, next) {
7edd0540 3682 struct lxc_sysctl *elem = it->elem;
0fd73091 3683
7edd0540
L
3684 if (!all && strcmp(elem->key, k) != 0)
3685 continue;
0fd73091 3686
7edd0540
L
3687 lxc_list_del(it);
3688 free(elem->key);
3689 free(elem->value);
3690 free(elem);
3691 free(it);
3692 }
0fd73091 3693
7edd0540
L
3694 return 0;
3695}
3696
61d7a733
YT
3697int lxc_clear_procs(struct lxc_conf *c, const char *key)
3698{
0fd73091 3699 struct lxc_list *it, *next;
61d7a733 3700 const char *k = NULL;
0fd73091 3701 bool all = false;
61d7a733
YT
3702
3703 if (strcmp(key, "lxc.proc") == 0)
3704 all = true;
3705 else if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3706 k = key + sizeof("lxc.proc.") - 1;
3707 else
3708 return -1;
3709
0fd73091 3710 lxc_list_for_each_safe (it, &c->procs, next) {
61d7a733 3711 struct lxc_proc *proc = it->elem;
0fd73091 3712
61d7a733
YT
3713 if (!all && strcmp(proc->filename, k) != 0)
3714 continue;
0fd73091 3715
61d7a733
YT
3716 lxc_list_del(it);
3717 free(proc->filename);
3718 free(proc->value);
3719 free(proc);
3720 free(it);
3721 }
3722
3723 return 0;
3724}
3725
ee1e7aa0
SG
3726int lxc_clear_groups(struct lxc_conf *c)
3727{
0fd73091 3728 struct lxc_list *it, *next;
ee1e7aa0 3729
0fd73091 3730 lxc_list_for_each_safe (it, &c->groups, next) {
ee1e7aa0
SG
3731 lxc_list_del(it);
3732 free(it->elem);
3733 free(it);
3734 }
0fd73091 3735
ee1e7aa0
SG
3736 return 0;
3737}
3738
ab799c0b
SG
3739int lxc_clear_environment(struct lxc_conf *c)
3740{
0fd73091 3741 struct lxc_list *it, *next;
ab799c0b 3742
0fd73091 3743 lxc_list_for_each_safe (it, &c->environment, next) {
ab799c0b
SG
3744 lxc_list_del(it);
3745 free(it->elem);
3746 free(it);
3747 }
0fd73091 3748
ab799c0b
SG
3749 return 0;
3750}
3751
72d0e1cb
SG
3752int lxc_clear_mount_entries(struct lxc_conf *c)
3753{
0fd73091 3754 struct lxc_list *it, *next;
72d0e1cb 3755
0fd73091 3756 lxc_list_for_each_safe (it, &c->mount_list, next) {
72d0e1cb
SG
3757 lxc_list_del(it);
3758 free(it->elem);
3759 free(it);
3760 }
0fd73091 3761
72d0e1cb
SG
3762 return 0;
3763}
3764
b099e9e9
SH
3765int lxc_clear_automounts(struct lxc_conf *c)
3766{
3767 c->auto_mounts = 0;
3768 return 0;
3769}
3770
12a50cc6 3771int lxc_clear_hooks(struct lxc_conf *c, const char *key)
72d0e1cb 3772{
72d0e1cb 3773 int i;
0fd73091
CB
3774 struct lxc_list *it, *next;
3775 const char *k = NULL;
3776 bool all = false, done = false;
72d0e1cb 3777
17ed13a3
SH
3778 if (strcmp(key, "lxc.hook") == 0)
3779 all = true;
0fd73091
CB
3780 else if (strncmp(key, "lxc.hook.", sizeof("lxc.hook.") - 1) == 0)
3781 k = key + sizeof("lxc.hook.") - 1;
a6390f01
WB
3782 else
3783 return -1;
17ed13a3 3784
0fd73091 3785 for (i = 0; i < NUM_LXC_HOOKS; i++) {
17ed13a3 3786 if (all || strcmp(k, lxchook_names[i]) == 0) {
0fd73091 3787 lxc_list_for_each_safe (it, &c->hooks[i], next) {
17ed13a3
SH
3788 lxc_list_del(it);
3789 free(it->elem);
3790 free(it);
3791 }
0fd73091 3792
17ed13a3 3793 done = true;
72d0e1cb
SG
3794 }
3795 }
17ed13a3
SH
3796
3797 if (!done) {
3798 ERROR("Invalid hook key: %s", key);
3799 return -1;
3800 }
0fd73091 3801
72d0e1cb
SG
3802 return 0;
3803}
8eb5694b 3804
4184c3e1
SH
3805static inline void lxc_clear_aliens(struct lxc_conf *conf)
3806{
0fd73091 3807 struct lxc_list *it, *next;
4184c3e1 3808
0fd73091 3809 lxc_list_for_each_safe (it, &conf->aliens, next) {
4184c3e1
SH
3810 lxc_list_del(it);
3811 free(it->elem);
3812 free(it);
3813 }
3814}
3815
c7b15d1e 3816void lxc_clear_includes(struct lxc_conf *conf)
f979ac15 3817{
0fd73091 3818 struct lxc_list *it, *next;
f979ac15 3819
0fd73091 3820 lxc_list_for_each_safe (it, &conf->includes, next) {
f979ac15
SH
3821 lxc_list_del(it);
3822 free(it->elem);
3823 free(it);
3824 }
3825}
3826
8eb5694b
SH
3827void lxc_conf_free(struct lxc_conf *conf)
3828{
3829 if (!conf)
3830 return;
0fd73091 3831
858377e4
SH
3832 if (current_config == conf)
3833 current_config = NULL;
aed105d5 3834 lxc_terminal_conf_free(&conf->console);
f10fad2f 3835 free(conf->rootfs.mount);
b3b8c97f 3836 free(conf->rootfs.bdev_type);
f10fad2f
ME
3837 free(conf->rootfs.options);
3838 free(conf->rootfs.path);
f10fad2f 3839 free(conf->logfile);
858377e4
SH
3840 if (conf->logfd != -1)
3841 close(conf->logfd);
f10fad2f
ME
3842 free(conf->utsname);
3843 free(conf->ttydir);
3844 free(conf->fstab);
3845 free(conf->rcfile);
5cda27c1 3846 free(conf->execute_cmd);
f10fad2f 3847 free(conf->init_cmd);
3c491553 3848 free(conf->init_cwd);
6b0d5538 3849 free(conf->unexpanded_config);
393903d1 3850 free(conf->pty_names);
76d0127f 3851 free(conf->syslog);
c302b476 3852 lxc_free_networks(&conf->network);
f10fad2f
ME
3853 free(conf->lsm_aa_profile);
3854 free(conf->lsm_se_context);
769872f9 3855 lxc_seccomp_free(conf);
8eb5694b 3856 lxc_clear_config_caps(conf);
1fb86a7c 3857 lxc_clear_config_keepcaps(conf);
54860ed0
CB
3858 lxc_clear_cgroups(conf, "lxc.cgroup", CGROUP_SUPER_MAGIC);
3859 lxc_clear_cgroups(conf, "lxc.cgroup2", CGROUP2_SUPER_MAGIC);
17ed13a3 3860 lxc_clear_hooks(conf, "lxc.hook");
8eb5694b 3861 lxc_clear_mount_entries(conf);
27c27d73 3862 lxc_clear_idmaps(conf);
ee1e7aa0 3863 lxc_clear_groups(conf);
f979ac15 3864 lxc_clear_includes(conf);
761d81ca 3865 lxc_clear_aliens(conf);
ab799c0b 3866 lxc_clear_environment(conf);
240d4b74 3867 lxc_clear_limits(conf, "lxc.prlimit");
7edd0540 3868 lxc_clear_sysctls(conf, "lxc.sysctl");
61d7a733 3869 lxc_clear_procs(conf, "lxc.proc");
43654d34
CB
3870 free(conf->cgroup_meta.dir);
3871 free(conf->cgroup_meta.controllers);
8eb5694b
SH
3872 free(conf);
3873}
4355ab5f
SH
3874
3875struct userns_fn_data {
3876 int (*fn)(void *);
c9b7c33e 3877 const char *fn_name;
4355ab5f
SH
3878 void *arg;
3879 int p[2];
3880};
3881
3882static int run_userns_fn(void *data)
3883{
4355ab5f 3884 char c;
0fd73091 3885 struct userns_fn_data *d = data;
4355ab5f 3886
f8aa4bf3 3887 /* Close write end of the pipe. */
4355ab5f 3888 close(d->p[1]);
f8aa4bf3
CB
3889
3890 /* Wait for parent to finish establishing a new mapping in the user
3891 * namespace we are executing in.
3892 */
4355ab5f
SH
3893 if (read(d->p[0], &c, 1) != 1)
3894 return -1;
f8aa4bf3
CB
3895
3896 /* Close read end of the pipe. */
4355ab5f 3897 close(d->p[0]);
f8aa4bf3 3898
c9b7c33e
CB
3899 if (d->fn_name)
3900 TRACE("calling function \"%s\"", d->fn_name);
0fd73091 3901
f8aa4bf3 3902 /* Call function to run. */
4355ab5f
SH
3903 return d->fn(d->arg);
3904}
3905
db7cfe23
CB
3906static struct id_map *mapped_nsid_add(struct lxc_conf *conf, unsigned id,
3907 enum idtype idtype)
3908{
3909 struct id_map *map, *retmap;
3910
3911 map = find_mapped_nsid_entry(conf, id, idtype);
3912 if (!map)
3913 return NULL;
3914
3915 retmap = malloc(sizeof(*retmap));
3916 if (!retmap)
3917 return NULL;
3918
3919 memcpy(retmap, map, sizeof(*retmap));
3920 return retmap;
3921}
3922
c4333195
CB
3923static struct id_map *find_mapped_hostid_entry(struct lxc_conf *conf,
3924 unsigned id, enum idtype idtype)
f8aa4bf3 3925{
f8aa4bf3 3926 struct id_map *map;
0fd73091 3927 struct lxc_list *it;
f8aa4bf3
CB
3928 struct id_map *retmap = NULL;
3929
0fd73091 3930 lxc_list_for_each (it, &conf->id_map) {
f8aa4bf3
CB
3931 map = it->elem;
3932 if (map->idtype != idtype)
3933 continue;
3934
3935 if (id >= map->hostid && id < map->hostid + map->range) {
3936 retmap = map;
3937 break;
3938 }
3939 }
3940
f8aa4bf3
CB
3941 return retmap;
3942}
3943
0fd73091 3944/* Allocate a new {g,u}id mapping for the given {g,u}id. Re-use an already
f8aa4bf3 3945 * existing one or establish a new one.
4355ab5f 3946 */
0fd73091
CB
3947static struct id_map *mapped_hostid_add(struct lxc_conf *conf, uid_t id,
3948 enum idtype type)
4355ab5f 3949{
28a2d9e7 3950 int hostid_mapped;
c4333195
CB
3951 struct id_map *entry = NULL, *tmp = NULL;
3952
3953 entry = malloc(sizeof(*entry));
3954 if (!entry)
3955 return NULL;
f8aa4bf3 3956
28a2d9e7 3957 /* Reuse existing mapping. */
c4333195
CB
3958 tmp = find_mapped_hostid_entry(conf, id, type);
3959 if (tmp)
3960 return memcpy(entry, tmp, sizeof(*entry));
f8aa4bf3 3961
28a2d9e7
CB
3962 /* Find new mapping. */
3963 hostid_mapped = find_unmapped_nsid(conf, type);
3964 if (hostid_mapped < 0) {
c4333195
CB
3965 DEBUG("Failed to find free mapping for id %d", id);
3966 free(entry);
28a2d9e7 3967 return NULL;
f8aa4bf3 3968 }
f8aa4bf3 3969
28a2d9e7
CB
3970 entry->idtype = type;
3971 entry->nsid = hostid_mapped;
3972 entry->hostid = (unsigned long)id;
3973 entry->range = 1;
4355ab5f 3974
28a2d9e7 3975 return entry;
4355ab5f
SH
3976}
3977
dcf0ffdf 3978struct lxc_list *get_minimal_idmap(struct lxc_conf *conf)
4355ab5f 3979{
f8aa4bf3 3980 uid_t euid, egid;
4160c3a0
CB
3981 uid_t nsuid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid;
3982 gid_t nsgid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid;
f8aa4bf3 3983 struct lxc_list *idmap = NULL, *tmplist = NULL;
28a2d9e7
CB
3984 struct id_map *container_root_uid = NULL, *container_root_gid = NULL,
3985 *host_uid_map = NULL, *host_gid_map = NULL;
4355ab5f 3986
db7cfe23 3987 /* Find container root mappings. */
4160c3a0 3988 container_root_uid = mapped_nsid_add(conf, nsuid, ID_TYPE_UID);
db7cfe23 3989 if (!container_root_uid) {
dcf0ffdf 3990 DEBUG("Failed to find mapping for namespace uid %d", 0);
db7cfe23 3991 goto on_error;
f8aa4bf3 3992 }
dcf0ffdf
CB
3993 euid = geteuid();
3994 if (euid >= container_root_uid->hostid &&
3995 euid < (container_root_uid->hostid + container_root_uid->range))
db7cfe23 3996 host_uid_map = container_root_uid;
f8aa4bf3 3997
4160c3a0 3998 container_root_gid = mapped_nsid_add(conf, nsgid, ID_TYPE_GID);
db7cfe23 3999 if (!container_root_gid) {
dcf0ffdf 4000 DEBUG("Failed to find mapping for namespace gid %d", 0);
f8aa4bf3
CB
4001 goto on_error;
4002 }
dcf0ffdf
CB
4003 egid = getegid();
4004 if (egid >= container_root_gid->hostid &&
4005 egid < (container_root_gid->hostid + container_root_gid->range))
db7cfe23 4006 host_gid_map = container_root_gid;
f8aa4bf3
CB
4007
4008 /* Check whether the {g,u}id of the user has a mapping. */
954b7d9b 4009 if (!host_uid_map)
c4333195 4010 host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID);
28a2d9e7 4011 if (!host_uid_map) {
db7cfe23 4012 DEBUG("Failed to find mapping for uid %d", euid);
f8aa4bf3
CB
4013 goto on_error;
4014 }
4015
dcf0ffdf
CB
4016 if (!host_gid_map)
4017 host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID);
28a2d9e7 4018 if (!host_gid_map) {
db7cfe23 4019 DEBUG("Failed to find mapping for gid %d", egid);
28a2d9e7
CB
4020 goto on_error;
4021 }
4022
4023 /* Allocate new {g,u}id map list. */
4024 idmap = malloc(sizeof(*idmap));
4025 if (!idmap)
4026 goto on_error;
4027 lxc_list_init(idmap);
4028
f8aa4bf3
CB
4029 /* Add container root to the map. */
4030 tmplist = malloc(sizeof(*tmplist));
4031 if (!tmplist)
4032 goto on_error;
4033 lxc_list_add_elem(tmplist, container_root_uid);
4034 lxc_list_add_tail(idmap, tmplist);
28a2d9e7 4035
1d90e064 4036 if (host_uid_map && (host_uid_map != container_root_uid)) {
28a2d9e7
CB
4037 /* idmap will now keep track of that memory. */
4038 container_root_uid = NULL;
4039
4040 /* Add container root to the map. */
4041 tmplist = malloc(sizeof(*tmplist));
4042 if (!tmplist)
4043 goto on_error;
4044 lxc_list_add_elem(tmplist, host_uid_map);
4045 lxc_list_add_tail(idmap, tmplist);
28a2d9e7 4046 }
1d90e064
CB
4047 /* idmap will now keep track of that memory. */
4048 container_root_uid = NULL;
4049 /* idmap will now keep track of that memory. */
4050 host_uid_map = NULL;
f8aa4bf3
CB
4051
4052 tmplist = malloc(sizeof(*tmplist));
4053 if (!tmplist)
4054 goto on_error;
4055 lxc_list_add_elem(tmplist, container_root_gid);
4056 lxc_list_add_tail(idmap, tmplist);
28a2d9e7 4057
1d90e064 4058 if (host_gid_map && (host_gid_map != container_root_gid)) {
28a2d9e7
CB
4059 /* idmap will now keep track of that memory. */
4060 container_root_gid = NULL;
4061
4062 tmplist = malloc(sizeof(*tmplist));
4063 if (!tmplist)
4064 goto on_error;
4065 lxc_list_add_elem(tmplist, host_gid_map);
4066 lxc_list_add_tail(idmap, tmplist);
28a2d9e7 4067 }
1d90e064
CB
4068 /* idmap will now keep track of that memory. */
4069 container_root_gid = NULL;
4070 /* idmap will now keep track of that memory. */
4071 host_gid_map = NULL;
f8aa4bf3 4072
dcf0ffdf
CB
4073 TRACE("Allocated minimal idmapping");
4074 return idmap;
4075
4076on_error:
4077 if (idmap)
4078 lxc_free_idmap(idmap);
4079 if (container_root_uid)
4080 free(container_root_uid);
4081 if (container_root_gid)
4082 free(container_root_gid);
4083 if (host_uid_map && (host_uid_map != container_root_uid))
4084 free(host_uid_map);
4085 if (host_gid_map && (host_gid_map != container_root_gid))
4086 free(host_gid_map);
4087
4088 return NULL;
4089}
4090
4091/* Run a function in a new user namespace.
4092 * The caller's euid/egid will be mapped if it is not already.
4093 * Afaict, userns_exec_1() is only used to operate based on privileges for the
4094 * user's own {g,u}id on the host and for the container root's unmapped {g,u}id.
4095 * This means we require only to establish a mapping from:
4096 * - the container root {g,u}id as seen from the host > user's host {g,u}id
4097 * - the container root -> some sub{g,u}id
4098 * The former we add, if the user did not specifiy a mapping. The latter we
4099 * retrieve from the ontainer's configured {g,u}id mappings as it must have been
4100 * there to start the container in the first place.
4101 */
4102int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data,
4103 const char *fn_name)
4104{
4105 pid_t pid;
dcf0ffdf 4106 int p[2];
0fd73091 4107 struct userns_fn_data d;
dcf0ffdf 4108 struct lxc_list *idmap;
0fd73091
CB
4109 int ret = -1, status = -1;
4110 char c = '1';
dcf0ffdf
CB
4111
4112 idmap = get_minimal_idmap(conf);
4113 if (!idmap)
4114 return -1;
4115
4116 ret = pipe(p);
4117 if (ret < 0) {
4118 SYSERROR("Failed to create pipe");
4119 return -1;
4120 }
4121 d.fn = fn;
4122 d.fn_name = fn_name;
4123 d.arg = data;
4124 d.p[0] = p[0];
4125 d.p[1] = p[1];
4126
4127 /* Clone child in new user namespace. */
4128 pid = lxc_raw_clone_cb(run_userns_fn, &d, CLONE_NEWUSER);
4129 if (pid < 0) {
0fd73091 4130 ERROR("Failed to clone process in new user namespace");
dcf0ffdf
CB
4131 goto on_error;
4132 }
4133
4134 close(p[0]);
4135 p[0] = -1;
4136
4b73005c
CB
4137 if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE ||
4138 conf->loglevel == LXC_LOG_LEVEL_TRACE) {
dcf0ffdf 4139 struct id_map *map;
0fd73091 4140 struct lxc_list *it;
dcf0ffdf 4141
0fd73091 4142 lxc_list_for_each (it, idmap) {
f8aa4bf3 4143 map = it->elem;
dcf0ffdf 4144 TRACE("Establishing %cid mapping for \"%d\" in new "
f8aa4bf3 4145 "user namespace: nsuid %lu - hostid %lu - range "
0fd73091
CB
4146 "%lu",
4147 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid,
4148 map->nsid, map->hostid, map->range);
f8aa4bf3 4149 }
4355ab5f
SH
4150 }
4151
f8aa4bf3 4152 /* Set up {g,u}id mapping for user namespace of child process. */
4355ab5f 4153 ret = lxc_map_ids(idmap, pid);
f8aa4bf3 4154 if (ret < 0) {
0fd73091 4155 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid);
f8aa4bf3 4156 goto on_error;
4355ab5f
SH
4157 }
4158
f8aa4bf3 4159 /* Tell child to proceed. */
4355ab5f 4160 if (write(p[1], &c, 1) != 1) {
dcf0ffdf 4161 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
f8aa4bf3 4162 goto on_error;
4355ab5f
SH
4163 }
4164
686dd5d1 4165on_error:
4355ab5f
SH
4166 if (p[0] != -1)
4167 close(p[0]);
4168 close(p[1]);
f8aa4bf3 4169
ee1b16bc
TA
4170 /* Wait for child to finish. */
4171 if (pid > 0)
4172 status = wait_for_pid(pid);
4173
686dd5d1
CB
4174 if (status < 0)
4175 ret = -1;
4176
f8aa4bf3 4177 return ret;
4355ab5f 4178}
97e9cfa0 4179
415a8851
CB
4180int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data,
4181 const char *fn_name)
4182{
4183 pid_t pid;
4184 uid_t euid, egid;
415a8851
CB
4185 int p[2];
4186 struct id_map *map;
4187 struct lxc_list *cur;
0fd73091 4188 struct userns_fn_data d;
415a8851 4189 int ret = -1;
0fd73091 4190 char c = '1';
415a8851
CB
4191 struct lxc_list *idmap = NULL, *tmplist = NULL;
4192 struct id_map *container_root_uid = NULL, *container_root_gid = NULL,
4193 *host_uid_map = NULL, *host_gid_map = NULL;
4194
4195 ret = pipe(p);
4196 if (ret < 0) {
4197 SYSERROR("opening pipe");
4198 return -1;
4199 }
4200 d.fn = fn;
4201 d.fn_name = fn_name;
4202 d.arg = data;
4203 d.p[0] = p[0];
4204 d.p[1] = p[1];
4205
4206 /* Clone child in new user namespace. */
4207 pid = lxc_clone(run_userns_fn, &d, CLONE_NEWUSER);
4208 if (pid < 0) {
0fd73091 4209 ERROR("Failed to clone process in new user namespace");
415a8851
CB
4210 goto on_error;
4211 }
4212
4213 close(p[0]);
4214 p[0] = -1;
4215
4216 euid = geteuid();
4217 egid = getegid();
4218
4219 /* Allocate new {g,u}id map list. */
4220 idmap = malloc(sizeof(*idmap));
4221 if (!idmap)
4222 goto on_error;
4223 lxc_list_init(idmap);
4224
4225 /* Find container root. */
0fd73091 4226 lxc_list_for_each (cur, &conf->id_map) {
415a8851
CB
4227 struct id_map *tmpmap;
4228
4229 tmplist = malloc(sizeof(*tmplist));
4230 if (!tmplist)
4231 goto on_error;
4232
4233 tmpmap = malloc(sizeof(*tmpmap));
4234 if (!tmpmap) {
4235 free(tmplist);
4236 goto on_error;
4237 }
4238
4239 memset(tmpmap, 0, sizeof(*tmpmap));
4240 memcpy(tmpmap, cur->elem, sizeof(*tmpmap));
4241 tmplist->elem = tmpmap;
4242
4243 lxc_list_add_tail(idmap, tmplist);
4244
4245 map = cur->elem;
4246
4247 if (map->idtype == ID_TYPE_UID)
4248 if (euid >= map->hostid && euid < map->hostid + map->range)
4249 host_uid_map = map;
4250
4251 if (map->idtype == ID_TYPE_GID)
4252 if (egid >= map->hostid && egid < map->hostid + map->range)
4253 host_gid_map = map;
4254
4255 if (map->nsid != 0)
4256 continue;
4257
4258 if (map->idtype == ID_TYPE_UID)
4259 if (container_root_uid == NULL)
4260 container_root_uid = map;
4261
4262 if (map->idtype == ID_TYPE_GID)
4263 if (container_root_gid == NULL)
4264 container_root_gid = map;
4265 }
4266
4267 if (!container_root_uid || !container_root_gid) {
4268 ERROR("No mapping for container root found");
4269 goto on_error;
4270 }
4271
4272 /* Check whether the {g,u}id of the user has a mapping. */
4273 if (!host_uid_map)
c4333195 4274 host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID);
415a8851
CB
4275 else
4276 host_uid_map = container_root_uid;
4277
4278 if (!host_gid_map)
c4333195 4279 host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID);
415a8851
CB
4280 else
4281 host_gid_map = container_root_gid;
4282
4283 if (!host_uid_map) {
4284 DEBUG("Failed to find mapping for uid %d", euid);
4285 goto on_error;
4286 }
4287
4288 if (!host_gid_map) {
4289 DEBUG("Failed to find mapping for gid %d", egid);
4290 goto on_error;
4291 }
4292
4293 if (host_uid_map && (host_uid_map != container_root_uid)) {
4294 /* Add container root to the map. */
4295 tmplist = malloc(sizeof(*tmplist));
4296 if (!tmplist)
4297 goto on_error;
4298 lxc_list_add_elem(tmplist, host_uid_map);
4299 lxc_list_add_tail(idmap, tmplist);
4300 }
4301 /* idmap will now keep track of that memory. */
4302 host_uid_map = NULL;
4303
4304 if (host_gid_map && (host_gid_map != container_root_gid)) {
4305 tmplist = malloc(sizeof(*tmplist));
4306 if (!tmplist)
4307 goto on_error;
4308 lxc_list_add_elem(tmplist, host_gid_map);
4309 lxc_list_add_tail(idmap, tmplist);
4310 }
4311 /* idmap will now keep track of that memory. */
4312 host_gid_map = NULL;
4313
4314 if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE ||
4315 conf->loglevel == LXC_LOG_LEVEL_TRACE) {
0fd73091 4316 lxc_list_for_each (cur, idmap) {
415a8851
CB
4317 map = cur->elem;
4318 TRACE("establishing %cid mapping for \"%d\" in new "
4319 "user namespace: nsuid %lu - hostid %lu - range "
4320 "%lu",
4321 (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid,
4322 map->nsid, map->hostid, map->range);
4323 }
4324 }
4325
4326 /* Set up {g,u}id mapping for user namespace of child process. */
4327 ret = lxc_map_ids(idmap, pid);
4328 if (ret < 0) {
0fd73091 4329 ERROR("error setting up {g,u}id mappings for child process \"%d\"", pid);
415a8851
CB
4330 goto on_error;
4331 }
4332
4333 /* Tell child to proceed. */
4334 if (write(p[1], &c, 1) != 1) {
0fd73091 4335 SYSERROR("Failed telling child process \"%d\" to proceed", pid);
415a8851
CB
4336 goto on_error;
4337 }
4338
686dd5d1 4339on_error:
ee1b16bc
TA
4340 if (p[0] != -1)
4341 close(p[0]);
4342 close(p[1]);
4343
415a8851 4344 /* Wait for child to finish. */
686dd5d1
CB
4345 if (pid > 0)
4346 ret = wait_for_pid(pid);
415a8851 4347
415a8851
CB
4348 if (idmap)
4349 lxc_free_idmap(idmap);
4350 if (host_uid_map && (host_uid_map != container_root_uid))
4351 free(host_uid_map);
4352 if (host_gid_map && (host_gid_map != container_root_gid))
4353 free(host_gid_map);
4354
415a8851
CB
4355 return ret;
4356}
4357
a96a8e8c 4358/* not thread-safe, do not use from api without first forking */
0fd73091 4359static char *getuname(void)
97e9cfa0 4360{
a96a8e8c 4361 struct passwd *result;
97e9cfa0 4362
a96a8e8c
SH
4363 result = getpwuid(geteuid());
4364 if (!result)
97e9cfa0
SH
4365 return NULL;
4366
a96a8e8c 4367 return strdup(result->pw_name);
97e9cfa0
SH
4368}
4369
a96a8e8c 4370/* not thread-safe, do not use from api without first forking */
97e9cfa0
SH
4371static char *getgname(void)
4372{
a96a8e8c 4373 struct group *result;
97e9cfa0 4374
a96a8e8c
SH
4375 result = getgrgid(getegid());
4376 if (!result)
97e9cfa0
SH
4377 return NULL;
4378
a96a8e8c 4379 return strdup(result->gr_name);
97e9cfa0
SH
4380}
4381
a96a8e8c 4382/* not thread-safe, do not use from api without first forking */
97e9cfa0
SH
4383void suggest_default_idmap(void)
4384{
0fd73091 4385 char *uname, *gname;
97e9cfa0
SH
4386 FILE *f;
4387 unsigned int uid = 0, urange = 0, gid = 0, grange = 0;
97e9cfa0 4388 size_t len = 0;
0fd73091 4389 char *line = NULL;
97e9cfa0 4390
0fd73091
CB
4391 uname = getuname();
4392 if (!uname)
97e9cfa0
SH
4393 return;
4394
0fd73091
CB
4395 gname = getgname();
4396 if (!gname) {
97e9cfa0
SH
4397 free(uname);
4398 return;
4399 }
4400
4401 f = fopen(subuidfile, "r");
4402 if (!f) {
4403 ERROR("Your system is not configured with subuids");
4404 free(gname);
4405 free(uname);
4406 return;
4407 }
0fd73091 4408
97e9cfa0 4409 while (getline(&line, &len, f) != -1) {
0fd73091 4410 char *p, *p2;
b7930180 4411 size_t no_newline = 0;
0fd73091
CB
4412
4413 p = strchr(line, ':');
97e9cfa0
SH
4414 if (*line == '#')
4415 continue;
4416 if (!p)
4417 continue;
4418 *p = '\0';
4419 p++;
0fd73091 4420
97e9cfa0
SH
4421 if (strcmp(line, uname))
4422 continue;
0fd73091 4423
97e9cfa0
SH
4424 p2 = strchr(p, ':');
4425 if (!p2)
4426 continue;
4427 *p2 = '\0';
4428 p2++;
4429 if (!*p2)
4430 continue;
b7930180
CB
4431 no_newline = strcspn(p2, "\n");
4432 p2[no_newline] = '\0';
4433
b7b2fde4 4434 if (lxc_safe_uint(p, &uid) < 0)
0fd73091 4435 WARN("Could not parse UID");
b7b2fde4 4436 if (lxc_safe_uint(p2, &urange) < 0)
0fd73091 4437 WARN("Could not parse UID range");
97e9cfa0
SH
4438 }
4439 fclose(f);
4440
6be7389a 4441 f = fopen(subgidfile, "r");
97e9cfa0
SH
4442 if (!f) {
4443 ERROR("Your system is not configured with subgids");
4444 free(gname);
4445 free(uname);
4446 return;
4447 }
0fd73091 4448
97e9cfa0 4449 while (getline(&line, &len, f) != -1) {
0fd73091 4450 char *p, *p2;
b7930180 4451 size_t no_newline = 0;
0fd73091
CB
4452
4453 p = strchr(line, ':');
97e9cfa0
SH
4454 if (*line == '#')
4455 continue;
4456 if (!p)
4457 continue;
4458 *p = '\0';
4459 p++;
0fd73091 4460
97e9cfa0
SH
4461 if (strcmp(line, uname))
4462 continue;
0fd73091 4463
97e9cfa0
SH
4464 p2 = strchr(p, ':');
4465 if (!p2)
4466 continue;
4467 *p2 = '\0';
4468 p2++;
4469 if (!*p2)
4470 continue;
b7930180
CB
4471 no_newline = strcspn(p2, "\n");
4472 p2[no_newline] = '\0';
4473
b7b2fde4 4474 if (lxc_safe_uint(p, &gid) < 0)
0fd73091 4475 WARN("Could not parse GID");
b7b2fde4 4476 if (lxc_safe_uint(p2, &grange) < 0)
0fd73091 4477 WARN("Could not parse GID range");
97e9cfa0
SH
4478 }
4479 fclose(f);
4480
f10fad2f 4481 free(line);
97e9cfa0
SH
4482
4483 if (!urange || !grange) {
4484 ERROR("You do not have subuids or subgids allocated");
4485 ERROR("Unprivileged containers require subuids and subgids");
4486 return;
4487 }
4488
4489 ERROR("You must either run as root, or define uid mappings");
4490 ERROR("To pass uid mappings to lxc-create, you could create");
4491 ERROR("~/.config/lxc/default.conf:");
4492 ERROR("lxc.include = %s", LXC_DEFAULT_CONFIG);
bdcbb6b3
CB
4493 ERROR("lxc.idmap = u 0 %u %u", uid, urange);
4494 ERROR("lxc.idmap = g 0 %u %u", gid, grange);
97e9cfa0
SH
4495
4496 free(gname);
4497 free(uname);
4498}
aaf26830 4499
a7307747
SH
4500static void free_cgroup_settings(struct lxc_list *result)
4501{
4502 struct lxc_list *iterator, *next;
4503
0fd73091 4504 lxc_list_for_each_safe (iterator, result, next) {
a7307747
SH
4505 lxc_list_del(iterator);
4506 free(iterator);
4507 }
4508 free(result);
4509}
4510
0fd73091 4511/* Return the list of cgroup_settings sorted according to the following rules
aaf26830
KT
4512 * 1. Put memory.limit_in_bytes before memory.memsw.limit_in_bytes
4513 */
0fd73091 4514struct lxc_list *sort_cgroup_settings(struct lxc_list *cgroup_settings)
aaf26830
KT
4515{
4516 struct lxc_list *result;
aaf26830 4517 struct lxc_cgroup *cg = NULL;
0fd73091 4518 struct lxc_list *it = NULL, *item = NULL, *memsw_limit = NULL;
aaf26830
KT
4519
4520 result = malloc(sizeof(*result));
0fd73091 4521 if (!result)
fac7c663 4522 return NULL;
aaf26830
KT
4523 lxc_list_init(result);
4524
0fd73091
CB
4525 /* Iterate over the cgroup settings and copy them to the output list. */
4526 lxc_list_for_each (it, cgroup_settings) {
aaf26830 4527 item = malloc(sizeof(*item));
fac7c663 4528 if (!item) {
a7307747 4529 free_cgroup_settings(result);
fac7c663
KT
4530 return NULL;
4531 }
0fd73091 4532
aaf26830
KT
4533 item->elem = it->elem;
4534 cg = it->elem;
4535 if (strcmp(cg->subsystem, "memory.memsw.limit_in_bytes") == 0) {
4536 /* Store the memsw_limit location */
4537 memsw_limit = item;
0fd73091
CB
4538 } else if (strcmp(cg->subsystem, "memory.limit_in_bytes") == 0 &&
4539 memsw_limit != NULL) {
4540 /* lxc.cgroup.memory.memsw.limit_in_bytes is found
4541 * before lxc.cgroup.memory.limit_in_bytes, swap these
4542 * two items */
aaf26830
KT
4543 item->elem = memsw_limit->elem;
4544 memsw_limit->elem = it->elem;
4545 }
4546 lxc_list_add_tail(result, item);
4547 }
4548
4549 return result;
a7307747 4550}