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