]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/tools/lxc_attach.c
efced97b1f2b983a671f14d4d7a523afadb6ae4b
[mirror_lxc.git] / src / lxc / tools / lxc_attach.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/ioctl.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <termios.h>
16 #include <unistd.h>
17
18 #include <lxc/lxccontainer.h>
19
20 #include "arguments.h"
21 #include "attach.h"
22 #include "caps.h"
23 #include "config.h"
24 #include "confile.h"
25 #include "log.h"
26 #ifdef ENFORCE_MEMFD_REXEC
27 #include "rexec.h"
28 #endif
29 #include "utils.h"
30
31 lxc_log_define(lxc_attach, lxc);
32
33 /**
34 * This function will copy any binary that calls liblxc into a memory file and
35 * will use the memfd to rexecute the binary. This is done to prevent attacks
36 * through the /proc/self/exe symlink to corrupt the host binary when host and
37 * container are in the same user namespace or have set up an identity id
38 * mapping: CVE-2019-5736.
39 */
40 #ifdef ENFORCE_MEMFD_REXEC
41 __attribute__((constructor)) static void lxc_attach_rexec(void)
42 {
43 if (!getenv("LXC_MEMFD_REXEC") && lxc_rexec("lxc-attach")) {
44 fprintf(stderr, "Failed to re-execute lxc-attach via memory file descriptor\n");
45 _exit(EXIT_FAILURE);
46 }
47 }
48 #endif
49
50 static int my_parser(struct lxc_arguments *args, int c, char *arg);
51 static int add_to_simple_array(char ***array, ssize_t *capacity, char *value);
52 static bool stdfd_is_pty(void);
53 static int lxc_attach_create_log_file(const char *log_file);
54
55 static int elevated_privileges;
56 static signed long new_personality = -1;
57 static int namespace_flags = -1;
58 static int remount_sys_proc;
59 static lxc_attach_env_policy_t env_policy = LXC_ATTACH_KEEP_ENV;
60 static char **extra_env;
61 static ssize_t extra_env_size;
62 static char **extra_keep;
63 static ssize_t extra_keep_size;
64 static char *selinux_context = NULL;
65
66 static const struct option my_longopts[] = {
67 {"elevated-privileges", optional_argument, 0, 'e'},
68 {"arch", required_argument, 0, 'a'},
69 {"namespaces", required_argument, 0, 's'},
70 {"remount-sys-proc", no_argument, 0, 'R'},
71 /* TODO: decide upon short option names */
72 {"clear-env", no_argument, 0, 500},
73 {"keep-env", no_argument, 0, 501},
74 {"keep-var", required_argument, 0, 502},
75 {"set-var", required_argument, 0, 'v'},
76 {"pty-log", required_argument, 0, 'L'},
77 {"rcfile", required_argument, 0, 'f'},
78 {"uid", required_argument, 0, 'u'},
79 {"gid", required_argument, 0, 'g'},
80 {"context", required_argument, 0, 'c'},
81 LXC_COMMON_OPTIONS
82 };
83
84 static struct lxc_arguments my_args = {
85 .progname = "lxc-attach",
86 .help = "\
87 --name=NAME [-- COMMAND]\n\
88 \n\
89 Execute the specified COMMAND - enter the container NAME\n\
90 \n\
91 Options :\n\
92 -n, --name=NAME NAME of the container\n\
93 -e, --elevated-privileges=PRIVILEGES\n\
94 Use elevated privileges instead of those of the\n\
95 container. If you don't specify privileges to be\n\
96 elevated as OR'd list: CAP, CGROUP and LSM (capabilities,\n\
97 cgroup and restrictions, respectively) then all of them\n\
98 will be elevated.\n\
99 WARNING: This may leak privileges into the container.\n\
100 Use with care.\n\
101 -a, --arch=ARCH Use ARCH for program instead of container's own\n\
102 architecture.\n\
103 -s, --namespaces=FLAGS\n\
104 Don't attach to all the namespaces of the container\n\
105 but just to the following OR'd list of flags:\n\
106 MOUNT, PID, UTSNAME, IPC, USER or NETWORK.\n\
107 WARNING: Using -s implies -e with all privileges\n\
108 elevated, it may therefore leak privileges into the\n\
109 container. Use with care.\n\
110 -R, --remount-sys-proc\n\
111 Remount /sys and /proc if not attaching to the\n\
112 mount namespace when using -s in order to properly\n\
113 reflect the correct namespace context. See the\n\
114 lxc-attach(1) manual page for details.\n\
115 --clear-env Clear all environment variables before attaching.\n\
116 The attached shell/program will start with only\n\
117 container=lxc set.\n\
118 --keep-env Keep all current environment variables. This\n\
119 is the current default behaviour, but is likely to\n\
120 change in the future.\n\
121 -L, --pty-log=FILE\n\
122 Log pty output to FILE\n\
123 -v, --set-var Set an additional variable that is seen by the\n\
124 attached program in the container. May be specified\n\
125 multiple times.\n\
126 --keep-var Keep an additional environment variable. Only\n\
127 applicable if --clear-env is specified. May be used\n\
128 multiple times.\n\
129 -f, --rcfile=FILE\n\
130 Load configuration file FILE\n\
131 -u, --uid=UID Execute COMMAND with UID inside the container\n\
132 -g, --gid=GID Execute COMMAND with GID inside the container\n\
133 -c, --context=context\n\
134 SELinux Context to transition into\n\
135 ",
136 .options = my_longopts,
137 .parser = my_parser,
138 .checker = NULL,
139 .log_priority = "ERROR",
140 .log_file = "none",
141 .uid = LXC_INVALID_UID,
142 .gid = LXC_INVALID_GID,
143 };
144
145 static int my_parser(struct lxc_arguments *args, int c, char *arg)
146 {
147 int ret;
148
149 switch (c) {
150 case 'e':
151 ret = lxc_fill_elevated_privileges(arg, &elevated_privileges);
152 if (ret)
153 return -1;
154 break;
155 case 'R': remount_sys_proc = 1; break;
156 case 'a':
157 new_personality = lxc_config_parse_arch(arg);
158 if (new_personality < 0) {
159 ERROR("Invalid architecture specified: %s", arg);
160 return -1;
161 }
162 break;
163 case 's':
164 namespace_flags = 0;
165
166 if (lxc_namespace_2_std_identifiers(arg) < 0)
167 return -1;
168
169 ret = lxc_fill_namespace_flags(arg, &namespace_flags);
170 if (ret)
171 return -1;
172
173 /* -s implies -e */
174 lxc_fill_elevated_privileges(NULL, &elevated_privileges);
175 break;
176 case 500: /* clear-env */
177 env_policy = LXC_ATTACH_CLEAR_ENV;
178 break;
179 case 501: /* keep-env */
180 env_policy = LXC_ATTACH_KEEP_ENV;
181 break;
182 case 502: /* keep-var */
183 ret = add_to_simple_array(&extra_keep, &extra_keep_size, arg);
184 if (ret < 0) {
185 ERROR("Failed to alloc memory");
186 return -1;
187 }
188 break;
189 case 'v':
190 ret = add_to_simple_array(&extra_env, &extra_env_size, arg);
191 if (ret < 0) {
192 ERROR("Failed to alloc memory");
193 return -1;
194 }
195 break;
196 case 'L':
197 args->console_log = arg;
198 break;
199 case 'f':
200 args->rcfile = arg;
201 break;
202 case 'u':
203 if (lxc_safe_uint(arg, &args->uid) < 0)
204 return -1;
205 break;
206 case 'g':
207 if (lxc_safe_uint(arg, &args->gid) < 0)
208 return -1;
209 break;
210 case 'c':
211 selinux_context = arg;
212 break;
213 }
214
215 return 0;
216 }
217
218 static int add_to_simple_array(char ***array, ssize_t *capacity, char *value)
219 {
220 ssize_t count = 0;
221
222 if (!array)
223 return -1;
224
225 if (*array)
226 for (; (*array)[count]; count++);
227
228 /* we have to reallocate */
229 if (count >= *capacity - 1) {
230 ssize_t new_capacity = ((count + 1) / 32 + 1) * 32;
231
232 char **new_array = realloc((void*)*array, sizeof(char *) * new_capacity);
233 if (!new_array)
234 return -1;
235
236 memset(&new_array[count], 0, sizeof(char*)*(new_capacity - count));
237
238 *array = new_array;
239 *capacity = new_capacity;
240 }
241
242 if (!(*array))
243 return -1;
244
245 (*array)[count] = value;
246 return 0;
247 }
248
249 static bool stdfd_is_pty(void)
250 {
251 if (isatty(STDIN_FILENO))
252 return true;
253
254 if (isatty(STDOUT_FILENO))
255 return true;
256
257 if (isatty(STDERR_FILENO))
258 return true;
259
260 return false;
261 }
262
263 static int lxc_attach_create_log_file(const char *log_file)
264 {
265 int fd;
266
267 fd = open(log_file, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600);
268 if (fd < 0) {
269 ERROR("Failed to open log file \"%s\"", log_file);
270 return -1;
271 }
272
273 return fd;
274 }
275
276 int main(int argc, char *argv[])
277 {
278 int ret = -1;
279 int wexit = 0;
280 struct lxc_log log;
281 pid_t pid;
282 lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
283 lxc_attach_command_t command = (lxc_attach_command_t){.program = NULL};
284
285 if (lxc_caps_init())
286 exit(EXIT_FAILURE);
287
288 if (lxc_arguments_parse(&my_args, argc, argv))
289 exit(EXIT_FAILURE);
290
291 log.name = my_args.name;
292 log.file = my_args.log_file;
293 log.level = my_args.log_priority;
294 log.prefix = my_args.progname;
295 log.quiet = my_args.quiet;
296 log.lxcpath = my_args.lxcpath[0];
297
298 if (lxc_log_init(&log))
299 exit(EXIT_FAILURE);
300
301 if (geteuid())
302 if (access(my_args.lxcpath[0], O_RDONLY) < 0) {
303 ERROR("You lack access to %s", my_args.lxcpath[0]);
304 exit(EXIT_FAILURE);
305 }
306
307 struct lxc_container *c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
308 if (!c)
309 exit(EXIT_FAILURE);
310
311 if (my_args.rcfile) {
312 c->clear_config(c);
313 if (!c->load_config(c, my_args.rcfile)) {
314 ERROR("Failed to load rcfile");
315 lxc_container_put(c);
316 exit(EXIT_FAILURE);
317 }
318
319 c->configfile = strdup(my_args.rcfile);
320 if (!c->configfile) {
321 ERROR("Out of memory setting new config filename");
322 lxc_container_put(c);
323 exit(EXIT_FAILURE);
324 }
325 }
326
327 if (!c->may_control(c)) {
328 ERROR("Insufficent privileges to control %s", c->name);
329 lxc_container_put(c);
330 exit(EXIT_FAILURE);
331 }
332
333 if (remount_sys_proc)
334 attach_options.attach_flags |= LXC_ATTACH_REMOUNT_PROC_SYS;
335
336 if (elevated_privileges)
337 attach_options.attach_flags &= ~(elevated_privileges);
338
339 if (stdfd_is_pty())
340 attach_options.attach_flags |= LXC_ATTACH_TERMINAL;
341
342 attach_options.namespaces = namespace_flags;
343 attach_options.personality = new_personality;
344 attach_options.env_policy = env_policy;
345 attach_options.extra_env_vars = extra_env;
346 attach_options.extra_keep_env = extra_keep;
347
348 if (my_args.argc > 0) {
349 command.program = my_args.argv[0];
350 command.argv = (char**)my_args.argv;
351 }
352
353 if (my_args.console_log) {
354 attach_options.log_fd = lxc_attach_create_log_file(my_args.console_log);
355 if (attach_options.log_fd < 0)
356 goto out;
357 }
358
359 if (my_args.uid != LXC_INVALID_UID)
360 attach_options.uid = my_args.uid;
361
362 if (my_args.gid != LXC_INVALID_GID)
363 attach_options.gid = my_args.gid;
364
365 // selinux_context will be NULL if not set
366 attach_options.lsm_label = selinux_context;
367
368 if (command.program) {
369 ret = c->attach_run_wait(c, &attach_options, command.program,
370 (const char **)command.argv);
371 if (ret < 0)
372 goto out;
373 } else {
374 ret = c->attach(c, lxc_attach_run_shell, NULL, &attach_options, &pid);
375 if (ret < 0)
376 goto out;
377
378 ret = lxc_wait_for_pid_status(pid);
379 if (ret < 0)
380 goto out;
381 }
382 if (WIFEXITED(ret))
383 wexit = WEXITSTATUS(ret);
384
385 out:
386 lxc_container_put(c);
387 if (ret >= 0)
388 exit(wexit);
389
390 exit(EXIT_FAILURE);
391 }