Commit Graph

204 Commits

Author SHA1 Message Date
Christian Brauner
464c46115f
utils: make lxc_switch_uid_gid() return bool
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-09-11 10:55:07 +02:00
Christian Brauner
8af07f821c
utils: make lxc_setgroups() return bool
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-09-11 10:54:46 +02:00
Christian Brauner
db2d1af171
utils: improve lxc_switch_uid_gid()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-09-11 00:44:57 +02:00
Christian Brauner
b962868f4c
utils: improve get_ns_uid() and add get_ns_gid()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-09-11 00:44:56 +02:00
Christian Brauner
37ef15bbd2
utils: split into {file,string}_utils.{c,h}
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-08-21 13:00:20 +02:00
Christian Brauner
f26dc127bf
CVE 2018-6556: verify netns fd in lxc-user-nic
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-08-06 12:15:39 -04:00
Christian Brauner
279c45eed3
macro: add new macro header
This allows us to use a bunch of macros in our static build for init.lxc.static
without having to link against all of utils.{c,h}.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-08-04 20:11:58 +02:00
Christian Brauner
521b4771a9
utils: add lxc_iterate_parts()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-07-26 16:23:09 +02:00
Wolfgang Bumiller
eb5c2e6aee utils: add must_concat helper
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2018-07-25 12:12:27 +02:00
Liza Tretyakova
f6310f1844
utils: add shared mount point detection
Signed-off-by: Liza Tretyakova <elizabet.tretyakova@gmail.com>
2018-07-22 15:25:15 +02:00
2xsec
d7ab03757c
utils: move recursive_destroy() from cfgsng to utils.
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-07-03 14:44:24 +09:00
Christian Brauner
a9d4ebc168
utils: add fd_cloexec()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Cc: Wolfgang Bumiller <w.bumiller@proxmox.com>
2018-07-01 20:00:14 +02:00
2xsec
b14fc1007c utils: code cleanups
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-06-30 18:45:21 +09:00
Christian Brauner
573ad77fc2
utils: fix task_blocking_signal()
Closes #2342.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-05-26 21:47:46 +02:00
Christian Brauner
7ad3767052
utils: add remove_trailing_newlines()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-05-24 13:38:08 +02:00
Felix Abecassis
eacebcc3cb seccomp: fix type mismatch when parsing syscall arguments filters
Specifier %lli was insufficient for the type uint64_t, all values
between 2^63-1 and 2^64-1 were silently converted to 2^63-1.

We can't use %llu since it doesn't handle hexadecimal. Instead, we
parse the values as strings and then use strtoull(3).

Signed-off-by: Felix Abecassis <fabecassis@nvidia.com>
2018-05-23 22:38:25 -07:00
Christian Brauner
aa769a272f
utils: add LXC_PROC_PID_FD_LEN
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-05-04 11:59:11 +02:00
Christian Brauner
7cea590585
lxccontainer: use thread-safe open() + write()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-04-29 16:42:44 +02:00
Christian Brauner
9e320621fd
utils: define __NR_setns if missing on old glibcs
Closes #2248.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-04-10 12:27:39 +02:00
Christian Brauner
238b3e5ea4
lxccontainer: move macros to utils.h
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-02-24 21:38:03 +01:00
Christian Brauner
9994d1402c
confile: use mmap() to parse config file
Sigh, this is going to be fun. Essentially, dynamic memory allocation through
malloc() and friends is unsafe when fork()ing in threads. The locking state
that glibc maintains internally might get messed up when the process that
fork()ed calls malloc or calls functions that malloc() internally. Functions
that internally malloc() include fopen(). One solution here is to use open() +
mmap() instead of fopen() + getline().

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-02-24 21:37:39 +01:00
Christian Brauner
5038d11a98
utils: include linux/types.h
Closes #2178.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-02-21 10:13:12 +01:00
Christian Brauner
1fd0f41eff
utils: add lxc_set_death_signal()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-02-19 10:58:56 +01:00
Serge Hallyn
477aa378e9 define am_guest_unpriv
Sometimes we want to know whether we are privileged wrt our
namespaces, and sometimes we want to know whether we are priv
wrt init_user_ns.

