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