Container security
This topic tells you about how Cloud Foundry secures the containers that host app instances on Linux.
For an overview of other Cloud Foundry security features, see Cloud Foundry Security.
The sections in this topic provide the following information:
-
Container Mechanics provides an overview of container isolation.
-
Inbound and Outbound Traffic from Cloud Foundry provides an overview of container networking and describes how Cloud Foundry admins customize container network traffic rules for their deployment.
-
Container Security describes how Cloud Foundry secures containers by running app instances in unprivileged containers and by hardening them.
Container mechanics​
Each instance of an app deployed to Cloud Foundry 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 Cloud Foundry is deployed. For more information about Garden containers, see Garden.
Cloud Foundry 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.
Cloud Foundry creates container filesystems using the Garden Rootfs (GrootFS) tool. It stacks the following using OverlayFS:
-
A read-only base filesystem: This filesystem has 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 layer.
-
A container-specific read-write layer: This layer is unique to each container and its size is limited by XFS project quotas. The quotas prevent the read-write layer from overflowing into unallocated space.
For more information about GrootFS, see Garden RootFS (GrootFS) in Garden.
Resource control is managed using Linux control groups. Associating each container with its own cgroup or job object limits the amount of memory that the container might use. Linux cgroups also require the container to use a fair share of CPU compared to the relative CPU share of other containers.
Cloud Foundry 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.
CPU​
Each container is placed in its own cgroup. Cgroups make each container use a fair share of CPU relative to the other containers. This prevents oversubscription on the host VM where one or more containers hog the CPU and leave no computing resources to the others.
The way cgroups allocate CPU time is based on shares. CPU shares do not work as direct percentages of total CPU usage. Instead, a share is relative in a given time window to the shares held by the other containers. The total amount of CPU that can be overall divided among the cgroups is what is left by other processes that might run in the host VM.
Generally, cgroups offers two possibilities for limiting the CPU usage: CPU affinity and CPU bandwidth, the latter of which is used in Cloud Foundry.
-
CPU affinity consists of binding a cgroup to certain CPU cores. The actual amount of CPU cycles that can be consumed by the cgroup is thus limited to what is available on the bound CPU cores.
-
CPU bandwidth sets the weight of a cgroup with the process scheduler. The process scheduler divides the available CPU cycles among cgroups depending on the shares held by each cgroup, relative to the shares held by the others. For example, consider two cgroups, one holding two shares and one holding four. Assuming the process scheduler gets to administer 60 CPU cycles, the first cgroup with two shares will get one third of those available CPU cycles, as it holds one third of the overall shares. Similarly, the second cgroup will get 40 cycles, as it holds two thirds of the collective shares.
The calculation of the CPU usage based on the percentage of the total CPU power available is quite sophisticated and is performed regularly as the CPU demands of the various containers fluctuates. Specifically, the percentage of CPU cycles a cgroup gets can be calculated by dividing the cpu.shares it holds by the sum of the cpu.shares of all the cgroups that are currently doing CPU activity, as shown in the following calculation:
process_cpu_share_percent = cpu.shares / sum_cpu_shares * 100
In Cloud Foundry, cgroup shares range from 10 to 1024. The actual number of shares a cgroup gets can be read from the cpu.shares file of the cgroup configurations pseudo-file-system available in the container at /sys/fs/cgroup/cpu. The number of shares given to an apps cgroup depends on the amount of memory the app declares to need in the manifest.
CPU Shares Algorithm 1​
When the Diego containers.set_cpu_weight bosh property is set to false, then Cloud Foundry scales the number of allocated shares linearly with the amount of memory, until the upper limit of 1024 shares (by default).
In this case, there are a handful of variables that play into the CPU shares calculation.
| variable name | default value | configurable? | link to property |
|---|---|---|---|
| cc.cpu_weight_min_memory | 128 | yes | bosh property |
| cc.cpu_weight_max_memory | 8192 | yes | bosh property |
| app_memory_in_mb | 1024 | yes | this is set in the app manifest |
| base_weight | 8192 | no | n/a |
| diego.executor.container_max_cpu_shares | 1024 | yes | bosh property |
These variables are used in the following algorithm to determine the number of CPU shares for an app:
app_memory_for_calculation = min( max(cc.cpu_weight_min_memory, app_memory_in_mb), cc.cpu_weight_max_memory)
app_cpu_weight = int( (100 * app_memory_for_calculation) / base_weight)
process_cpu.shares = int( (diego.executor.container_max_cpu_shares * app_cpu_weight) / 100)
For example:
cc.cpu_weight_min_memory = 128
cc.cpu_weight_max_memory = 8192
app_memory_in_mb = 32
base_weight = 8192
diego.executor.container_max_cpu_shares = 1024
app_memory_for_calculation = min(max(cc.cpu_weight_min_memory, app_memory_in_mb)
app_memory_for_calculation = min(max(128, 32), 8192) = min(128, 8192) = 128
app_cpu_weight = int((100 * app_memory_for_calculation)/base_weight))
app_cpu_weight = int((100 * 128) / 8192) = int(12800 / 8192) = int(1.56) = 1
process_cpu.shares = int( (diego.executor.container_max_cpu_shares * app_cpu_weight) / 100)
process_cpu.shares = int( (1024 * 1) / 100) = int(10.24) = 10
CPU Shares Algorithm 2​
When the Diego containers.set_cpu_weight BOSH property is set to true, then Cloud Foundry scales the number of allocated shares linearly with the amount of memory
In this case, there are two variables that play into the CPU shares calculation.
| variable name | default value | configurable? | link to property |
|---|---|---|---|
| app_memory_in_mb | 1024 | yes | this is set in the app manifest |
| containers.proxy.additional_memory_allocation_mb | 32 | yes | bosh property |
In this case, the following algorithm is used to determine the number of CPU shares for an app:
process_cpu.shares = app_memory_in_mb + containers.proxy.additional_memory_allocation_mb
CPU Share Interaction​
The next example helps to illustrate this better. Consider three processes: P1, P2 and P3, which are assigned cpu.shares of 5, 20 and 30, respectively.
P1 is active, while P2 and P3 require no CPU. Hence, P1 might use the whole CPU. When P2 joins in and is doing some actual work, such as when a request comes in, the CPU share between P1 and P2 is calculated as follows:
- P1 -> 5 / (5+20) = 0.2 = 20%
- P2 -> 20 / (5+20) = 0.8 = 80%
- P3 (idle)
At some point, process P3 joins in. Then the distribution is recalculated again:
- P1 -> 5 / (5+20+30) = 0.0909 = ~9%
- P2 -> 20 / (5+20+30) = 0.363 = ~36%
- P3 -> 30 / (5+20+30) = 0.545 = ~55%
If P1 become idle, the following recalculation between P2 and P3 takes place:
- P1 (idle)
- P2 -> 20 / (20+30) = 0.4 = 40%
- P3 -> 30 / (20+30) = 0.6 = 60%
If P3 finishes or becomes idle, then P2 can consume all the CPU, as another recalculation is performed.
In summary, the cgroup shares are the minimum guaranteed CPU share that the process can get. This limitation becomes effective only when processes on the same host compete for resources.
Inbound and outbound traffic from Cloud Foundry​
Learn about container networking and how Cloud Foundry admins customize container network traffic rules for their deployment.
Networking overview​
A host VM has a single IP address. If you configure the deployment with the cluster on a VLAN, as Cloud Foundry recommends, then all traffic goes through the following levels of network address translation, as shown in the following diagram:
-
Inbound requests flow from the load balancer through the router to the host Diego Cell, then into the app container. The router determines which app instance receives each request.
-
Outbound traffic flows from the app container to the Diego Cell, then to the gateway on the Diego Cell's virtual network interface. Depending on your IaaS, this gateway might be a NAT to external networks.
The networking diagram shows the following:
-
Inbound requests go to Load Balancer
-
Outbound traffic comes from NAT
Cloud Foundry vLAN (on right side):
-
Load balancer goes to router and to the app container inside the Diego Cell
-
App container response goes to NAT
Network traffic rules​
Admins configure rules to govern container network traffic. This is how containers send traffic outside of Cloud Foundry and receive traffic from outside, the Internet. These rules can prevent system access from external networks and between internal components and determine if apps can establish connections over the virtual network interface.
Admins configure these rules at two levels:
-
Application Security Groups (ASGs) apply network traffic rules at the container level. For more information about ASGs, see [App Security Groups](../adminguide/app-sec-groups.html).
-
Container-to-container networking policies determine app-to-app communication. Within Cloud Foundry, apps can communicate directly with each other, but the containers are isolated from outside Cloud Foundry. For information about administering container-to-container network policies, see Configuring Container-to-Container Networking.