Signed-off-by: Serge Hallyn <shallyn@cisco.com>
2018-02-08 12:06:39 -06:00
Christian Brauner
0c3deb94f4
cgroups: cgfsng_create: handle unified hierarchy
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-01-31 16:35:31 +01:00
Serge Hallyn
26f0e9151a
Merge pull request #2102 from brauner/2018-01-22/lsm_simplifications
lsm: simplifcations
2018-01-29 17:08:38 -06:00
Tycho Andersen
5384e99dee rename am_unpriv to am_host_unpriv
Signed-off-by: Tycho Andersen <tycho@tycho.ws>
2018-01-26 22:11:50 +00:00
Tycho Andersen
4692c01af2 better unprivileged detection
In particular, if we are already in a user namespace we are unprivileged,
and doing things like moving the physical nics back to the host netns won't
work. Let's do the same thing LXD does if euid == 0: inspect
/proc/self/uid_map and see what that says.

Signed-off-by: Tycho Andersen <tycho@tycho.ws>
2018-01-26 22:11:50 +00:00
Christian Brauner
5288a74faa
apparmor: do not call aa_change_profile()
We can simply write the label ourselves. There's no magic happening.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-01-22 12:14:14 +01:00
Christian Brauner
0059379ff4
tree-wide: s/getpid()/lxc_raw_getpid()/g
This is to avoid bad surprises caused by older glibc's pid cache (up to 2.25)
when using clone().

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-12-21 23:01:03 +01:00
Serge Hallyn
be459e9997
Merge pull request #1950 from brauner/2017-11-27/criu_fixes
bugfixes
2017-12-08 21:59:09 -06:00
Christian Brauner
2650fb4d5e
utils: declare sethostname() static inline
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-12-01 23:18:09 +01:00
Christian Brauner
41dc71558e
storage: constify where possible
This was made necessary by changes to the overlay driver.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Adrian Reber <areber@redhat.com>
2017-11-28 13:52:47 +01:00
Marcos Paulo de Souza
ac181b5c3c utils.h: Avoid duplicated sethostname implementation
Move duplicated implementatin of sethostname from conf.c and
lxc_unshare.c to utils.h

Signed-off-by: Marcos Paulo de Souza <marcos.souza.org@gmail.com>
2017-11-28 00:13:49 -02:00
Christian Brauner
478fb51d4e
conf: move CAP_SYS_* definitions to utils.h
Closes #1946.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-11-24 13:25:51 +01:00
Christian Brauner
6222c3f48b
utils: add lxc_find_next_power2()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-10-21 18:38:13 +02:00
Christian Brauner
e3db0162ff
utils: parse_byte_size_string()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-10-21 18:38:13 +02:00
Christian Brauner
b037bc675b
utils: add lxc_safe_long_long()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-10-21 18:38:13 +02:00
Christian Brauner
e46361235c
utils: add lxc_getpagesize()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-10-21 18:38:13 +02:00
Christian Brauner
127c6e703b
utils: add lxc_make_tmpfile()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-10-21 18:38:12 +02:00
Christian Brauner
b499121f1f
utils: add lxc_cloexec()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-10-21 16:30:08 +02:00
Christian Brauner
f749d524f1
utils: move memfd_create() definition
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-10-21 16:30:03 +02:00
Christian Brauner
a6f151a7ce
init: rework dumb init
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-10-10 18:01:35 +02:00
Serge Hallyn
3dca1af064 implement lxc_string_split_quoted
lxc_string_split_quoted() splits a string on spaces, but keeps
groups in single or double qoutes together.  In other words,
generally what we'd want for argv behavior.

Switch lxc-execute to use this for lxc.execute.cmd.

Switch lxc-oci template to put the lxc.execute.cmd inside single
quotes, because parse_line() will eat those.  If we don't do that,
then if we have lxc.execute.cmd = /bin/echo "hello, world", then the
last double quote will disappear.

