]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/namespace.c
Merge pull request #2987 from tych0/pass-zero-to-clone
[mirror_lxc.git] / src / lxc / namespace.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2009
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sched.h>
30 #include <signal.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/syscall.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include "config.h"
38 #include "log.h"
39 #include "memory_utils.h"
40 #include "namespace.h"
41 #include "utils.h"
42
43 lxc_log_define(namespace, lxc);
44
45 #define __LXC_STACK_SIZE (8 * 1024 * 1024)
46 pid_t lxc_clone(int (*fn)(void *), void *arg, int flags, int *pidfd)
47 {
48 pid_t ret;
49 void *stack;
50
51 stack = malloc(__LXC_STACK_SIZE);
52 if (!stack) {
53 SYSERROR("Failed to allocate clone stack");
54 return -ENOMEM;
55 }
56
57 #ifdef __ia64__
58 ret = __clone2(fn, stack, __LXC_STACK_SIZE, flags | SIGCHLD, arg, pidfd);
59 #else
60 ret = clone(fn, stack + __LXC_STACK_SIZE, flags | SIGCHLD, arg, pidfd);
61 #endif
62 if (ret < 0)
63 SYSERROR("Failed to clone (%#x)", flags);
64
65 return ret;
66 }
67
68 /* Leave the user namespace at the first position in the array of structs so
69 * that we always attach to it first when iterating over the struct and using
70 * setns() to switch namespaces. This especially affects lxc_attach(): Suppose
71 * you cloned a new user namespace and mount namespace as an unprivileged user
72 * on the host and want to setns() to the mount namespace. This requires you to
73 * attach to the user namespace first otherwise the kernel will fail this check:
74 *
75 * if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
76 * !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
77 * !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
78 * return -EPERM;
79 *
80 * in
81 *
82 * linux/fs/namespace.c:mntns_install().
83 */
84 const struct ns_info ns_info[LXC_NS_MAX] = {
85 [LXC_NS_USER] = { "user", CLONE_NEWUSER, "CLONE_NEWUSER", "LXC_USER_NS" },
86 [LXC_NS_MNT] = { "mnt", CLONE_NEWNS, "CLONE_NEWNS", "LXC_MNT_NS" },
87 [LXC_NS_PID] = { "pid", CLONE_NEWPID, "CLONE_NEWPID", "LXC_PID_NS" },
88 [LXC_NS_UTS] = { "uts", CLONE_NEWUTS, "CLONE_NEWUTS", "LXC_UTS_NS" },
89 [LXC_NS_IPC] = { "ipc", CLONE_NEWIPC, "CLONE_NEWIPC", "LXC_IPC_NS" },
90 [LXC_NS_NET] = { "net", CLONE_NEWNET, "CLONE_NEWNET", "LXC_NET_NS" },
91 [LXC_NS_CGROUP] = { "cgroup", CLONE_NEWCGROUP, "CLONE_NEWCGROUP", "LXC_CGROUP_NS" }
92 };
93
94 int lxc_namespace_2_cloneflag(const char *namespace)
95 {
96 int i;
97
98 for (i = 0; i < LXC_NS_MAX; i++)
99 if (!strcasecmp(ns_info[i].proc_name, namespace))
100 return ns_info[i].clone_flag;
101
102 ERROR("Invalid namespace name \"%s\"", namespace);
103 return -EINVAL;
104 }
105
106 int lxc_namespace_2_ns_idx(const char *namespace)
107 {
108 int i;
109
110 for (i = 0; i < LXC_NS_MAX; i++)
111 if (!strcmp(ns_info[i].proc_name, namespace))
112 return i;
113
114 ERROR("Invalid namespace name \"%s\"", namespace);
115 return -EINVAL;
116 }
117
118 extern int lxc_namespace_2_std_identifiers(char *namespaces)
119 {
120 char **it;
121 char *del;
122
123 /* The identifiers for namespaces used with lxc-attach and lxc-unshare
124 * as given on the manpage do not align with the standard identifiers.
125 * This affects network, mount, and uts namespaces. The standard identifiers
126 * are: "mnt", "uts", and "net" whereas lxc-attach and lxc-unshare uses
127 * "MOUNT", "UTSNAME", and "NETWORK". So let's use some cheap memmove()s
128 * to replace them by their standard identifiers.
129 * Let's illustrate this with an example:
130 * Assume the string:
131 *
132 * "IPC|MOUNT|PID"
133 *
134 * then we memmove()
135 *
136 * dest: del + 1 == OUNT|PID
137 * src: del + 3 == NT|PID
138 */
139 if (!namespaces)
140 return -1;
141
142 while ((del = strstr(namespaces, "MOUNT")))
143 memmove(del + 1, del + 3, strlen(del) - 2);
144
145 for (it = (char *[]){"NETWORK", "UTSNAME", NULL}; it && *it; it++)
146 while ((del = strstr(namespaces, *it)))
147 memmove(del + 3, del + 7, strlen(del) - 6);
148
149 return 0;
150 }
151
152 int lxc_fill_namespace_flags(char *flaglist, int *flags)
153 {
154 char *token;
155 int aflag;
156
157 if (!flaglist) {
158 ERROR("At least one namespace is needed.");
159 return -1;
160 }
161
162 lxc_iterate_parts(token, flaglist, "|") {
163 aflag = lxc_namespace_2_cloneflag(token);
164 if (aflag < 0)
165 return -1;
166
167 *flags |= aflag;
168 }
169
170 return 0;
171 }