Hardening LXC containers running systemd

August 22, 2017

A real container

A container. Photo by Mr. Rollers. CC BY-NC-ND 2.0

Hardening Linux Containers, and more especially LXC containers, is needed to prevent a malicious user to escape your container. However, even hardened, a container can't be considered totally safe today. You can consider this article as part of a defence in depth strategy. But before starting, we need to understand how containers work under the hood.

As said by Jessie Frazelle in her blog post Setting the Record Straight: containers vs. Zones vs. Jails vs. VMs, containers in Linux are not a top level design like Zone in Solaris and Jails in BSD.

A "container" is just a term people use to describe a combination of Linux namespaces and cgroups. Linux namespaces and cgroups ARE first class objects. NOT containers.

In this article, we'll discuss the different "primitives" exposed by the Linux kernel like namespaces, cgroups, Linux Security Modules, capabilities and seccomp. Our container tool like LXC or Docker, which is a user space binary, will interact with these primitives. We'll see that we can interact with them through the LXC configuration file to improve (or worsen) the security of our LXC container.

The challenge when it comes to hardening a LXC container, compared to other solutions, is that there is a great probability that you'll run systemd in your container. And systemd heavily uses the primitives quoted before. Especially, systemd rely on cgroups to handle its services. We can also mention that many systemd daemon will be provided with a configuration that need to interact with the capabilities.

If you feel a bit lost with all these terms, a good start is the reading of this whitepaper by the NCCGroup: Understanding and Hardening Linux Containers. This post is also inspired by the article written by Christian Seiler, LXC containers without CAP_SYS_ADMIN under Debian Jessie, but we'll see that, due to evolutions in the Linux kernel, the proposed configuration does not work anymore out of the box.

Creating a standard LXC container

A factory

A factory building containers ? Photo by Thomas Berg. CC BY-SA 2.0

Before starting, you'll need a very recent version of LXC, at least lxc-2.0.9 (not yet released as of this writing). Fortunately, you can compile it from its master branch. We'll see later why we need a such recent version. Here is a quick reminder on how to compile LXC:

git clone https://github.com/lxc/lxc
cd lxc
./autogen.sh
./configure
make -j8
sudo make install

Now let's create a basic container (we'll use Fedora but the instructions should work for every distributions):

sudo lxc-create -n harden -t fedora

As you'll need to debug the launch of your container, I can only recommend you this command line :

sudo lxc-start -n harden -lDEBUG -F