Signed-off-by: Serge Hallyn <shallyn@cisco.com>
2017-10-04 18:01:53 +00:00
Christian Brauner
8bd8018e3d
utils: fix lxc_popen()/lxc_pclose()
- rework and fix pipe fd leak

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-09-13 17:07:43 +02:00
Christian Brauner
d75c14e262
utils: add lxc_nic_exists()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-09-03 16:35:48 +02:00
Christian Brauner
1a0e70ace8
tree-wide: non-functional changes
- replace all "//" with "/* */"

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-08-24 12:45:47 +02:00
Christian Brauner
eda0afd4b4
utils: rework lxc_deslashify()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-08-02 01:37:02 +02:00
Christian Brauner
29a11a7f1b
utils: switch to has_fs_type()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-08-02 01:37:02 +02:00
Christian Brauner
a035c53af1
utils: add has_fs_type() + is_fs_type()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-08-02 01:37:02 +02:00
Christian Brauner
04ad7ffe2a
utils: move helpers from cgfsng.c to utils.{c,h}
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-07-26 14:57:35 +02:00
Christian Brauner
a394f952cd
conf: fix bionic builds
bionic seems to lack a definition of __S_ISTYPE().

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-06-05 23:44:22 +02:00
Serge Hallyn
a9849a0685 Merge pull request #1592 from brauner/2017-05-28/idmap_handling
idmap improvements
2017-06-01 17:29:55 -05:00
Christian Brauner
681188c14e
utils: add lxc_safe_ulong()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-05-30 18:43:07 +02:00
Christian Brauner
ea3a694fe1
utils: add run_command
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-05-30 09:11:47 +02:00
Christian Brauner
74251e49bb
utils: add lxc_unstack_mountpoint()
lxc_unstack_mountpoint() tries to clear all mountpoints from a given path.
It return the number of successful umounts on success and -errno on error.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-05-09 23:27:47 +02:00
Christian Brauner
943144d931
conf: non-functional changes
Closes #1475.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-04-29 00:05:29 +02:00
Christian Brauner
c6868a1f81
utils: use loop device helpers from LXD
Use the loop device helpers I wrote for LXD in LXC as well. They should be more
efficient.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-04-22 14:04:34 +02:00
Christian Brauner
df6a294548
conf: check for {filecaps,setuid} on new{g,u}idmap
The new{g,u}idmap binaries where a source of trouble for users when they lacked
sufficient privileges. This commit adds code to check for sufficient privilege.
It checks whether new{g,u}idmap is root owned and has the setuid bit set and if
it doesn't it checks whether new{g,u}idmap is root owned and has CAP_SETUID in
its CAP_PERMITTED and CAP_EFFECTIVE set.

Closes #296.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-04-16 00:59:46 +02:00
Christian Brauner
4bc3b75957
conf: lxc_map_ids() non-functional changes
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-04-15 13:30:00 +02:00
Christian Brauner
dbaf55a353
utils: add uid, gid, group convenience wrappers
This commit adds lxc_switch_uid_gid() which allows to switch the uid and gid of
a process via setuid() and setgid() and lxc_setgroups() which allows to set
groups via setgroups(). The main advantage is that they nicely log the switches
they perform.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-01-07 11:18:19 +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
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
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
b5f845e7e0
utils: add lxc_safe_int()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:41:05 +01:00
Christian Brauner
6bc2eafe90
utils: add lxc_safe_uint()
This function safely parses an unsigned integer. On success it returns 0 and
stores the unsigned integer in @converted. On error it returns a negative
errno.

Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-22 03:40:59 +01:00
Christian Brauner
a687256f1d
utils: add lxc_preserve_ns()
This allows to retrieve a file descriptor referring to a namespace.

Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-19 05:11:12 +01:00
Christian Brauner
000dfda7f3
utils: add lxc_append_string()
lxc_append_string() appends strings without separator. This is mostly useful
for reading in whole files line-by-line.

Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-11-09 19:27:58 +01:00
Christian Brauner
fa454c8e3b utils: make detect_ramfs_rootfs() return bool
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-09-27 22:48:14 +02:00
Christian Brauner
c56a9652d7
tools: lxc_deslashify() handle special cases
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
2016-09-26 19:41:34 +02:00
Tycho Andersen
aeb3682ff6 utils: add lxc_deslashify
Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
2016-09-16 15:17:03 -06:00
James Cowgill
f53b591652 utils: Add mips signalfd syscall numbers
Signed-off-by: James Cowgill <james410@cowgill.org.uk>
2016-08-18 16:49:05 +01:00
Christian Brauner
330ae3d350
lxccontainer: detect if we should send SIGRTMIN+3
This is required by systemd to cleanly shutdown. Other init systems should not
have SIGRTMIN+3 in the blocked signals set.

