Commit Graph

4664 Commits

Author SHA1 Message Date
Stéphane Graber
d53d0a01a9 Merge pull request #1315 from brauner/2016-11-23/memfd_create
conf, criu: add make_anonymous_mount_file()
2016-11-25 23:50:28 -05:00
Stéphane Graber
3f37ede22a Merge pull request #1320 from brauner/2016-11-25/remove_vlas
tree-wide: remove any variable length arrays
2016-11-25 23:04:21 -05:00
Christian Brauner
5ef5c9a344
conf, criu: add make_anonymous_mount_file()
Before we used tmpfile() to write out mount entries for the container. This
requires a writeable /tmp file system which can be a problem for systems where
this filesystem is not present. This commit switches from tmpfile() to using
the memfd_create() syscall. It allows us to create an anonymous tmpfs file (And
is somewhat similar to mmap().) which is automatically deleted as soon as any
references to it are dropped. In case we detect that syscall is not
implemented, we fallback to using tmpfile().

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 04:28:03 +01:00
Christian Brauner
413c294f75 configure: check for memfd_create()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 04:17:14 +01:00
Christian Brauner
8f3e280e77 conf: non-functional changes
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 04:15:40 +01:00
Christian Brauner
de3c491bff
tree-wide: remove any variable length arrays
They are pointless and marked optional since C11.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 03:56:06 +01:00
Stéphane Graber
1145b82876 Merge pull request #1319 from brauner/2016-11-25/fix_logging_race
log: fix race
2016-11-25 18:20:25 -05:00
Christian Brauner
d8f2dda5a5
configure: do not allow variable length arrays
There pointless and marked as optional since C11.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-25 22:02:28 +01:00
Christian Brauner
450b6d3d68
log: use N/A if getpid() != gettid() when threaded
Sample log output:

    lxc 20161125201943.819 INFO     lxc_start - start.c:lxc_check_inherited:243 - Closed inherited fd: 54.
--> lxc N/A                INFO     lxc_monitor - monitor.c:lxc_monitor_sock_name:178 - using monitor sock name lxc/ad055575fe28ddd5//var/lib/lxc
    lxc 20161125201943.958 DEBUG    lxc_commands - commands.c:lxc_cmd_handler:893 - peer has disconnected
--> lxc N/A                DEBUG    lxc_commands - commands.c:lxc_cmd_get_state:579 - 'lxc-test-concurrent-0' is in 'RUNNING' state
    lxc 20161125201943.960 DEBUG    lxc_commands - commands.c:lxc_cmd_handler:893 - peer has disconnected
    lxc 20161125201944.009 INFO     lxc_start - start.c:lxc_check_inherited:243 - Closed inherited fd: 3.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-25 20:24:26 +01:00
Christian Brauner
0dcdbf8a67
log: fix race
localtime_r() can lead to deadlocks because it calls __tzset() and
__tzconvert() internally. The deadlock stems from an interaction between these
functions and the functions in monitor.c and commands.{c,h}. The latter
functions will write to the log independent of the container thread that is
currently running. Since the monitor fork()ed it seems to duplicate the mutex
states of the time functions mentioned above causing the deadlock.
As a short termm fix, I suggest to simply disable receiving the time when
monitor.c or command.{c,h} functions are called. This should be ok, since the
[lxc monitor] will only emit a few messages and thread-safety is currently more
important than beautiful logs. The rest of the log stays the same as it was
before.

Here is an example output from logs where I printed the pid and tid of the
process that is currently writing to the log:

            lxc 20161125170200.619 INFO     lxc_start:   18695-18695: - start.c:lxc_check_inherited:243 - Closed inherited fd: 23.
            lxc 20161125170200.640 DEBUG    lxc_start:   18677-18677: - start.c:__lxc_start:1334 - Not dropping CAP_SYS_BOOT or watching utmp.
            lxc 20161125170200.640 INFO     lxc_cgroup:  18677-18677: - cgroups/cgroup.c:cgroup_init:68 - cgroup driver cgroupfs-ng initing for lxc-test-concurrent-0

