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