]>
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"); | |
cf4026f1 CB |
434 | if (!pipe_f) { |
435 | close(pipes[0]); | |
436 | goto reap_child; | |
437 | } | |
438 | /* Transfer ownership of pipes[0] to pipe_f. */ | |
439 | move_fd(pipes[0]); | |
440 | ||
3fa23ac3 CB |
441 | while (getline(&line, &line_bufsz, pipe_f) != -1) { |
442 | int i; | |
443 | long value; | |
444 | char *token; | |
445 | char *endptr = NULL, *saveptr = NULL; | |
446 | ||
447 | /* If we already found something, just continue to read | |
448 | * until the pipe doesn't deliver any more data, but | |
449 | * don't modify the existing data structure. | |
450 | */ | |
451 | if (found) | |
452 | continue; | |
453 | ||
18d4ffde | 454 | if (!line) |
455 | continue; | |
456 | ||
3fa23ac3 CB |
457 | /* Trim line on the right hand side. */ |
458 | for (i = strlen(line); i > 0 && (line[i - 1] == '\n' || line[i - 1] == '\r'); --i) | |
459 | line[i - 1] = '\0'; | |
460 | ||
461 | /* Split into tokens: first: user name. */ | |
462 | token = strtok_r(line, ":", &saveptr); | |
463 | if (!token) | |
464 | continue; | |
465 | ||
466 | /* next: dummy password field */ | |
467 | token = strtok_r(NULL, ":", &saveptr); | |
468 | if (!token) | |
469 | continue; | |
470 | ||
471 | /* next: user id */ | |
472 | token = strtok_r(NULL, ":", &saveptr); | |
473 | value = token ? strtol(token, &endptr, 10) : 0; | |
474 | if (!token || !endptr || *endptr || value == LONG_MIN || | |
ea918412 | 475 | value == LONG_MAX) |
3fa23ac3 CB |
476 | continue; |
477 | ||
478 | /* dummy sanity check: user id matches */ | |
479 | if ((uid_t)value != uid) | |
480 | continue; | |
481 | ||
482 | /* skip fields: gid, gecos, dir, go to next field 'shell' */ | |
483 | for (i = 0; i < 4; i++) { | |
484 | token = strtok_r(NULL, ":", &saveptr); | |
485 | if (!token) | |
486 | continue; | |
487 | } | |
ea918412 | 488 | |
3fa23ac3 CB |
489 | if (!token) |
490 | continue; | |
ea918412 | 491 | |
3fa23ac3 CB |
492 | free(result); |
493 | result = strdup(token); | |
494 | ||
495 | /* Sanity check that there are no fields after that. */ | |
496 | token = strtok_r(NULL, ":", &saveptr); | |
497 | if (token) | |
498 | continue; | |
499 | ||
500 | found = true; | |
501 | } | |
ea918412 | 502 | |
cf4026f1 | 503 | reap_child: |
3fa23ac3 CB |
504 | ret = wait_for_pid(pid); |
505 | if (ret < 0) { | |
506 | free(result); | |
507 | return NULL; | |
508 | } | |
509 | ||
510 | if (!found) { | |
511 | free(result); | |
512 | return NULL; | |
513 | } | |
514 | ||
515 | return result; | |
905022f7 | 516 | } |
cb3e61fa | 517 | |
6f4f1937 | 518 | static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid) |
cb3e61fa | 519 | { |
cd8f5663 CB |
520 | __do_free char *line = NULL; |
521 | __do_fclose FILE *proc_file = NULL; | |
604ca1c0 | 522 | char proc_fn[LXC_PROC_STATUS_LEN]; |
8ce83369 | 523 | int ret; |
cb3e61fa | 524 | size_t line_bufsz = 0; |
cb3e61fa CS |
525 | long value = -1; |
526 | uid_t uid = (uid_t)-1; | |
527 | gid_t gid = (gid_t)-1; | |
528 | ||
604ca1c0 CB |
529 | ret = snprintf(proc_fn, LXC_PROC_STATUS_LEN, "/proc/%d/status", 1); |
530 | if (ret < 0 || ret >= LXC_PROC_STATUS_LEN) | |
7fb45c93 | 531 | return; |
cb3e61fa CS |
532 | |
533 | proc_file = fopen(proc_fn, "r"); | |
534 | if (!proc_file) | |
535 | return; | |
536 | ||
537 | while (getline(&line, &line_bufsz, proc_file) != -1) { | |
8ce83369 CB |
538 | /* Format is: real, effective, saved set user, fs we only care |
539 | * about real uid. | |
cb3e61fa CS |
540 | */ |
541 | ret = sscanf(line, "Uid: %ld", &value); | |
8ce83369 | 542 | if (ret != EOF && ret == 1) { |
6f4f1937 | 543 | uid = (uid_t)value; |
cb3e61fa CS |
544 | } else { |
545 | ret = sscanf(line, "Gid: %ld", &value); | |
8ce83369 | 546 | if (ret != EOF && ret == 1) |
6f4f1937 | 547 | gid = (gid_t)value; |
cb3e61fa | 548 | } |
ea918412 | 549 | |
cb3e61fa CS |
550 | if (uid != (uid_t)-1 && gid != (gid_t)-1) |
551 | break; | |
552 | } | |
553 | ||
8ce83369 | 554 | /* Only override arguments if we found something. */ |
cb3e61fa CS |
555 | if (uid != (uid_t)-1) |
556 | *init_uid = uid; | |
ea918412 | 557 | |
cb3e61fa CS |
558 | if (gid != (gid_t)-1) |
559 | *init_gid = gid; | |
560 | ||
561 | /* TODO: we should also parse supplementary groups and use | |
8ce83369 CB |
562 | * setgroups() to set them. |
563 | */ | |
cb3e61fa | 564 | } |
9c4693b8 | 565 | |
d4db3d14 | 566 | static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options) |
2c4ea790 | 567 | { |
cd8f5663 | 568 | __do_free char *path = NULL; |
d4db3d14 CB |
569 | int ret; |
570 | bool bret; | |
2eef2bda | 571 | |
6f4f1937 CB |
572 | if (!(options->namespaces & CLONE_NEWNS) || |
573 | !(options->attach_flags & LXC_ATTACH_LSM)) { | |
c3e3c21a CB |
574 | free(c->lxc_conf->seccomp.seccomp); |
575 | c->lxc_conf->seccomp.seccomp = NULL; | |
2c4ea790 | 576 | return true; |
bd4307f0 | 577 | } |
bd7b4e28 | 578 | |
2e812c16 | 579 | /* Remove current setting. */ |
d4db3d14 | 580 | if (!c->set_config_item(c, "lxc.seccomp.profile", "") && |
ea918412 | 581 | !c->set_config_item(c, "lxc.seccomp", "")) |
2c4ea790 | 582 | return false; |
bd7b4e28 | 583 | |
8ce83369 | 584 | /* Fetch the current profile path over the cmd interface. */ |
0b427da0 | 585 | path = c->get_running_config_item(c, "lxc.seccomp.profile"); |
bd7b4e28 | 586 | if (!path) { |
d4db3d14 | 587 | INFO("Failed to retrieve lxc.seccomp.profile"); |
ea918412 | 588 | |
0b427da0 | 589 | path = c->get_running_config_item(c, "lxc.seccomp"); |
d4db3d14 CB |
590 | if (!path) { |
591 | INFO("Failed to retrieve lxc.seccomp"); | |
592 | return true; | |
593 | } | |
bd7b4e28 SG |
594 | } |
595 | ||
8ce83369 | 596 | /* Copy the value into the new lxc_conf. */ |
d4db3d14 | 597 | bret = c->set_config_item(c, "lxc.seccomp.profile", path); |
d4db3d14 CB |
598 | if (!bret) |
599 | return false; | |
bd7b4e28 | 600 | |
8ce83369 | 601 | /* Attempt to parse the resulting config. */ |
d4db3d14 CB |
602 | ret = lxc_read_seccomp_config(c->lxc_conf); |
603 | if (ret < 0) { | |
604 | ERROR("Failed to retrieve seccomp policy"); | |
2c4ea790 SH |
605 | return false; |
606 | } | |
607 | ||
d4db3d14 | 608 | INFO("Retrieved seccomp policy"); |
2e812c16 CB |
609 | return true; |
610 | } | |
611 | ||
6f4f1937 | 612 | static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options) |
2e812c16 | 613 | { |
cd8f5663 | 614 | __do_free char *val = NULL; |
2e812c16 | 615 | |
2e812c16 | 616 | /* Remove current setting. */ |
bcbef733 CB |
617 | if (!c->set_config_item(c, "lxc.no_new_privs", "")) { |
618 | INFO("Failed to unset lxc.no_new_privs"); | |
2e812c16 | 619 | return false; |
bcbef733 | 620 | } |
2e812c16 CB |
621 | |
622 | /* Retrieve currently active setting. */ | |
623 | val = c->get_running_config_item(c, "lxc.no_new_privs"); | |
624 | if (!val) { | |
bcbef733 | 625 | INFO("Failed to retrieve lxc.no_new_privs"); |
2e812c16 CB |
626 | return false; |
627 | } | |
628 | ||
629 | /* Set currently active setting. */ | |
cd8f5663 | 630 | return c->set_config_item(c, "lxc.no_new_privs", val); |
2c4ea790 SH |
631 | } |
632 | ||
9b8e3c96 SH |
633 | static signed long get_personality(const char *name, const char *lxcpath) |
634 | { | |
7c737378 | 635 | __do_free char *p = NULL; |
9b8e3c96 | 636 | |
6f4f1937 | 637 | p = lxc_cmd_get_config_item(name, "lxc.arch", lxcpath); |
9b8e3c96 SH |
638 | if (!p) |
639 | return -1; | |
6f4f1937 | 640 | |
cd8f5663 | 641 | return lxc_config_parse_arch(p); |
9b8e3c96 SH |
642 | } |
643 | ||
a998454a CB |
644 | struct attach_clone_payload { |
645 | int ipc_socket; | |
9e84479f | 646 | int terminal_slave_fd; |
a998454a CB |
647 | lxc_attach_options_t *options; |
648 | struct lxc_proc_context_info *init_ctx; | |
649 | lxc_attach_exec_t exec_function; | |
650 | void *exec_payload; | |
651 | }; | |
652 | ||
ba2be1a8 CB |
653 | static void lxc_put_attach_clone_payload(struct attach_clone_payload *p) |
654 | { | |
81102768 CB |
655 | close_prot_errno_disarm(p->ipc_socket); |
656 | close_prot_errno_disarm(p->terminal_slave_fd); | |
b21da190 | 657 | if (p->init_ctx) { |
ba2be1a8 | 658 | lxc_proc_put_context_info(p->init_ctx); |
b21da190 CB |
659 | p->init_ctx = NULL; |
660 | } | |
ba2be1a8 CB |
661 | } |
662 | ||
a998454a CB |
663 | static int attach_child_main(struct attach_clone_payload *payload) |
664 | { | |
427a8067 | 665 | int lsm_fd, ret; |
a998454a CB |
666 | uid_t new_uid; |
667 | gid_t new_gid; | |
936efc72 CB |
668 | uid_t ns_root_uid = 0; |
669 | gid_t ns_root_gid = 0; | |
a998454a CB |
670 | lxc_attach_options_t* options = payload->options; |
671 | struct lxc_proc_context_info* init_ctx = payload->init_ctx; | |
57de839f CB |
672 | bool needs_lsm = (options->namespaces & CLONE_NEWNS) && |
673 | (options->attach_flags & LXC_ATTACH_LSM) && | |
674 | init_ctx->lsm_label; | |
a998454a CB |
675 | |
676 | /* A description of the purpose of this functionality is provided in the | |
677 | * lxc-attach(1) manual page. We have to remount here and not in the | |
678 | * parent process, otherwise /proc may not properly reflect the new pid | |
679 | * namespace. | |
680 | */ | |
681 | if (!(options->namespaces & CLONE_NEWNS) && | |
682 | (options->attach_flags & LXC_ATTACH_REMOUNT_PROC_SYS)) { | |
683 | ret = lxc_attach_remount_sys_proc(); | |
b75c344c CB |
684 | if (ret < 0) |
685 | goto on_error; | |
ea918412 | 686 | |
b75c344c | 687 | TRACE("Remounted \"/proc\" and \"/sys\""); |
a998454a CB |
688 | } |
689 | ||
b75c344c | 690 | /* Now perform additional attachments. */ |
a998454a | 691 | #if HAVE_SYS_PERSONALITY_H |
a998454a | 692 | if (options->attach_flags & LXC_ATTACH_SET_PERSONALITY) { |
b75c344c CB |
693 | long new_personality; |
694 | ||
695 | if (options->personality < 0) | |
696 | new_personality = init_ctx->personality; | |
697 | else | |
698 | new_personality = options->personality; | |
ea918412 | 699 | |
a998454a | 700 | ret = personality(new_personality); |
b75c344c CB |
701 | if (ret < 0) |
702 | goto on_error; | |
ea918412 | 703 | |
b75c344c | 704 | TRACE("Set new personality"); |
a998454a CB |
705 | } |
706 | #endif | |
707 | ||
708 | if (options->attach_flags & LXC_ATTACH_DROP_CAPABILITIES) { | |
709 | ret = lxc_attach_drop_privs(init_ctx); | |
b75c344c CB |
710 | if (ret < 0) |
711 | goto on_error; | |
ea918412 | 712 | |
b75c344c | 713 | TRACE("Dropped capabilities"); |
a998454a CB |
714 | } |
715 | ||
716 | /* Always set the environment (specify (LXC_ATTACH_KEEP_ENV, NULL, NULL) | |
717 | * if you want this to be a no-op). | |
718 | */ | |
7385273f | 719 | ret = lxc_attach_set_environment(init_ctx, |
720 | options->env_policy, | |
a998454a CB |
721 | options->extra_env_vars, |
722 | options->extra_keep_env); | |
b75c344c CB |
723 | if (ret < 0) |
724 | goto on_error; | |
ea918412 | 725 | |
b75c344c | 726 | TRACE("Set up environment"); |
a998454a | 727 | |
57de839f CB |
728 | /* This remark only affects fully unprivileged containers: |
729 | * Receive fd for LSM security module before we set{g,u}id(). The reason | |
730 | * is that on set{g,u}id() the kernel will a) make us undumpable and b) | |
731 | * we will change our effective uid. This means our effective uid will | |
732 | * be different from the effective uid of the process that created us | |
733 | * which means that this processs no longer has capabilities in our | |
734 | * namespace including CAP_SYS_PTRACE. This means we will not be able to | |
735 | * read and /proc/<pid> files for the process anymore when /proc is | |
736 | * mounted with hidepid={1,2}. So let's get the lsm label fd before the | |
737 | * set{g,u}id(). | |
738 | */ | |
739 | if (needs_lsm) { | |
b75c344c | 740 | ret = lxc_abstract_unix_recv_fds(payload->ipc_socket, &lsm_fd, 1, NULL, 0); |
9044b79e | 741 | if (ret <= 0) { |
742 | if (ret < 0) | |
743 | SYSERROR("Failed to receive lsm label fd"); | |
744 | ||
b75c344c | 745 | goto on_error; |
9044b79e | 746 | } |
747 | ||
57de839f CB |
748 | TRACE("Received LSM label file descriptor %d from parent", lsm_fd); |
749 | } | |
750 | ||
08ea9270 | 751 | if (options->stdin_fd > 0 && isatty(options->stdin_fd)) { |
cd0a2b2f | 752 | ret = lxc_make_controlling_terminal(options->stdin_fd); |
08ea9270 CB |
753 | if (ret < 0) |
754 | goto on_error; | |
755 | } | |
756 | ||
b58214ac CB |
757 | if (!lxc_setgroups(0, NULL) && errno != EPERM) |
758 | goto on_error; | |
759 | ||
936efc72 CB |
760 | if (options->namespaces & CLONE_NEWUSER) { |
761 | /* Check whether nsuid 0 has a mapping. */ | |
762 | ns_root_uid = get_ns_uid(0); | |
ea918412 | 763 | |
936efc72 CB |
764 | /* Check whether nsgid 0 has a mapping. */ |
765 | ns_root_gid = get_ns_gid(0); | |
a998454a | 766 | |
936efc72 CB |
767 | /* If there's no mapping for nsuid 0 try to retrieve the nsuid |
768 | * init was started with. | |
769 | */ | |
770 | if (ns_root_uid == LXC_INVALID_UID) | |
771 | lxc_attach_get_init_uidgid(&ns_root_uid, &ns_root_gid); | |
ea918412 | 772 | |
936efc72 CB |
773 | if (ns_root_uid == LXC_INVALID_UID) |
774 | goto on_error; | |
a998454a | 775 | |
464c4611 | 776 | if (!lxc_switch_uid_gid(ns_root_uid, ns_root_gid)) |
b75c344c | 777 | goto on_error; |
a998454a CB |
778 | } |
779 | ||
936efc72 CB |
780 | /* Set {u,g}id. */ |
781 | if (options->uid != LXC_INVALID_UID) | |
782 | new_uid = options->uid; | |
783 | else | |
784 | new_uid = ns_root_uid; | |
785 | ||
786 | if (options->gid != LXC_INVALID_GID) | |
787 | new_gid = options->gid; | |
788 | else | |
789 | new_gid = ns_root_gid; | |
790 | ||
a998454a CB |
791 | if ((init_ctx->container && init_ctx->container->lxc_conf && |
792 | init_ctx->container->lxc_conf->no_new_privs) || | |
793 | (options->attach_flags & LXC_ATTACH_NO_NEW_PRIVS)) { | |
b81689a1 CB |
794 | ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0), |
795 | prctl_arg(0), prctl_arg(0)); | |
b75c344c CB |
796 | if (ret < 0) |
797 | goto on_error; | |
ea918412 | 798 | |
b75c344c | 799 | TRACE("Set PR_SET_NO_NEW_PRIVS"); |
a998454a CB |
800 | } |
801 | ||
57de839f | 802 | if (needs_lsm) { |
d3ba7c98 | 803 | bool on_exec; |
a998454a CB |
804 | |
805 | /* Change into our new LSM profile. */ | |
d3ba7c98 | 806 | on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false; |
ea918412 | 807 | |
d3ba7c98 | 808 | ret = lsm_process_label_set_at(lsm_fd, init_ctx->lsm_label, on_exec); |
57de839f | 809 | close(lsm_fd); |
b75c344c CB |
810 | if (ret < 0) |
811 | goto on_error; | |
ea918412 | 812 | |
d3ba7c98 | 813 | TRACE("Set %s LSM label to \"%s\"", lsm_name(), init_ctx->lsm_label); |
a998454a CB |
814 | } |
815 | ||
816 | if (init_ctx->container && init_ctx->container->lxc_conf && | |
c3e3c21a | 817 | init_ctx->container->lxc_conf->seccomp.seccomp) { |
cdb2a47f CB |
818 | struct lxc_conf *conf = init_ctx->container->lxc_conf; |
819 | ||
820 | ret = lxc_seccomp_load(conf); | |
b75c344c CB |
821 | if (ret < 0) |
822 | goto on_error; | |
ea918412 | 823 | |
b75c344c | 824 | TRACE("Loaded seccomp profile"); |
cdb2a47f | 825 | |
c3e3c21a CB |
826 | ret = lxc_seccomp_send_notifier_fd(&conf->seccomp, payload->ipc_socket); |
827 | if (ret < 0) | |
828 | goto on_error; | |
a998454a | 829 | } |
ea918412 | 830 | |
b75c344c | 831 | close(payload->ipc_socket); |
ba2be1a8 CB |
832 | payload->ipc_socket = -EBADF; |
833 | lxc_proc_put_context_info(init_ctx); | |
d35b372a | 834 | payload->init_ctx = NULL; |
a998454a CB |
835 | |
836 | /* The following is done after the communication socket is shut down. | |
837 | * That way, all errors that might (though unlikely) occur up until this | |
838 | * point will have their messages printed to the original stderr (if | |
839 | * logging is so configured) and not the fd the user supplied, if any. | |
840 | */ | |
841 | ||
842 | /* Fd handling for stdin, stdout and stderr; ignore errors here, user | |
843 | * may want to make sure the fds are closed, for example. | |
844 | */ | |
08ea9270 | 845 | if (options->stdin_fd >= 0 && options->stdin_fd != STDIN_FILENO) |
00c72a93 | 846 | (void)dup2(options->stdin_fd, STDIN_FILENO); |
08ea9270 CB |
847 | |
848 | if (options->stdout_fd >= 0 && options->stdout_fd != STDOUT_FILENO) | |
00c72a93 | 849 | (void)dup2(options->stdout_fd, STDOUT_FILENO); |
08ea9270 CB |
850 | |
851 | if (options->stderr_fd >= 0 && options->stderr_fd != STDERR_FILENO) | |
00c72a93 | 852 | (void)dup2(options->stderr_fd, STDERR_FILENO); |
a998454a CB |
853 | |
854 | /* close the old fds */ | |
08ea9270 | 855 | if (options->stdin_fd > STDERR_FILENO) |
a998454a | 856 | close(options->stdin_fd); |
08ea9270 CB |
857 | |
858 | if (options->stdout_fd > STDERR_FILENO) | |
a998454a | 859 | close(options->stdout_fd); |
08ea9270 CB |
860 | |
861 | if (options->stderr_fd > STDERR_FILENO) | |
a998454a CB |
862 | close(options->stderr_fd); |
863 | ||
427a8067 CB |
864 | /* |
865 | * Try to remove FD_CLOEXEC flag from stdin/stdout/stderr, but also | |
a998454a CB |
866 | * here, ignore errors. |
867 | */ | |
427a8067 | 868 | for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) { |
3f62938a | 869 | ret = fd_cloexec(fd, false); |
b75c344c CB |
870 | if (ret < 0) { |
871 | SYSERROR("Failed to clear FD_CLOEXEC from file descriptor %d", fd); | |
872 | goto on_error; | |
873 | } | |
a998454a CB |
874 | } |
875 | ||
9e84479f CB |
876 | if (options->attach_flags & LXC_ATTACH_TERMINAL) { |
877 | ret = lxc_terminal_prepare_login(payload->terminal_slave_fd); | |
ba2be1a8 | 878 | if (ret < 0) { |
9e84479f | 879 | SYSERROR("Failed to prepare terminal file descriptor %d", payload->terminal_slave_fd); |
ba2be1a8 CB |
880 | goto on_error; |
881 | } | |
ea918412 | 882 | |
9e84479f | 883 | TRACE("Prepared terminal file descriptor %d", payload->terminal_slave_fd); |
ba2be1a8 CB |
884 | } |
885 | ||
936efc72 CB |
886 | /* Avoid unnecessary syscalls. */ |
887 | if (new_uid == ns_root_uid) | |
888 | new_uid = LXC_INVALID_UID; | |
889 | ||
890 | if (new_gid == ns_root_gid) | |
891 | new_gid = LXC_INVALID_GID; | |
892 | ||
464c4611 | 893 | if (!lxc_switch_uid_gid(new_uid, new_gid)) |
936efc72 CB |
894 | goto on_error; |
895 | ||
a998454a | 896 | /* We're done, so we can now do whatever the user intended us to do. */ |
c7ac2e1c | 897 | _exit(payload->exec_function(payload->exec_payload)); |
b75c344c CB |
898 | |
899 | on_error: | |
ba2be1a8 | 900 | lxc_put_attach_clone_payload(payload); |
c7ac2e1c | 901 | _exit(EXIT_FAILURE); |
a998454a CB |
902 | } |
903 | ||
9e84479f CB |
904 | static int lxc_attach_terminal(struct lxc_conf *conf, |
905 | struct lxc_terminal *terminal) | |
ba2be1a8 CB |
906 | { |
907 | int ret; | |
908 | ||
9e84479f | 909 | lxc_terminal_init(terminal); |
ba2be1a8 | 910 | |
9e84479f | 911 | ret = lxc_terminal_create(terminal); |
ba2be1a8 | 912 | if (ret < 0) { |
ea918412 | 913 | ERROR("Failed to create terminal"); |
ba2be1a8 CB |
914 | return -1; |
915 | } | |
916 | ||
917 | /* Shift ttys to container. */ | |
9e84479f | 918 | ret = lxc_terminal_map_ids(conf, terminal); |
ba2be1a8 | 919 | if (ret < 0) { |
9e84479f | 920 | ERROR("Failed to chown terminal"); |
ba2be1a8 CB |
921 | goto on_error; |
922 | } | |
923 | ||
924 | return 0; | |
925 | ||
926 | on_error: | |
9e84479f CB |
927 | lxc_terminal_delete(terminal); |
928 | lxc_terminal_conf_free(terminal); | |
ba2be1a8 CB |
929 | return -1; |
930 | } | |
931 | ||
9e84479f CB |
932 | static int lxc_attach_terminal_mainloop_init(struct lxc_terminal *terminal, |
933 | struct lxc_epoll_descr *descr) | |
ba2be1a8 CB |
934 | { |
935 | int ret; | |
936 | ||
937 | ret = lxc_mainloop_open(descr); | |
938 | if (ret < 0) { | |
939 | ERROR("Failed to create mainloop"); | |
940 | return -1; | |
941 | } | |
942 | ||
9e84479f | 943 | ret = lxc_terminal_mainloop_add(descr, terminal); |
ba2be1a8 CB |
944 | if (ret < 0) { |
945 | ERROR("Failed to add handlers to mainloop"); | |
946 | lxc_mainloop_close(descr); | |
947 | return -1; | |
948 | } | |
949 | ||
950 | return 0; | |
951 | } | |
952 | ||
9e84479f | 953 | static inline void lxc_attach_terminal_close_master(struct lxc_terminal *terminal) |
ba2be1a8 | 954 | { |
19a3e906 | 955 | close_prot_errno_disarm(terminal->master); |
ba2be1a8 CB |
956 | } |
957 | ||
9e84479f | 958 | static inline void lxc_attach_terminal_close_slave(struct lxc_terminal *terminal) |
ba2be1a8 | 959 | { |
19a3e906 | 960 | close_prot_errno_disarm(terminal->slave); |
ba2be1a8 CB |
961 | } |
962 | ||
9e84479f | 963 | static inline void lxc_attach_terminal_close_peer(struct lxc_terminal *terminal) |
ba2be1a8 | 964 | { |
19a3e906 | 965 | close_prot_errno_disarm(terminal->peer); |
ba2be1a8 CB |
966 | } |
967 | ||
9e84479f | 968 | static inline void lxc_attach_terminal_close_log(struct lxc_terminal *terminal) |
ba2be1a8 | 969 | { |
19a3e906 | 970 | close_prot_errno_disarm(terminal->log_fd); |
ba2be1a8 CB |
971 | } |
972 | ||
908fbc1a CB |
973 | int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function, |
974 | void *exec_payload, lxc_attach_options_t *options, | |
975 | pid_t *attached_process) | |
9c4693b8 | 976 | { |
877f3a04 | 977 | int i, ret, status; |
9c4693b8 | 978 | int ipc_sockets[2]; |
6f4f1937 | 979 | char *cwd, *new_cwd; |
9b8e3c96 | 980 | signed long personality; |
ba2be1a8 | 981 | pid_t attached_pid, init_pid, pid; |
6f4f1937 | 982 | struct lxc_proc_context_info *init_ctx; |
9e84479f | 983 | struct lxc_terminal terminal; |
1cce35e6 | 984 | struct lxc_conf *conf; |
908fbc1a | 985 | char *name, *lxcpath; |
a998454a | 986 | struct attach_clone_payload payload = {0}; |
9c4693b8 | 987 | |
877f3a04 CB |
988 | ret = access("/proc/self/ns", X_OK); |
989 | if (ret) { | |
ea918412 | 990 | SYSERROR("Does this kernel version support namespaces?"); |
877f3a04 CB |
991 | return -1; |
992 | } | |
993 | ||
908fbc1a | 994 | if (!container) |
540a2f70 | 995 | return ret_set_errno(-1, EINVAL); |
908fbc1a CB |
996 | |
997 | if (!lxc_container_get(container)) | |
540a2f70 | 998 | return ret_set_errno(-1, EINVAL); |
908fbc1a CB |
999 | |
1000 | name = container->name; | |
1001 | lxcpath = container->config_path; | |
1002 | ||
9c4693b8 CS |
1003 | if (!options) |
1004 | options = &attach_static_default_options; | |
1005 | ||
1006 | init_pid = lxc_cmd_get_init_pid(name, lxcpath); | |
1007 | if (init_pid < 0) { | |
ae026f55 | 1008 | ERROR("Failed to get init pid"); |
908fbc1a | 1009 | lxc_container_put(container); |
9c4693b8 CS |
1010 | return -1; |
1011 | } | |
1012 | ||
1013 | init_ctx = lxc_proc_get_context_info(init_pid); | |
1014 | if (!init_ctx) { | |
6f4f1937 | 1015 | ERROR("Failed to get context of init process: %ld", (long)init_pid); |
908fbc1a | 1016 | lxc_container_put(container); |
9c4693b8 CS |
1017 | return -1; |
1018 | } | |
1019 | ||
908fbc1a CB |
1020 | init_ctx->container = container; |
1021 | ||
9b8e3c96 SH |
1022 | personality = get_personality(name, lxcpath); |
1023 | if (init_ctx->personality < 0) { | |
6f4f1937 | 1024 | ERROR("Failed to get personality of the container"); |
9b8e3c96 SH |
1025 | lxc_proc_put_context_info(init_ctx); |
1026 | return -1; | |
1027 | } | |
1028 | init_ctx->personality = personality; | |
1029 | ||
ba773996 CB |
1030 | if (!init_ctx->container->lxc_conf) { |
1031 | init_ctx->container->lxc_conf = lxc_conf_init(); | |
62de1db6 CB |
1032 | if (!init_ctx->container->lxc_conf) { |
1033 | lxc_proc_put_context_info(init_ctx); | |
ea918412 | 1034 | return -1; |
62de1db6 | 1035 | } |
ba773996 | 1036 | } |
1cce35e6 | 1037 | conf = init_ctx->container->lxc_conf; |
ba773996 | 1038 | |
bd4307f0 | 1039 | if (!fetch_seccomp(init_ctx->container, options)) |
ae026f55 | 1040 | WARN("Failed to get seccomp policy"); |
2c4ea790 | 1041 | |
bd4307f0 | 1042 | if (!no_new_privs(init_ctx->container, options)) |
ae026f55 | 1043 | WARN("Could not determine whether PR_SET_NO_NEW_PRIVS is set"); |
2e812c16 | 1044 | |
9c4693b8 CS |
1045 | cwd = getcwd(NULL, 0); |
1046 | ||
8ce83369 CB |
1047 | /* Determine which namespaces the container was created with |
1048 | * by asking lxc-start, if necessary. | |
9c4693b8 CS |
1049 | */ |
1050 | if (options->namespaces == -1) { | |
1051 | options->namespaces = lxc_cmd_get_clone_flags(name, lxcpath); | |
1052 | /* call failed */ | |
1053 | if (options->namespaces == -1) { | |
8ce83369 | 1054 | ERROR("Failed to automatically determine the " |
877f3a04 | 1055 | "namespaces which the container uses"); |
9c4693b8 | 1056 | free(cwd); |
fe4de9a6 | 1057 | lxc_proc_put_context_info(init_ctx); |
9c4693b8 CS |
1058 | return -1; |
1059 | } | |
877f3a04 CB |
1060 | |
1061 | for (i = 0; i < LXC_NS_MAX; i++) { | |
1062 | if (ns_info[i].clone_flag & CLONE_NEWCGROUP) | |
1063 | if (!(options->attach_flags & LXC_ATTACH_MOVE_TO_CGROUP) || | |
1064 | !cgns_supported()) | |
1065 | continue; | |
1066 | ||
1067 | if (ns_info[i].clone_flag & options->namespaces) | |
1068 | continue; | |
1069 | ||
1070 | init_ctx->ns_inherited |= ns_info[i].clone_flag; | |
1071 | } | |
1072 | } | |
1073 | ||
0059379f | 1074 | pid = lxc_raw_getpid(); |
ea918412 | 1075 | |
877f3a04 | 1076 | for (i = 0; i < LXC_NS_MAX; i++) { |
ea918412 | 1077 | int j; |
877f3a04 CB |
1078 | |
1079 | if (options->namespaces & ns_info[i].clone_flag) | |
1080 | init_ctx->ns_fd[i] = lxc_preserve_ns(init_pid, ns_info[i].proc_name); | |
1081 | else if (init_ctx->ns_inherited & ns_info[i].clone_flag) | |
1082 | init_ctx->ns_fd[i] = in_same_namespace(pid, init_pid, ns_info[i].proc_name); | |
1083 | else | |
1084 | continue; | |
ea918412 | 1085 | |
877f3a04 CB |
1086 | if (init_ctx->ns_fd[i] >= 0) |
1087 | continue; | |
1088 | ||
1089 | if (init_ctx->ns_fd[i] == -EINVAL) { | |
1090 | DEBUG("Inheriting %s namespace from %d", | |
1091 | ns_info[i].proc_name, pid); | |
1092 | init_ctx->ns_inherited &= ~ns_info[i].clone_flag; | |
1093 | continue; | |
1094 | } | |
1095 | ||
1096 | /* We failed to preserve the namespace. */ | |
ea918412 | 1097 | SYSERROR("Failed to attach to %s namespace of %d", |
1098 | ns_info[i].proc_name, pid); | |
1099 | ||
877f3a04 CB |
1100 | /* Close all already opened file descriptors before we return an |
1101 | * error, so we don't leak them. | |
1102 | */ | |
1103 | for (j = 0; j < i; j++) | |
1104 | close(init_ctx->ns_fd[j]); | |
1105 | ||
877f3a04 CB |
1106 | free(cwd); |
1107 | lxc_proc_put_context_info(init_ctx); | |
1108 | return -1; | |
9c4693b8 CS |
1109 | } |
1110 | ||
9e84479f CB |
1111 | if (options->attach_flags & LXC_ATTACH_TERMINAL) { |
1112 | ret = lxc_attach_terminal(conf, &terminal); | |
ba2be1a8 | 1113 | if (ret < 0) { |
9e84479f | 1114 | ERROR("Failed to setup new terminal"); |
ba2be1a8 CB |
1115 | free(cwd); |
1116 | lxc_proc_put_context_info(init_ctx); | |
1117 | return -1; | |
1118 | } | |
1119 | ||
9e84479f | 1120 | terminal.log_fd = options->log_fd; |
c948657b | 1121 | } else { |
9e84479f | 1122 | lxc_terminal_init(&terminal); |
ba2be1a8 CB |
1123 | } |
1124 | ||
8ce83369 CB |
1125 | /* Create a socket pair for IPC communication; set SOCK_CLOEXEC in order |
1126 | * to make sure we don't irritate other threads that want to fork+exec | |
1127 | * away | |
9c4693b8 CS |
1128 | * |
1129 | * IMPORTANT: if the initial process is multithreaded and another call | |
1130 | * just fork()s away without exec'ing directly after, the socket fd will | |
1131 | * exist in the forked process from the other thread and any close() in | |
8ce83369 | 1132 | * our own child process will not really cause the socket to close |
1d801260 | 1133 | * properly, potentially causing the parent to hang. |
9c4693b8 CS |
1134 | * |
1135 | * For this reason, while IPC is still active, we have to use shutdown() | |
8ce83369 CB |
1136 | * if the child exits prematurely in order to signal that the socket is |
1137 | * closed and cannot assume that the child exiting will automatically do | |
1138 | * that. | |
9c4693b8 CS |
1139 | * |
1140 | * IPC mechanism: (X is receiver) | |
1141 | * initial process intermediate attached | |
1142 | * X <--- send pid of | |
1143 | * attached proc, | |
1144 | * then exit | |
1145 | * send 0 ------------------------------------> X | |
1146 | * [do initialization] | |
1147 | * X <------------------------------------ send 1 | |
1148 | * [add to cgroup, ...] | |
1149 | * send 2 ------------------------------------> X | |
81f466d0 CB |
1150 | * [set LXC_ATTACH_NO_NEW_PRIVS] |
1151 | * X <------------------------------------ send 3 | |
1152 | * [open LSM label fd] | |
1153 | * send 4 ------------------------------------> X | |
1154 | * [set LSM label] | |
9c4693b8 CS |
1155 | * close socket close socket |
1156 | * run program | |
1157 | */ | |
1158 | ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets); | |
1159 | if (ret < 0) { | |
ae026f55 | 1160 | SYSERROR("Could not set up required IPC mechanism for attaching"); |
9c4693b8 | 1161 | free(cwd); |
fe4de9a6 | 1162 | lxc_proc_put_context_info(init_ctx); |
9c4693b8 CS |
1163 | return -1; |
1164 | } | |
1165 | ||
e3f0e436 CB |
1166 | /* Create intermediate subprocess, two reasons: |
1167 | * 1. We can't setns() in the child itself, since we want to make | |
8ce83369 | 1168 | * sure we are properly attached to the pidns. |
e3f0e436 | 1169 | * 2. Also, the initial thread has to put the attached process |
8ce83369 CB |
1170 | * into the cgroup, which we can only do if we didn't already |
1171 | * setns() (otherwise, user namespaces will hate us). | |
9c4693b8 CS |
1172 | */ |
1173 | pid = fork(); | |
9c4693b8 | 1174 | if (pid < 0) { |
ae026f55 | 1175 | SYSERROR("Failed to create first subprocess"); |
9c4693b8 | 1176 | free(cwd); |
fe4de9a6 | 1177 | lxc_proc_put_context_info(init_ctx); |
9c4693b8 CS |
1178 | return -1; |
1179 | } | |
1180 | ||
1181 | if (pid) { | |
ba2be1a8 | 1182 | int ret_parent = -1; |
9c4693b8 | 1183 | pid_t to_cleanup_pid = pid; |
ba2be1a8 | 1184 | struct lxc_epoll_descr descr = {0}; |
9c4693b8 | 1185 | |
ba2be1a8 | 1186 | /* close unneeded file descriptors */ |
9c4693b8 CS |
1187 | close(ipc_sockets[1]); |
1188 | free(cwd); | |
ba2be1a8 | 1189 | lxc_proc_close_ns_fd(init_ctx); |
9e84479f CB |
1190 | if (options->attach_flags & LXC_ATTACH_TERMINAL) |
1191 | lxc_attach_terminal_close_slave(&terminal); | |
9c4693b8 | 1192 | |
8ce83369 | 1193 | /* Attach to cgroup, if requested. */ |
f4364484 | 1194 | if (options->attach_flags & LXC_ATTACH_MOVE_TO_CGROUP) { |
900b6606 CB |
1195 | /* |
1196 | * If this is the unified hierarchy cgroup_attach() is | |
1197 | * enough. | |
1198 | */ | |
1199 | ret = cgroup_attach(name, lxcpath, pid); | |
1200 | if (ret) { | |
1201 | __do_cgroup_exit struct cgroup_ops *cgroup_ops = NULL; | |
2202afc9 | 1202 | |
900b6606 CB |
1203 | cgroup_ops = cgroup_init(conf); |
1204 | if (!cgroup_ops) | |
1205 | goto on_error; | |
2202afc9 | 1206 | |
900b6606 CB |
1207 | if (!cgroup_ops->attach(cgroup_ops, name, lxcpath, pid)) |
1208 | goto on_error; | |
1209 | } | |
2202afc9 | 1210 | TRACE("Moved intermediate process %d into container's cgroups", pid); |
f4364484 SG |
1211 | } |
1212 | ||
bb2ada6f | 1213 | /* Setup /proc limits */ |
1cce35e6 CB |
1214 | if (!lxc_list_empty(&conf->procs)) { |
1215 | ret = setup_proc_filesystem(&conf->procs, pid); | |
bb2ada6f CB |
1216 | if (ret < 0) |
1217 | goto on_error; | |
1218 | } | |
1219 | ||
c6d09e15 | 1220 | /* Setup resource limits */ |
1cce35e6 CB |
1221 | if (!lxc_list_empty(&conf->limits)) { |
1222 | ret = setup_resource_limits(&conf->limits, pid); | |
ba2be1a8 CB |
1223 | if (ret < 0) |
1224 | goto on_error; | |
1225 | } | |
1226 | ||
9e84479f CB |
1227 | if (options->attach_flags & LXC_ATTACH_TERMINAL) { |
1228 | ret = lxc_attach_terminal_mainloop_init(&terminal, &descr); | |
ba2be1a8 | 1229 | if (ret < 0) |
6f4f1937 | 1230 | goto on_error; |
ea918412 | 1231 | |
9e84479f | 1232 | TRACE("Initialized terminal mainloop"); |
ba2be1a8 | 1233 | } |
c6d09e15 | 1234 | |
8ce83369 | 1235 | /* Let the child process know to go ahead. */ |
f4364484 SG |
1236 | status = 0; |
1237 | ret = lxc_write_nointr(ipc_sockets[0], &status, sizeof(status)); | |
94ac256f | 1238 | if (ret != sizeof(status)) |
ba2be1a8 | 1239 | goto close_mainloop; |
ea918412 | 1240 | |
94ac256f | 1241 | TRACE("Told intermediate process to start initializing"); |
f4364484 | 1242 | |
8ce83369 | 1243 | /* Get pid of attached process from intermediate process. */ |
94ac256f CB |
1244 | ret = lxc_read_nointr(ipc_sockets[0], &attached_pid, sizeof(attached_pid)); |
1245 | if (ret != sizeof(attached_pid)) | |
ba2be1a8 | 1246 | goto close_mainloop; |
ea918412 | 1247 | |
94ac256f | 1248 | TRACE("Received pid %d of attached process in parent pid namespace", attached_pid); |
9c4693b8 | 1249 | |
8ce83369 | 1250 | /* Ignore SIGKILL (CTRL-C) and SIGQUIT (CTRL-\) - issue #313. */ |
62183f1a SH |
1251 | if (options->stdin_fd == 0) { |
1252 | signal(SIGINT, SIG_IGN); | |
1253 | signal(SIGQUIT, SIG_IGN); | |
1254 | } | |
2eef2bda | 1255 | |
8ce83369 | 1256 | /* Reap intermediate process. */ |
9c4693b8 CS |
1257 | ret = wait_for_pid(pid); |
1258 | if (ret < 0) | |
ba2be1a8 | 1259 | goto close_mainloop; |
ea918412 | 1260 | |
94ac256f | 1261 | TRACE("Intermediate process %d exited", pid); |
9c4693b8 | 1262 | |
8ce83369 | 1263 | /* We will always have to reap the attached process now. */ |
9c4693b8 CS |
1264 | to_cleanup_pid = attached_pid; |
1265 | ||
81f466d0 | 1266 | /* Open LSM fd and send it to child. */ |
6f4f1937 CB |
1267 | if ((options->namespaces & CLONE_NEWNS) && |
1268 | (options->attach_flags & LXC_ATTACH_LSM) && | |
1269 | init_ctx->lsm_label) { | |
47ce2cb7 CB |
1270 | int labelfd; |
1271 | bool on_exec; | |
6f4f1937 | 1272 | |
a7547c5c | 1273 | ret = -1; |
47ce2cb7 CB |
1274 | on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false; |
1275 | labelfd = lsm_process_label_fd_get(attached_pid, on_exec); | |
81f466d0 | 1276 | if (labelfd < 0) |
ba2be1a8 | 1277 | goto close_mainloop; |
ea918412 | 1278 | |
94ac256f | 1279 | TRACE("Opened LSM label file descriptor %d", labelfd); |
81f466d0 CB |
1280 | |
1281 | /* Send child fd of the LSM security module to write to. */ | |
ae467c54 | 1282 | ret = lxc_abstract_unix_send_fds(ipc_sockets[0], &labelfd, 1, NULL, 0); |
81f466d0 | 1283 | if (ret <= 0) { |
9044b79e | 1284 | if (ret < 0) |
1285 | SYSERROR("Failed to send lsm label fd"); | |
1286 | ||
1287 | close(labelfd); | |
ba2be1a8 | 1288 | goto close_mainloop; |
81f466d0 | 1289 | } |
9044b79e | 1290 | |
1291 | close(labelfd); | |
94ac256f | 1292 | TRACE("Sent LSM label file descriptor %d to child", labelfd); |
81f466d0 CB |
1293 | } |
1294 | ||
2ac0f627 CB |
1295 | if (conf && conf->seccomp.seccomp) { |
1296 | ret = lxc_seccomp_recv_notifier_fd(&conf->seccomp, ipc_sockets[0]); | |
1297 | if (ret < 0) | |
1298 | goto close_mainloop; | |
cdb2a47f | 1299 | |
2ac0f627 CB |
1300 | ret = lxc_seccomp_add_notifier(name, lxcpath, &conf->seccomp); |
1301 | if (ret < 0) | |
1302 | goto close_mainloop; | |
1303 | } | |
cdb2a47f | 1304 | |
8ce83369 CB |
1305 | /* We're done, the child process should now execute whatever it |
1306 | * is that the user requested. The parent can now track it with | |
1307 | * waitpid() or similar. | |
9c4693b8 CS |
1308 | */ |
1309 | ||
1310 | *attached_process = attached_pid; | |
9c4693b8 | 1311 | |
ba2be1a8 | 1312 | /* Now shut down communication with child, we're done. */ |
9c4693b8 CS |
1313 | shutdown(ipc_sockets[0], SHUT_RDWR); |
1314 | close(ipc_sockets[0]); | |
ba2be1a8 CB |
1315 | ipc_sockets[0] = -1; |
1316 | ||
1317 | ret_parent = 0; | |
1318 | to_cleanup_pid = -1; | |
ea918412 | 1319 | |
9e84479f | 1320 | if (options->attach_flags & LXC_ATTACH_TERMINAL) { |
ba2be1a8 CB |
1321 | ret = lxc_mainloop(&descr, -1); |
1322 | if (ret < 0) { | |
1323 | ret_parent = -1; | |
1324 | to_cleanup_pid = attached_pid; | |
1325 | } | |
1326 | } | |
1327 | ||
1328 | close_mainloop: | |
9e84479f | 1329 | if (options->attach_flags & LXC_ATTACH_TERMINAL) |
ba2be1a8 CB |
1330 | lxc_mainloop_close(&descr); |
1331 | ||
1332 | on_error: | |
1333 | if (ipc_sockets[0] >= 0) { | |
1334 | shutdown(ipc_sockets[0], SHUT_RDWR); | |
1335 | close(ipc_sockets[0]); | |
1336 | } | |
1337 | ||
1338 | if (to_cleanup_pid > 0) | |
6f4f1937 | 1339 | (void)wait_for_pid(to_cleanup_pid); |
ba2be1a8 | 1340 | |
9e84479f CB |
1341 | if (options->attach_flags & LXC_ATTACH_TERMINAL) { |
1342 | lxc_terminal_delete(&terminal); | |
1343 | lxc_terminal_conf_free(&terminal); | |
ba2be1a8 | 1344 | } |
ea918412 | 1345 | |
fe4de9a6 | 1346 | lxc_proc_put_context_info(init_ctx); |
ba2be1a8 | 1347 | return ret_parent; |
9c4693b8 CS |
1348 | } |
1349 | ||
ba2be1a8 | 1350 | /* close unneeded file descriptors */ |
9c4693b8 | 1351 | close(ipc_sockets[0]); |
ba2be1a8 | 1352 | ipc_sockets[0] = -EBADF; |
ea918412 | 1353 | |
9e84479f CB |
1354 | if (options->attach_flags & LXC_ATTACH_TERMINAL) { |
1355 | lxc_attach_terminal_close_master(&terminal); | |
1356 | lxc_attach_terminal_close_peer(&terminal); | |
1357 | lxc_attach_terminal_close_log(&terminal); | |
ba2be1a8 | 1358 | } |
9c4693b8 | 1359 | |
8ce83369 | 1360 | /* Wait for the parent to have setup cgroups. */ |
94ac256f | 1361 | ret = lxc_read_nointr(ipc_sockets[1], &status, sizeof(status)); |
ba2be1a8 | 1362 | if (ret != sizeof(status)) { |
f4364484 | 1363 | shutdown(ipc_sockets[1], SHUT_RDWR); |
62de1db6 | 1364 | lxc_proc_put_context_info(init_ctx); |
c7ac2e1c | 1365 | _exit(EXIT_FAILURE); |
f4364484 | 1366 | } |
ea918412 | 1367 | |
94ac256f | 1368 | TRACE("Intermediate process starting to initialize"); |
f4364484 | 1369 | |
8ce83369 CB |
1370 | /* Attach now, create another subprocess later, since pid namespaces |
1371 | * only really affect the children of the current process. | |
9c4693b8 | 1372 | */ |
877f3a04 | 1373 | ret = lxc_attach_to_ns(init_pid, init_ctx); |
9c4693b8 | 1374 | if (ret < 0) { |
94ac256f | 1375 | ERROR("Failed to enter namespaces"); |
9c4693b8 | 1376 | shutdown(ipc_sockets[1], SHUT_RDWR); |
62de1db6 | 1377 | lxc_proc_put_context_info(init_ctx); |
c7ac2e1c | 1378 | _exit(EXIT_FAILURE); |
9c4693b8 | 1379 | } |
ea918412 | 1380 | |
877f3a04 CB |
1381 | /* close namespace file descriptors */ |
1382 | lxc_proc_close_ns_fd(init_ctx); | |
9c4693b8 | 1383 | |
8ce83369 | 1384 | /* Attach succeeded, try to cwd. */ |
9c4693b8 CS |
1385 | if (options->initial_cwd) |
1386 | new_cwd = options->initial_cwd; | |
1387 | else | |
1388 | new_cwd = cwd; | |
d6d979bc CB |
1389 | if (new_cwd) { |
1390 | ret = chdir(new_cwd); | |
1391 | if (ret < 0) | |
1392 | WARN("Could not change directory to \"%s\"", new_cwd); | |
1393 | } | |
9c4693b8 CS |
1394 | free(cwd); |
1395 | ||
a998454a CB |
1396 | /* Create attached process. */ |
1397 | payload.ipc_socket = ipc_sockets[1]; | |
1398 | payload.options = options; | |
1399 | payload.init_ctx = init_ctx; | |
9e84479f | 1400 | payload.terminal_slave_fd = terminal.slave; |
a998454a CB |
1401 | payload.exec_function = exec_function; |
1402 | payload.exec_payload = exec_payload; | |
9c4693b8 | 1403 | |
a59440be | 1404 | pid = lxc_raw_clone(CLONE_PARENT, NULL); |
a998454a | 1405 | if (pid < 0) { |
94ac256f | 1406 | SYSERROR("Failed to clone attached process"); |
9c4693b8 | 1407 | shutdown(ipc_sockets[1], SHUT_RDWR); |
62de1db6 | 1408 | lxc_proc_put_context_info(init_ctx); |
c7ac2e1c | 1409 | _exit(EXIT_FAILURE); |
9c4693b8 | 1410 | } |
a998454a CB |
1411 | |
1412 | if (pid == 0) { | |
f157b056 CB |
1413 | if (options->attach_flags & LXC_ATTACH_TERMINAL) { |
1414 | ret = pthread_sigmask(SIG_SETMASK, | |
1415 | &terminal.tty_state->oldmask, NULL); | |
1416 | if (ret < 0) { | |
1417 | SYSERROR("Failed to reset signal mask"); | |
1418 | _exit(EXIT_FAILURE); | |
1419 | } | |
1420 | } | |
1421 | ||
a998454a CB |
1422 | ret = attach_child_main(&payload); |
1423 | if (ret < 0) | |
1424 | ERROR("Failed to exec"); | |
ea918412 | 1425 | |
a998454a CB |
1426 | _exit(EXIT_FAILURE); |
1427 | } | |
ea918412 | 1428 | |
9e84479f CB |
1429 | if (options->attach_flags & LXC_ATTACH_TERMINAL) |
1430 | lxc_attach_terminal_close_slave(&terminal); | |
9c4693b8 | 1431 | |
8ce83369 | 1432 | /* Tell grandparent the pid of the pid of the newly created child. */ |
9c4693b8 CS |
1433 | ret = lxc_write_nointr(ipc_sockets[1], &pid, sizeof(pid)); |
1434 | if (ret != sizeof(pid)) { | |
8ce83369 CB |
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. | |
9c4693b8 | 1440 | */ |
9c4693b8 | 1441 | shutdown(ipc_sockets[1], SHUT_RDWR); |
62de1db6 | 1442 | lxc_proc_put_context_info(init_ctx); |
c7ac2e1c | 1443 | _exit(EXIT_FAILURE); |
9c4693b8 | 1444 | } |
ea918412 | 1445 | |
94ac256f | 1446 | TRACE("Sending pid %d of attached process", pid); |
9c4693b8 | 1447 | |
8ce83369 | 1448 | /* The rest is in the hands of the initial and the attached process. */ |
62de1db6 | 1449 | lxc_proc_put_context_info(init_ctx); |
57017714 | 1450 | _exit(EXIT_SUCCESS); |
9c4693b8 CS |
1451 | } |
1452 | ||
06346bb0 | 1453 | int lxc_attach_run_command(void *payload) |
9c4693b8 | 1454 | { |
06346bb0 CB |
1455 | int ret = -1; |
1456 | lxc_attach_command_t *cmd = payload; | |
9c4693b8 | 1457 | |
06346bb0 CB |
1458 | ret = execvp(cmd->program, cmd->argv); |
1459 | if (ret < 0) { | |
1460 | switch (errno) { | |
1461 | case ENOEXEC: | |
1462 | ret = 126; | |
cf0fd972 | 1463 | break; |
06346bb0 CB |
1464 | case ENOENT: |
1465 | ret = 127; | |
cf0fd972 | 1466 | break; |
06346bb0 CB |
1467 | } |
1468 | } | |
ea918412 | 1469 | |
1470 | SYSERROR("Failed to exec \"%s\"", cmd->program); | |
06346bb0 | 1471 | return ret; |
9c4693b8 CS |
1472 | } |
1473 | ||
1474 | int lxc_attach_run_shell(void* payload) | |
1475 | { | |
cd8f5663 | 1476 | __do_free char *buf = NULL; |
9c4693b8 | 1477 | uid_t uid; |
cb7aa5e8 DJ |
1478 | struct passwd pwent; |
1479 | struct passwd *pwentp = NULL; | |
9c4693b8 | 1480 | char *user_shell; |
cb7aa5e8 DJ |
1481 | size_t bufsize; |
1482 | int ret; | |
9c4693b8 | 1483 | |
8ce83369 | 1484 | /* Ignore payload parameter. */ |
9c4693b8 CS |
1485 | (void)payload; |
1486 | ||
1487 | uid = getuid(); | |
cb7aa5e8 DJ |
1488 | |
1489 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); | |
1490 | if (bufsize == -1) | |
1491 | bufsize = 1024; | |
1492 | ||
1493 | buf = malloc(bufsize); | |
1494 | if (buf) { | |
1495 | ret = getpwuid_r(uid, &pwent, buf, bufsize, &pwentp); | |
1496 | if (!pwentp) { | |
1497 | if (ret == 0) | |
ea918412 | 1498 | WARN("Could not find matched password record"); |
cb7aa5e8 DJ |
1499 | |
1500 | WARN("Failed to get password record - %u", uid); | |
1501 | } | |
1502 | } | |
9c4693b8 | 1503 | |
8ce83369 CB |
1504 | /* This probably happens because of incompatible nss implementations in |
1505 | * host and container (remember, this code is still using the host's | |
1506 | * glibc but our mount namespace is in the container) we may try to get | |
1507 | * the information by spawning a [getent passwd uid] process and parsing | |
1508 | * the result. | |
9c4693b8 | 1509 | */ |
cb7aa5e8 | 1510 | if (!pwentp) |
9c4693b8 CS |
1511 | user_shell = lxc_attach_getpwshell(uid); |
1512 | else | |
cb7aa5e8 | 1513 | user_shell = pwent.pw_shell; |
ea918412 | 1514 | |
9c4693b8 | 1515 | if (user_shell) |
acf47e1b | 1516 | execlp(user_shell, user_shell, (char *)NULL); |
9c4693b8 | 1517 | |
8ce83369 CB |
1518 | /* Executed if either no passwd entry or execvp fails, we will fall back |
1519 | * on /bin/sh as a default shell. | |
9c4693b8 | 1520 | */ |
acf47e1b | 1521 | execlp("/bin/sh", "/bin/sh", (char *)NULL); |
ea918412 | 1522 | |
edeb1836 | 1523 | SYSERROR("Failed to execute shell"); |
cb7aa5e8 | 1524 | if (!pwentp) |
edeb1836 | 1525 | free(user_shell); |
ea918412 | 1526 | |
9c4693b8 CS |
1527 | return -1; |
1528 | } |