----------> lxc 20150427012246.000 INFO     lxc_monitor: 13017-18622: - monitor.c:lxc_monitor_sock_name:178 - using monitor sock name lxc/ad055575fe28ddd5//var/lib/lxc

            lxc 20161125170200.662 DEBUG    lxc_cgfsng:  18677-18677: - cgroups/cgfsng.c:filter_and_set_cpus:478 - No isolated cpus detected.
            lxc 20161125170200.662 DEBUG    lxc_cgfsng:  18677-18677: - cgroups/cgfsng.c:handle_cpuset_hierarchy:648 - "cgroup.clone_children" was already set to "1".

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-25 20:04:23 +01:00
Christian Brauner
6a22e86258
configure: check whether gettid() is declared
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-25 20:03:50 +01:00
Stéphane Graber
8d3786cb75 Merge pull request #1317 from brauner/2016-11-24/cleanup_attach
cleanup attach
2016-11-25 12:46:03 -05:00
Stéphane Graber
b5e3950161 Merge pull request #1318 from brauner/2016-11-25/fix_logging_race
log: use thread-safe localtime_r()
2016-11-24 21:22:04 -05:00
Christian Brauner
b4c424743a
log: use thread-safe localtime_r()
This fixes a race in liblxc logging which can lead to deadlocks. The reproducer
for this issue before this is to simply compile with --enable-tests and then
run:

    lxc-test-concurrent -j 20 -m create,start,stop,destroy -D

which should deadlock.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-25 02:42:51 +01:00
Christian Brauner
8ce83369ef
attach: non-functional changes
- improve logging
- simplify functions

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-24 08:34:10 +01:00
Christian Brauner
82b1f317c8
attach: simplify lsm_openat()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-24 07:58:01 +01:00
Christian Brauner
81f466d05f attach: do not send procfd to attached process
So far, we opened a file descriptor refering to proc on the host inside the
host namespace and handed that fd to the attached process in
attach_child_main(). This was done to ensure that LSM labels were correctly
setup. However, by exploiting a potential kernel bug, ptrace could be used to
prevent the file descriptor from being closed which in turn could be used by an
unprivileged container to gain access to the host namespace. Aside from this
needing an upstream kernel fix, we should make sure that we don't pass the fd
for proc itself to the attached process. However, we cannot completely prevent
this, as the attached process needs to be able to change its apparmor profile
by writing to /proc/self/attr/exec or /proc/self/attr/current. To minimize the
attack surface, we only send the fd for /proc/self/attr/exec or
/proc/self/attr/current to the attached process. To do this we introduce a
little more IPC between the child and parent:

	 * IPC mechanism: (X is receiver)
	 *   initial process        intermediate          attached
	 *        X           <---  send pid of
	 *                          attached proc,
	 *                          then exit
	 *    send 0 ------------------------------------>    X
	 *                                              [do initialization]
	 *        X  <------------------------------------  send 1
	 *   [add to cgroup, ...]
	 *    send 2 ------------------------------------>    X
	 *						[set LXC_ATTACH_NO_NEW_PRIVS]
	 *        X  <------------------------------------  send 3
	 *   [open LSM label fd]
	 *    send 4 ------------------------------------>    X
	 *   						[set LSM label]
	 *   close socket                                 close socket
	 *                                                run program

