]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/attach.c
lsm: add lsm_process_label_set_at()
[mirror_lxc.git] / src / lxc / attach.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #define _GNU_SOURCE
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <grp.h>
28 #include <pwd.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <linux/unistd.h>
35 #include <sys/mount.h>
36 #include <sys/param.h>
37 #include <sys/prctl.h>
38 #include <sys/socket.h>
39 #include <sys/syscall.h>
40 #include <sys/wait.h>
41
42 #include <lxc/lxccontainer.h>
43
44 #ifndef HAVE_DECL_PR_CAPBSET_DROP
45 #define PR_CAPBSET_DROP 24
46 #endif
47
48 #ifndef HAVE_DECL_PR_SET_NO_NEW_PRIVS
49 #define PR_SET_NO_NEW_PRIVS 38
50 #endif
51
52 #ifndef HAVE_DECL_PR_GET_NO_NEW_PRIVS
53 #define PR_GET_NO_NEW_PRIVS 39
54 #endif
55
56 #include "af_unix.h"
57 #include "attach.h"
58 #include "caps.h"
59 #include "cgroup.h"
60 #include "commands.h"
61 #include "conf.h"
62 #include "config.h"
63 #include "confile.h"
64 #include "console.h"
65 #include "log.h"
66 #include "lsm/lsm.h"
67 #include "lxclock.h"
68 #include "lxcseccomp.h"
69 #include "mainloop.h"
70 #include "namespace.h"
71 #include "utils.h"
72
73 #if HAVE_SYS_PERSONALITY_H
74 #include <sys/personality.h>
75 #endif
76
77 #ifndef SOCK_CLOEXEC
78 #define SOCK_CLOEXEC 02000000
79 #endif
80
81 #ifndef MS_REC
82 #define MS_REC 16384
83 #endif
84
85 #ifndef MS_SLAVE
86 #define MS_SLAVE (1 << 19)
87 #endif
88
89 lxc_log_define(lxc_attach, lxc);
90
91 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
92 #define __PROC_STATUS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
93 static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
94 {
95 int ret;
96 bool found;
97 FILE *proc_file;
98 char proc_fn[__PROC_STATUS_LEN];
99 size_t line_bufsz = 0;
100 char *line = NULL;
101 struct lxc_proc_context_info *info = NULL;
102
103 /* Read capabilities. */
104 ret = snprintf(proc_fn, __PROC_STATUS_LEN, "/proc/%d/status", pid);
105 if (ret < 0 || ret >= __PROC_STATUS_LEN)
106 goto on_error;
107
108 proc_file = fopen(proc_fn, "r");
109 if (!proc_file) {
110 SYSERROR("Could not open %s.", proc_fn);
111 goto on_error;
112 }
113
114 info = calloc(1, sizeof(*info));
115 if (!info) {
116 SYSERROR("Could not allocate memory.");
117 fclose(proc_file);
118 return NULL;
119 }
120
121 found = false;
122 while (getline(&line, &line_bufsz, proc_file) != -1) {
123 ret = sscanf(line, "CapBnd: %llx", &info->capability_mask);
124 if (ret != EOF && ret == 1) {
125 found = true;
126 break;
127 }
128 }
129
130 free(line);
131 fclose(proc_file);
132
133 if (!found) {
134 SYSERROR("Could not read capability bounding set from %s.",
135 proc_fn);
136 errno = ENOENT;
137 goto on_error;
138 }
139
140 info->lsm_label = lsm_process_label_get(pid);
141 info->ns_inherited = 0;
142 memset(info->ns_fd, -1, sizeof(int) * LXC_NS_MAX);
143
144 return info;
145
146 on_error:
147 free(info);
148 return NULL;
149 }
150
151 static inline void lxc_proc_close_ns_fd(struct lxc_proc_context_info *ctx)
152 {
153 int i;
154
155 for (i = 0; i < LXC_NS_MAX; i++) {
156 if (ctx->ns_fd[i] < 0)
157 continue;
158 close(ctx->ns_fd[i]);
159 ctx->ns_fd[i] = -EBADF;
160 }
161 }
162
163 static void lxc_proc_put_context_info(struct lxc_proc_context_info *ctx)
164 {
165 free(ctx->lsm_label);
166 ctx->lsm_label = NULL;
167
168 if (ctx->container) {
169 lxc_container_put(ctx->container);
170 ctx->container = NULL;
171 }
172
173 lxc_proc_close_ns_fd(ctx);
174 free(ctx);
175 ctx = NULL;
176 }
177
178 /**
179 * in_same_namespace - Check whether two processes are in the same namespace.
180 * @pid1 - PID of the first process.
181 * @pid2 - PID of the second process.
182 * @ns - Name of the namespace to check. Must correspond to one of the names
183 * for the namespaces as shown in /proc/<pid/ns/
184 *
185 * If the two processes are not in the same namespace returns an fd to the
186 * namespace of the second process identified by @pid2. If the two processes are
187 * in the same namespace returns -EINVAL, -1 if an error occurred.
188 */
189 static int in_same_namespace(pid_t pid1, pid_t pid2, const char *ns)
190 {
191 int ns_fd1 = -1, ns_fd2 = -1, ret = -1;
192 struct stat ns_st1, ns_st2;
193
194 ns_fd1 = lxc_preserve_ns(pid1, ns);
195 if (ns_fd1 < 0) {
196 /* The kernel does not support this namespace. This is not an
197 * error.
198 */
199 if (errno == ENOENT)
200 return -EINVAL;
201
202 goto out;
203 }
204
205 ns_fd2 = lxc_preserve_ns(pid2, ns);
206 if (ns_fd2 < 0)
207 goto out;
208
209 ret = fstat(ns_fd1, &ns_st1);
210 if (ret < 0)
211 goto out;
212
213 ret = fstat(ns_fd2, &ns_st2);
214 if (ret < 0)
215 goto out;
216
217 /* processes are in the same namespace */
218 ret = -EINVAL;
219 if ((ns_st1.st_dev == ns_st2.st_dev ) && (ns_st1.st_ino == ns_st2.st_ino))
220 goto out;
221
222 /* processes are in different namespaces */
223 ret = ns_fd2;
224 ns_fd2 = -1;
225
226 out:
227
228 if (ns_fd1 >= 0)
229 close(ns_fd1);
230 if (ns_fd2 >= 0)
231 close(ns_fd2);
232
233 return ret;
234 }
235
236 static int lxc_attach_to_ns(pid_t pid, struct lxc_proc_context_info *ctx)
237 {
238 int i, ret;
239
240 for (i = 0; i < LXC_NS_MAX; i++) {
241 if (ctx->ns_fd[i] < 0)
242 continue;
243
244 ret = setns(ctx->ns_fd[i], ns_info[i].clone_flag);
245 if (ret < 0) {
246 SYSERROR("Failed to attach to %s namespace of %d",
247 ns_info[i].proc_name, pid);
248 return -1;
249 }
250
251 DEBUG("Attached to %s namespace of %d", ns_info[i].proc_name, pid);
252 }
253
254 return 0;
255 }
256
257 static int lxc_attach_remount_sys_proc(void)
258 {
259 int ret;
260
261 ret = unshare(CLONE_NEWNS);
262 if (ret < 0) {
263 SYSERROR("Failed to unshare mount namespace.");
264 return -1;
265 }
266
267 if (detect_shared_rootfs()) {
268 if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL)) {
269 SYSERROR("Failed to make / rslave.");
270 ERROR("Continuing...");
271 }
272 }
273
274 /* Assume /proc is always mounted, so remount it. */
275 ret = umount2("/proc", MNT_DETACH);
276 if (ret < 0) {
277 SYSERROR("Failed to unmount /proc.");
278 return -1;
279 }
280
281 ret = mount("none", "/proc", "proc", 0, NULL);
282 if (ret < 0) {
283 SYSERROR("Failed to remount /proc.");
284 return -1;
285 }
286
287 /* Try to umount /sys. If it's not a mount point, we'll get EINVAL, then
288 * we ignore it because it may not have been mounted in the first place.
289 */
290 ret = umount2("/sys", MNT_DETACH);
291 if (ret < 0 && errno != EINVAL) {
292 SYSERROR("Failed to unmount /sys.");
293 return -1;
294 } else if (ret == 0) {
295 /* Remount it. */
296 ret = mount("none", "/sys", "sysfs", 0, NULL);
297 if (ret < 0) {
298 SYSERROR("Failed to remount /sys.");
299 return -1;
300 }
301 }
302
303 return 0;
304 }
305
306 static int lxc_attach_drop_privs(struct lxc_proc_context_info *ctx)
307 {
308 int cap, last_cap;
309
310 last_cap = lxc_caps_last_cap();
311 for (cap = 0; cap <= last_cap; cap++) {
312 if (ctx->capability_mask & (1LL << cap))
313 continue;
314
315 if (prctl(PR_CAPBSET_DROP, cap, 0, 0, 0)) {
316 SYSERROR("Failed to drop capability %d", cap);
317 return -1;
318 }
319 TRACE("Dropped capability %d", cap);
320 }
321
322 return 0;
323 }
324
325 static int lxc_attach_set_environment(enum lxc_attach_env_policy_t policy,
326 char **extra_env, char **extra_keep)
327 {
328 if (policy == LXC_ATTACH_CLEAR_ENV) {
329 int path_kept = 0;
330 char **extra_keep_store = NULL;
331
332 if (extra_keep) {
333 size_t count, i;
334
335 for (count = 0; extra_keep[count]; count++);
336
337 extra_keep_store = calloc(count, sizeof(char *));
338 if (!extra_keep_store) {
339 SYSERROR("Failed to allocate memory for storing current "
340 "environment variable values that will be kept.");
341 return -1;
342 }
343 for (i = 0; i < count; i++) {
344 char *v = getenv(extra_keep[i]);
345 if (v) {
346 extra_keep_store[i] = strdup(v);
347 if (!extra_keep_store[i]) {
348 SYSERROR("Failed to allocate memory for storing current "
349 "environment variable values that will be kept.");
350 while (i > 0)
351 free(extra_keep_store[--i]);
352 free(extra_keep_store);
353 return -1;
354 }
355 if (strcmp(extra_keep[i], "PATH") == 0)
356 path_kept = 1;
357 }
358 /* Calloc sets entire array to zero, so we don't
359 * need an else.
360 */
361 }
362 }
363
364 if (clearenv()) {
365 char **p;
366
367 SYSERROR("Failed to clear environment.");
368 if (extra_keep_store) {
369 for (p = extra_keep_store; *p; p++)
370 free(*p);
371 free(extra_keep_store);
372 }
373 return -1;
374 }
375
376 if (extra_keep_store) {
377 size_t i;
378
379 for (i = 0; extra_keep[i]; i++) {
380 if (extra_keep_store[i]) {
381 if (setenv(extra_keep[i], extra_keep_store[i], 1) < 0)
382 SYSERROR("Unable to set environment variable.");
383 }
384 free(extra_keep_store[i]);
385 }
386 free(extra_keep_store);
387 }
388
389 /* Always set a default path; shells and execlp tend to be fine
390 * without it, but there is a disturbing number of C programs
391 * out there that just assume that getenv("PATH") is never NULL
392 * and then die a painful segfault death.
393 */
394 if (!path_kept)
395 setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 1);
396 }
397
398 if (putenv("container=lxc")) {
399 SYSERROR("Failed to set environment variable.");
400 return -1;
401 }
402
403 /* Set extra environment variables. */
404 if (extra_env) {
405 for (; *extra_env; extra_env++) {
406 /* Duplicate the string, just to be on the safe side,
407 * because putenv does not do it for us.
408 */
409 char *p = strdup(*extra_env);
410 /* We just assume the user knows what they are doing, so
411 * we don't do any checks.
412 */
413 if (!p) {
414 SYSERROR("Failed to allocate memory for additional environment "
415 "variables.");
416 return -1;
417 }
418 putenv(p);
419 }
420 }
421
422 return 0;
423 }
424
425 static char *lxc_attach_getpwshell(uid_t uid)
426 {
427 int fd, ret;
428 pid_t pid;
429 int pipes[2];
430 char *result = NULL;
431
432 /* We need to fork off a process that runs the getent program, and we
433 * need to capture its output, so we use a pipe for that purpose.
434 */
435 ret = pipe(pipes);
436 if (ret < 0)
437 return NULL;
438
439 pid = fork();
440 if (pid < 0) {
441 close(pipes[0]);
442 close(pipes[1]);
443 return NULL;
444 }
445
446 if (pid) {
447 int status;
448 FILE *pipe_f;
449 int found = 0;
450 size_t line_bufsz = 0;
451 char *line = NULL;
452
453 close(pipes[1]);
454
455 pipe_f = fdopen(pipes[0], "r");
456 while (getline(&line, &line_bufsz, pipe_f) != -1) {
457 int i;
458 long value;
459 char *token;
460 char *endptr = NULL, *saveptr = NULL;
461
462 /* If we already found something, just continue to read
463 * until the pipe doesn't deliver any more data, but
464 * don't modify the existing data structure.
465 */
466 if (found)
467 continue;
468
469 /* Trim line on the right hand side. */
470 for (i = strlen(line); i > 0 && (line[i - 1] == '\n' || line[i - 1] == '\r'); --i)
471 line[i - 1] = '\0';
472
473 /* Split into tokens: first: user name. */
474 token = strtok_r(line, ":", &saveptr);
475 if (!token)
476 continue;
477 /* next: dummy password field */
478 token = strtok_r(NULL, ":", &saveptr);
479 if (!token)
480 continue;
481 /* next: user id */
482 token = strtok_r(NULL, ":", &saveptr);
483 value = token ? strtol(token, &endptr, 10) : 0;
484 if (!token || !endptr || *endptr || value == LONG_MIN || value == LONG_MAX)
485 continue;
486 /* dummy sanity check: user id matches */
487 if ((uid_t) value != uid)
488 continue;
489 /* skip fields: gid, gecos, dir, go to next field 'shell' */
490 for (i = 0; i < 4; i++) {
491 token = strtok_r(NULL, ":", &saveptr);
492 if (!token)
493 break;
494 }
495 if (!token)
496 continue;
497 free(result);
498 result = strdup(token);
499
500 /* Sanity check that there are no fields after that. */
501 token = strtok_r(NULL, ":", &saveptr);
502 if (token)
503 continue;
504
505 found = 1;
506 }
507
508 free(line);
509 fclose(pipe_f);
510 again:
511 if (waitpid(pid, &status, 0) < 0) {
512 if (errno == EINTR)
513 goto again;
514 free(result);
515 return NULL;
516 }
517
518 /* Some sanity checks. If anything even hinted at going wrong,
519 * we can't be sure we have a valid result, so we assume we
520 * don't.
521 */
522
523 if (!WIFEXITED(status)) {
524 free(result);
525 return NULL;
526 }
527
528 if (WEXITSTATUS(status) != 0) {
529 free(result);
530 return NULL;
531 }
532
533 if (!found) {
534 free(result);
535 return NULL;
536 }
537
538 return result;
539 } else {
540 char uid_buf[32];
541 char *arguments[] = {
542 "getent",
543 "passwd",
544 uid_buf,
545 NULL
546 };
547
548 close(pipes[0]);
549
550 /* We want to capture stdout. */
551 dup2(pipes[1], 1);
552 close(pipes[1]);
553
554 /* Get rid of stdin/stderr, so we try to associate it with
555 * /dev/null.
556 */
557 fd = open("/dev/null", O_RDWR);
558 if (fd < 0) {
559 close(0);
560 close(2);
561 } else {
562 dup2(fd, 0);
563 dup2(fd, 2);
564 close(fd);
565 }
566
567 /* Finish argument list. */
568 ret = snprintf(uid_buf, sizeof(uid_buf), "%ld", (long) uid);
569 if (ret <= 0)
570 exit(-1);
571
572 /* Try to run getent program. */
573 (void) execvp("getent", arguments);
574 exit(-1);
575 }
576 }
577
578 static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
579 {
580 FILE *proc_file;
581 char proc_fn[__PROC_STATUS_LEN];
582 int ret;
583 char *line = NULL;
584 size_t line_bufsz = 0;
585 long value = -1;
586 uid_t uid = (uid_t)-1;
587 gid_t gid = (gid_t)-1;
588
589 /* Read capabilities. */
590 snprintf(proc_fn, __PROC_STATUS_LEN, "/proc/%d/status", 1);
591
592 proc_file = fopen(proc_fn, "r");
593 if (!proc_file)
594 return;
595
596 while (getline(&line, &line_bufsz, proc_file) != -1) {
597 /* Format is: real, effective, saved set user, fs we only care
598 * about real uid.
599 */
600 ret = sscanf(line, "Uid: %ld", &value);
601 if (ret != EOF && ret == 1) {
602 uid = (uid_t)value;
603 } else {
604 ret = sscanf(line, "Gid: %ld", &value);
605 if (ret != EOF && ret == 1)
606 gid = (gid_t)value;
607 }
608 if (uid != (uid_t)-1 && gid != (gid_t)-1)
609 break;
610 }
611
612 fclose(proc_file);
613 free(line);
614
615 /* Only override arguments if we found something. */
616 if (uid != (uid_t)-1)
617 *init_uid = uid;
618 if (gid != (gid_t)-1)
619 *init_gid = gid;
620
621 /* TODO: we should also parse supplementary groups and use
622 * setgroups() to set them.
623 */
624 }
625
626 /* Help the optimizer along if it doesn't know that exit always exits. */
627 #define rexit(c) \
628 do { \
629 int __c = (c); \
630 _exit(__c); \
631 return __c; \
632 } while (0)
633
634 /* Define default options if no options are supplied by the user. */
635 static lxc_attach_options_t attach_static_default_options = LXC_ATTACH_OPTIONS_DEFAULT;
636
637 static bool fetch_seccomp(struct lxc_container *c,
638 lxc_attach_options_t *options)
639 {
640 char *path;
641
642 if (!(options->namespaces & CLONE_NEWNS) ||
643 !(options->attach_flags & LXC_ATTACH_LSM)) {
644 free(c->lxc_conf->seccomp);
645 c->lxc_conf->seccomp = NULL;
646 return true;
647 }
648
649 /* Remove current setting. */
650 if (!c->set_config_item(c, "lxc.seccomp", "") &&
651 !c->set_config_item(c, "lxc.seccomp.profile", "")) {
652 return false;
653 }
654
655 /* Fetch the current profile path over the cmd interface. */
656 path = c->get_running_config_item(c, "lxc.seccomp.profile");
657 if (!path) {
658 INFO("Failed to get running config item for lxc.seccomp.profile");
659 path = c->get_running_config_item(c, "lxc.seccomp");
660 }
661 if (!path) {
662 INFO("Failed to get running config item for lxc.seccomp");
663 return true;
664 }
665
666 /* Copy the value into the new lxc_conf. */
667 if (!c->set_config_item(c, "lxc.seccomp.profile", path)) {
668 free(path);
669 return false;
670 }
671 free(path);
672
673 /* Attempt to parse the resulting config. */
674 if (lxc_read_seccomp_config(c->lxc_conf) < 0) {
675 ERROR("Error reading seccomp policy.");
676 return false;
677 }
678
679 INFO("Retrieved seccomp policy.");
680 return true;
681 }
682
683 static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options)
684 {
685 char *val;
686
687 /* Remove current setting. */
688 if (!c->set_config_item(c, "lxc.no_new_privs", ""))
689 return false;
690
691 /* Retrieve currently active setting. */
692 val = c->get_running_config_item(c, "lxc.no_new_privs");
693 if (!val) {
694 INFO("Failed to get running config item for lxc.no_new_privs.");
695 return false;
696 }
697
698 /* Set currently active setting. */
699 if (!c->set_config_item(c, "lxc.no_new_privs", val)) {
700 free(val);
701 return false;
702 }
703 free(val);
704
705 return true;
706 }
707
708 static signed long get_personality(const char *name, const char *lxcpath)
709 {
710 char *p;
711 signed long ret;
712
713 p = lxc_cmd_get_config_item(name, "lxc.arch", lxcpath);
714 if (!p)
715 return -1;
716
717 ret = lxc_config_parse_arch(p);
718 free(p);
719
720 return ret;
721 }
722
723 struct attach_clone_payload {
724 int ipc_socket;
725 int pty_fd;
726 lxc_attach_options_t *options;
727 struct lxc_proc_context_info *init_ctx;
728 lxc_attach_exec_t exec_function;
729 void *exec_payload;
730 };
731
732 static void lxc_put_attach_clone_payload(struct attach_clone_payload *p)
733 {
734 if (p->ipc_socket >= 0) {
735 shutdown(p->ipc_socket, SHUT_RDWR);
736 close(p->ipc_socket);
737 p->ipc_socket = -EBADF;
738 }
739
740 if (p->pty_fd >= 0) {
741 close(p->pty_fd);
742 p->pty_fd = -EBADF;
743 }
744
745 if (p->init_ctx)
746 lxc_proc_put_context_info(p->init_ctx);
747 }
748
749 static int attach_child_main(struct attach_clone_payload *payload)
750 {
751 int fd, lsm_fd, ret;
752 uid_t new_uid;
753 gid_t new_gid;
754 lxc_attach_options_t* options = payload->options;
755 struct lxc_proc_context_info* init_ctx = payload->init_ctx;
756 bool needs_lsm = (options->namespaces & CLONE_NEWNS) &&
757 (options->attach_flags & LXC_ATTACH_LSM) &&
758 init_ctx->lsm_label;
759
760 /* A description of the purpose of this functionality is provided in the
761 * lxc-attach(1) manual page. We have to remount here and not in the
762 * parent process, otherwise /proc may not properly reflect the new pid
763 * namespace.
764 */
765 if (!(options->namespaces & CLONE_NEWNS) &&
766 (options->attach_flags & LXC_ATTACH_REMOUNT_PROC_SYS)) {
767 ret = lxc_attach_remount_sys_proc();
768 if (ret < 0)
769 goto on_error;
770 TRACE("Remounted \"/proc\" and \"/sys\"");
771 }
772
773 /* Now perform additional attachments. */
774 #if HAVE_SYS_PERSONALITY_H
775 if (options->attach_flags & LXC_ATTACH_SET_PERSONALITY) {
776 long new_personality;
777
778 if (options->personality < 0)
779 new_personality = init_ctx->personality;
780 else
781 new_personality = options->personality;
782 ret = personality(new_personality);
783 if (ret < 0)
784 goto on_error;
785 TRACE("Set new personality");
786 }
787 #endif
788
789 if (options->attach_flags & LXC_ATTACH_DROP_CAPABILITIES) {
790 ret = lxc_attach_drop_privs(init_ctx);
791 if (ret < 0)
792 goto on_error;
793 TRACE("Dropped capabilities");
794 }
795
796 /* Always set the environment (specify (LXC_ATTACH_KEEP_ENV, NULL, NULL)
797 * if you want this to be a no-op).
798 */
799 ret = lxc_attach_set_environment(options->env_policy,
800 options->extra_env_vars,
801 options->extra_keep_env);
802 if (ret < 0)
803 goto on_error;
804 TRACE("Set up environment");
805
806 /* This remark only affects fully unprivileged containers:
807 * Receive fd for LSM security module before we set{g,u}id(). The reason
808 * is that on set{g,u}id() the kernel will a) make us undumpable and b)
809 * we will change our effective uid. This means our effective uid will
810 * be different from the effective uid of the process that created us
811 * which means that this processs no longer has capabilities in our
812 * namespace including CAP_SYS_PTRACE. This means we will not be able to
813 * read and /proc/<pid> files for the process anymore when /proc is
814 * mounted with hidepid={1,2}. So let's get the lsm label fd before the
815 * set{g,u}id().
816 */
817 if (needs_lsm) {
818 ret = lxc_abstract_unix_recv_fds(payload->ipc_socket, &lsm_fd, 1, NULL, 0);
819 if (ret <= 0)
820 goto on_error;
821 TRACE("Received LSM label file descriptor %d from parent", lsm_fd);
822 }
823
824 if (options->stdin_fd > 0 && isatty(options->stdin_fd)) {
825 ret = lxc_make_controlling_pty(options->stdin_fd);
826 if (ret < 0)
827 goto on_error;
828 }
829
830 /* Set {u,g}id. */
831 new_uid = 0;
832 new_gid = 0;
833 /* Ignore errors, we will fall back to root in that case (/proc was not
834 * mounted etc.).
835 */
836 if (options->namespaces & CLONE_NEWUSER)
837 lxc_attach_get_init_uidgid(&new_uid, &new_gid);
838
839 if (options->uid != (uid_t)-1)
840 new_uid = options->uid;
841 if (options->gid != (gid_t)-1)
842 new_gid = options->gid;
843
844 /* Try to set the {u,g}id combination. */
845 if (new_uid != 0 || new_gid != 0 || options->namespaces & CLONE_NEWUSER) {
846 ret = lxc_switch_uid_gid(new_uid, new_gid);
847 if (ret < 0)
848 goto on_error;
849
850 ret = lxc_setgroups(0, NULL);
851 if (ret < 0)
852 goto on_error;
853 }
854
855 if ((init_ctx->container && init_ctx->container->lxc_conf &&
856 init_ctx->container->lxc_conf->no_new_privs) ||
857 (options->attach_flags & LXC_ATTACH_NO_NEW_PRIVS)) {
858 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
859 if (ret < 0)
860 goto on_error;
861 TRACE("Set PR_SET_NO_NEW_PRIVS");
862 }
863
864 if (needs_lsm) {
865 bool on_exec;
866
867 /* Change into our new LSM profile. */
868 on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false;
869 ret = lsm_process_label_set_at(lsm_fd, init_ctx->lsm_label, on_exec);
870 close(lsm_fd);
871 if (ret < 0)
872 goto on_error;
873 TRACE("Set %s LSM label to \"%s\"", lsm_name(), init_ctx->lsm_label);
874 }
875
876 if (init_ctx->container && init_ctx->container->lxc_conf &&
877 init_ctx->container->lxc_conf->seccomp) {
878 ret = lxc_seccomp_load(init_ctx->container->lxc_conf);
879 if (ret < 0)
880 goto on_error;
881 TRACE("Loaded seccomp profile");
882 }
883 shutdown(payload->ipc_socket, SHUT_RDWR);
884 close(payload->ipc_socket);
885 payload->ipc_socket = -EBADF;
886 lxc_proc_put_context_info(init_ctx);
887
888 /* The following is done after the communication socket is shut down.
889 * That way, all errors that might (though unlikely) occur up until this
890 * point will have their messages printed to the original stderr (if
891 * logging is so configured) and not the fd the user supplied, if any.
892 */
893
894 /* Fd handling for stdin, stdout and stderr; ignore errors here, user
895 * may want to make sure the fds are closed, for example.
896 */
897 if (options->stdin_fd >= 0 && options->stdin_fd != STDIN_FILENO)
898 dup2(options->stdin_fd, STDIN_FILENO);
899
900 if (options->stdout_fd >= 0 && options->stdout_fd != STDOUT_FILENO)
901 dup2(options->stdout_fd, STDOUT_FILENO);
902
903 if (options->stderr_fd >= 0 && options->stderr_fd != STDERR_FILENO)
904 dup2(options->stderr_fd, STDERR_FILENO);
905
906 /* close the old fds */
907 if (options->stdin_fd > STDERR_FILENO)
908 close(options->stdin_fd);
909
910 if (options->stdout_fd > STDERR_FILENO)
911 close(options->stdout_fd);
912
913 if (options->stderr_fd > STDERR_FILENO)
914 close(options->stderr_fd);
915
916 /* Try to remove FD_CLOEXEC flag from stdin/stdout/stderr, but also
917 * here, ignore errors.
918 */
919 for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) {
920 int flags;
921
922 flags = fcntl(fd, F_GETFL);
923 if (flags < 0)
924 continue;
925
926 if ((flags & FD_CLOEXEC) == 0)
927 continue;
928
929 ret = fcntl(fd, F_SETFL, flags & ~FD_CLOEXEC);
930 if (ret < 0) {
931 SYSERROR("Failed to clear FD_CLOEXEC from file descriptor %d", fd);
932 goto on_error;
933 }
934 }
935
936 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
937 ret = lxc_login_pty(payload->pty_fd);
938 if (ret < 0) {
939 SYSERROR("Failed to prepare pty file descriptor %d", payload->pty_fd);
940 goto on_error;
941 }
942 TRACE("Prepared pty file descriptor %d", payload->pty_fd);
943 }
944
945 /* We're done, so we can now do whatever the user intended us to do. */
946 rexit(payload->exec_function(payload->exec_payload));
947
948 on_error:
949 lxc_put_attach_clone_payload(payload);
950 rexit(EXIT_FAILURE);
951 }
952
953 static int lxc_attach_pty(struct lxc_conf *conf, struct lxc_console *pty)
954 {
955 int ret;
956
957 lxc_pty_init(pty);
958
959 ret = lxc_pty_create(pty);
960 if (ret < 0) {
961 SYSERROR("Failed to create pty");
962 return -1;
963 }
964
965 /* Shift ttys to container. */
966 ret = lxc_pty_map_ids(conf, pty);
967 if (ret < 0) {
968 ERROR("Failed to shift pty");
969 goto on_error;
970 }
971
972 return 0;
973
974 on_error:
975 lxc_console_delete(pty);
976 lxc_pty_conf_free(pty);
977 return -1;
978 }
979
980 static int lxc_attach_pty_mainloop_init(struct lxc_console *pty,
981 struct lxc_epoll_descr *descr)
982 {
983 int ret;
984
985 ret = lxc_mainloop_open(descr);
986 if (ret < 0) {
987 ERROR("Failed to create mainloop");
988 return -1;
989 }
990
991 ret = lxc_console_mainloop_add(descr, pty);
992 if (ret < 0) {
993 ERROR("Failed to add handlers to mainloop");
994 lxc_mainloop_close(descr);
995 return -1;
996 }
997
998 return 0;
999 }
1000
1001 static inline void lxc_attach_pty_close_master(struct lxc_console *pty)
1002 {
1003 if (pty->master < 0)
1004 return;
1005
1006 close(pty->master);
1007 pty->master = -EBADF;
1008 }
1009
1010 static inline void lxc_attach_pty_close_slave(struct lxc_console *pty)
1011 {
1012 if (pty->slave < 0)
1013 return;
1014
1015 close(pty->slave);
1016 pty->slave = -EBADF;
1017 }
1018
1019 static inline void lxc_attach_pty_close_peer(struct lxc_console *pty)
1020 {
1021 if (pty->peer < 0)
1022 return;
1023
1024 close(pty->peer);
1025 pty->peer = -EBADF;
1026 }
1027
1028 static inline void lxc_attach_pty_close_log(struct lxc_console *pty)
1029 {
1030 if (pty->log_fd < 0)
1031 return;
1032
1033 close(pty->log_fd);
1034 pty->log_fd = -EBADF;
1035 }
1036
1037 int lxc_attach(const char *name, const char *lxcpath,
1038 lxc_attach_exec_t exec_function, void *exec_payload,
1039 lxc_attach_options_t *options, pid_t *attached_process)
1040 {
1041 int i, ret, status;
1042 int ipc_sockets[2];
1043 char *cwd, *new_cwd;
1044 signed long personality;
1045 pid_t attached_pid, init_pid, pid;
1046 struct lxc_proc_context_info *init_ctx;
1047 struct lxc_console pty;
1048 struct attach_clone_payload payload = {0};
1049
1050 ret = access("/proc/self/ns", X_OK);
1051 if (ret) {
1052 ERROR("Does this kernel version support namespaces?");
1053 return -1;
1054 }
1055
1056 if (!options)
1057 options = &attach_static_default_options;
1058
1059 init_pid = lxc_cmd_get_init_pid(name, lxcpath);
1060 if (init_pid < 0) {
1061 ERROR("Failed to get init pid.");
1062 return -1;
1063 }
1064
1065 init_ctx = lxc_proc_get_context_info(init_pid);
1066 if (!init_ctx) {
1067 ERROR("Failed to get context of init process: %ld", (long)init_pid);
1068 return -1;
1069 }
1070
1071 personality = get_personality(name, lxcpath);
1072 if (init_ctx->personality < 0) {
1073 ERROR("Failed to get personality of the container");
1074 lxc_proc_put_context_info(init_ctx);
1075 return -1;
1076 }
1077 init_ctx->personality = personality;
1078
1079 init_ctx->container = lxc_container_new(name, lxcpath);
1080 if (!init_ctx->container) {
1081 lxc_proc_put_context_info(init_ctx);
1082 return -1;
1083 }
1084
1085 if (!init_ctx->container->lxc_conf) {
1086 init_ctx->container->lxc_conf = lxc_conf_init();
1087 if (!init_ctx->container->lxc_conf) {
1088 lxc_proc_put_context_info(init_ctx);
1089 return -ENOMEM;
1090 }
1091 }
1092
1093 if (!fetch_seccomp(init_ctx->container, options))
1094 WARN("Failed to get seccomp policy.");
1095
1096 if (!no_new_privs(init_ctx->container, options))
1097 WARN("Could not determine whether PR_SET_NO_NEW_PRIVS is set.");
1098
1099 cwd = getcwd(NULL, 0);
1100
1101 /* Determine which namespaces the container was created with
1102 * by asking lxc-start, if necessary.
1103 */
1104 if (options->namespaces == -1) {
1105 options->namespaces = lxc_cmd_get_clone_flags(name, lxcpath);
1106 /* call failed */
1107 if (options->namespaces == -1) {
1108 ERROR("Failed to automatically determine the "
1109 "namespaces which the container uses");
1110 free(cwd);
1111 lxc_proc_put_context_info(init_ctx);
1112 return -1;
1113 }
1114
1115 for (i = 0; i < LXC_NS_MAX; i++) {
1116 if (ns_info[i].clone_flag & CLONE_NEWCGROUP)
1117 if (!(options->attach_flags & LXC_ATTACH_MOVE_TO_CGROUP) ||
1118 !cgns_supported())
1119 continue;
1120
1121 if (ns_info[i].clone_flag & options->namespaces)
1122 continue;
1123
1124 init_ctx->ns_inherited |= ns_info[i].clone_flag;
1125 }
1126 }
1127
1128 pid = lxc_raw_getpid();
1129 for (i = 0; i < LXC_NS_MAX; i++) {
1130 int j, saved_errno;
1131
1132 if (options->namespaces & ns_info[i].clone_flag)
1133 init_ctx->ns_fd[i] = lxc_preserve_ns(init_pid, ns_info[i].proc_name);
1134 else if (init_ctx->ns_inherited & ns_info[i].clone_flag)
1135 init_ctx->ns_fd[i] = in_same_namespace(pid, init_pid, ns_info[i].proc_name);
1136 else
1137 continue;
1138 if (init_ctx->ns_fd[i] >= 0)
1139 continue;
1140
1141 if (init_ctx->ns_fd[i] == -EINVAL) {
1142 DEBUG("Inheriting %s namespace from %d",
1143 ns_info[i].proc_name, pid);
1144 init_ctx->ns_inherited &= ~ns_info[i].clone_flag;
1145 continue;
1146 }
1147
1148 /* We failed to preserve the namespace. */
1149 saved_errno = errno;
1150 /* Close all already opened file descriptors before we return an
1151 * error, so we don't leak them.
1152 */
1153 for (j = 0; j < i; j++)
1154 close(init_ctx->ns_fd[j]);
1155
1156 errno = saved_errno;
1157 SYSERROR("Failed to attach to %s namespace of %d",
1158 ns_info[i].proc_name, pid);
1159 free(cwd);
1160 lxc_proc_put_context_info(init_ctx);
1161 return -1;
1162 }
1163
1164 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1165 ret = lxc_attach_pty(init_ctx->container->lxc_conf, &pty);
1166 if (ret < 0) {
1167 ERROR("Failed to allocate pty");
1168 free(cwd);
1169 lxc_proc_put_context_info(init_ctx);
1170 return -1;
1171 }
1172
1173 pty.log_fd = options->log_fd;
1174 }
1175
1176 /* Create a socket pair for IPC communication; set SOCK_CLOEXEC in order
1177 * to make sure we don't irritate other threads that want to fork+exec
1178 * away
1179 *
1180 * IMPORTANT: if the initial process is multithreaded and another call
1181 * just fork()s away without exec'ing directly after, the socket fd will
1182 * exist in the forked process from the other thread and any close() in
1183 * our own child process will not really cause the socket to close
1184 * properly, potentiall causing the parent to hang.
1185 *
1186 * For this reason, while IPC is still active, we have to use shutdown()
1187 * if the child exits prematurely in order to signal that the socket is
1188 * closed and cannot assume that the child exiting will automatically do
1189 * that.
1190 *
1191 * IPC mechanism: (X is receiver)
1192 * initial process intermediate attached
1193 * X <--- send pid of
1194 * attached proc,
1195 * then exit
1196 * send 0 ------------------------------------> X
1197 * [do initialization]
1198 * X <------------------------------------ send 1
1199 * [add to cgroup, ...]
1200 * send 2 ------------------------------------> X
1201 * [set LXC_ATTACH_NO_NEW_PRIVS]
1202 * X <------------------------------------ send 3
1203 * [open LSM label fd]
1204 * send 4 ------------------------------------> X
1205 * [set LSM label]
1206 * close socket close socket
1207 * run program
1208 */
1209 ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
1210 if (ret < 0) {
1211 SYSERROR("Could not set up required IPC mechanism for attaching.");
1212 free(cwd);
1213 lxc_proc_put_context_info(init_ctx);
1214 return -1;
1215 }
1216
1217 /* Create intermediate subprocess, three reasons:
1218 * 1. Runs all pthread_atfork handlers and the child will no
1219 * longer be threaded (we can't properly setns() in a threaded
1220 * process).
1221 * 2. We can't setns() in the child itself, since we want to make
1222 * sure we are properly attached to the pidns.
1223 * 3. Also, the initial thread has to put the attached process
1224 * into the cgroup, which we can only do if we didn't already
1225 * setns() (otherwise, user namespaces will hate us).
1226 */
1227 pid = fork();
1228 if (pid < 0) {
1229 SYSERROR("Failed to create first subprocess.");
1230 free(cwd);
1231 lxc_proc_put_context_info(init_ctx);
1232 return -1;
1233 }
1234
1235 if (pid) {
1236 int ret_parent = -1;
1237 pid_t to_cleanup_pid = pid;
1238 struct lxc_epoll_descr descr = {0};
1239
1240 /* close unneeded file descriptors */
1241 close(ipc_sockets[1]);
1242 free(cwd);
1243 lxc_proc_close_ns_fd(init_ctx);
1244 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY)
1245 lxc_attach_pty_close_slave(&pty);
1246
1247 /* Attach to cgroup, if requested. */
1248 if (options->attach_flags & LXC_ATTACH_MOVE_TO_CGROUP) {
1249 if (!cgroup_attach(name, lxcpath, pid))
1250 goto on_error;
1251 TRACE("Moved intermediate process %d into container's "
1252 "cgroups", pid);
1253 }
1254
1255 /* Setup resource limits */
1256 if (!lxc_list_empty(&init_ctx->container->lxc_conf->limits)) {
1257 ret = setup_resource_limits(&init_ctx->container->lxc_conf->limits, pid);
1258 if (ret < 0)
1259 goto on_error;
1260 }
1261
1262 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1263 ret = lxc_attach_pty_mainloop_init(&pty, &descr);
1264 if (ret < 0)
1265 goto on_error;
1266 TRACE("Initalized pty mainloop");
1267 }
1268
1269 /* Let the child process know to go ahead. */
1270 status = 0;
1271 ret = lxc_write_nointr(ipc_sockets[0], &status, sizeof(status));
1272 if (ret != sizeof(status))
1273 goto close_mainloop;
1274 TRACE("Told intermediate process to start initializing");
1275
1276 /* Get pid of attached process from intermediate process. */
1277 ret = lxc_read_nointr(ipc_sockets[0], &attached_pid, sizeof(attached_pid));
1278 if (ret != sizeof(attached_pid))
1279 goto close_mainloop;
1280 TRACE("Received pid %d of attached process in parent pid namespace", attached_pid);
1281
1282 /* Ignore SIGKILL (CTRL-C) and SIGQUIT (CTRL-\) - issue #313. */
1283 if (options->stdin_fd == 0) {
1284 signal(SIGINT, SIG_IGN);
1285 signal(SIGQUIT, SIG_IGN);
1286 }
1287
1288 /* Reap intermediate process. */
1289 ret = wait_for_pid(pid);
1290 if (ret < 0)
1291 goto close_mainloop;
1292 TRACE("Intermediate process %d exited", pid);
1293
1294 /* We will always have to reap the attached process now. */
1295 to_cleanup_pid = attached_pid;
1296
1297 /* Open LSM fd and send it to child. */
1298 if ((options->namespaces & CLONE_NEWNS) &&
1299 (options->attach_flags & LXC_ATTACH_LSM) &&
1300 init_ctx->lsm_label) {
1301 int ret = -1;
1302 int labelfd;
1303 bool on_exec;
1304
1305 on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false;
1306 labelfd = lsm_process_label_fd_get(attached_pid, on_exec);
1307 if (labelfd < 0)
1308 goto close_mainloop;
1309 TRACE("Opened LSM label file descriptor %d", labelfd);
1310
1311 /* Send child fd of the LSM security module to write to. */
1312 ret = lxc_abstract_unix_send_fds(ipc_sockets[0], &labelfd, 1, NULL, 0);
1313 close(labelfd);
1314 if (ret <= 0) {
1315 SYSERROR("%d", (int)ret);
1316 goto close_mainloop;
1317 }
1318 TRACE("Sent LSM label file descriptor %d to child", labelfd);
1319 }
1320
1321 /* We're done, the child process should now execute whatever it
1322 * is that the user requested. The parent can now track it with
1323 * waitpid() or similar.
1324 */
1325
1326 *attached_process = attached_pid;
1327
1328 /* Now shut down communication with child, we're done. */
1329 shutdown(ipc_sockets[0], SHUT_RDWR);
1330 close(ipc_sockets[0]);
1331 ipc_sockets[0] = -1;
1332
1333 ret_parent = 0;
1334 to_cleanup_pid = -1;
1335 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1336 ret = lxc_mainloop(&descr, -1);
1337 if (ret < 0) {
1338 ret_parent = -1;
1339 to_cleanup_pid = attached_pid;
1340 }
1341 }
1342
1343 close_mainloop:
1344 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY)
1345 lxc_mainloop_close(&descr);
1346
1347 on_error:
1348 if (ipc_sockets[0] >= 0) {
1349 shutdown(ipc_sockets[0], SHUT_RDWR);
1350 close(ipc_sockets[0]);
1351 }
1352
1353 if (to_cleanup_pid > 0)
1354 (void)wait_for_pid(to_cleanup_pid);
1355
1356 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1357 lxc_console_delete(&pty);
1358 lxc_pty_conf_free(&pty);
1359 }
1360 lxc_proc_put_context_info(init_ctx);
1361 return ret_parent;
1362 }
1363
1364 /* close unneeded file descriptors */
1365 close(ipc_sockets[0]);
1366 ipc_sockets[0] = -EBADF;
1367 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1368 lxc_attach_pty_close_master(&pty);
1369 lxc_attach_pty_close_peer(&pty);
1370 lxc_attach_pty_close_log(&pty);
1371 }
1372
1373 /* Wait for the parent to have setup cgroups. */
1374 ret = lxc_read_nointr(ipc_sockets[1], &status, sizeof(status));
1375 if (ret != sizeof(status)) {
1376 shutdown(ipc_sockets[1], SHUT_RDWR);
1377 lxc_proc_put_context_info(init_ctx);
1378 rexit(-1);
1379 }
1380 TRACE("Intermediate process starting to initialize");
1381
1382 /* Attach now, create another subprocess later, since pid namespaces
1383 * only really affect the children of the current process.
1384 */
1385 ret = lxc_attach_to_ns(init_pid, init_ctx);
1386 if (ret < 0) {
1387 ERROR("Failed to enter namespaces");
1388 shutdown(ipc_sockets[1], SHUT_RDWR);
1389 lxc_proc_put_context_info(init_ctx);
1390 rexit(-1);
1391 }
1392 /* close namespace file descriptors */
1393 lxc_proc_close_ns_fd(init_ctx);
1394
1395 /* Attach succeeded, try to cwd. */
1396 if (options->initial_cwd)
1397 new_cwd = options->initial_cwd;
1398 else
1399 new_cwd = cwd;
1400 if (new_cwd) {
1401 ret = chdir(new_cwd);
1402 if (ret < 0)
1403 WARN("Could not change directory to \"%s\"", new_cwd);
1404 }
1405 free(cwd);
1406
1407 /* Create attached process. */
1408 payload.ipc_socket = ipc_sockets[1];
1409 payload.options = options;
1410 payload.init_ctx = init_ctx;
1411 payload.pty_fd = pty.slave;
1412 payload.exec_function = exec_function;
1413 payload.exec_payload = exec_payload;
1414
1415 pid = lxc_raw_clone(CLONE_PARENT);
1416 if (pid < 0) {
1417 SYSERROR("Failed to clone attached process");
1418 shutdown(ipc_sockets[1], SHUT_RDWR);
1419 lxc_proc_put_context_info(init_ctx);
1420 rexit(-1);
1421 }
1422
1423 if (pid == 0) {
1424 ret = attach_child_main(&payload);
1425 if (ret < 0)
1426 ERROR("Failed to exec");
1427 _exit(EXIT_FAILURE);
1428 }
1429 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY)
1430 lxc_attach_pty_close_slave(&pty);
1431
1432 /* Tell grandparent the pid of the pid of the newly created child. */
1433 ret = lxc_write_nointr(ipc_sockets[1], &pid, sizeof(pid));
1434 if (ret != sizeof(pid)) {
1435 /* If this really happens here, this is very unfortunate, since
1436 * the parent will not know the pid of the attached process and
1437 * will not be able to wait for it (and we won't either due to
1438 * CLONE_PARENT) so the parent won't be able to reap it and the
1439 * attached process will remain a zombie.
1440 */
1441 shutdown(ipc_sockets[1], SHUT_RDWR);
1442 lxc_proc_put_context_info(init_ctx);
1443 rexit(-1);
1444 }
1445 TRACE("Sending pid %d of attached process", pid);
1446
1447 /* The rest is in the hands of the initial and the attached process. */
1448 lxc_proc_put_context_info(init_ctx);
1449 rexit(0);
1450 }
1451
1452 int lxc_attach_run_command(void* payload)
1453 {
1454 lxc_attach_command_t* cmd = (lxc_attach_command_t*)payload;
1455
1456 execvp(cmd->program, cmd->argv);
1457 SYSERROR("Failed to exec \"%s\".", cmd->program);
1458 return -1;
1459 }
1460
1461 int lxc_attach_run_shell(void* payload)
1462 {
1463 uid_t uid;
1464 struct passwd *passwd;
1465 char *user_shell;
1466
1467 /* Ignore payload parameter. */
1468 (void)payload;
1469
1470 uid = getuid();
1471 passwd = getpwuid(uid);
1472
1473 /* This probably happens because of incompatible nss implementations in
1474 * host and container (remember, this code is still using the host's
1475 * glibc but our mount namespace is in the container) we may try to get
1476 * the information by spawning a [getent passwd uid] process and parsing
1477 * the result.
1478 */
1479 if (!passwd)
1480 user_shell = lxc_attach_getpwshell(uid);
1481 else
1482 user_shell = passwd->pw_shell;
1483 if (user_shell)
1484 execlp(user_shell, user_shell, (char *)NULL);
1485
1486 /* Executed if either no passwd entry or execvp fails, we will fall back
1487 * on /bin/sh as a default shell.
1488 */
1489 execlp("/bin/sh", "/bin/sh", (char *)NULL);
1490 SYSERROR("Failed to execute shell");
1491 if (!passwd)
1492 free(user_shell);
1493 return -1;
1494 }