Signed-off-by: Christian Brauner <cbrauner@suse.de>
2016-07-19 16:20:02 +02:00
Christian Brauner
a1e5280d9e use smarter error handling for lxc_strmmap()
Additionally, fix the comment for lxc_strmmap() and cleanup #includes in
utils.{c,h}.

Signed-off-by: Christian Brauner <christian.brauner@mailbox.org>
2016-03-30 10:20:43 +02:00
Christian Brauner
25086a5fe4 better naming for mmap helpers
Rename lxc_mmap()/lxc_munmap() to lxc_strmmap()/lxc_strmunmap().

Signed-off-by: Christian Brauner <christian.brauner@mailbox.org>
2016-03-29 08:50:27 +02:00
Christian Brauner
1adbd02064 add funs to mmap() files to \0-terminated strings
In order to do this we make use of the MAP_FIXED flag of mmap(). MAP_FIXED
should be safe to use when it replaces an already existing mapping. To this
end, we establish an anonymous mapping that is one byte larger than the
underlying file. The pages handed to us are zero filled.  Now we establish a
fixed-address mapping starting at the address we received from our anonymous
mapping and replace all bytes excluding the additional \0-byte with the file.
This allows us to use normal string-handling function. The idea implemented
here is similar to how shared libraries are mapped.

Signed-off-by: Christian Brauner <christian.brauner@mailbox.org>
2016-03-29 08:48:27 +02:00
Aleksandr Mezin
f8dd027559 utils: split null_stdfds() to open_devnull() and set_stdfds()
Signed-off-by: Aleksandr Mezin <mezin.alexander@gmail.com>
2016-03-24 23:20:42 +06:00
Serge Hallyn
ccb4cabe02 cgfsng: next generation filesystem-backed cgroup implementation
This makes simplifying assumptions:  all usable cgroups must be
mounted under /sys/fs/cgroup/controller or /sys/fs/cgroup/contr1,contr2.

Currently this will only work with cgroup namespaces, because
lxc.mount.auto = cgroup is not implemented.  So cgfsng_ops_init()
returns NULL if cgroup namespaces are not enabled.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
2016-03-04 18:19:30 -08:00
Serge Hallyn
12983ba458 container start: unshare cgns when possible
We'll probably want to make this configurable with a
lxc.cgroupns = [1|0], but for now just always do it.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>

---
Changelog 20160104: only try to unshare if /proc/self/ns/cgroup exists.
2016-01-08 13:03:37 -08:00
Serge Hallyn
592fd47a62 CVE-2015-1335: Protect container mounts against symlinks
When a container starts up, lxc sets up the container's inital fstree
by doing a bunch of mounting, guided by the container configuration
file.  The container config is owned by the admin or user on the host,
so we do not try to guard against bad entries.  However, since the
mount target is in the container, it's possible that the container admin
could divert the mount with symbolic links.  This could bypass proper
container startup (i.e. confinement of a root-owned container by the
restrictive apparmor policy, by diverting the required write to
/proc/self/attr/current), or bypass the (path-based) apparmor policy
by diverting, say, /proc to /mnt in the container.