It will launch your container in foreground (so you'll be able to see systemd logs at boot) and it will log many useful informations in the /var/log/lxc/harden.log file.

Capabilities: split the root

Puzzle

The great puzzle of root. Photo by Kevin Dooley. CC BY 2.0.

Historically, there is a huge difference between the root user (with uid 0) which bypass any access control and the other users of the system which must pass every control. So, if you want to send an ICMP request via the ping command for example, you must run the command as root (with the magic of setuid to enable non privileged users to launch it). As the command is launched as root for everyone, ping can load a kernel module, change the time on your system, erase every files, etc. That's dangerous, particularly if someone find a vulnerability in your command and use it to do a privilege escalation.

A good idea would be to only allow the ping command to execute actions related to network as root, not everything. You can do that by using capabilities, by giving the CAP_NET_RAW capability to your ping command.

But capabilities, and more precisely capability bounding set, can also be used to reduce the capabilities that any process of your container can inquire. Indeed, if you allow a process in your container to load kernel modules, what prevent him to load a faulty module enabling him to escape the container ? So, one way to prevent this catastrophic scenario is to drop CAP_SYS_MODULE from the capability bounding set. When you use lxc.cap.keep and lxc.cap.drop, you're modifying the capability bounding set of your container. You can show your current capability bounding set with the following command:

capsh --print

One capability is a bit special, CAP_SYS_ADMIN, as it is sometimes considered as "the new root" because of its large and not strictly defined scope. This capability is very useful because it permits to mount filesystems from the container. Unfortunately, it also enables interaction with ioctl, IPC resources, namespaces, etc. So, we want to drop this capability. Can we just drop it ?

# /var/lib/harden/config
lxc.cap.drop = sys_admin

Now try to restart your container... we can't just drop the capability:

Failed to mount tmpfs at /dev/shm: Operation not permitted
Failed to mount tmpfs at /run: Operation not permitted
Failed to mount tmpfs at /sys/fs/cgroup: Operation not permitted
Failed to mount cgroup at /sys/fs/cgroup/systemd: No such file or directory
[!!!!!!] Failed to mount API filesystems, freezing.
Freezing execution.

It looks like the only solution is to manually mount these folders before systemd execution. The operation will be slightly different from what Christian Seiler wrote as our kernel supports the cgroup namespace. Indeed, the following directive will do nothing:

# /var/lib/harden/config
lxc.mount.entry = cgroup:mixed

Here is why in the code:

/*
 /src/lxc/cgroups/cgfsng.c
 /src/lxc/cgroups/cgfs.c
*/
static bool cgfsng_mount(void *hdata, const char *root, int type)
{
  /* some initializations */
  if (cgns_supported())
    return true;
  /* rest of the function */
}

Developpers put this condition as, with the cgroup namespace, we can safely mount the cgroup hierarchy like any other filesystem in our LXC configuration file:

# /var/lib/harden/config
lxc.mount.entry = tmpfs dev/shm tmpfs rw,nosuid,nodev,create=dir 0 0
lxc.mount.entry = tmpfs run tmpfs rw,nosuid,nodev,mode=755,create=dir 0 0
lxc.mount.entry = tmpfs run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k,create=dir 0 0
lxc.mount.entry = tmpfs run/user tmpfs rw,nosuid,nodev,mode=755,size=50m,create=dir 0 0
lxc.mount.entry = tmpfs sys/fs/cgroup tmpfs rw,nosuid,nodev,create=dir 0 0

But to mount our cgroup hierarchy (we only need one, for systemd), we need to create the mount point first... We can't put the following line:

# /var/lib/harden/config
lxc.mount.entry = cgroup sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,name=systemd 0 0

Instead, the only solution I found was to create a (simple) LXC mount hook:

#!/bin/bash
# /usr/local/bin/mount-cgroup on the host
mkdir $LXC_ROOTFS_MOUNT/sys/fs/cgroup/systemd
mount cgroup $LXC_ROOTFS_MOUNT/sys/fs/cgroup/systemd \
  -t cgroup \
  -o rw,nosuid,nodev,noexec,relatime,xattr,name=systemd

Now, we call this script from our configuration:

# /var/lib/harden/config
lxc.hook.mount = /usr/local/bin/mount-cgroup

And now, your container is working !

But instead of creating a capabilities blacklist, can we create a capabilities whitelist ?

lxc.cap.keep =
lxc.cap.keep = chown ipc_lock ipc_owner kill net_admin net_bind_service

You can find the whole capability list in the dedicated man page capabilities(7) and how to use them with LXC in the LXC man page lxc.container.conf(5).

cgroups: group your processes

Lions

A cgroup of lions with parents and children. Photo by Joel Herzog. Unsplash license.

Wikipedia proposes the following definition:

cgroups is a Linux kernel feature that limits, accounts for, and isolates the resource usage (CPU, memory, disk I/O, network, etc.) of a collection of processes.

It might not be totally clear at the first read, but cgroups are two differents things:

  1. A method to create groups of processus
  2. A method to apply limitation, accounting, etc. on these groups

If you want to read more on this, the article Control Groups vs. Control Groups by Lennart Poettering explains how systemd uses cgroups and why the distinction is crucial.

Namespaces: isolate your system resources

Michael Kerrisk wrote an interesting serie of articles about namespaces on LWN. I find its definition of namespaces particularly interesting:

The purpose of each namespace is to wrap a particular global system resource in an abstraction that makes it appear to the processes within the namespace that they have their own isolated instance of the global resource.

At first glance, namespaces handle could appear trivial in LXC: every available namespaces are used and that's all. The reality is more complex.

Seccomp: filter your syscalls

Barriers

The seccomp barrier protects the access to the kernel. Photo by Mike Wilson. Unsplash license.

LSM: mandatory access control

Top Secret

Back to the origin. Photo by Michelangelo Carrieri. CC BY-ND 2.0

Other

prlimit

/dev

The bridge

ethtable