2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2010
7 * Daniel Lezcano <daniel.lezcano at free.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/ioctl.h>
34 #include <sys/types.h>
39 #include <lxc/lxccontainer.h>
41 #include "arguments.h"
50 lxc_log_define(lxc_attach
, lxc
);
53 * This function will copy any binary that calls liblxc into a memory file and
54 * will use the memfd to rexecute the binary. This is done to prevent attacks
55 * through the /proc/self/exe symlink to corrupt the host binary when host and
56 * container are in the same user namespace or have set up an identity id
57 * mapping: CVE-2019-5736.
59 #ifdef ENFORCE_MEMFD_REXEC
60 __attribute__((constructor
)) static void lxc_attach_rexec(void)
62 if (!getenv("LXC_MEMFD_REXEC") && lxc_rexec("lxc-attach")) {
63 fprintf(stderr
, "Failed to re-execute lxc-attach via memory file descriptor\n");
69 static int my_parser(struct lxc_arguments
*args
, int c
, char *arg
);
70 static int add_to_simple_array(char ***array
, ssize_t
*capacity
, char *value
);
71 static bool stdfd_is_pty(void);
72 static int lxc_attach_create_log_file(const char *log_file
);
74 static int elevated_privileges
;
75 static signed long new_personality
= -1;
76 static int namespace_flags
= -1;
77 static int remount_sys_proc
;
78 static lxc_attach_env_policy_t env_policy
= LXC_ATTACH_KEEP_ENV
;
79 static char **extra_env
;
80 static ssize_t extra_env_size
;
81 static char **extra_keep
;
82 static ssize_t extra_keep_size
;
84 static const struct option my_longopts
[] = {
85 {"elevated-privileges", optional_argument
, 0, 'e'},
86 {"arch", required_argument
, 0, 'a'},
87 {"namespaces", required_argument
, 0, 's'},
88 {"remount-sys-proc", no_argument
, 0, 'R'},
89 /* TODO: decide upon short option names */
90 {"clear-env", no_argument
, 0, 500},
91 {"keep-env", no_argument
, 0, 501},
92 {"keep-var", required_argument
, 0, 502},
93 {"set-var", required_argument
, 0, 'v'},
94 {"pty-log", required_argument
, 0, 'L'},
95 {"rcfile", required_argument
, 0, 'f'},
96 {"uid", required_argument
, 0, 'u'},
97 {"gid", required_argument
, 0, 'g'},
101 static struct lxc_arguments my_args
= {
102 .progname
= "lxc-attach",
104 --name=NAME [-- COMMAND]\n\
106 Execute the specified COMMAND - enter the container NAME\n\
109 -n, --name=NAME NAME of the container\n\
110 -e, --elevated-privileges=PRIVILEGES\n\
111 Use elevated privileges instead of those of the\n\
112 container. If you don't specify privileges to be\n\
113 elevated as OR'd list: CAP, CGROUP and LSM (capabilities,\n\
114 cgroup and restrictions, respectively) then all of them\n\
116 WARNING: This may leak privileges into the container.\n\
118 -a, --arch=ARCH Use ARCH for program instead of container's own\n\
120 -s, --namespaces=FLAGS\n\
121 Don't attach to all the namespaces of the container\n\
122 but just to the following OR'd list of flags:\n\
123 MOUNT, PID, UTSNAME, IPC, USER or NETWORK.\n\
124 WARNING: Using -s implies -e with all privileges\n\
125 elevated, it may therefore leak privileges into the\n\
126 container. Use with care.\n\
127 -R, --remount-sys-proc\n\
128 Remount /sys and /proc if not attaching to the\n\
129 mount namespace when using -s in order to properly\n\
130 reflect the correct namespace context. See the\n\
131 lxc-attach(1) manual page for details.\n\
132 --clear-env Clear all environment variables before attaching.\n\
133 The attached shell/program will start with only\n\
134 container=lxc set.\n\
135 --keep-env Keep all current environment variables. This\n\
136 is the current default behaviour, but is likely to\n\
137 change in the future.\n\
138 -L, --pty-log=FILE\n\
139 Log pty output to FILE\n\
140 -v, --set-var Set an additional variable that is seen by the\n\
141 attached program in the container. May be specified\n\
143 --keep-var Keep an additional environment variable. Only\n\
144 applicable if --clear-env is specified. May be used\n\
147 Load configuration file FILE\n\
148 -u, --uid=UID Execute COMMAND with UID inside the container\n\
149 -g, --gid=GID Execute COMMAND with GID inside the container\n\
151 .options
= my_longopts
,
154 .log_priority
= "ERROR",
158 static int my_parser(struct lxc_arguments
*args
, int c
, char *arg
)
164 ret
= lxc_fill_elevated_privileges(arg
, &elevated_privileges
);
168 case 'R': remount_sys_proc
= 1; break;
170 new_personality
= lxc_config_parse_arch(arg
);
171 if (new_personality
< 0) {
172 ERROR("Invalid architecture specified: %s", arg
);
179 if (lxc_namespace_2_std_identifiers(arg
) < 0)
182 ret
= lxc_fill_namespace_flags(arg
, &namespace_flags
);
187 lxc_fill_elevated_privileges(NULL
, &elevated_privileges
);
189 case 500: /* clear-env */
190 env_policy
= LXC_ATTACH_CLEAR_ENV
;
192 case 501: /* keep-env */
193 env_policy
= LXC_ATTACH_KEEP_ENV
;
195 case 502: /* keep-var */
196 ret
= add_to_simple_array(&extra_keep
, &extra_keep_size
, arg
);
198 ERROR("Failed to alloc memory");
203 ret
= add_to_simple_array(&extra_env
, &extra_env_size
, arg
);
205 ERROR("Failed to alloc memory");
210 args
->console_log
= arg
;
216 if (lxc_safe_uint(arg
, &args
->uid
) < 0)
220 if (lxc_safe_uint(arg
, &args
->gid
) < 0)
228 static int add_to_simple_array(char ***array
, ssize_t
*capacity
, char *value
)
236 for (; (*array
)[count
]; count
++);
238 /* we have to reallocate */
239 if (count
>= *capacity
- 1) {
240 ssize_t new_capacity
= ((count
+ 1) / 32 + 1) * 32;
242 char **new_array
= realloc((void*)*array
, sizeof(char *) * new_capacity
);
246 memset(&new_array
[count
], 0, sizeof(char*)*(new_capacity
- count
));
249 *capacity
= new_capacity
;
255 (*array
)[count
] = value
;
259 static bool stdfd_is_pty(void)
261 if (isatty(STDIN_FILENO
))
264 if (isatty(STDOUT_FILENO
))
267 if (isatty(STDERR_FILENO
))
273 static int lxc_attach_create_log_file(const char *log_file
)
277 fd
= open(log_file
, O_CLOEXEC
| O_RDWR
| O_CREAT
| O_APPEND
, 0600);
279 ERROR("Failed to open log file \"%s\"", log_file
);
286 int main(int argc
, char *argv
[])
292 lxc_attach_options_t attach_options
= LXC_ATTACH_OPTIONS_DEFAULT
;
293 lxc_attach_command_t command
= (lxc_attach_command_t
){.program
= NULL
};
298 if (lxc_arguments_parse(&my_args
, argc
, argv
))
301 log
.name
= my_args
.name
;
302 log
.file
= my_args
.log_file
;
303 log
.level
= my_args
.log_priority
;
304 log
.prefix
= my_args
.progname
;
305 log
.quiet
= my_args
.quiet
;
306 log
.lxcpath
= my_args
.lxcpath
[0];
308 if (lxc_log_init(&log
))
312 if (access(my_args
.lxcpath
[0], O_RDONLY
) < 0) {
313 ERROR("You lack access to %s", my_args
.lxcpath
[0]);
317 struct lxc_container
*c
= lxc_container_new(my_args
.name
, my_args
.lxcpath
[0]);
321 if (my_args
.rcfile
) {
323 if (!c
->load_config(c
, my_args
.rcfile
)) {
324 ERROR("Failed to load rcfile");
325 lxc_container_put(c
);
329 c
->configfile
= strdup(my_args
.rcfile
);
330 if (!c
->configfile
) {
331 ERROR("Out of memory setting new config filename");
332 lxc_container_put(c
);
337 if (!c
->may_control(c
)) {
338 ERROR("Insufficent privileges to control %s", c
->name
);
339 lxc_container_put(c
);
343 if (remount_sys_proc
)
344 attach_options
.attach_flags
|= LXC_ATTACH_REMOUNT_PROC_SYS
;
346 if (elevated_privileges
)
347 attach_options
.attach_flags
&= ~(elevated_privileges
);
350 attach_options
.attach_flags
|= LXC_ATTACH_TERMINAL
;
352 attach_options
.namespaces
= namespace_flags
;
353 attach_options
.personality
= new_personality
;
354 attach_options
.env_policy
= env_policy
;
355 attach_options
.extra_env_vars
= extra_env
;
356 attach_options
.extra_keep_env
= extra_keep
;
358 if (my_args
.argc
> 0) {
359 command
.program
= my_args
.argv
[0];
360 command
.argv
= (char**)my_args
.argv
;
363 if (my_args
.console_log
) {
364 attach_options
.log_fd
= lxc_attach_create_log_file(my_args
.console_log
);
365 if (attach_options
.log_fd
< 0)
370 attach_options
.uid
= my_args
.uid
;
373 attach_options
.gid
= my_args
.gid
;
375 if (command
.program
) {
376 ret
= c
->attach_run_wait(c
, &attach_options
, command
.program
,
377 (const char **)command
.argv
);
381 ret
= c
->attach(c
, lxc_attach_run_shell
, NULL
, &attach_options
, &pid
);
385 ret
= lxc_wait_for_pid_status(pid
);
390 wexit
= WEXITSTATUS(ret
);
393 lxc_container_put(c
);