Understanding Container Security
Page last updated:
This topic describes how Cloud Foundry (CF) secures the containers that host application instances on Linux. For an overview of other CF security features, see the Understanding Cloud Foundry Security topic.
- Container Mechanics provides an overview of container isolation.
- Container Networking provides an overview of container networking and describes how CF administrators customize container network traffic rules for their deployment.
- Container Security describes how CF secures containers by running application instances in unprivileged containers and by hardening them.
Each instance of an application deployed to CF runs within its own self-contained environment, a Garden container. This container isolates processes, memory, and the filesystem using operating system features and the characteristics of the virtual and physical infrastructure where CF is deployed.
CF achieves container isolation by namespacing kernel resources that would otherwise be shared. The intended level of isolation is set to prevent multiple containers that are present on the same host from detecting each other. Every container includes a private root filesystem, which includes a Process ID (PID), namespace, network namespace, and mount namespace.
CF creates the container filesystem by stacking a read-only base filesystem and a container-specific read-write filesystem, commonly known as an overlay filesystem. The read-only filesystem contains the minimal set of operating system packages and Garden-specific modifications common to all containers. Containers can share the same read-only base filesystem because all writes are applied to the read-write filesystem. The read-write filesystem is unique to each container and is created by formatting a large sparse file of a fixed size. This fixed size prevents the read-write filesystem from overflowing into unallocated space.
Resource control is managed using Linux control groups (cgroups). Associating each container with its own cgroup or job object limits the amount of memory that the container may use. Linux cgroups also require the container to use a fair share of CPU compared to the relative CPU share of other containers.
Note: CF does not support a RedHat Enterprise Linux OS stemcell. This is due to an inherent security issue with the way RedHat handles user namespacing and container isolation.
To isolate applications and control outgoing traffic, each Garden container uses a dedicated virtual network interface that consists of a pair of Ethernet addresses: one visible to the application instance running in the container, and the other visible to the host VM’s root namespace. The pair is configured to use IP addresses in a small and static subnet. Applications are typically allowed to invoke other applications in CF only by leaving the system and re-entering through the load balancer positioned in front of the CF routers.
When an application instance starts, the Diego cell on the host VM allocates an IP address and assigns an arbitrary port to the application container. The application uses the
PORT environment variable provided in the container environment to determine which port to listen on. Because the host assigns a random value to the
PORT environment variable, the value is generally unique for each application instance.
A host VM has a single IP address. If you configure the deployment with the cluster on a VLAN, as recommended, then all traffic goes through the following levels of network address translation, as shown in the diagram below.
Inbound requests flow from the load balancer through the router to the host cell, then into the application container. The router determines which application instance receives each request.
Outbound traffic flows from the application container to the cell, then to the gateway on the cell’s virtual network interface. Depending on your IaaS, this gateway may be a NAT to external networks.
Administrators can configure rules to govern container network traffic. These rules can prevent system access from external networks and between internal components, and restrict applications from establishing connections over the virtual network interface.
Application Security Groups (ASGs) apply network traffic rules at the container level. For information about creating and configuring ASGs, see the Application Security Groups topic.
CF secures containers through the following measures:
- Running application instances in unprivileged containers by default
- Hardening containers by limiting functionality and access rights
- Allowing administrators to configure ASGs to block outbound connections from application containers. For information about creating and configuring ASGs, see the Application Security Groups topic.
Garden has two container types: unprivileged and privileged. Currently, CF runs all application instances and staging tasks in unprivileged containers by default. This measure increases security by eliminating the threat of root escalation inside the container.
Formerly, CF ran applications based on Docker images in unprivileged containers, and buildpack-based applications and staging tasks in privileged containers. CF ran applications based on Docker images in unprivileged containers because Docker images come with their own root filesystem and user, so CF could not trust the root filesystem and could not assume that the container user process would never be root. CF ran build-pack based applications and staging tasks in privileged containers because they used the cflinuxfs2 root filesystem and all processes were run as the unprivileged user
CF mitigates against container breakout and denial of service attacks in the following ways:
- CF uses the full set of Linux namespaces (IPC, Network, Mount, PID, User, UTS) to provide isolation between containers running on the same host. The User namespace is not used for privileged containers.
- In unprivileged containers, CF maps UID/GID 0 (root) inside the container user namespace to a different UID/GID on the host to prevent an application from inheriting UID/GID 0 on the host if it breaks out of the container.
- CF uses the same UID/GID for all containers.
- CF maps all UIDs except UID 0 to themselves. CF maps UID 0 inside the container namespace to
MAX_UID-1outside of the container namespace.
- Container Root does not grant Host Root permissions.
- CF mounts
/sysas read-only inside containers.
- CF disallows
dmesgaccess for unprivileged users and all users in unprivileged containers.
- CF uses
chrootwhen importing docker images from docker registries.
- CF establishes a container-specific overlay filesystem mount. CF uses
pivot_rootto move the root filesystem into this overlay, in order to isolate the container from the host system’s filesystem.
- CF does not call any binary or script inside the container filesystem, in order to eliminate any dependencies on scripts and binaries inside the root filesystem.
- CF avoids side-loading binaries in the container through bind mounts or other methods. Instead, it re-executes the same binary by reading it from
/proc/self/exewhenever it needs to run a binary in a container.
- CF establishes a virtual Ethernet pair for each container for network traffic. See the Container Network Traffic section above for more information. The virtual Ethernet pair has the following features:
- One interface in the pair is inside the container’s network namespace, and is the only non-loopback interface accessible inside the container.
- The other interface remains in the host network namespace and is bridged to the container-side interface.
- Egress whitelist rules are applied to these interfaces according to ASGs configured by the administrator.
- First-packet logging rules may also be enabled on TCP whitelist rules.
- DNAT rules are established on the host to enable traffic ingress from the host interface to whitelisted ports on the container-side interface.
- CF applies disk quotas by confining container-specific filesystem layers to loop devices with the specified disk-quota capacity.
- CF applies a total memory usage quota through the memory cgroup and destroys the container if the memory usage exceeds the quota.
- CF applies a fair-use limit to CPU usage for processes inside the container through the
- CF limits access to devices using cgroups but explicitly whitelists the following safe device nodes:
- CF drops the following Linux capabilities for all container processes. Every dropped capability limits what actions the root user can perform.
CAP_SYS_ADMIN(for unprivileged containers)