The attached child tells the parent when it is ready to have its LSM labels set
up. The parent then opens an approriate fd for the child PID to
/proc/<pid>/attr/exec or /proc/<pid>/attr/current and sends it via SCM_RIGHTS
to the child. The child can then set its LSM laben. Both sides then close the
socket fds and the child execs the requested process.

Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-23 11:13:49 -05:00
Christian Brauner
8f227cdcb8 Merge pull request #1314 from ccope/static-mac
Configure a static MAC address on the LXC bridge
2016-11-22 23:04:45 -05:00
Cam Cope
84432621ed
Configure a static MAC address on the LXC bridge
Signed-off-by: Cam Cope <cam@dropbox.com>
2016-11-22 15:16:32 -08:00
Stéphane Graber
dea1cfbd16 Merge pull request #1312 from brauner/2016-11-22/fix_lxc_unshare_parsing
tools: replace non-standard namespace identifiers
2016-11-22 12:12:21 -05:00
Stéphane Graber
70c9390ea6 Merge pull request #1313 from brauner/2016-11-22/fix_lxc_tests
tests: remove overflow tests
2016-11-22 12:10:41 -05:00
Christian Brauner
777eb3ba25
tests: remove overflow tests
They do not behave correctly on some architectures, so let's remove them for
now and come up with better ones later.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-22 17:38:18 +01:00
Christian Brauner
9420e0c278
tools: replace non-standard namespace identifiers
The identifiers for namespaces used with lxc-unshare and lxc-attach as given on
the manpage do not align with the standard identifiers. This affects network,
mount, and uts namespaces. The standard identifiers are: "mnt", "uts", and
"net" whereas lxc-unshare and lxc-attach use "MOUNT", "UTSNAME", and "NETWORK".
I'm weary to hack this into namespace.{c.h} by e.g. adding additional members
to the ns_info struct or to special case this in lxc_fill_namespace_flags().
Internally, we should only accept standard identifiers to ensure that we are
always correctly aligned with the kernel. So let's use some cheap memmove()s to
replace them by their standard identifiers in lxc-unshare and lxc-attach.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-22 17:27:45 +01:00
Serge Hallyn
7688de67c6 Merge pull request #1256 from brauner/2016-09-06/remove_atoi
remove atoi
2016-11-21 22:46:21 -06:00
Christian Brauner
d47f1b43c6
confile: non-functional change
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:47:06 +01:00
Christian Brauner
31299e70d8
tools/lxc_console: use lxc_safe_uint()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:47:04 +01:00
Christian Brauner
63610340c2
tools/lxc_autostart: use lxc_safe_{int,long}()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:47:02 +01:00
Christian Brauner
98de68602c
tools/lxc_ls: use lxc_safe_uint()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:19 +01:00
Christian Brauner
45419e74ef
tools/lxc_top: use lxc_safe_int()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:17 +01:00
Christian Brauner
2a9a36aa0e
tools/lxc_stop: use lxc_safe_long()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:15 +01:00
Christian Brauner
cb9df6d4a7
tests: add unit tests for lxc_safe_long()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:13 +01:00
Christian Brauner
8c57d93034
utils: add lxc_safe_long()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:11 +01:00
Christian Brauner
0406409813
tools/lxc_stop: use lxc_safe_uint()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:10 +01:00
Christian Brauner
fb111ba0ad
tools/lxc_execute: use lxc_safe_uint()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:08 +01:00
Christian Brauner
b7b2fde48f
conf: use lxc_safe_{u}int()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:06 +01:00
Christian Brauner
d4cff0d21a
start: use lxc_safe_int()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:04 +01:00
Christian Brauner
7e5af997d3
lxc_monitord: use lxc_safe_int() && use exit()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:02 +01:00
Christian Brauner
2d036ccac0
utils: use lxc_safe_int()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:46:00 +01:00
Christian Brauner
e8ec7c9efc
conf/ile: avoid atoi() in config_no_new_privs()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:45:58 +01:00
Christian Brauner
66ffdb1a20
conf/ile: avoid atoi() in config_ephemeral()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:43:02 +01:00
Christian Brauner
ff6cb4ed82
conf/ile: use lxc_safe_uint() in config_autodev()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:41:37 +01:00
Christian Brauner
a56e2df9cf
conf/ile: avoid atoi in config_lsm_aa_incomplete()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 04:39:25 +01:00
Christian Brauner
91863d360b
conf/ile: use lxc_safe_uint() in config_kmsg()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:41:22 +01:00
Christian Brauner
1c30b4adbd
conf/ile: use lxc_safe_uint() in config_tty()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:41:19 +01:00
Christian Brauner
226dc30e4f
conf/ile: use lxc_safe_uint() in config_monitor()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:41:17 +01:00
Christian Brauner
3590152f01
conf/ile: use lxc_safe_u/int() in config_start()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:41:15 +01:00
Christian Brauner
1791996979
conf/ile: use lxc_safe_uint() in config_pts()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:41:13 +01:00
Christian Brauner
d1e5d63673
confile: use lxc_safe_u/int in config_init_{u,g}id
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:41:11 +01:00
Christian Brauner
1c6333988f
conf/ile: get ip prefix via lxc_safe_uint()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:41:09 +01:00
Christian Brauner
aa75cefcef
tests: add unit tests for lxc_safe_int()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:41:07 +01:00