To prevent this,

1. do not allow mounts to paths containing symbolic links

2. do not allow bind mounts from relative paths containing symbolic
links.

Details:

Define safe_mount which ensures that the container has not inserted any
symbolic links into any mount targets for mounts to be done during
container setup.

The host's mount path may contain symbolic links.  As it is under the
control of the administrator, that's ok.  So safe_mount begins the check
for symbolic links after the rootfs->mount, by opening that directory.

It opens each directory along the path using openat() relative to the
parent directory using O_NOFOLLOW.  When the target is reached, it
mounts onto /proc/self/fd/<targetfd>.

Use safe_mount() in mount_entry(), when mounting container proc,
and when needed.  In particular, safe_mount() need not be used in
any case where:

1. the mount is done in the container's namespace
2. the mount is for the container's rootfs
3. the mount is relative to a tmpfs or proc/sysfs which we have
   just safe_mount()ed ourselves

Since we were using proc/net as a temporary placeholder for /proc/sys/net
during container startup, and proc/net is a symbolic link, use proc/tty
instead.

Update the lxc.container.conf manpage with details about the new
restrictions.

Finally, add a testcase to test some symbolic link possibilities.

Reported-by: Roman Fiedler
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2015-09-29 09:52:55 -04:00
Tycho Andersen
69aeabac1a uniformly nullify std fds
In various places throughout the code, we want to "nullify" the std fds,
opening them to /dev/null or zero or so. Instead, let's unify this code and do
it in such a way that Coverity (probably) won't complain.

v2: use /dev/null for stdin as well
v3: add a comment about use of C's short circuiting
v4: axe comment, check errors on dup2, s/quiet/need_null_stdfds

Reported-by: Coverity
Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
2015-06-10 23:04:51 -05:00
Tycho Andersen
5b72de5fd3 move utils.h #endif to end of file
Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
2015-06-10 23:04:47 -05:00
Serge Hallyn
ced03a017b attach: mount a sane prox for LSM setup
To set lsm labels, a namespace-local proc mount is needed.

If a container does not have a lxc.mount.auto = proc set, then
tasks in the container do not have a correct /proc mount until
init feels like doing the mount.  At startup we handlie this
by mounting a temporary /proc if needed.  We weren't doing this
at attach, though, so that

lxc-start -n $container
lxc-wait -t 5 -s RUNNING -n $container
lxc-attach -n $container -- uname -a

could in a racy way fail with something like

lxc-attach: lsm/apparmor.c: apparmor_process_label_set: 183 No such file or directory - failed to change apparmor profile to lxc-container-default

Thanks to Chris Townsend for finding this bug at
https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/1452451

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2015-05-26 13:06:59 -04:00
Serge Hallyn
4295c5de9e lxc-destroy: remove btrfs subvolumes
Doing this requires some btrfs functions from bdev to be used in
utils.c  Because utils.h is imported by lxc_init.c, I had to create
a new initutils.[ch] which are used by both lxc_init.c and utils.c
We could instead put the btrfs functions into utils.c, which would
be a shorter patch, but it really doesn't belong there.  So I went
the other way figuring there may be more such cases coming up of
fns in utils.c needing code from bdev.c which can't go into lxc_init.

Currently, if we detect a btrfs subvolume we just remove it.  The
st_dev on that dir is different, so we cannot detect if this is
bound in from another fs easily.  If we care, we should check
whether this is a mountpoint, this patch doesn't do that.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2015-05-26 11:14:22 -04:00
Tycho Andersen
0a4be28d8c set the monitor process title to something useful
Instead of having a parent process that's called whatever the caller of the
library is called, we instead set it to "[lxc monitor] <lxcpath> <container>"

Closes #180

