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