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