v2: check for null in tok for loop, only truncate environment when necessary

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
2015-01-30 15:14:21 +01:00
Stéphane Graber
6010a41622 Fix lxc-create -h with absolute template path
Close #421

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
2015-01-28 12:47:12 +01:00
Serge Hallyn
e1daebd905 implement lxc.include for directories
If you have 'lxc.include = /some/dir' and /some/dir is a directory, then any
'*.conf" files under /some/dir will be read.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2015-01-16 18:01:58 -05:00
Dongsheng Yang
51d0854cd6 utils: move useful helper functions from lxccontainer to utils.
Function of enter_to_ns() is useful but currently is static for
lxccontainer.c.

This patch split it into two parts named as switch_to_newuser()
and switch_to_newnet() into utils.c.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
2014-10-15 12:02:34 +02:00
Tycho Andersen
735f2c6e50 Add support for checkpoint and restore via CRIU
This patch adds support for checkpointing and restoring containers via CRIU.
It adds two api calls, ->checkpoint and ->restore, which are wrappers around
the CRIU CLI. CRIU has an RPC API, but reasons for preferring exec() are
discussed in [1].

To checkpoint, users specify a directory to dump the container metadata (CRIU
dump files, plus some additional information about veth pairs and which
bridges they are attached to) into this directory. On restore, this
information is read out of the directory, a CRIU command line is constructed,
and CRIU is exec()d. CRIU uses the lxc-restore-net callback (which in turn
inspects the image directory with the NIC data) to properly restore the
network.

This will only work with the current git master of CRIU; anything as of
a152c843 should work. There is a known bug where containers which have been
restored cannot be checkpointed [2].

[1]: http://lists.openvz.org/pipermail/criu/2014-July/015117.html
[2]: http://lists.openvz.org/pipermail/criu/2014-August/015876.html

v2: fixed some problems with the s/int/bool return code form api function
v3: added a testcase, fixed up the man page synopsis
v4: fix a small typo in lxc-test-checkpoint-restore
v5: remove a reference to the old CRIU_PATH, and a bad error about the same

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2014-08-26 10:40:05 -04:00
Serge Hallyn
18aa217bb1 snapshots: move snapshot directory
Originally we kept snapshots under /var/lib/lxcsnaps.  If a
separate btrfs is mounted at /var/lib/lxc, then we can't
make btrfs snapshots under /var/lib/lxcsnaps.

This patch moves the default directory to /var/lib/lxc/c/snaps.
If /var/lib/lxcsnaps already exists, then we continue to use that.

add c->destroy_with_snapshots() and c->snapshot_destroy_all()
API methods.  c->snashot_destroy_all() can be triggered from
lxc-snapshot using '-d ALL'.  There is no command to call
c->destroy_with_snapshots(c) as of yet.

lxclock: use ".$lxcname" for container lock files
that way we can use /run/lock/lxc/$lxcpath/$lxcname/snaps as a
directory when locking snapshots without having to worry about
/run/lock//lxc/$lxcpath/$lxcname being a file.

destroy: split off a container_destroy
container_destroy() doesn't check for snapshots, so snapshot_rename can
use it.  api_destroy() now does check for snapshots (previously it only
checked for fs - i.e. overlayfs/aufs - snapshots).

Add destroy to the manpage, as it was previously undocumented.

Update snapshot testcase accordingly.

[ rebased in the face of commits 840f05df and 7e36f87e. ]

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: S.Çağlar Onur <caglar@10ur.org>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2014-06-18 16:28:39 -05:00
Serge Hallyn
9d9c111c61 execute: don't bind mount init.lxc.static if lxc-init is in the container
Move choose_init into utils.c so we can re-use it.  Make it and on_path
accept an optional rootfs argument to prepend to the paths when checking
whether the file exists.

Also add lxc.init.static to .gitignore

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2014-06-03 10:11:01 -04:00
Serge Hallyn
76a26f559f add support for nbd
backing stores supported by qemu-nbd can be attached to a nbd block
device using qemu-nbd.  This user-space process (pair) stays around for
the duration of the device attachment.  Obviously we want it to go away
when the container shuts down, but not before the filesystems have been
cleanly unmounted.

