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