Commit Graph

5245 Commits

Author SHA1 Message Date
Stéphane Graber
f024b7165d Merge pull request #1333 from brauner/2016-11-29/macro_cleanups
tree-wide: random macro cleanups
2016-11-29 13:33:50 -05:00
Stéphane Graber
181df78f26 Merge pull request #1332 from brauner/2016-11-29/fix_possible_hanging_monitord
monitord: close mainloop on exit if we opened it
2016-11-29 13:33:34 -05:00
Christian Brauner
940ef906b4
state: use async signal safe fun in lxc_wait()
gettimeofday() is not async signal safe. So let's switch to clock_gettime() to
be on the safe side.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-29 19:21:46 +01:00
Christian Brauner
5cc0f22d3e
monitord: close mainloop on exit if we opened it
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-29 08:38:20 +01:00
Christian Brauner
9d7468fdd3
log: make sure that date is correctly formatted
- single digit months, days, hours, minutes, and seconds should always be
  preceded by a 0.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-29 07:52:30 +01:00
Christian Brauner
eab15c1ee6
tree-wide: random macro cleanups
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-29 07:29:32 +01:00
Serge Hallyn
258e3e43a8 Merge pull request #1331 from brauner/2016-11-27/use_sec_nanosec_since_epoch
log: drop all timezone conversion functions
2016-11-28 21:24:57 -06:00
Christian Brauner
d86c0d08c4
log: annotate lxc_unix_epoch_to_utc()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-29 01:26:25 +01:00
Christian Brauner
e1378d3591
log: use lxc_unix_epoch_to_utc()
This allows us to generate nice timestamps in a thread-safe manner without
relying on locale touching functions from any libc.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-28 22:22:14 +01:00
Christian Brauner
65a9df895e
log: add lxc_unix_epoch_to_utc()
Converts a unix time Epoch given by a struct timespec to a UTC string useable
in our logging functions. Maybe expanded to allow for more generic formatting.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-28 22:22:12 +01:00
Christian Brauner
c57dbb9658
log: drop all timezone conversion functions
Our log functions need to make extra sure that they are thread-safe. We had
some problems with that before. This especially involves time-conversion
functions. I don't want to find any localtime() or gmtime() functions or
relatives in here. Not even localtime_r() or gmtime_r() or relatives. They all
fiddle with global variables and locking in various libcs. They cause deadlocks
when liblxc is used multi-threaded and no matter how smart you think you are,
you __will__ cause trouble using them.
(As a short example how this can cause trouble: LXD uses forkstart to fork off
a new process that runs the container. At the same time the go runtime LXD
relies on does its own multi-threading thing which we can't control. The
fork()ing + threading then seems to mess with the locking states in these time
functions causing deadlocks.)
The current solution is to be good old unix people and use the Epoch as our
reference point and simply use the seconds and nanoseconds that have past since
then. This relies on clock_gettime() which is explicitly marked MT-Safe with no
restrictions! This way, anyone who is really strongly invested in getting the
actual time the log entry was created, can just convert it for themselves. Our
logging is mostly done for debugging purposes so don't try to make it pretty.
Pretty might cost you thread-safety.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-28 05:27:39 +01:00
Christian Brauner
b07511df5c
utils: add macro __LXC_NUMSTRLEN
This macro can be used to set or allocate a string buffer that can hold any
64bit representable number.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-27 23:41:06 +01:00
Stéphane Graber
1fbb7fae52 Merge pull request #1329 from brauner/2016-11-27/log_signal_error_on_container_stop
lxccontainer: log failure to send sig to init pid
2016-11-27 01:00:55 -05:00
Stéphane Graber
d0f03fd752 Merge pull request #1328 from brauner/2016-11-27/make_running_hooks_async_signal_safe
conf: remove thread-unsafe strsignal + improve log
2016-11-27 00:10:53 -05:00
Christian Brauner
591614a762
lxccontainer: log failure to send sig to init pid
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-27 06:01:36 +01:00
Stéphane Graber
1e0a00460a Merge pull request #1327 from brauner/2016-11-26/make_lxc_monitord_async_signal_safe
lxc_monitord: make lxc-monitord async signal safe
2016-11-26 22:56:58 -05:00
Christian Brauner
062b72c6b9
conf: remove thread-unsafe strsignal + improve log
The thread-unsafe functions strsignal() is called in run_buffer() which in turn
is called in run_buffer_argv() which is responsible for running __all__ lxc
hooks. This is pretty dangerous for multi-threaded users like LXD.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-27 04:44:06 +01:00
Christian Brauner
d0dacf05a6
lxc_monitord: make lxc-monitord async signal safe
Before lxc_monitord called lxc_monitord_cleanup() from a signal handler.  This
function calls a bunch of async signal unsafe functions and basically begs for
deadlocks. This commit switches lxc-monitord to using sigsetjmp() and
siglongjmp() in the signal handler to jump to a cleanup label that call
lxc_monitord_cleanup(). In this way, we avoid using async signal unsafe
functions.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-27 04:28:01 +01:00
Stéphane Graber
8d58eea994 Merge pull request #1326 from brauner/2016-11-26/improve_logging_for_monitord
lxc_monitord: improve log + set log level to DEBUG
2016-11-26 15:13:41 -05:00
Christian Brauner
60e4f6fa4d
lxc_monitord: improve log + set log level to DEBUG
Setting loglevel to DEBUG will allow us to retrieve more useful information in
case something goes wrong. The total size of the log will not increase
significantly.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 19:49:27 +01:00
Stéphane Graber
888ccbf1d1 Merge pull request #1325 from brauner/2016-11-26/improve_command_logging
commands: improve logging
2016-11-26 12:57:40 -05:00
Stéphane Graber
6ade052281 Merge pull request #1324 from brauner/2016-11-26/more_logging_for_monitor
monitor: add more logging
2016-11-26 12:57:16 -05:00
Stéphane Graber
d44a6abde4 Merge pull request #1322 from brauner/2016-11-26/remove_werror_vla
configure: remove -Werror=vla
2016-11-26 12:56:54 -05:00
Christian Brauner
08aa08fe57
commands: improve logging
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 09:28:51 +01:00
Christian Brauner
aec1ea6261
monitor: add more logging
Execing lxc-monitord is quite a crucial step so let's be very obsessive about
logging possible errors to guide us in debugging.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 07:40:59 +01:00
Christian Brauner
1154e59065 Merge pull request #1323 from hallyn/2016-11-25/droplock
lvm: supress warnings about leaked files
2016-11-26 01:31:22 -05:00
Christian Brauner
ce44bee117
configure: remove -Werror=vla
Because we include a header that uses a vla (/me scoffs at header).

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 07:07:25 +01:00
Serge Hallyn
79816ab3f4 lvm: supress warnings about leaked files
Closes #1283

Signed-off-by: Serge Hallyn <serge@hallyn.com>
2016-11-26 00:07:15 -06:00
Stéphane Graber
e7e49f6003 Merge pull request #1321 from brauner/2016-11-25/cleanup_monitor
cleanup monitor + improve log
2016-11-26 00:22:19 -05:00
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
Christian Brauner
487b14b6ae
monitor: log which pipe fd is currently used
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 05:30:36 +01:00
Christian Brauner
292b1d177a
monitor: non-functional changes
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-26 05:30:34 +01: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