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