]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #ifndef _GNU_SOURCE | |
4 | #define _GNU_SOURCE 1 | |
5 | #endif | |
6 | #include <dirent.h> | |
7 | #include <errno.h> | |
8 | #include <fcntl.h> | |
9 | #include <grp.h> | |
10 | #include <poll.h> | |
11 | #include <pthread.h> | |
12 | #include <signal.h> | |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
16 | #include <sys/file.h> | |
17 | #include <sys/mount.h> | |
18 | #include <sys/param.h> | |
19 | #include <sys/prctl.h> | |
20 | #include <sys/socket.h> | |
21 | #include <sys/stat.h> | |
22 | #include <sys/syscall.h> | |
23 | #include <sys/types.h> | |
24 | #include <sys/un.h> | |
25 | #include <sys/wait.h> | |
26 | #include <unistd.h> | |
27 | ||
28 | #include "af_unix.h" | |
29 | #include "caps.h" | |
30 | #include "cgroups/cgroup.h" | |
31 | #include "cgroups/cgroup_utils.h" | |
32 | #include "commands.h" | |
33 | #include "commands_utils.h" | |
34 | #include "compiler.h" | |
35 | #include "conf.h" | |
36 | #include "config.h" | |
37 | #include "confile_utils.h" | |
38 | #include "error.h" | |
39 | #include "file_utils.h" | |
40 | #include "list.h" | |
41 | #include "log.h" | |
42 | #include "lsm/lsm.h" | |
43 | #include "lxccontainer.h" | |
44 | #include "lxclock.h" | |
45 | #include "lxcseccomp.h" | |
46 | #include "macro.h" | |
47 | #include "mainloop.h" | |
48 | #include "memory_utils.h" | |
49 | #include "monitor.h" | |
50 | #include "namespace.h" | |
51 | #include "network.h" | |
52 | #include "process_utils.h" | |
53 | #include "start.h" | |
54 | #include "storage/storage.h" | |
55 | #include "storage/storage_utils.h" | |
56 | #include "sync.h" | |
57 | #include "syscall_wrappers.h" | |
58 | #include "terminal.h" | |
59 | #include "utils.h" | |
60 | ||
61 | #if HAVE_LIBCAP | |
62 | #include <sys/capability.h> | |
63 | #endif | |
64 | ||
65 | #ifndef HAVE_STRLCPY | |
66 | #include "include/strlcpy.h" | |
67 | #endif | |
68 | ||
69 | lxc_log_define(start, lxc); | |
70 | ||
71 | extern void mod_all_rdeps(struct lxc_container *c, bool inc); | |
72 | static bool do_destroy_container(struct lxc_handler *handler); | |
73 | static int lxc_rmdir_onedev_wrapper(void *data); | |
74 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
75 | const char *name); | |
76 | ||
77 | static void print_top_failing_dir(const char *path) | |
78 | { | |
79 | __do_free char *copy = NULL; | |
80 | int ret; | |
81 | char *e, *p, saved; | |
82 | ||
83 | copy = must_copy_string(path); | |
84 | p = copy; | |
85 | e = copy + strlen(path); | |
86 | ||
87 | while (p < e) { | |
88 | while (p < e && *p == '/') | |
89 | p++; | |
90 | ||
91 | while (p < e && *p != '/') | |
92 | p++; | |
93 | ||
94 | saved = *p; | |
95 | *p = '\0'; | |
96 | ||
97 | ret = access(copy, X_OK); | |
98 | if (ret != 0) { | |
99 | SYSERROR("Could not access %s. Please grant it x access, or add an ACL for the container " "root", copy); | |
100 | return; | |
101 | } | |
102 | *p = saved; | |
103 | } | |
104 | } | |
105 | ||
106 | static void lxc_put_nsfds(struct lxc_handler *handler) | |
107 | { | |
108 | for (int i = 0; i < LXC_NS_MAX; i++) { | |
109 | if (handler->nsfd[i] < 0) | |
110 | continue; | |
111 | ||
112 | close_prot_errno_disarm(handler->nsfd[i]); | |
113 | } | |
114 | } | |
115 | ||
116 | static int lxc_try_preserve_namespace(struct lxc_handler *handler, | |
117 | lxc_namespace_t idx, const char *ns) | |
118 | { | |
119 | __do_close int fd = -EBADF; | |
120 | int ret; | |
121 | ||
122 | fd = lxc_preserve_ns(handler->pid, ns); | |
123 | if (fd < 0) | |
124 | return -errno; | |
125 | ||
126 | ret = strnprintf(handler->nsfd_paths[idx], | |
127 | sizeof(handler->nsfd_paths[idx]), "%s:/proc/%d/fd/%d", | |
128 | ns_info[idx].proc_name, handler->monitor_pid, fd); | |
129 | if (ret < 0) | |
130 | return ret_errno(EIO); | |
131 | ||
132 | /* | |
133 | * In case LXC is configured for exposing information to hooks as | |
134 | * argv-style arguments prepare an argv array we can use. | |
135 | */ | |
136 | handler->hook_argv[handler->hook_argc] = handler->nsfd_paths[idx]; | |
137 | handler->hook_argc++; | |
138 | ||
139 | DEBUG("Preserved %s namespace via fd %d and stashed path as %s", | |
140 | ns_info[idx].proc_name, fd, handler->nsfd_paths[idx]); | |
141 | ||
142 | handler->nsfd[idx] = move_fd(fd); | |
143 | return 0; | |
144 | } | |
145 | ||
146 | /* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace | |
147 | * specified in ns_clone_flags. | |
148 | * Return true on success, false on failure. | |
149 | */ | |
150 | static bool lxc_try_preserve_namespaces(struct lxc_handler *handler, | |
151 | int ns_clone_flags) | |
152 | { | |
153 | for (lxc_namespace_t ns_idx = 0; ns_idx < LXC_NS_MAX; ns_idx++) | |
154 | handler->nsfd[ns_idx] = -EBADF; | |
155 | ||
156 | for (lxc_namespace_t ns_idx = 0; ns_idx < LXC_NS_MAX; ns_idx++) { | |
157 | int ret; | |
158 | const char *ns = ns_info[ns_idx].proc_name; | |
159 | ||
160 | if ((ns_clone_flags & ns_info[ns_idx].clone_flag) == 0) | |
161 | continue; | |
162 | ||
163 | ret = lxc_try_preserve_namespace(handler, ns_idx, | |
164 | ns_info[ns_idx].proc_name); | |
165 | if (ret < 0) { | |
166 | if (ret == -ENOENT) { | |
167 | SYSERROR("Kernel does not support preserving %s namespaces", ns); | |
168 | continue; | |
169 | } | |
170 | ||
171 | /* | |
172 | * Handle kernels that do not support interacting with | |
173 | * namespaces through procfs. | |
174 | */ | |
175 | lxc_put_nsfds(handler); | |
176 | return log_error_errno(false, errno, "Failed to preserve %s namespace", ns); | |
177 | } | |
178 | } | |
179 | ||
180 | return true; | |
181 | } | |
182 | ||
183 | static inline bool match_stdfds(int fd) | |
184 | { | |
185 | return (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO); | |
186 | } | |
187 | ||
188 | #ifdef HAVE_DLOG | |
189 | static bool match_dlog_fds(struct dirent *direntp) | |
190 | { | |
191 | char path[PATH_MAX] = {0}; | |
192 | char link[PATH_MAX] = {0}; | |
193 | ssize_t linklen; | |
194 | int ret; | |
195 | ||
196 | ret = strnprintf(path, sizeof(path), "/proc/self/fd/%s", direntp->d_name); | |
197 | if (ret < 0) | |
198 | return log_error(false, "Failed to create file descriptor name"); | |
199 | ||
200 | linklen = readlink(path, link, PATH_MAX); | |
201 | if (linklen < 0) | |
202 | return log_error(false, "Failed to read link path - \"%s\"", path); | |
203 | else if (linklen >= PATH_MAX) | |
204 | return log_error(false, "The name of link path is too long - \"%s\"", path); | |
205 | ||
206 | if (strequal(link, "/dev/log_main") || | |
207 | strequal(link, "/dev/log_system") || | |
208 | strequal(link, "/dev/log_radio")) | |
209 | return true; | |
210 | ||
211 | return false; | |
212 | } | |
213 | #endif | |
214 | ||
215 | int lxc_check_inherited(struct lxc_conf *conf, bool closeall, | |
216 | int *fds_to_ignore, size_t len_fds) | |
217 | { | |
218 | int fd, fddir; | |
219 | size_t i; | |
220 | DIR *dir; | |
221 | struct dirent *direntp; | |
222 | ||
223 | if (conf && conf->close_all_fds) | |
224 | closeall = true; | |
225 | ||
226 | /* | |
227 | * Disable syslog at this point to avoid the above logging | |
228 | * function to open a new fd and make the check_inherited function | |
229 | * enter an infinite loop. | |
230 | */ | |
231 | lxc_log_syslog_disable(); | |
232 | ||
233 | restart: | |
234 | dir = opendir("/proc/self/fd"); | |
235 | if (!dir) | |
236 | return log_warn(-1, "Failed to open directory"); | |
237 | ||
238 | fddir = dirfd(dir); | |
239 | ||
240 | while ((direntp = readdir(dir))) { | |
241 | int ret; | |
242 | struct lxc_list *cur; | |
243 | bool matched = false; | |
244 | ||
245 | if (strequal(direntp->d_name, ".")) | |
246 | continue; | |
247 | ||
248 | if (strequal(direntp->d_name, "..")) | |
249 | continue; | |
250 | ||
251 | ret = lxc_safe_int(direntp->d_name, &fd); | |
252 | if (ret < 0) { | |
253 | INFO("Could not parse file descriptor for \"%s\"", direntp->d_name); | |
254 | continue; | |
255 | } | |
256 | ||
257 | for (i = 0; i < len_fds; i++) | |
258 | if (fds_to_ignore[i] == fd) | |
259 | break; | |
260 | ||
261 | if (fd == fddir || fd == lxc_log_fd || | |
262 | (i < len_fds && fd == fds_to_ignore[i])) | |
263 | continue; | |
264 | ||
265 | /* Keep state clients that wait on reboots. */ | |
266 | if (conf) { | |
267 | lxc_list_for_each(cur, &conf->state_clients) { | |
268 | struct lxc_state_client *client = cur->elem; | |
269 | ||
270 | if (client->clientfd != fd) | |
271 | continue; | |
272 | ||
273 | matched = true; | |
274 | break; | |
275 | } | |
276 | } | |
277 | ||
278 | if (matched) | |
279 | continue; | |
280 | ||
281 | if (current_config && fd == current_config->logfd) | |
282 | continue; | |
283 | ||
284 | if (match_stdfds(fd)) | |
285 | continue; | |
286 | ||
287 | #ifdef HAVE_DLOG | |
288 | if (match_dlog_fds(direntp)) | |
289 | continue; | |
290 | ||
291 | #endif | |
292 | if (closeall) { | |
293 | if (close(fd)) | |
294 | SYSINFO("Closed inherited fd %d", fd); | |
295 | else | |
296 | INFO("Closed inherited fd %d", fd); | |
297 | closedir(dir); | |
298 | goto restart; | |
299 | } | |
300 | WARN("Inherited fd %d", fd); | |
301 | } | |
302 | closedir(dir); | |
303 | ||
304 | /* | |
305 | * Only enable syslog at this point to avoid the above logging | |
306 | * function to open a new fd and make the check_inherited function | |
307 | * enter an infinite loop. | |
308 | */ | |
309 | lxc_log_syslog_enable(); | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
314 | static int setup_signal_fd(sigset_t *oldmask) | |
315 | { | |
316 | int ret; | |
317 | sigset_t mask; | |
318 | const int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH}; | |
319 | ||
320 | /* Block everything except serious error signals. */ | |
321 | ret = sigfillset(&mask); | |
322 | if (ret < 0) | |
323 | return -EBADF; | |
324 | ||
325 | for (int sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) { | |
326 | ret = sigdelset(&mask, signals[sig]); | |
327 | if (ret < 0) | |
328 | return -EBADF; | |
329 | } | |
330 | ||
331 | ret = pthread_sigmask(SIG_BLOCK, &mask, oldmask); | |
332 | if (ret < 0) | |
333 | return log_error_errno(-EBADF, errno, | |
334 | "Failed to set signal mask"); | |
335 | ||
336 | ret = signalfd(-1, &mask, SFD_CLOEXEC); | |
337 | if (ret < 0) | |
338 | return log_error_errno(-EBADF, | |
339 | errno, "Failed to create signal file descriptor"); | |
340 | ||
341 | TRACE("Created signal file descriptor %d", ret); | |
342 | ||
343 | return ret; | |
344 | } | |
345 | ||
346 | static int signal_handler(int fd, uint32_t events, void *data, | |
347 | struct lxc_epoll_descr *descr) | |
348 | { | |
349 | int ret; | |
350 | siginfo_t info; | |
351 | struct signalfd_siginfo siginfo; | |
352 | struct lxc_handler *hdlr = data; | |
353 | ||
354 | ret = lxc_read_nointr(fd, &siginfo, sizeof(siginfo)); | |
355 | if (ret < 0) | |
356 | return log_error(LXC_MAINLOOP_ERROR, "Failed to read signal info from signal file descriptor %d", fd); | |
357 | ||
358 | if (ret != sizeof(siginfo)) | |
359 | return log_error(LXC_MAINLOOP_ERROR, "Unexpected size for struct signalfd_siginfo"); | |
360 | ||
361 | /* Check whether init is running. */ | |
362 | info.si_pid = 0; | |
363 | ret = waitid(P_PID, hdlr->pid, &info, WEXITED | WNOWAIT | WNOHANG); | |
364 | if (ret == 0 && info.si_pid == hdlr->pid) | |
365 | hdlr->init_died = true; | |
366 | ||
367 | /* Try to figure out a reasonable exit status to report. */ | |
368 | if (hdlr->init_died) { | |
369 | switch (info.si_code) { | |
370 | case CLD_EXITED: | |
371 | hdlr->exit_status = info.si_status << 8; | |
372 | break; | |
373 | case CLD_KILLED: | |
374 | case CLD_DUMPED: | |
375 | case CLD_STOPPED: | |
376 | hdlr->exit_status = info.si_status << 8 | 0x7f; | |
377 | break; | |
378 | case CLD_CONTINUED: | |
379 | /* Huh? The waitid() told us it's dead *and* continued? */ | |
380 | WARN("Init %d dead and continued?", hdlr->pid); | |
381 | hdlr->exit_status = 1; | |
382 | break; | |
383 | default: | |
384 | ERROR("Unknown si_code: %d", info.si_code); | |
385 | hdlr->exit_status = 1; | |
386 | } | |
387 | } | |
388 | ||
389 | if (siginfo.ssi_signo == SIGHUP) { | |
390 | if (hdlr->pidfd >= 0) | |
391 | lxc_raw_pidfd_send_signal(hdlr->pidfd, SIGTERM, NULL, 0); | |
392 | else | |
393 | kill(hdlr->pid, SIGTERM); | |
394 | INFO("Killing %d since terminal hung up", hdlr->pid); | |
395 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE | |
396 | : LXC_MAINLOOP_CONTINUE; | |
397 | } | |
398 | ||
399 | if (siginfo.ssi_signo != SIGCHLD) { | |
400 | if (hdlr->pidfd >= 0) | |
401 | lxc_raw_pidfd_send_signal(hdlr->pidfd, | |
402 | siginfo.ssi_signo, NULL, 0); | |
403 | else | |
404 | kill(hdlr->pid, siginfo.ssi_signo); | |
405 | INFO("Forwarded signal %d to pid %d", siginfo.ssi_signo, hdlr->pid); | |
406 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE | |
407 | : LXC_MAINLOOP_CONTINUE; | |
408 | } | |
409 | ||
410 | /* More robustness, protect ourself from a SIGCHLD sent | |
411 | * by a process different from the container init. | |
412 | */ | |
413 | if (siginfo.ssi_pid != hdlr->pid) { | |
414 | NOTICE("Received %d from pid %d instead of container init %d", | |
415 | siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid); | |
416 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE | |
417 | : LXC_MAINLOOP_CONTINUE; | |
418 | } | |
419 | ||
420 | if (siginfo.ssi_code == CLD_STOPPED) { | |
421 | INFO("Container init process was stopped"); | |
422 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE | |
423 | : LXC_MAINLOOP_CONTINUE; | |
424 | } | |
425 | ||
426 | if (siginfo.ssi_code == CLD_CONTINUED) { | |
427 | INFO("Container init process was continued"); | |
428 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE | |
429 | : LXC_MAINLOOP_CONTINUE; | |
430 | } | |
431 | ||
432 | return log_debug(LXC_MAINLOOP_CLOSE, "Container init process %d exited", hdlr->pid); | |
433 | } | |
434 | ||
435 | int lxc_serve_state_clients(const char *name, struct lxc_handler *handler, | |
436 | lxc_state_t state) | |
437 | { | |
438 | size_t retlen; | |
439 | ssize_t ret; | |
440 | struct lxc_list *cur, *next; | |
441 | struct lxc_msg msg = {.type = lxc_msg_state, .value = state}; | |
442 | ||
443 | if (state == THAWED) | |
444 | handler->state = RUNNING; | |
445 | else | |
446 | handler->state = state; | |
447 | ||
448 | TRACE("Set container state to %s", lxc_state2str(state)); | |
449 | ||
450 | if (lxc_list_empty(&handler->conf->state_clients)) | |
451 | return log_trace(0, "No state clients registered"); | |
452 | ||
453 | retlen = strlcpy(msg.name, name, sizeof(msg.name)); | |
454 | if (retlen >= sizeof(msg.name)) | |
455 | return -E2BIG; | |
456 | ||
457 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { | |
458 | struct lxc_state_client *client = cur->elem; | |
459 | ||
460 | if (client->states[state] == 0) { | |
461 | TRACE("State %s not registered for state client %d", | |
462 | lxc_state2str(state), client->clientfd); | |
463 | continue; | |
464 | } | |
465 | ||
466 | TRACE("Sending state %s to state client %d", | |
467 | lxc_state2str(state), client->clientfd); | |
468 | ||
469 | ret = lxc_send_nointr(client->clientfd, &msg, sizeof(msg), MSG_NOSIGNAL); | |
470 | if (ret <= 0) | |
471 | SYSERROR("Failed to send message to client"); | |
472 | ||
473 | /* kick client from list */ | |
474 | lxc_list_del(cur); | |
475 | close(client->clientfd); | |
476 | free(cur->elem); | |
477 | free(cur); | |
478 | } | |
479 | ||
480 | return 0; | |
481 | } | |
482 | ||
483 | static int lxc_serve_state_socket_pair(const char *name, | |
484 | struct lxc_handler *handler, | |
485 | lxc_state_t state) | |
486 | { | |
487 | ssize_t ret; | |
488 | ||
489 | if (!handler->daemonize || | |
490 | handler->state_socket_pair[1] < 0 || | |
491 | state == STARTING) | |
492 | return 0; | |
493 | ||
494 | /* Close read end of the socket pair. */ | |
495 | close_prot_errno_disarm(handler->state_socket_pair[0]); | |
496 | ||
497 | again: | |
498 | ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1], | |
499 | &(int){state}, sizeof(int)); | |
500 | if (ret < 0) { | |
501 | SYSERROR("Failed to send state to %d", handler->state_socket_pair[1]); | |
502 | ||
503 | if (errno == EINTR) | |
504 | goto again; | |
505 | ||
506 | return -1; | |
507 | } | |
508 | ||
509 | if (ret != sizeof(int)) | |
510 | return log_error(-1, "Message too long : %d", handler->state_socket_pair[1]); | |
511 | ||
512 | TRACE("Sent container state \"%s\" to %d", lxc_state2str(state), | |
513 | handler->state_socket_pair[1]); | |
514 | ||
515 | /* Close write end of the socket pair. */ | |
516 | close_prot_errno_disarm(handler->state_socket_pair[1]); | |
517 | ||
518 | return 0; | |
519 | } | |
520 | ||
521 | int lxc_set_state(const char *name, struct lxc_handler *handler, | |
522 | lxc_state_t state) | |
523 | { | |
524 | int ret; | |
525 | ||
526 | ret = lxc_serve_state_socket_pair(name, handler, state); | |
527 | if (ret < 0) | |
528 | return log_error(-1, "Failed to synchronize via anonymous pair of unix sockets"); | |
529 | ||
530 | ret = lxc_serve_state_clients(name, handler, state); | |
531 | if (ret < 0) | |
532 | return -1; | |
533 | ||
534 | /* This function will try to connect to the legacy lxc-monitord state | |
535 | * server and only exists for backwards compatibility. | |
536 | */ | |
537 | lxc_monitor_send_state(name, state, handler->lxcpath); | |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
542 | int lxc_poll(const char *name, struct lxc_handler *handler) | |
543 | { | |
544 | int ret; | |
545 | bool has_console = true; | |
546 | struct lxc_epoll_descr descr, descr_console; | |
547 | ||
548 | if (handler->conf->console.path && | |
549 | strequal(handler->conf->console.path, "none")) | |
550 | has_console = false; | |
551 | ||
552 | ret = lxc_mainloop_open(&descr); | |
553 | if (ret < 0) { | |
554 | ERROR("Failed to create mainloop"); | |
555 | goto out_sigfd; | |
556 | } | |
557 | ||
558 | if (has_console) { | |
559 | ret = lxc_mainloop_open(&descr_console); | |
560 | if (ret < 0) { | |
561 | ERROR("Failed to create console mainloop"); | |
562 | goto out_mainloop; | |
563 | } | |
564 | } | |
565 | ||
566 | ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler); | |
567 | if (ret < 0) { | |
568 | ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd); | |
569 | goto out_mainloop_console; | |
570 | } | |
571 | ||
572 | ret = lxc_seccomp_setup_proxy(&handler->conf->seccomp, &descr, handler); | |
573 | if (ret < 0) { | |
574 | ERROR("Failed to setup seccomp proxy"); | |
575 | goto out_mainloop_console; | |
576 | } | |
577 | ||
578 | if (has_console) { | |
579 | struct lxc_terminal *console = &handler->conf->console; | |
580 | ||
581 | ret = lxc_terminal_mainloop_add(&descr, console); | |
582 | if (ret < 0) { | |
583 | ERROR("Failed to add console handlers to mainloop"); | |
584 | goto out_mainloop_console; | |
585 | } | |
586 | ||
587 | ret = lxc_terminal_mainloop_add(&descr_console, console); | |
588 | if (ret < 0) { | |
589 | ERROR("Failed to add console handlers to console mainloop"); | |
590 | goto out_mainloop_console; | |
591 | } | |
592 | ||
593 | handler->conf->console.descr = &descr; | |
594 | } | |
595 | ||
596 | ret = lxc_cmd_mainloop_add(name, &descr, handler); | |
597 | if (ret < 0) { | |
598 | ERROR("Failed to add command handler to mainloop"); | |
599 | goto out_mainloop_console; | |
600 | } | |
601 | ||
602 | TRACE("Mainloop is ready"); | |
603 | ||
604 | ret = lxc_mainloop(&descr, -1); | |
605 | close_prot_errno_disarm(descr.epfd); | |
606 | if (ret < 0 || !handler->init_died) | |
607 | goto out_mainloop_console; | |
608 | ||
609 | if (has_console) | |
610 | ret = lxc_mainloop(&descr_console, 0); | |
611 | ||
612 | out_mainloop_console: | |
613 | if (has_console) { | |
614 | lxc_mainloop_close(&descr_console); | |
615 | TRACE("Closed console mainloop"); | |
616 | } | |
617 | ||
618 | out_mainloop: | |
619 | lxc_mainloop_close(&descr); | |
620 | TRACE("Closed mainloop"); | |
621 | ||
622 | out_sigfd: | |
623 | TRACE("Closed signal file descriptor %d", handler->sigfd); | |
624 | close_prot_errno_disarm(handler->sigfd); | |
625 | ||
626 | return ret; | |
627 | } | |
628 | ||
629 | void lxc_put_handler(struct lxc_handler *handler) | |
630 | { | |
631 | close_prot_errno_disarm(handler->pidfd); | |
632 | close_prot_errno_disarm(handler->sigfd); | |
633 | lxc_put_nsfds(handler); | |
634 | if (handler->conf && handler->conf->reboot == REBOOT_NONE) | |
635 | close_prot_errno_disarm(handler->conf->maincmd_fd); | |
636 | close_prot_errno_disarm(handler->monitor_status_fd); | |
637 | close_prot_errno_disarm(handler->state_socket_pair[0]); | |
638 | close_prot_errno_disarm(handler->state_socket_pair[1]); | |
639 | cgroup_exit(handler->cgroup_ops); | |
640 | if (handler->conf && handler->conf->reboot == REBOOT_NONE) | |
641 | free_disarm(handler); | |
642 | else | |
643 | handler->conf = NULL; | |
644 | } | |
645 | ||
646 | struct lxc_handler *lxc_init_handler(struct lxc_handler *old, | |
647 | const char *name, struct lxc_conf *conf, | |
648 | const char *lxcpath, bool daemonize) | |
649 | { | |
650 | int nr_keep_fds = 0; | |
651 | int ret; | |
652 | struct lxc_handler *handler; | |
653 | ||
654 | if (!old) | |
655 | handler = zalloc(sizeof(*handler)); | |
656 | else | |
657 | handler = old; | |
658 | if (!handler) | |
659 | return NULL; | |
660 | ||
661 | /* Note that am_guest_unpriv() checks the effective uid. We | |
662 | * probably don't care if we are real root only if we are running | |
663 | * as root so this should be fine. | |
664 | */ | |
665 | handler->am_root = !am_guest_unpriv(); | |
666 | handler->conf = conf; | |
667 | handler->lxcpath = lxcpath; | |
668 | handler->init_died = false; | |
669 | handler->data_sock[0] = -EBADF; | |
670 | handler->data_sock[1] = -EBADF; | |
671 | handler->monitor_status_fd = -EBADF; | |
672 | handler->pidfd = -EBADF; | |
673 | handler->sigfd = -EBADF; | |
674 | handler->state_socket_pair[0] = -EBADF; | |
675 | handler->state_socket_pair[1] = -EBADF; | |
676 | if (handler->conf->reboot == REBOOT_NONE) | |
677 | lxc_list_init(&handler->conf->state_clients); | |
678 | ||
679 | for (lxc_namespace_t idx = 0; idx < LXC_NS_MAX; idx++) { | |
680 | handler->nsfd[idx] = -EBADF; | |
681 | ||
682 | if (handler->conf->reboot == REBOOT_NONE) | |
683 | continue; | |
684 | ||
685 | handler->nsfd_paths[idx][0] = '\0'; | |
686 | handler->hook_argv[idx] = NULL; | |
687 | ||
688 | if (handler->hook_argc != 0) | |
689 | handler->hook_argc = 0; | |
690 | } | |
691 | ||
692 | handler->name = name; | |
693 | if (daemonize) | |
694 | handler->transient_pid = lxc_raw_getpid(); | |
695 | else | |
696 | handler->transient_pid = -1; | |
697 | ||
698 | if (daemonize && handler->conf->reboot == REBOOT_NONE) { | |
699 | /* Create socketpair() to synchronize on daemonized startup. | |
700 | * When the container reboots we don't need to synchronize | |
701 | * again currently so don't open another socketpair(). | |
702 | */ | |
703 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
704 | handler->state_socket_pair); | |
705 | if (ret < 0) { | |
706 | ERROR("Failed to create anonymous pair of unix sockets"); | |
707 | goto on_error; | |
708 | } | |
709 | ||
710 | TRACE("Created anonymous pair {%d,%d} of unix sockets", | |
711 | handler->state_socket_pair[0], | |
712 | handler->state_socket_pair[1]); | |
713 | handler->keep_fds[nr_keep_fds++] = handler->state_socket_pair[0]; | |
714 | handler->keep_fds[nr_keep_fds++] = handler->state_socket_pair[1]; | |
715 | } | |
716 | ||
717 | if (handler->conf->reboot == REBOOT_NONE) { | |
718 | handler->conf->maincmd_fd = lxc_server_init(name, lxcpath, "command"); | |
719 | if (handler->conf->maincmd_fd < 0) { | |
720 | ERROR("Failed to set up command socket"); | |
721 | goto on_error; | |
722 | } | |
723 | handler->keep_fds[nr_keep_fds++] = handler->conf->maincmd_fd; | |
724 | } | |
725 | ||
726 | TRACE("Unix domain socket %d for command server is ready", | |
727 | handler->conf->maincmd_fd); | |
728 | ||
729 | return handler; | |
730 | ||
731 | on_error: | |
732 | lxc_put_handler(handler); | |
733 | ||
734 | return NULL; | |
735 | } | |
736 | ||
737 | int lxc_init(const char *name, struct lxc_handler *handler) | |
738 | { | |
739 | __do_close int status_fd = -EBADF; | |
740 | int ret; | |
741 | const char *loglevel; | |
742 | struct lxc_conf *conf = handler->conf; | |
743 | ||
744 | handler->monitor_pid = lxc_raw_getpid(); | |
745 | status_fd = open("/proc/self/status", O_RDONLY | O_CLOEXEC); | |
746 | if (status_fd < 0) | |
747 | return log_error_errno(-1, errno, "Failed to open monitor status fd"); | |
748 | ||
749 | handler->lsm_ops = lsm_init_static(); | |
750 | TRACE("Initialized LSM"); | |
751 | ||
752 | /* Begin by setting the state to STARTING. */ | |
753 | ret = lxc_set_state(name, handler, STARTING); | |
754 | if (ret < 0) | |
755 | return log_error(-1, "Failed to set state to \"%s\"", lxc_state2str(STARTING)); | |
756 | TRACE("Set container state to \"STARTING\""); | |
757 | ||
758 | /* Start of environment variable setup for hooks. */ | |
759 | ret = setenv("LXC_NAME", name, 1); | |
760 | if (ret < 0) | |
761 | SYSERROR("Failed to set environment variable: LXC_NAME=%s", name); | |
762 | ||
763 | if (conf->rcfile) { | |
764 | ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1); | |
765 | if (ret < 0) | |
766 | SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s", conf->rcfile); | |
767 | } | |
768 | ||
769 | if (conf->rootfs.mount) { | |
770 | ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1); | |
771 | if (ret < 0) | |
772 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount); | |
773 | } | |
774 | ||
775 | if (conf->rootfs.path) { | |
776 | ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1); | |
777 | if (ret < 0) | |
778 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s", conf->rootfs.path); | |
779 | } | |
780 | ||
781 | if (conf->console.path) { | |
782 | ret = setenv("LXC_CONSOLE", conf->console.path, 1); | |
783 | if (ret < 0) | |
784 | SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s", conf->console.path); | |
785 | } | |
786 | ||
787 | if (conf->console.log_path) { | |
788 | ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1); | |
789 | if (ret < 0) | |
790 | SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s", conf->console.log_path); | |
791 | } | |
792 | ||
793 | if (cgns_supported()) { | |
794 | ret = setenv("LXC_CGNS_AWARE", "1", 1); | |
795 | if (ret < 0) | |
796 | SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1"); | |
797 | } | |
798 | ||
799 | loglevel = lxc_log_priority_to_string(lxc_log_get_level()); | |
800 | ret = setenv("LXC_LOG_LEVEL", loglevel, 1); | |
801 | if (ret < 0) | |
802 | SYSERROR("Set environment variable LXC_LOG_LEVEL=%s", loglevel); | |
803 | ||
804 | if (conf->hooks_version == 0) | |
805 | ret = setenv("LXC_HOOK_VERSION", "0", 1); | |
806 | else | |
807 | ret = setenv("LXC_HOOK_VERSION", "1", 1); | |
808 | if (ret < 0) | |
809 | SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version); | |
810 | /* End of environment variable setup for hooks. */ | |
811 | ||
812 | TRACE("Set environment variables"); | |
813 | ||
814 | ret = run_lxc_hooks(name, "pre-start", conf, NULL); | |
815 | if (ret < 0) | |
816 | return log_error(-1, "Failed to run lxc.hook.pre-start for container \"%s\"", name); | |
817 | TRACE("Ran pre-start hooks"); | |
818 | ||
819 | /* The signal fd has to be created before forking otherwise if the child | |
820 | * process exits before we setup the signal fd, the event will be lost | |
821 | * and the command will be stuck. | |
822 | */ | |
823 | handler->sigfd = setup_signal_fd(&handler->oldmask); | |
824 | if (handler->sigfd < 0) | |
825 | return log_error(-1, "Failed to setup SIGCHLD fd handler."); | |
826 | TRACE("Set up signal fd"); | |
827 | ||
828 | /* Do this after setting up signals since it might unblock SIGWINCH. */ | |
829 | ret = lxc_terminal_setup(conf); | |
830 | if (ret < 0) { | |
831 | ERROR("Failed to create console"); | |
832 | goto out_restore_sigmask; | |
833 | } | |
834 | TRACE("Created console"); | |
835 | ||
836 | handler->cgroup_ops = cgroup_init(handler->conf); | |
837 | if (!handler->cgroup_ops) { | |
838 | ERROR("Failed to initialize cgroup driver"); | |
839 | goto out_delete_terminal; | |
840 | } | |
841 | TRACE("Initialized cgroup driver"); | |
842 | ||
843 | ret = lxc_read_seccomp_config(conf); | |
844 | if (ret < 0) | |
845 | return log_error(-1, "Failed loading seccomp policy"); | |
846 | TRACE("Read seccomp policy"); | |
847 | ||
848 | ret = handler->lsm_ops->prepare(handler->lsm_ops, conf, handler->lxcpath); | |
849 | if (ret < 0) { | |
850 | ERROR("Failed to initialize LSM"); | |
851 | goto out_delete_terminal; | |
852 | } | |
853 | TRACE("Initialized LSM"); | |
854 | ||
855 | INFO("Container \"%s\" is initialized", name); | |
856 | handler->monitor_status_fd = move_fd(status_fd); | |
857 | return 0; | |
858 | ||
859 | out_delete_terminal: | |
860 | lxc_terminal_delete(&handler->conf->console); | |
861 | ||
862 | out_restore_sigmask: | |
863 | (void)pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); | |
864 | ||
865 | return -1; | |
866 | } | |
867 | ||
868 | void lxc_expose_namespace_environment(const struct lxc_handler *handler) | |
869 | { | |
870 | for (lxc_namespace_t i = 0; i < LXC_NS_MAX; i++) { | |
871 | int ret; | |
872 | const char *fd_path; | |
873 | ||
874 | if (handler->nsfd[i] < 0) | |
875 | continue; | |
876 | ||
877 | fd_path = handler->nsfd_paths[i] + strcspn(handler->nsfd_paths[i], "/"); | |
878 | ret = setenv(ns_info[i].env_name, fd_path, 1); | |
879 | if (ret < 0) | |
880 | SYSERROR("Failed to set environment variable %s=%s", | |
881 | ns_info[i].env_name, fd_path); | |
882 | else | |
883 | TRACE("Set environment variable %s=%s", | |
884 | ns_info[i].env_name, fd_path); | |
885 | } | |
886 | } | |
887 | ||
888 | void lxc_end(struct lxc_handler *handler) | |
889 | { | |
890 | int ret; | |
891 | struct lxc_list *cur, *next; | |
892 | const char *name = handler->name; | |
893 | struct cgroup_ops *cgroup_ops = handler->cgroup_ops; | |
894 | ||
895 | /* The STOPPING state is there for future cleanup code which can take | |
896 | * awhile. | |
897 | */ | |
898 | lxc_set_state(name, handler, STOPPING); | |
899 | ||
900 | /* Passing information to hooks via environment variables. */ | |
901 | if (handler->conf->hooks_version > 0) | |
902 | lxc_expose_namespace_environment(handler); | |
903 | ||
904 | if (handler->conf->reboot > REBOOT_NONE) { | |
905 | ret = setenv("LXC_TARGET", "reboot", 1); | |
906 | if (ret < 0) | |
907 | SYSERROR("Failed to set environment variable: LXC_TARGET=reboot"); | |
908 | } | |
909 | ||
910 | if (handler->conf->reboot == REBOOT_NONE) { | |
911 | ret = setenv("LXC_TARGET", "stop", 1); | |
912 | if (ret < 0) | |
913 | SYSERROR("Failed to set environment variable: LXC_TARGET=stop"); | |
914 | } | |
915 | ||
916 | if (handler->conf->hooks_version == 0) | |
917 | ret = run_lxc_hooks(name, "stop", handler->conf, handler->hook_argv); | |
918 | else | |
919 | ret = run_lxc_hooks(name, "stop", handler->conf, NULL); | |
920 | if (ret < 0) | |
921 | ERROR("Failed to run \"lxc.hook.stop\" hook"); | |
922 | ||
923 | handler->lsm_ops->cleanup(handler->lsm_ops, handler->conf, handler->lxcpath); | |
924 | ||
925 | if (cgroup_ops) { | |
926 | cgroup_ops->payload_destroy(cgroup_ops, handler); | |
927 | cgroup_ops->monitor_destroy(cgroup_ops, handler); | |
928 | } | |
929 | ||
930 | put_lxc_rootfs(&handler->conf->rootfs, true); | |
931 | ||
932 | if (handler->conf->reboot == REBOOT_NONE) { | |
933 | /* For all new state clients simply close the command socket. | |
934 | * This will inform all state clients that the container is | |
935 | * STOPPED and also prevents a race between a open()/close() on | |
936 | * the command socket causing a new process to get ECONNREFUSED | |
937 | * because we haven't yet closed the command socket. | |
938 | */ | |
939 | close_prot_errno_disarm(handler->conf->maincmd_fd); | |
940 | TRACE("Closed command socket"); | |
941 | ||
942 | /* This function will try to connect to the legacy lxc-monitord | |
943 | * state server and only exists for backwards compatibility. | |
944 | */ | |
945 | lxc_monitor_send_state(name, STOPPED, handler->lxcpath); | |
946 | ||
947 | /* The command socket is closed so no one can acces the command | |
948 | * socket anymore so there's no need to lock it. | |
949 | */ | |
950 | handler->state = STOPPED; | |
951 | TRACE("Set container state to \"STOPPED\""); | |
952 | } else { | |
953 | lxc_set_state(name, handler, STOPPED); | |
954 | TRACE("Set container state to \"STOPPED\""); | |
955 | } | |
956 | ||
957 | /* Avoid lingering namespace references. */ | |
958 | lxc_put_nsfds(handler); | |
959 | ||
960 | ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL); | |
961 | if (ret < 0) { | |
962 | ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name); | |
963 | if (handler->conf->reboot > REBOOT_NONE) { | |
964 | WARN("Container will be stopped instead of rebooted"); | |
965 | handler->conf->reboot = REBOOT_NONE; | |
966 | ||
967 | ret = setenv("LXC_TARGET", "stop", 1); | |
968 | if (ret < 0) | |
969 | WARN("Failed to set environment variable: LXC_TARGET=stop"); | |
970 | } | |
971 | } | |
972 | ||
973 | /* Reset mask set by setup_signal_fd. */ | |
974 | ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); | |
975 | if (ret < 0) | |
976 | SYSWARN("Failed to restore signal mask"); | |
977 | ||
978 | lxc_terminal_delete(&handler->conf->console); | |
979 | lxc_delete_tty(&handler->conf->ttys); | |
980 | close_prot_errno_disarm(handler->conf->devpts_fd); | |
981 | ||
982 | /* The command socket is now closed, no more state clients can register | |
983 | * themselves from now on. So free the list of state clients. | |
984 | */ | |
985 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { | |
986 | struct lxc_state_client *client = cur->elem; | |
987 | ||
988 | /* Keep state clients that want to be notified about reboots. */ | |
989 | if ((handler->conf->reboot > REBOOT_NONE) && | |
990 | (client->states[RUNNING] == 2)) | |
991 | continue; | |
992 | ||
993 | /* close state client socket */ | |
994 | lxc_list_del(cur); | |
995 | close(client->clientfd); | |
996 | free(cur->elem); | |
997 | free(cur); | |
998 | } | |
999 | ||
1000 | if (handler->conf->ephemeral == 1 && handler->conf->reboot != REBOOT_REQ) | |
1001 | lxc_destroy_container_on_signal(handler, name); | |
1002 | ||
1003 | lxc_put_handler(handler); | |
1004 | } | |
1005 | ||
1006 | void lxc_abort(struct lxc_handler *handler) | |
1007 | { | |
1008 | int ret = 0; | |
1009 | int status; | |
1010 | ||
1011 | lxc_set_state(handler->name, handler, ABORTING); | |
1012 | ||
1013 | if (handler->pidfd >= 0) { | |
1014 | ret = lxc_raw_pidfd_send_signal(handler->pidfd, SIGKILL, NULL, 0); | |
1015 | if (ret) | |
1016 | SYSWARN("Failed to send SIGKILL via pidfd %d for process %d", | |
1017 | handler->pidfd, handler->pid); | |
1018 | } | |
1019 | ||
1020 | if ((!ret || errno != ESRCH) && handler->pid > 0) | |
1021 | if (kill(handler->pid, SIGKILL)) | |
1022 | SYSWARN("Failed to send SIGKILL to %d", handler->pid); | |
1023 | ||
1024 | do { | |
1025 | ret = waitpid(-1, &status, 0); | |
1026 | } while (ret > 0); | |
1027 | } | |
1028 | ||
1029 | static int do_start(void *data) | |
1030 | { | |
1031 | struct lxc_handler *handler = data; | |
1032 | __lxc_unused __do_close int data_sock0 = handler->data_sock[0], | |
1033 | data_sock1 = handler->data_sock[1]; | |
1034 | __do_close int devnull_fd = -EBADF, status_fd = -EBADF; | |
1035 | int ret; | |
1036 | uid_t new_uid; | |
1037 | gid_t new_gid; | |
1038 | struct lxc_list *iterator; | |
1039 | uid_t nsuid = 0; | |
1040 | gid_t nsgid = 0; | |
1041 | ||
1042 | lxc_sync_fini_parent(handler); | |
1043 | ||
1044 | if (lxc_abstract_unix_recv_one_fd(data_sock1, &status_fd, NULL, 0) < 0) { | |
1045 | ERROR("Failed to receive status file descriptor to child process"); | |
1046 | goto out_warn_father; | |
1047 | } | |
1048 | ||
1049 | /* This prctl must be before the synchro, so if the parent dies before | |
1050 | * we set the parent death signal, we will detect its death with the | |
1051 | * synchro right after, otherwise we have a window where the parent can | |
1052 | * exit before we set the pdeath signal leading to a unsupervized | |
1053 | * container. | |
1054 | */ | |
1055 | ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd); | |
1056 | if (ret < 0) { | |
1057 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
1058 | goto out_warn_father; | |
1059 | } | |
1060 | ||
1061 | ret = lxc_ambient_caps_up(); | |
1062 | if (ret < 0) { | |
1063 | ERROR("Failed to raise ambient capabilities"); | |
1064 | goto out_warn_father; | |
1065 | } | |
1066 | ||
1067 | ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); | |
1068 | if (ret < 0) { | |
1069 | SYSERROR("Failed to set signal mask"); | |
1070 | goto out_warn_father; | |
1071 | } | |
1072 | ||
1073 | if (!lxc_sync_wait_parent(handler, START_SYNC_STARTUP)) | |
1074 | goto out_warn_father; | |
1075 | ||
1076 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See | |
1077 | * https://github.com/lxc/lxd/issues/1978. | |
1078 | */ | |
1079 | if (handler->ns_unshare_flags & CLONE_NEWNET) { | |
1080 | ret = unshare(CLONE_NEWNET); | |
1081 | if (ret < 0) { | |
1082 | SYSERROR("Failed to unshare CLONE_NEWNET"); | |
1083 | goto out_warn_father; | |
1084 | } | |
1085 | INFO("Unshared CLONE_NEWNET"); | |
1086 | } | |
1087 | ||
1088 | /* If we are in a new user namespace, become root there to have | |
1089 | * privilege over our namespace. | |
1090 | */ | |
1091 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
1092 | if (!handler->conf->root_nsuid_map) | |
1093 | nsuid = handler->conf->init_uid; | |
1094 | ||
1095 | if (!handler->conf->root_nsgid_map) | |
1096 | nsgid = handler->conf->init_gid; | |
1097 | ||
1098 | /* Drop groups only after we switched to a valid gid in the new | |
1099 | * user namespace. | |
1100 | */ | |
1101 | if (!lxc_drop_groups() && | |
1102 | (handler->am_root || errno != EPERM)) | |
1103 | goto out_warn_father; | |
1104 | ||
1105 | if (!lxc_switch_uid_gid(nsuid, nsgid)) | |
1106 | goto out_warn_father; | |
1107 | ||
1108 | ret = prctl(PR_SET_DUMPABLE, prctl_arg(1), prctl_arg(0), | |
1109 | prctl_arg(0), prctl_arg(0)); | |
1110 | if (ret < 0) | |
1111 | goto out_warn_father; | |
1112 | ||
1113 | /* set{g,u}id() clears deathsignal */ | |
1114 | ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd); | |
1115 | if (ret < 0) { | |
1116 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
1117 | goto out_warn_father; | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | ret = access(handler->lxcpath, X_OK); | |
1122 | if (ret != 0) { | |
1123 | print_top_failing_dir(handler->lxcpath); | |
1124 | goto out_warn_father; | |
1125 | } | |
1126 | ||
1127 | /* In order to checkpoint restore, we need to have everything in the | |
1128 | * same mount namespace. However, some containers may not have a | |
1129 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
1130 | * can't set init's std fds to /dev/null by opening it from inside the | |
1131 | * container. | |
1132 | * | |
1133 | * If that's the case, fall back to using the host's /dev/null. This | |
1134 | * means that migration won't work, but at least we won't spew output | |
1135 | * where it isn't wanted. | |
1136 | */ | |
1137 | if (handler->daemonize && !handler->conf->autodev) { | |
1138 | char path[PATH_MAX]; | |
1139 | ||
1140 | ret = strnprintf(path, sizeof(path), "%s/dev/null", | |
1141 | handler->conf->rootfs.mount); | |
1142 | if (ret < 0) | |
1143 | goto out_warn_father; | |
1144 | ||
1145 | ret = access(path, F_OK); | |
1146 | if (ret != 0) { | |
1147 | devnull_fd = open_devnull(); | |
1148 | ||
1149 | if (devnull_fd < 0) | |
1150 | goto out_warn_father; | |
1151 | WARN("Using /dev/null from the host for container init's standard file descriptors. Migration will not work"); | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | /* | |
1156 | * Tell the parent task it can begin to configure the container and wait | |
1157 | * for it to finish. | |
1158 | */ | |
1159 | if (!lxc_sync_wake_parent(handler, START_SYNC_CONFIGURE)) | |
1160 | goto out_error; | |
1161 | ||
1162 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it | |
1163 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
1164 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
1165 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
1166 | * /proc/self/cgroup would show us: | |
1167 | * | |
1168 | * 8:cpuset:/lxc/c | |
1169 | * | |
1170 | * whereas it should actually show | |
1171 | * | |
1172 | * 8:cpuset:/ | |
1173 | */ | |
1174 | if (handler->ns_unshare_flags & CLONE_NEWCGROUP) { | |
1175 | ret = unshare(CLONE_NEWCGROUP); | |
1176 | if (ret < 0) { | |
1177 | if (errno != EINVAL) { | |
1178 | SYSERROR("Failed to unshare CLONE_NEWCGROUP"); | |
1179 | goto out_warn_father; | |
1180 | } | |
1181 | ||
1182 | handler->ns_clone_flags &= ~CLONE_NEWCGROUP; | |
1183 | SYSINFO("Kernel does not support CLONE_NEWCGROUP"); | |
1184 | } else { | |
1185 | INFO("Unshared CLONE_NEWCGROUP"); | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | if (handler->ns_unshare_flags & CLONE_NEWTIME) { | |
1190 | ret = unshare(CLONE_NEWTIME); | |
1191 | if (ret < 0) { | |
1192 | if (errno != EINVAL) { | |
1193 | SYSERROR("Failed to unshare CLONE_NEWTIME"); | |
1194 | goto out_warn_father; | |
1195 | } | |
1196 | ||
1197 | handler->ns_clone_flags &= ~CLONE_NEWTIME; | |
1198 | SYSINFO("Kernel does not support CLONE_NEWTIME"); | |
1199 | } else { | |
1200 | __do_close int timens_fd = -EBADF; | |
1201 | ||
1202 | INFO("Unshared CLONE_NEWTIME"); | |
1203 | ||
1204 | if (handler->conf->timens.s_boot) | |
1205 | ret = timens_offset_write(CLOCK_BOOTTIME, handler->conf->timens.s_boot, 0); | |
1206 | else if (handler->conf->timens.ns_boot) | |
1207 | ret = timens_offset_write(CLOCK_BOOTTIME, 0, handler->conf->timens.ns_boot); | |
1208 | if (ret) { | |
1209 | SYSERROR("Failed to write CLONE_BOOTTIME offset"); | |
1210 | goto out_warn_father; | |
1211 | } | |
1212 | TRACE("Wrote CLOCK_BOOTTIME offset"); | |
1213 | ||
1214 | if (handler->conf->timens.s_monotonic) | |
1215 | ret = timens_offset_write(CLOCK_MONOTONIC, handler->conf->timens.s_monotonic, 0); | |
1216 | else if (handler->conf->timens.ns_monotonic) | |
1217 | ret = timens_offset_write(CLOCK_MONOTONIC, 0, handler->conf->timens.ns_monotonic); | |
1218 | if (ret) { | |
1219 | SYSERROR("Failed to write CLONE_MONOTONIC offset"); | |
1220 | goto out_warn_father; | |
1221 | } | |
1222 | TRACE("Wrote CLOCK_MONOTONIC offset"); | |
1223 | ||
1224 | timens_fd = open("/proc/self/ns/time_for_children", O_RDONLY | O_CLOEXEC); | |
1225 | if (timens_fd < 0) { | |
1226 | SYSERROR("Failed to open \"/proc/self/ns/time_for_children\""); | |
1227 | goto out_warn_father; | |
1228 | } | |
1229 | ||
1230 | ret = setns(timens_fd, CLONE_NEWTIME); | |
1231 | if (ret) { | |
1232 | SYSERROR("Failed to setns(%d(\"/proc/self/ns/time_for_children\"))", timens_fd); | |
1233 | goto out_warn_father; | |
1234 | } | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | /* Add the requested environment variables to the current environment to | |
1239 | * allow them to be used by the various hooks, such as the start hook | |
1240 | * below. | |
1241 | */ | |
1242 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
1243 | ret = putenv((char *)iterator->elem); | |
1244 | if (ret < 0) { | |
1245 | SYSERROR("Failed to set environment variable: %s", | |
1246 | (char *)iterator->elem); | |
1247 | goto out_warn_father; | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | if (!lxc_sync_wait_parent(handler, START_SYNC_POST_CONFIGURE)) | |
1252 | goto out_warn_father; | |
1253 | ||
1254 | /* Setup the container, ip, names, utsname, ... */ | |
1255 | ret = lxc_setup(handler); | |
1256 | if (ret < 0) { | |
1257 | ERROR("Failed to setup container \"%s\"", handler->name); | |
1258 | goto out_warn_father; | |
1259 | } | |
1260 | ||
1261 | /* Set the label to change to when we exec(2) the container's init. */ | |
1262 | ret = handler->lsm_ops->process_label_set(handler->lsm_ops, NULL, handler->conf, true); | |
1263 | if (ret < 0) | |
1264 | goto out_warn_father; | |
1265 | ||
1266 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it | |
1267 | * before we aren't allowed anymore. | |
1268 | */ | |
1269 | if (handler->conf->no_new_privs) { | |
1270 | ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0), | |
1271 | prctl_arg(0), prctl_arg(0)); | |
1272 | if (ret < 0) { | |
1273 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges"); | |
1274 | goto out_warn_father; | |
1275 | } | |
1276 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges"); | |
1277 | } | |
1278 | ||
1279 | /* If we mounted a temporary proc, then unmount it now. */ | |
1280 | tmp_proc_unmount(handler->conf); | |
1281 | ||
1282 | ret = lxc_seccomp_load(handler->conf); | |
1283 | if (ret < 0) | |
1284 | goto out_warn_father; | |
1285 | ||
1286 | ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL); | |
1287 | if (ret < 0) { | |
1288 | ERROR("Failed to run lxc.hook.start for container \"%s\"", | |
1289 | handler->name); | |
1290 | goto out_warn_father; | |
1291 | } | |
1292 | ||
1293 | close_prot_errno_disarm(handler->sigfd); | |
1294 | ||
1295 | if (handler->conf->console.pty < 0 && handler->daemonize) { | |
1296 | if (devnull_fd < 0) { | |
1297 | devnull_fd = open_devnull(); | |
1298 | if (devnull_fd < 0) | |
1299 | goto out_warn_father; | |
1300 | } | |
1301 | ||
1302 | ret = set_stdfds(devnull_fd); | |
1303 | if (ret < 0) { | |
1304 | ERROR("Failed to redirect std{in,out,err} to \"/dev/null\""); | |
1305 | goto out_warn_father; | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | close_prot_errno_disarm(devnull_fd); | |
1310 | ||
1311 | setsid(); | |
1312 | ||
1313 | if (handler->conf->init_cwd) { | |
1314 | ret = chdir(handler->conf->init_cwd); | |
1315 | if (ret < 0) { | |
1316 | SYSERROR("Could not change directory to \"%s\"", | |
1317 | handler->conf->init_cwd); | |
1318 | goto out_warn_father; | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | if (!lxc_sync_barrier_parent(handler, START_SYNC_CGROUP_LIMITS)) | |
1323 | goto out_warn_father; | |
1324 | ||
1325 | ret = lxc_sync_fds_child(handler); | |
1326 | if (ret < 0) { | |
1327 | SYSERROR("Failed to sync file descriptors with parent"); | |
1328 | goto out_warn_father; | |
1329 | } | |
1330 | ||
1331 | if (!lxc_sync_wait_parent(handler, START_SYNC_READY_START)) | |
1332 | goto out_warn_father; | |
1333 | ||
1334 | /* Reset the environment variables the user requested in a clear | |
1335 | * environment. | |
1336 | */ | |
1337 | ret = clearenv(); | |
1338 | /* Don't error out though. */ | |
1339 | if (ret < 0) | |
1340 | SYSERROR("Failed to clear environment."); | |
1341 | ||
1342 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
1343 | ret = putenv((char *)iterator->elem); | |
1344 | if (ret < 0) { | |
1345 | SYSERROR("Failed to set environment variable: %s", | |
1346 | (char *)iterator->elem); | |
1347 | goto out_warn_father; | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | ret = putenv("container=lxc"); | |
1352 | if (ret < 0) { | |
1353 | SYSERROR("Failed to set environment variable: container=lxc"); | |
1354 | goto out_warn_father; | |
1355 | } | |
1356 | ||
1357 | if (handler->conf->ttys.tty_names) { | |
1358 | ret = putenv(handler->conf->ttys.tty_names); | |
1359 | if (ret < 0) { | |
1360 | SYSERROR("Failed to set environment variable for container ptys"); | |
1361 | goto out_warn_father; | |
1362 | } | |
1363 | } | |
1364 | ||
1365 | /* The container has been setup. We can now switch to an unprivileged | |
1366 | * uid/gid. | |
1367 | */ | |
1368 | new_uid = handler->conf->init_uid; | |
1369 | new_gid = handler->conf->init_gid; | |
1370 | ||
1371 | /* Avoid unnecessary syscalls. */ | |
1372 | if (new_uid == nsuid) | |
1373 | new_uid = LXC_INVALID_UID; | |
1374 | ||
1375 | if (new_gid == nsgid) | |
1376 | new_gid = LXC_INVALID_GID; | |
1377 | ||
1378 | /* Make sure that the processes STDIO is correctly owned by the user that we are switching to */ | |
1379 | ret = fix_stdio_permissions(new_uid); | |
1380 | if (ret) | |
1381 | WARN("Failed to ajust stdio permissions"); | |
1382 | ||
1383 | /* If we are in a new user namespace we already dropped all groups when | |
1384 | * we switched to root in the new user namespace further above. Only | |
1385 | * drop groups if we can, so ensure that we have necessary privilege. | |
1386 | */ | |
1387 | if (lxc_list_empty(&handler->conf->id_map)) { | |
1388 | #if HAVE_LIBCAP | |
1389 | if (lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE)) | |
1390 | #endif | |
1391 | { | |
1392 | if (handler->conf->init_groups.size > 0) { | |
1393 | if (!lxc_setgroups(handler->conf->init_groups.list, | |
1394 | handler->conf->init_groups.size)) | |
1395 | goto out_warn_father; | |
1396 | } else { | |
1397 | if (!lxc_drop_groups()) | |
1398 | goto out_warn_father; | |
1399 | } | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | if (!lxc_switch_uid_gid(new_uid, new_gid)) | |
1404 | goto out_warn_father; | |
1405 | ||
1406 | ret = lxc_ambient_caps_down(); | |
1407 | if (ret < 0) { | |
1408 | ERROR("Failed to clear ambient capabilities"); | |
1409 | goto out_warn_father; | |
1410 | } | |
1411 | ||
1412 | if (handler->conf->monitor_signal_pdeath != SIGKILL) { | |
1413 | ret = lxc_set_death_signal(handler->conf->monitor_signal_pdeath, | |
1414 | handler->monitor_pid, status_fd); | |
1415 | if (ret < 0) { | |
1416 | SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", | |
1417 | handler->conf->monitor_signal_pdeath); | |
1418 | goto out_warn_father; | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | /* | |
1423 | * After this call, we are in error because this ops should not return | |
1424 | * as it execs. | |
1425 | */ | |
1426 | handler->ops->start(handler, handler->data); | |
1427 | ||
1428 | out_warn_father: | |
1429 | /* | |
1430 | * We want the parent to know something went wrong, so we return a | |
1431 | * special error code. | |
1432 | */ | |
1433 | lxc_sync_wake_parent(handler, SYNC_ERROR); | |
1434 | ||
1435 | out_error: | |
1436 | return -1; | |
1437 | } | |
1438 | ||
1439 | int resolve_clone_flags(struct lxc_handler *handler) | |
1440 | { | |
1441 | int i; | |
1442 | struct lxc_conf *conf = handler->conf; | |
1443 | bool wants_timens = conf->timens.s_boot || conf->timens.ns_boot || | |
1444 | conf->timens.s_monotonic || conf->timens.ns_monotonic; | |
1445 | ||
1446 | for (i = 0; i < LXC_NS_MAX; i++) { | |
1447 | if (conf->ns_keep) { | |
1448 | if (!(conf->ns_keep & ns_info[i].clone_flag)) | |
1449 | handler->ns_clone_flags |= ns_info[i].clone_flag; | |
1450 | } else if (conf->ns_clone) { | |
1451 | if ((conf->ns_clone & ns_info[i].clone_flag)) | |
1452 | handler->ns_clone_flags |= ns_info[i].clone_flag; | |
1453 | } else { | |
1454 | if (i == LXC_NS_USER && lxc_list_empty(&handler->conf->id_map)) | |
1455 | continue; | |
1456 | ||
1457 | if (i == LXC_NS_NET && lxc_requests_empty_network(handler)) | |
1458 | continue; | |
1459 | ||
1460 | if (i == LXC_NS_CGROUP && !cgns_supported()) | |
1461 | continue; | |
1462 | ||
1463 | if (i == LXC_NS_TIME && !wants_timens) | |
1464 | continue; | |
1465 | ||
1466 | handler->ns_clone_flags |= ns_info[i].clone_flag; | |
1467 | } | |
1468 | ||
1469 | if (!conf->ns_share[i]) | |
1470 | continue; | |
1471 | ||
1472 | handler->ns_clone_flags &= ~ns_info[i].clone_flag; | |
1473 | TRACE("Sharing %s namespace", ns_info[i].proc_name); | |
1474 | } | |
1475 | ||
1476 | if (wants_timens && (conf->ns_keep & ns_info[LXC_NS_TIME].clone_flag)) | |
1477 | return log_trace_errno(-1, EINVAL, "Requested to keep time namespace while also specifying offsets"); | |
1478 | ||
1479 | /* Deal with namespaces that are unshared. */ | |
1480 | if (handler->ns_clone_flags & CLONE_NEWTIME) | |
1481 | handler->ns_unshare_flags |= CLONE_NEWTIME; | |
1482 | ||
1483 | if (!pure_unified_layout(handler->cgroup_ops) && handler->ns_clone_flags & CLONE_NEWCGROUP) | |
1484 | handler->ns_unshare_flags |= CLONE_NEWCGROUP; | |
1485 | ||
1486 | if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == | |
1487 | (CLONE_NEWNET | CLONE_NEWUSER)) | |
1488 | handler->ns_unshare_flags |= CLONE_NEWNET; | |
1489 | ||
1490 | /* Deal with namespaces that are spawned. */ | |
1491 | handler->ns_on_clone_flags = handler->ns_clone_flags & ~handler->ns_unshare_flags; | |
1492 | ||
1493 | handler->clone_flags = handler->ns_on_clone_flags | CLONE_PIDFD; | |
1494 | ||
1495 | return 0; | |
1496 | } | |
1497 | ||
1498 | /* Note that this function is used with clone(CLONE_VM). Some glibc versions | |
1499 | * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD. | |
1500 | * But since the memory between parent and child is shared on CLONE_VM this | |
1501 | * would invalidate the getpid() cache that glibc used to maintain and so | |
1502 | * getpid() in the child would return the parent's pid. This is all fixed in | |
1503 | * newer glibc versions where the getpid() cache is removed and the pid/tid is | |
1504 | * not reset anymore. | |
1505 | * However, if for whatever reason you - dear committer - somehow need to get the | |
1506 | * pid of the dummy intermediate process for do_share_ns() you need to call | |
1507 | * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and | |
1508 | * will be fine. | |
1509 | */ | |
1510 | static inline int do_share_ns(void *arg) | |
1511 | { | |
1512 | int i, flags, ret; | |
1513 | struct lxc_handler *handler = arg; | |
1514 | ||
1515 | for (i = 0; i < LXC_NS_MAX; i++) { | |
1516 | if (handler->nsfd[i] < 0) | |
1517 | continue; | |
1518 | ||
1519 | ret = setns(handler->nsfd[i], 0); | |
1520 | if (ret < 0) { | |
1521 | /* | |
1522 | * Note that joining a user and/or mount namespace | |
1523 | * requires the process is not multithreaded otherwise | |
1524 | * setns() will fail here. | |
1525 | */ | |
1526 | SYSERROR("Failed to inherit %s namespace", | |
1527 | ns_info[i].proc_name); | |
1528 | return -1; | |
1529 | } | |
1530 | ||
1531 | DEBUG("Inherited %s namespace", ns_info[i].proc_name); | |
1532 | } | |
1533 | ||
1534 | flags = handler->ns_on_clone_flags; | |
1535 | flags |= CLONE_PARENT; | |
1536 | handler->pid = lxc_raw_clone_cb(do_start, handler, CLONE_PIDFD | flags, | |
1537 | &handler->pidfd); | |
1538 | if (handler->pid < 0) | |
1539 | return -1; | |
1540 | ||
1541 | return 0; | |
1542 | } | |
1543 | ||
1544 | /* lxc_spawn() performs crucial setup tasks and clone()s the new process which | |
1545 | * exec()s the requested container binary. | |
1546 | * Note that lxc_spawn() runs in the parent namespaces. Any operations performed | |
1547 | * right here should be double checked if they'd pose a security risk. (For | |
1548 | * example, any {u}mount() operations performed here will be reflected on the | |
1549 | * host!) | |
1550 | */ | |
1551 | static int lxc_spawn(struct lxc_handler *handler) | |
1552 | { | |
1553 | __do_close int data_sock0 = -EBADF, data_sock1 = -EBADF; | |
1554 | int i, ret; | |
1555 | char pidstr[20]; | |
1556 | bool wants_to_map_ids; | |
1557 | struct lxc_list *id_map; | |
1558 | const char *name = handler->name; | |
1559 | const char *lxcpath = handler->lxcpath; | |
1560 | bool share_ns = false; | |
1561 | struct lxc_conf *conf = handler->conf; | |
1562 | struct cgroup_ops *cgroup_ops = handler->cgroup_ops; | |
1563 | ||
1564 | id_map = &conf->id_map; | |
1565 | wants_to_map_ids = !lxc_list_empty(id_map); | |
1566 | ||
1567 | for (i = 0; i < LXC_NS_MAX; i++) { | |
1568 | if (!conf->ns_share[i]) | |
1569 | continue; | |
1570 | ||
1571 | handler->nsfd[i] = lxc_inherit_namespace(conf->ns_share[i], lxcpath, ns_info[i].proc_name); | |
1572 | if (handler->nsfd[i] < 0) | |
1573 | return -1; | |
1574 | ||
1575 | share_ns = true; | |
1576 | } | |
1577 | ||
1578 | if (!lxc_sync_init(handler)) | |
1579 | return -1; | |
1580 | ||
1581 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
1582 | handler->data_sock); | |
1583 | if (ret < 0) | |
1584 | goto out_sync_fini; | |
1585 | data_sock0 = handler->data_sock[0]; | |
1586 | data_sock1 = handler->data_sock[1]; | |
1587 | ||
1588 | ret = resolve_clone_flags(handler); | |
1589 | if (ret < 0) | |
1590 | goto out_sync_fini; | |
1591 | ||
1592 | if (handler->ns_clone_flags & CLONE_NEWNET) { | |
1593 | ret = lxc_find_gateway_addresses(handler); | |
1594 | if (ret) { | |
1595 | ERROR("Failed to find gateway addresses"); | |
1596 | goto out_sync_fini; | |
1597 | } | |
1598 | } | |
1599 | ||
1600 | if (!cgroup_ops->payload_create(cgroup_ops, handler)) { | |
1601 | ERROR("Failed creating cgroups"); | |
1602 | goto out_delete_net; | |
1603 | } | |
1604 | ||
1605 | /* Create a process in a new set of namespaces. */ | |
1606 | if (share_ns) { | |
1607 | pid_t attacher_pid; | |
1608 | ||
1609 | attacher_pid = lxc_clone(do_share_ns, handler, | |
1610 | CLONE_VFORK | CLONE_VM | CLONE_FILES, NULL); | |
1611 | if (attacher_pid < 0) { | |
1612 | SYSERROR(LXC_CLONE_ERROR); | |
1613 | goto out_delete_net; | |
1614 | } | |
1615 | ||
1616 | ret = wait_for_pid(attacher_pid); | |
1617 | if (ret < 0) { | |
1618 | SYSERROR("Intermediate process failed"); | |
1619 | goto out_delete_net; | |
1620 | } | |
1621 | ||
1622 | if (handler->pid < 0) { | |
1623 | SYSERROR(LXC_CLONE_ERROR); | |
1624 | goto out_delete_net; | |
1625 | } | |
1626 | } else { | |
1627 | int cgroup_fd = -EBADF; | |
1628 | ||
1629 | struct lxc_clone_args clone_args = { | |
1630 | .flags = handler->clone_flags, | |
1631 | .pidfd = ptr_to_u64(&handler->pidfd), | |
1632 | .exit_signal = SIGCHLD, | |
1633 | }; | |
1634 | ||
1635 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { | |
1636 | cgroup_fd = cgroup_unified_fd(cgroup_ops); | |
1637 | if (cgroup_fd >= 0) { | |
1638 | handler->clone_flags |= CLONE_INTO_CGROUP; | |
1639 | clone_args.flags |= CLONE_INTO_CGROUP; | |
1640 | clone_args.cgroup = cgroup_fd; | |
1641 | } | |
1642 | } | |
1643 | ||
1644 | /* Try to spawn directly into target cgroup. */ | |
1645 | handler->pid = lxc_clone3(&clone_args, CLONE_ARGS_SIZE_VER2); | |
1646 | if (handler->pid < 0) { | |
1647 | SYSTRACE("Failed to spawn container directly into target cgroup"); | |
1648 | ||
1649 | /* Kernel might simply be too old for CLONE_INTO_CGROUP. */ | |
1650 | handler->clone_flags &= ~(CLONE_INTO_CGROUP | CLONE_NEWCGROUP); | |
1651 | handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP; | |
1652 | handler->ns_unshare_flags |= CLONE_NEWCGROUP; | |
1653 | ||
1654 | clone_args.flags = handler->clone_flags; | |
1655 | ||
1656 | handler->pid = lxc_clone3(&clone_args, CLONE_ARGS_SIZE_VER0); | |
1657 | } else if (cgroup_fd >= 0) { | |
1658 | TRACE("Spawned container directly into target cgroup via cgroup2 fd %d", cgroup_fd); | |
1659 | } | |
1660 | ||
1661 | /* Kernel might be too old for clone3(). */ | |
1662 | if (handler->pid < 0) { | |
1663 | SYSTRACE("Failed to spawn container via clone3()"); | |
1664 | ||
1665 | /* | |
1666 | * In contrast to all other architectures arm64 verifies that | |
1667 | * the argument we use to retrieve the pidfd with is | |
1668 | * initialized to 0. But we need to be able to initialize it to | |
1669 | * a negative value such as our customary -EBADF so we can | |
1670 | * detect whether this kernel supports pidfds. If the syscall | |
1671 | * returns and the pidfd variable is set to something >= 0 then | |
1672 | * we know this is a kernel supporting pidfds. But if we can't | |
1673 | * set it to -EBADF then this won't work since 0 is a valid | |
1674 | * file descriptor too. And since legacy clone silently ignores | |
1675 | * unknown flags we are left without any way to detect support | |
1676 | * for pidfds. So let's special-case arm64 to not fail starting | |
1677 | * containers. | |
1678 | */ | |
1679 | #if defined(__aarch64__) | |
1680 | handler->pid = lxc_raw_legacy_clone(handler->clone_flags & ~CLONE_PIDFD, NULL); | |
1681 | #else | |
1682 | handler->pid = lxc_raw_legacy_clone(handler->clone_flags, &handler->pidfd); | |
1683 | #endif | |
1684 | } | |
1685 | ||
1686 | if (handler->pid < 0) { | |
1687 | SYSERROR(LXC_CLONE_ERROR); | |
1688 | goto out_delete_net; | |
1689 | } | |
1690 | ||
1691 | if (handler->pid == 0) { | |
1692 | (void)do_start(handler); | |
1693 | _exit(EXIT_FAILURE); | |
1694 | } | |
1695 | } | |
1696 | if (handler->pidfd < 0) | |
1697 | handler->clone_flags &= ~CLONE_PIDFD; | |
1698 | TRACE("Cloned child process %d", handler->pid); | |
1699 | ||
1700 | /* Verify that we can actually make use of pidfds. */ | |
1701 | if (!lxc_can_use_pidfd(handler->pidfd)) | |
1702 | close_prot_errno_disarm(handler->pidfd); | |
1703 | ||
1704 | ret = strnprintf(pidstr, 20, "%d", handler->pid); | |
1705 | if (ret < 0) | |
1706 | goto out_delete_net; | |
1707 | ||
1708 | ret = setenv("LXC_PID", pidstr, 1); | |
1709 | if (ret < 0) | |
1710 | SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr); | |
1711 | ||
1712 | for (i = 0; i < LXC_NS_MAX; i++) | |
1713 | if (handler->ns_on_clone_flags & ns_info[i].clone_flag) | |
1714 | INFO("Cloned %s", ns_info[i].flag_name); | |
1715 | ||
1716 | if (!lxc_try_preserve_namespaces(handler, handler->ns_on_clone_flags)) { | |
1717 | ERROR("Failed to preserve cloned namespaces for lxc.hook.stop"); | |
1718 | goto out_delete_net; | |
1719 | } | |
1720 | ||
1721 | lxc_sync_fini_child(handler); | |
1722 | ||
1723 | if (lxc_abstract_unix_send_fds(handler->data_sock[0], &handler->monitor_status_fd, 1, NULL, 0) < 0) { | |
1724 | ERROR("Failed to send status file descriptor to child process"); | |
1725 | goto out_delete_net; | |
1726 | } | |
1727 | close_prot_errno_disarm(handler->monitor_status_fd); | |
1728 | ||
1729 | /* Map the container uids. The container became an invalid userid the | |
1730 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1731 | * anything immediately, but allows the container to setuid(0) (0 being | |
1732 | * mapped to something else on the host.) later to become a valid uid | |
1733 | * again. | |
1734 | */ | |
1735 | if (wants_to_map_ids) { | |
1736 | if (!handler->conf->ns_share[LXC_NS_USER] && | |
1737 | (handler->conf->ns_keep & CLONE_NEWUSER) == 0) { | |
1738 | ret = lxc_map_ids(id_map, handler->pid); | |
1739 | if (ret < 0) { | |
1740 | ERROR("Failed to set up id mapping."); | |
1741 | goto out_delete_net; | |
1742 | } | |
1743 | } | |
1744 | } | |
1745 | ||
1746 | if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, false)) { | |
1747 | ERROR("Failed to setup cgroup limits for container \"%s\"", name); | |
1748 | goto out_delete_net; | |
1749 | } | |
1750 | ||
1751 | if (!cgroup_ops->payload_delegate_controllers(cgroup_ops)) { | |
1752 | ERROR("Failed to delegate controllers to payload cgroup"); | |
1753 | goto out_delete_net; | |
1754 | } | |
1755 | ||
1756 | if (!cgroup_ops->payload_enter(cgroup_ops, handler)) { | |
1757 | ERROR("Failed to enter cgroups"); | |
1758 | goto out_delete_net; | |
1759 | } | |
1760 | ||
1761 | if (!cgroup_ops->setup_limits(cgroup_ops, handler)) { | |
1762 | ERROR("Failed to setup cgroup limits for container \"%s\"", name); | |
1763 | goto out_delete_net; | |
1764 | } | |
1765 | ||
1766 | if (!cgroup_ops->chown(cgroup_ops, handler->conf)) | |
1767 | goto out_delete_net; | |
1768 | ||
1769 | if (!lxc_sync_barrier_child(handler, START_SYNC_STARTUP)) | |
1770 | goto out_delete_net; | |
1771 | ||
1772 | /* If not done yet, we're now ready to preserve the network namespace */ | |
1773 | if (handler->nsfd[LXC_NS_NET] < 0) { | |
1774 | ret = lxc_try_preserve_namespace(handler, LXC_NS_NET, "net"); | |
1775 | if (ret < 0) { | |
1776 | if (ret != -ENOENT) { | |
1777 | SYSERROR("Failed to preserve net namespace"); | |
1778 | goto out_delete_net; | |
1779 | } | |
1780 | } | |
1781 | } | |
1782 | ret = lxc_netns_set_nsid(handler->nsfd[LXC_NS_NET]); | |
1783 | if (ret < 0) | |
1784 | SYSWARN("Failed to allocate new network namespace id"); | |
1785 | else | |
1786 | TRACE("Allocated new network namespace id"); | |
1787 | ||
1788 | /* Create the network configuration. */ | |
1789 | if (handler->ns_clone_flags & CLONE_NEWNET) { | |
1790 | ret = lxc_create_network(handler); | |
1791 | if (ret < 0) { | |
1792 | ERROR("Failed to create the network"); | |
1793 | goto out_delete_net; | |
1794 | } | |
1795 | } | |
1796 | ||
1797 | if (!lxc_list_empty(&conf->procs)) { | |
1798 | ret = setup_proc_filesystem(&conf->procs, handler->pid); | |
1799 | if (ret < 0) | |
1800 | goto out_delete_net; | |
1801 | } | |
1802 | ||
1803 | if (!lxc_list_empty(&conf->limits)) { | |
1804 | ret = setup_resource_limits(&conf->limits, handler->pid); | |
1805 | if (ret < 0) { | |
1806 | ERROR("Failed to setup resource limits"); | |
1807 | goto out_delete_net; | |
1808 | } | |
1809 | } | |
1810 | ||
1811 | /* Tell the child to continue its initialization. */ | |
1812 | if (!lxc_sync_wake_child(handler, START_SYNC_POST_CONFIGURE)) | |
1813 | goto out_delete_net; | |
1814 | ||
1815 | ret = lxc_rootfs_prepare_parent(handler); | |
1816 | if (ret) { | |
1817 | ERROR("Failed to prepare rootfs"); | |
1818 | goto out_delete_net; | |
1819 | } | |
1820 | ||
1821 | if (handler->ns_clone_flags & CLONE_NEWNET) { | |
1822 | ret = lxc_network_send_to_child(handler); | |
1823 | if (ret < 0) { | |
1824 | SYSERROR("Failed to send veth names to child"); | |
1825 | goto out_delete_net; | |
1826 | } | |
1827 | } | |
1828 | ||
1829 | if (!lxc_sync_wait_child(handler, START_SYNC_IDMAPPED_MOUNTS)) | |
1830 | goto out_delete_net; | |
1831 | ||
1832 | ret = lxc_idmapped_mounts_parent(handler); | |
1833 | if (ret) { | |
1834 | ERROR("Failed to setup mount entries"); | |
1835 | goto out_delete_net; | |
1836 | } | |
1837 | ||
1838 | if (!lxc_sync_wait_child(handler, START_SYNC_CGROUP_LIMITS)) | |
1839 | goto out_delete_net; | |
1840 | ||
1841 | /* | |
1842 | * With isolation the limiting devices cgroup was already setup, so | |
1843 | * only setup devices here if we have no namespace directory. | |
1844 | */ | |
1845 | if (!handler->conf->cgroup_meta.namespace_dir && | |
1846 | !cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, true)) { | |
1847 | ERROR("Failed to setup legacy device cgroup controller limits"); | |
1848 | goto out_delete_net; | |
1849 | } | |
1850 | TRACE("Set up legacy device cgroup controller limits"); | |
1851 | ||
1852 | if (!cgroup_ops->devices_activate(cgroup_ops, handler)) { | |
1853 | ERROR("Failed to setup cgroup2 device controller limits"); | |
1854 | goto out_delete_net; | |
1855 | } | |
1856 | TRACE("Set up cgroup2 device controller limits"); | |
1857 | ||
1858 | cgroup_ops->finalize(cgroup_ops); | |
1859 | TRACE("Finished setting up cgroups"); | |
1860 | ||
1861 | /* Run any host-side start hooks */ | |
1862 | ret = run_lxc_hooks(name, "start-host", conf, NULL); | |
1863 | if (ret < 0) { | |
1864 | ERROR("Failed to run lxc.hook.start-host"); | |
1865 | goto out_delete_net; | |
1866 | } | |
1867 | ||
1868 | if (!lxc_sync_wake_child(handler, START_SYNC_FDS)) | |
1869 | goto out_delete_net; | |
1870 | ||
1871 | if (handler->ns_unshare_flags & CLONE_NEWCGROUP) { | |
1872 | /* Now we're ready to preserve the cgroup namespace */ | |
1873 | ret = lxc_try_preserve_namespace(handler, LXC_NS_CGROUP, "cgroup"); | |
1874 | if (ret < 0) { | |
1875 | if (ret != -ENOENT) { | |
1876 | SYSERROR("Failed to preserve cgroup namespace"); | |
1877 | goto out_delete_net; | |
1878 | } | |
1879 | } | |
1880 | } | |
1881 | ||
1882 | if (handler->ns_unshare_flags & CLONE_NEWTIME) { | |
1883 | /* Now we're ready to preserve the time namespace */ | |
1884 | ret = lxc_try_preserve_namespace(handler, LXC_NS_TIME, "time"); | |
1885 | if (ret < 0) { | |
1886 | if (ret != -ENOENT) { | |
1887 | SYSERROR("Failed to preserve time namespace"); | |
1888 | goto out_delete_net; | |
1889 | } | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | ret = lxc_sync_fds_parent(handler); | |
1894 | if (ret < 0) { | |
1895 | SYSERROR("Failed to sync file descriptors with child"); | |
1896 | goto out_delete_net; | |
1897 | } | |
1898 | ||
1899 | /* | |
1900 | * Tell the child to complete its initialization and wait for it to | |
1901 | * exec or return an error. (The child will never return | |
1902 | * START_SYNC_READY_START+1. It will either close the sync pipe, | |
1903 | * causing lxc_sync_barrier_child to return success, or return a | |
1904 | * different value, causing us to error out). | |
1905 | */ | |
1906 | if (!lxc_sync_barrier_child(handler, START_SYNC_READY_START)) | |
1907 | goto out_delete_net; | |
1908 | ||
1909 | /* Now all networks are created, network devices are moved into place, | |
1910 | * and the correct names and ifindices in the respective namespaces have | |
1911 | * been recorded. The corresponding structs have now all been filled. So | |
1912 | * log them for debugging purposes. | |
1913 | */ | |
1914 | lxc_log_configured_netdevs(conf); | |
1915 | ||
1916 | ret = handler->ops->post_start(handler, handler->data); | |
1917 | if (ret < 0) | |
1918 | goto out_abort; | |
1919 | ||
1920 | ret = lxc_set_state(name, handler, RUNNING); | |
1921 | if (ret < 0) { | |
1922 | ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING)); | |
1923 | goto out_abort; | |
1924 | } | |
1925 | ||
1926 | lxc_sync_fini(handler); | |
1927 | ||
1928 | return 0; | |
1929 | ||
1930 | out_delete_net: | |
1931 | if (handler->ns_clone_flags & CLONE_NEWNET) | |
1932 | lxc_delete_network(handler); | |
1933 | ||
1934 | out_abort: | |
1935 | lxc_abort(handler); | |
1936 | ||
1937 | out_sync_fini: | |
1938 | lxc_sync_fini(handler); | |
1939 | ||
1940 | return -1; | |
1941 | } | |
1942 | ||
1943 | int __lxc_start(struct lxc_handler *handler, struct lxc_operations *ops, | |
1944 | void *data, const char *lxcpath, bool daemonize, int *error_num) | |
1945 | { | |
1946 | int ret, status; | |
1947 | const char *name = handler->name; | |
1948 | struct lxc_conf *conf = handler->conf; | |
1949 | struct cgroup_ops *cgroup_ops; | |
1950 | ||
1951 | ret = lxc_init(name, handler); | |
1952 | if (ret < 0) { | |
1953 | ERROR("Failed to initialize container \"%s\"", name); | |
1954 | goto out_abort; | |
1955 | } | |
1956 | handler->ops = ops; | |
1957 | handler->data = data; | |
1958 | handler->daemonize = daemonize; | |
1959 | cgroup_ops = handler->cgroup_ops; | |
1960 | ||
1961 | if (!attach_block_device(handler->conf)) { | |
1962 | ERROR("Failed to attach block device"); | |
1963 | ret = -1; | |
1964 | goto out_abort; | |
1965 | } | |
1966 | ||
1967 | if (!cgroup_ops->monitor_create(cgroup_ops, handler)) { | |
1968 | ERROR("Failed to create monitor cgroup"); | |
1969 | ret = -1; | |
1970 | goto out_abort; | |
1971 | } | |
1972 | ||
1973 | if (!cgroup_ops->monitor_delegate_controllers(cgroup_ops)) { | |
1974 | ERROR("Failed to delegate controllers to monitor cgroup"); | |
1975 | ret = -1; | |
1976 | goto out_abort; | |
1977 | } | |
1978 | ||
1979 | if (!cgroup_ops->monitor_enter(cgroup_ops, handler)) { | |
1980 | ERROR("Failed to enter monitor cgroup"); | |
1981 | ret = -1; | |
1982 | goto out_abort; | |
1983 | } | |
1984 | ||
1985 | /* If the rootfs is not a blockdev, prevent the container from marking | |
1986 | * it readonly. | |
1987 | * If the container is unprivileged then skip rootfs pinning. | |
1988 | */ | |
1989 | ret = lxc_rootfs_init(conf, !lxc_list_empty(&conf->id_map)); | |
1990 | if (ret) { | |
1991 | ERROR("Failed to handle rootfs pinning for container \"%s\"", handler->name); | |
1992 | ret = -1; | |
1993 | goto out_abort; | |
1994 | } | |
1995 | ||
1996 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { | |
1997 | /* | |
1998 | * Most filesystems can't be mounted inside a userns so handle them here. | |
1999 | */ | |
2000 | if (rootfs_is_blockdev(conf)) { | |
2001 | ret = unshare(CLONE_NEWNS); | |
2002 | if (ret < 0) { | |
2003 | ERROR("Failed to unshare CLONE_NEWNS"); | |
2004 | goto out_abort; | |
2005 | } | |
2006 | INFO("Unshared CLONE_NEWNS"); | |
2007 | ||
2008 | ret = lxc_setup_rootfs_prepare_root(conf, name, lxcpath); | |
2009 | if (ret < 0) { | |
2010 | ERROR("Error setting up rootfs mount as root before spawn"); | |
2011 | goto out_abort; | |
2012 | } | |
2013 | INFO("Set up container rootfs as host root"); | |
2014 | } | |
2015 | } | |
2016 | ||
2017 | ret = lxc_spawn(handler); | |
2018 | if (ret < 0) { | |
2019 | ERROR("Failed to spawn container \"%s\"", name); | |
2020 | goto out_detach_blockdev; | |
2021 | } | |
2022 | ||
2023 | handler->conf->reboot = REBOOT_NONE; | |
2024 | ||
2025 | ret = lxc_poll(name, handler); | |
2026 | if (ret) { | |
2027 | ERROR("LXC mainloop exited with error: %d", ret); | |
2028 | goto out_delete_network; | |
2029 | } | |
2030 | ||
2031 | if (!handler->init_died && handler->pid > 0) { | |
2032 | ERROR("Child process is not killed"); | |
2033 | ret = -1; | |
2034 | goto out_delete_network; | |
2035 | } | |
2036 | ||
2037 | status = lxc_wait_for_pid_status(handler->pid); | |
2038 | if (status < 0) | |
2039 | SYSERROR("Failed to retrieve status for %d", handler->pid); | |
2040 | ||
2041 | /* If the child process exited but was not signaled, it didn't call | |
2042 | * reboot. This should mean it was an lxc-execute which simply exited. | |
2043 | * In any case, treat it as a 'halt'. | |
2044 | */ | |
2045 | if (WIFSIGNALED(status)) { | |
2046 | switch(WTERMSIG(status)) { | |
2047 | case SIGINT: /* halt */ | |
2048 | DEBUG("Container \"%s\" is halting", name); | |
2049 | break; | |
2050 | case SIGHUP: /* reboot */ | |
2051 | DEBUG("Container \"%s\" is rebooting", name); | |
2052 | handler->conf->reboot = REBOOT_REQ; | |
2053 | break; | |
2054 | case SIGSYS: /* seccomp */ | |
2055 | DEBUG("Container \"%s\" violated its seccomp policy", name); | |
2056 | break; | |
2057 | default: | |
2058 | DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status)); | |
2059 | break; | |
2060 | } | |
2061 | } | |
2062 | ||
2063 | ret = lxc_restore_phys_nics_to_netns(handler); | |
2064 | if (ret < 0) | |
2065 | ERROR("Failed to move physical network devices back to parent network namespace"); | |
2066 | ||
2067 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); | |
2068 | lxc_error_set_and_log(handler->pid, status); | |
2069 | if (error_num) | |
2070 | *error_num = handler->exit_status; | |
2071 | ||
2072 | lxc_delete_network(handler); | |
2073 | detach_block_device(handler->conf); | |
2074 | lxc_end(handler); | |
2075 | return ret; | |
2076 | ||
2077 | out_abort: | |
2078 | lxc_abort(handler); | |
2079 | lxc_end(handler); | |
2080 | return ret; | |
2081 | ||
2082 | out_detach_blockdev: | |
2083 | lxc_abort(handler); | |
2084 | detach_block_device(handler->conf); | |
2085 | lxc_end(handler); | |
2086 | return ret; | |
2087 | ||
2088 | out_delete_network: | |
2089 | lxc_abort(handler); | |
2090 | lxc_restore_phys_nics_to_netns(handler); | |
2091 | lxc_delete_network(handler); | |
2092 | detach_block_device(handler->conf); | |
2093 | lxc_end(handler); | |
2094 | return ret; | |
2095 | } | |
2096 | ||
2097 | struct start_args { | |
2098 | char *const *argv; | |
2099 | }; | |
2100 | ||
2101 | static int start(struct lxc_handler *handler, void* data) | |
2102 | { | |
2103 | struct start_args *arg = data; | |
2104 | ||
2105 | NOTICE("Exec'ing \"%s\"", arg->argv[0]); | |
2106 | ||
2107 | execvp(arg->argv[0], arg->argv); | |
2108 | SYSERROR("Failed to exec \"%s\"", arg->argv[0]); | |
2109 | return 0; | |
2110 | } | |
2111 | ||
2112 | static int post_start(struct lxc_handler *handler, void* data) | |
2113 | { | |
2114 | struct start_args *arg = data; | |
2115 | ||
2116 | NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid); | |
2117 | return 0; | |
2118 | } | |
2119 | ||
2120 | static struct lxc_operations start_ops = { | |
2121 | .start = start, | |
2122 | .post_start = post_start | |
2123 | }; | |
2124 | ||
2125 | int lxc_start(char *const argv[], struct lxc_handler *handler, | |
2126 | const char *lxcpath, bool daemonize, int *error_num) | |
2127 | { | |
2128 | struct start_args start_arg = { | |
2129 | .argv = argv, | |
2130 | }; | |
2131 | ||
2132 | TRACE("Doing lxc_start"); | |
2133 | return __lxc_start(handler, &start_ops, &start_arg, lxcpath, daemonize, error_num); | |
2134 | } | |
2135 | ||
2136 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
2137 | const char *name) | |
2138 | { | |
2139 | char destroy[PATH_MAX]; | |
2140 | struct lxc_container *c; | |
2141 | int ret = 0; | |
2142 | bool bret = true; | |
2143 | ||
2144 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { | |
2145 | bret = do_destroy_container(handler); | |
2146 | if (!bret) { | |
2147 | ERROR("Error destroying rootfs for container \"%s\"", name); | |
2148 | return; | |
2149 | } | |
2150 | } | |
2151 | INFO("Destroyed rootfs for container \"%s\"", name); | |
2152 | ||
2153 | ret = strnprintf(destroy, sizeof(destroy), "%s/%s", handler->lxcpath, name); | |
2154 | if (ret < 0) { | |
2155 | ERROR("Error destroying directory for container \"%s\"", name); | |
2156 | return; | |
2157 | } | |
2158 | ||
2159 | c = lxc_container_new(name, handler->lxcpath); | |
2160 | if (c) { | |
2161 | if (container_disk_lock(c)) { | |
2162 | INFO("Could not update lxc_snapshots file"); | |
2163 | lxc_container_put(c); | |
2164 | } else { | |
2165 | mod_all_rdeps(c, false); | |
2166 | container_disk_unlock(c); | |
2167 | lxc_container_put(c); | |
2168 | } | |
2169 | } | |
2170 | ||
2171 | if (!handler->am_root) | |
2172 | ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper, | |
2173 | destroy, "lxc_rmdir_onedev_wrapper"); | |
2174 | else | |
2175 | ret = lxc_rmdir_onedev(destroy, NULL); | |
2176 | ||
2177 | if (ret < 0) { | |
2178 | ERROR("Error destroying directory for container \"%s\"", name); | |
2179 | return; | |
2180 | } | |
2181 | INFO("Destroyed directory for container \"%s\"", name); | |
2182 | } | |
2183 | ||
2184 | static int lxc_rmdir_onedev_wrapper(void *data) | |
2185 | { | |
2186 | char *arg = (char *) data; | |
2187 | return lxc_rmdir_onedev(arg, NULL); | |
2188 | } | |
2189 | ||
2190 | static bool do_destroy_container(struct lxc_handler *handler) | |
2191 | { | |
2192 | int ret; | |
2193 | ||
2194 | if (!handler->am_root) { | |
2195 | ret = userns_exec_full(handler->conf, storage_destroy_wrapper, | |
2196 | handler->conf, "storage_destroy_wrapper"); | |
2197 | if (ret < 0) | |
2198 | return false; | |
2199 | ||
2200 | return true; | |
2201 | } | |
2202 | ||
2203 | return storage_destroy(handler->conf); | |
2204 | } |