The device attachment is done from the task which will become the
container monitor before the container setup+init task is spawned.
That task starts in a new pid namespace to ensure that the qemu-nbd
process will be killed if need be.  It sets its parent death signal
to sighup, and, on receiving sighup, attempts to do a clean
qemu-device detach, then exits.  This should ensure that the
device is detached if the qemu monitor crashes or exits.

It may be worth adding a delay before the qemu-nbd is detached, but
my brief tests haven't seen any data corruption.

Only the parts required for running a nbd-backed container are
implemented here.  Create, destroy, and clone are not.  The first
use of this that I imagine is for people to use downloaded nbd-backed
images (like ubuntu cloud images, or anything previously used with
qemu).  I imagine people will want to create/clone/destroy out of
band using qemu-img, but if I'm wrong about that we can implement
the rest later.

Because attach_block_device() is done before the bdev is initialized,
and bdev_init needs to know the nbd index so that it can mount the
filesystem, we now need to pass the lxc_conf.

file_exists() is moved to utils.c so we can use it from bdev.c

The nbd attach/detach should lay the groundwork for trivial implementation
of qed and raw images.

changelog (may 12): fix idx check at detach
changelog (may 15): generalize qcow2 to nbd

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
2014-05-16 09:58:03 -04:00
S.Çağlar Onur
f1a4a029f6 use same ifndef/define format for all headers
Signed-off-by: S.Çağlar Onur <caglar@10ur.org>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2014-05-09 05:12:22 -05:00
Stéphane Graber
8afb3e612e Use on_path to find init.lxc
Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
2014-03-31 19:56:16 -04:00
Serge Hallyn
b7f954bbdd simpler shared rootfs handling
Only do the funky chroot_into_slave if / is in fact the rootfs.
Rootfs is a special blacklisted case for pivot_root.

If / is not rootfs but is shared, just mount / rslave.  We're
already in our own namespace.

This appears to solve the extra /proc/$$/mount entries in
containers and the host directories in lxc-attach which have
been plagueing at least fedora and arch.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2014-03-03 11:04:29 -05:00
Stéphane Graber
0e6e3a4108 Fix unprivileged containers started by root
This change makes it possible to create unprivileged containers as root.
They will be stored in the usual system wide location, use the usual
system wide cache but will be running using a uid/gid map.

This also updates lxc_usernsexec to use the same function as the rest of
LXC, centralizing all the userns switch in a single function.

That function now detects the presence of newuidmap and newgidmap on the
system, if they are present, they will be used for containers created as
either user or root. If they're not and the user isn't root, an error is
shown. If they're not and the user is root, LXC will directly set the
uid_map and gid_map values.

All that should allow for a consistent experience as well as supporting
distributions that don't yet ship newuidmap/newgidmap.

To make things simpler in the future, an helper function "on_path" is
also introduced and used to detect the presence of newuidmap and
newgidmap.

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
2014-02-27 17:47:31 -05:00
Serge Hallyn
2c6f3fc932 always check whether rootfs is shared
(this expands on Dwight's recent patch, commit c597baa8f9)

After unshare(CLONE_NEWNS) and before doing any mounting, always
check whether rootfs is shared.  Otherwise template runs or clone
scripts can bleed mount activity to the host.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2014-02-25 11:16:08 -05:00
Stéphane Graber
fd8c277753 coverity: rundir: Fix memory leaks
Since we're no longer always returning a getenv result or some defined
string, the callers should cleanup the variable after use.

As a result, change from const char* to char*, add the needed free()
everywhere and use strdup() on strings coming from getenv.

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
2014-02-19 10:38:38 -05:00
S.Çağlar Onur
93c379f09c move fnv_64a_buf to utils.c and remove mutliple copies (v2)
Signed-off-by: S.Çağlar Onur <caglar@10ur.org>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
2014-02-14 10:10:17 -05:00
Stéphane Graber
e1579aaff6 Revert "move fnv_64a_buf to utils.c and remove mutliple copies"
Fix clang breakage.

This reverts commit 7cd32872b6.
2014-02-13 17:34:26 -05:00