2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
|
|
|
title = "Docker security"
|
|
|
|
description = "Review of the Docker Daemon attack surface"
|
|
|
|
keywords = ["Docker, Docker documentation, security"]
|
|
|
|
[menu.main]
|
|
|
|
parent = "smn_administrate"
|
|
|
|
weight = 2
|
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
# Docker security
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
There are three major areas to consider when reviewing Docker security:
|
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
- the intrinsic security of the kernel and its support for
|
2014-04-23 16:48:28 -04:00
|
|
|
namespaces and cgroups;
|
|
|
|
- the attack surface of the Docker daemon itself;
|
2014-12-30 14:34:35 -05:00
|
|
|
- loopholes in the container configuration profile, either by default,
|
|
|
|
or when customized by users.
|
2014-04-23 16:48:28 -04:00
|
|
|
- the "hardening" security features of the kernel and how they
|
|
|
|
interact with containers.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
## Kernel namespaces
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
Docker containers are very similar to LXC containers, and they have
|
2015-05-22 19:38:55 -04:00
|
|
|
similar security features. When you start a container with
|
|
|
|
`docker run`, behind the scenes Docker creates a set of namespaces and control
|
2014-06-19 13:07:39 -04:00
|
|
|
groups for the container.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
**Namespaces provide the first and most straightforward form of
|
|
|
|
isolation**: processes running within a container cannot see, and even
|
|
|
|
less affect, processes running in another container, or in the host
|
|
|
|
system.
|
|
|
|
|
|
|
|
**Each container also gets its own network stack**, meaning that a
|
2014-12-30 14:34:35 -05:00
|
|
|
container doesn't get privileged access to the sockets or interfaces
|
2014-04-15 20:53:12 -04:00
|
|
|
of another container. Of course, if the host system is setup
|
|
|
|
accordingly, containers can interact with each other through their
|
|
|
|
respective network interfaces — just like they can interact with
|
|
|
|
external hosts. When you specify public ports for your containers or use
|
2015-09-30 16:11:36 -04:00
|
|
|
[*links*](../userguide/networking/default_network/dockerlinks.md)
|
2014-04-15 20:53:12 -04:00
|
|
|
then IP traffic is allowed between containers. They can ping each other,
|
|
|
|
send/receive UDP packets, and establish TCP connections, but that can be
|
|
|
|
restricted if necessary. From a network architecture point of view, all
|
|
|
|
containers on a given Docker host are sitting on bridge interfaces. This
|
|
|
|
means that they are just like physical machines connected through a
|
|
|
|
common Ethernet switch; no more, no less.
|
|
|
|
|
|
|
|
How mature is the code providing kernel namespaces and private
|
|
|
|
networking? Kernel namespaces were introduced [between kernel version
|
|
|
|
2.6.15 and
|
|
|
|
2.6.26](http://lxc.sourceforge.net/index.php/about/kernel-namespaces/).
|
2015-09-09 08:00:23 -04:00
|
|
|
This means that since July 2008 (date of the 2.6.26 release, now 7 years
|
2014-04-15 20:53:12 -04:00
|
|
|
ago), namespace code has been exercised and scrutinized on a large
|
|
|
|
number of production systems. And there is more: the design and
|
|
|
|
inspiration for the namespaces code are even older. Namespaces are
|
2014-04-23 16:48:28 -04:00
|
|
|
actually an effort to reimplement the features of [OpenVZ](
|
2014-06-19 13:07:39 -04:00
|
|
|
http://en.wikipedia.org/wiki/OpenVZ) in such a way that they could be
|
|
|
|
merged within the mainstream kernel. And OpenVZ was initially released
|
|
|
|
in 2005, so both the design and the implementation are pretty mature.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
## Control groups
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
Control Groups are another key component of Linux Containers. They
|
|
|
|
implement resource accounting and limiting. They provide many
|
|
|
|
useful metrics, but they also help ensure that each container gets
|
2014-04-15 20:53:12 -04:00
|
|
|
its fair share of memory, CPU, disk I/O; and, more importantly, that a
|
|
|
|
single container cannot bring the system down by exhausting one of those
|
|
|
|
resources.
|
|
|
|
|
|
|
|
So while they do not play a role in preventing one container from
|
|
|
|
accessing or affecting the data and processes of another container, they
|
|
|
|
are essential to fend off some denial-of-service attacks. They are
|
|
|
|
particularly important on multi-tenant platforms, like public and
|
|
|
|
private PaaS, to guarantee a consistent uptime (and performance) even
|
|
|
|
when some applications start to misbehave.
|
|
|
|
|
|
|
|
Control Groups have been around for a while as well: the code was
|
|
|
|
started in 2006, and initially merged in kernel 2.6.24.
|
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
## Docker daemon attack surface
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Running containers (and applications) with Docker implies running the
|
2014-06-19 13:07:39 -04:00
|
|
|
Docker daemon. This daemon currently requires `root` privileges, and you
|
2014-04-15 20:53:12 -04:00
|
|
|
should therefore be aware of some important details.
|
|
|
|
|
|
|
|
First of all, **only trusted users should be allowed to control your
|
|
|
|
Docker daemon**. This is a direct consequence of some powerful Docker
|
|
|
|
features. Specifically, Docker allows you to share a directory between
|
|
|
|
the Docker host and a guest container; and it allows you to do so
|
|
|
|
without limiting the access rights of the container. This means that you
|
2014-04-23 16:48:28 -04:00
|
|
|
can start a container where the `/host` directory will be the `/` directory
|
|
|
|
on your host; and the container will be able to alter your host filesystem
|
2014-12-30 14:34:35 -05:00
|
|
|
without any restriction. This is similar to how virtualization systems
|
|
|
|
allow filesystem resource sharing. Nothing prevents you from sharing your
|
|
|
|
root filesystem (or even your root block device) with a virtual machine.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-06-29 21:31:15 -04:00
|
|
|
This has a strong security implication: for example, if you instrument Docker
|
|
|
|
from a web server to provision containers through an API, you should be
|
2014-04-15 20:53:12 -04:00
|
|
|
even more careful than usual with parameter checking, to make sure that
|
|
|
|
a malicious user cannot pass crafted parameters causing Docker to create
|
|
|
|
arbitrary containers.
|
|
|
|
|
|
|
|
For this reason, the REST API endpoint (used by the Docker CLI to
|
|
|
|
communicate with the Docker daemon) changed in Docker 0.5.2, and now
|
|
|
|
uses a UNIX socket instead of a TCP socket bound on 127.0.0.1 (the
|
|
|
|
latter being prone to cross-site-scripting attacks if you happen to run
|
|
|
|
Docker directly on your local machine, outside of a VM). You can then
|
|
|
|
use traditional UNIX permission checks to limit access to the control
|
|
|
|
socket.
|
|
|
|
|
2015-05-22 19:38:55 -04:00
|
|
|
You can also expose the REST API over HTTP if you explicitly decide to do so.
|
2014-06-14 17:13:55 -04:00
|
|
|
However, if you do that, being aware of the above mentioned security
|
2014-04-15 20:53:12 -04:00
|
|
|
implication, you should ensure that it will be reachable only from a
|
2014-06-29 21:31:15 -04:00
|
|
|
trusted network or VPN; or protected with e.g., `stunnel` and client SSL
|
2014-06-19 13:07:39 -04:00
|
|
|
certificates. You can also secure them with [HTTPS and
|
2015-10-09 19:50:41 -04:00
|
|
|
certificates](../articles/https/).
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
The daemon is also potentially vulnerable to other inputs, such as image
|
|
|
|
loading from either disk with 'docker load', or from the network with
|
|
|
|
'docker pull'. This has been a focus of improvement in the community,
|
|
|
|
especially for 'pull' security. While these overlap, it should be noted
|
|
|
|
that 'docker load' is a mechanism for backup and restore and is not
|
|
|
|
currently considered a secure mechanism for loading images. As of
|
|
|
|
Docker 1.3.2, images are now extracted in a chrooted subprocess on
|
|
|
|
Linux/Unix platforms, being the first-step in a wider effort toward
|
|
|
|
privilege separation.
|
|
|
|
|
|
|
|
Eventually, it is expected that the Docker daemon will run restricted
|
|
|
|
privileges, delegating operations well-audited sub-processes,
|
2015-09-30 16:11:36 -04:00
|
|
|
each with its own (very limited) scope of Linux capabilities,
|
2014-12-30 14:34:35 -05:00
|
|
|
virtual network setup, filesystem management, etc. That is, most likely,
|
|
|
|
pieces of the Docker engine itself will run inside of containers.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Finally, if you run Docker on a server, it is recommended to run
|
|
|
|
exclusively Docker in the server, and move all other services within
|
|
|
|
containers controlled by Docker. Of course, it is fine to keep your
|
|
|
|
favorite admin tools (probably at least an SSH server), as well as
|
2014-06-29 21:31:15 -04:00
|
|
|
existing monitoring/supervision processes (e.g., NRPE, collectd, etc).
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
## Linux kernel capabilities
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
By default, Docker starts containers with a restricted set of
|
2014-04-15 20:53:12 -04:00
|
|
|
capabilities. What does that mean?
|
|
|
|
|
|
|
|
Capabilities turn the binary "root/non-root" dichotomy into a
|
|
|
|
fine-grained access control system. Processes (like web servers) that
|
|
|
|
just need to bind on a port below 1024 do not have to run as root: they
|
2014-04-23 16:48:28 -04:00
|
|
|
can just be granted the `net_bind_service` capability instead. And there
|
|
|
|
are many other capabilities, for almost all the specific areas where root
|
|
|
|
privileges are usually needed.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
This means a lot for container security; let's see why!
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Your average server (bare metal or virtual machine) needs to run a bunch
|
|
|
|
of processes as root. Those typically include SSH, cron, syslogd;
|
2014-06-29 21:31:15 -04:00
|
|
|
hardware management tools (e.g., load modules), network configuration
|
|
|
|
tools (e.g., to handle DHCP, WPA, or VPNs), and much more. A container is
|
2014-04-15 20:53:12 -04:00
|
|
|
very different, because almost all of those tasks are handled by the
|
|
|
|
infrastructure around the container:
|
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
- SSH access will typically be managed by a single server running on
|
2014-04-23 16:48:28 -04:00
|
|
|
the Docker host;
|
|
|
|
- `cron`, when necessary, should run as a user
|
|
|
|
process, dedicated and tailored for the app that needs its
|
|
|
|
scheduling service, rather than as a platform-wide facility;
|
|
|
|
- log management will also typically be handed to Docker, or by
|
|
|
|
third-party services like Loggly or Splunk;
|
|
|
|
- hardware management is irrelevant, meaning that you never need to
|
|
|
|
run `udevd` or equivalent daemons within
|
|
|
|
containers;
|
|
|
|
- network management happens outside of the containers, enforcing
|
|
|
|
separation of concerns as much as possible, meaning that a container
|
|
|
|
should never need to perform `ifconfig`,
|
|
|
|
`route`, or ip commands (except when a container
|
|
|
|
is specifically engineered to behave like a router or firewall, of
|
|
|
|
course).
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
This means that in most cases, containers will not need "real" root
|
|
|
|
privileges *at all*. And therefore, containers can run with a reduced
|
|
|
|
capability set; meaning that "root" within a container has much less
|
|
|
|
privileges than the real "root". For instance, it is possible to:
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
- deny all "mount" operations;
|
|
|
|
- deny access to raw sockets (to prevent packet spoofing);
|
|
|
|
- deny access to some filesystem operations, like creating new device
|
|
|
|
nodes, changing the owner of files, or altering attributes (including
|
|
|
|
the immutable flag);
|
|
|
|
- deny module loading;
|
|
|
|
- and many others.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
This means that even if an intruder manages to escalate to root within a
|
|
|
|
container, it will be much harder to do serious damage, or to escalate
|
|
|
|
to the host.
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
This won't affect regular web apps; but malicious users will find that
|
2014-06-19 13:07:39 -04:00
|
|
|
the arsenal at their disposal has shrunk considerably! By default Docker
|
|
|
|
drops all capabilities except [those
|
2015-11-12 13:15:49 -05:00
|
|
|
needed](https://github.com/docker/docker/blob/87de5fdd5972343a11847922e0f41d9898b5cff7/daemon/execdriver/native/template/default_template_linux.go#L16-L29),
|
2014-06-19 13:07:39 -04:00
|
|
|
a whitelist instead of a blacklist approach. You can see a full list of
|
|
|
|
available capabilities in [Linux
|
2014-04-15 20:53:12 -04:00
|
|
|
manpages](http://man7.org/linux/man-pages/man7/capabilities.7.html).
|
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
One primary risk with running Docker containers is that the default set
|
|
|
|
of capabilities and mounts given to a container may provide incomplete
|
|
|
|
isolation, either independently, or when used in combination with
|
|
|
|
kernel vulnerabilities.
|
|
|
|
|
|
|
|
Docker supports the addition and removal of capabilities, allowing use
|
|
|
|
of a non-default profile. This may make Docker more secure through
|
|
|
|
capability removal, or less secure through the addition of capabilities.
|
|
|
|
The best practice for users would be to remove all capabilities except
|
|
|
|
those explicitly required for their processes.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
## Other kernel security features
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Capabilities are just one of the many security features provided by
|
|
|
|
modern Linux kernels. It is also possible to leverage existing,
|
|
|
|
well-known systems like TOMOYO, AppArmor, SELinux, GRSEC, etc. with
|
|
|
|
Docker.
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
While Docker currently only enables capabilities, it doesn't interfere
|
2014-04-15 20:53:12 -04:00
|
|
|
with the other systems. This means that there are many different ways to
|
|
|
|
harden a Docker host. Here are a few examples.
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
- You can run a kernel with GRSEC and PAX. This will add many safety
|
|
|
|
checks, both at compile-time and run-time; it will also defeat many
|
2014-06-19 13:07:39 -04:00
|
|
|
exploits, thanks to techniques like address randomization. It doesn't
|
|
|
|
require Docker-specific configuration, since those security features
|
2014-12-30 14:34:35 -05:00
|
|
|
apply system-wide, independent of containers.
|
2014-06-19 13:07:39 -04:00
|
|
|
- If your distribution comes with security model templates for
|
|
|
|
Docker containers, you can use them out of the box. For instance, we
|
|
|
|
ship a template that works with AppArmor and Red Hat comes with SELinux
|
|
|
|
policies for Docker. These templates provide an extra safety net (even
|
|
|
|
though it overlaps greatly with capabilities).
|
2014-04-23 16:48:28 -04:00
|
|
|
- You can define your own policies using your favorite access control
|
2014-06-19 13:07:39 -04:00
|
|
|
mechanism.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Just like there are many third-party tools to augment Docker containers
|
2014-06-29 21:31:15 -04:00
|
|
|
with e.g., special network topologies or shared filesystems, you can
|
2014-04-15 20:53:12 -04:00
|
|
|
expect to see tools to harden existing Docker containers without
|
2014-04-23 16:48:28 -04:00
|
|
|
affecting Docker's core.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
Recent improvements in Linux namespaces will soon allow to run
|
|
|
|
full-featured containers without root privileges, thanks to the new user
|
|
|
|
namespace. This is covered in detail [here](
|
|
|
|
http://s3hh.wordpress.com/2013/07/19/creating-and-using-containers-without-privilege/).
|
|
|
|
Moreover, this will solve the problem caused by sharing filesystems
|
|
|
|
between host and guest, since the user namespace allows users within
|
|
|
|
containers (including the root user) to be mapped to other users in the
|
|
|
|
host system.
|
|
|
|
|
|
|
|
Today, Docker does not directly support user namespaces, but they
|
|
|
|
may still be utilized by Docker containers on supported kernels,
|
|
|
|
by directly using the clone syscall, or utilizing the 'unshare'
|
|
|
|
utility. Using this, some users may find it possible to drop
|
|
|
|
more capabilities from their process as user namespaces provide
|
2015-04-25 14:57:01 -04:00
|
|
|
an artificial capabilities set. Likewise, however, this artificial
|
2014-12-30 14:34:35 -05:00
|
|
|
capabilities set may require use of 'capsh' to restrict the
|
|
|
|
user-namespace capabilities set when using 'unshare'.
|
|
|
|
|
2015-05-22 19:38:55 -04:00
|
|
|
Eventually, it is expected that Docker will have direct, native support
|
2014-12-30 14:34:35 -05:00
|
|
|
for user-namespaces, simplifying the process of hardening containers.
|
|
|
|
|
2014-04-15 20:53:12 -04:00
|
|
|
## Conclusions
|
|
|
|
|
|
|
|
Docker containers are, by default, quite secure; especially if you take
|
|
|
|
care of running your processes inside the containers as non-privileged
|
2015-01-06 23:23:32 -05:00
|
|
|
users (i.e., non-`root`).
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-06-13 12:21:50 -04:00
|
|
|
You can add an extra layer of safety by enabling AppArmor, SELinux,
|
2014-04-15 20:53:12 -04:00
|
|
|
GRSEC, or your favorite hardening solution.
|
|
|
|
|
|
|
|
Last but not least, if you see interesting security features in other
|
2014-12-30 14:34:35 -05:00
|
|
|
containerization systems, these are simply kernels features that may
|
|
|
|
be implemented in Docker as well. We welcome users to submit issues,
|
|
|
|
pull requests, and communicate via the mailing list.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
References:
|
2015-07-29 15:36:04 -04:00
|
|
|
|
2014-12-30 14:34:35 -05:00
|
|
|
* [Docker Containers: How Secure Are They? (2013)](
|
2014-07-01 20:30:25 -04:00
|
|
|
http://blog.docker.com/2013/08/containers-docker-how-secure-are-they/).
|
2014-12-30 14:34:35 -05:00
|
|
|
* [On the Security of Containers (2014)](https://medium.com/@ewindisch/on-the-security-of-containers-2c60ffe25a9e).
|