Review added Linux capabilities in containers

Verify the need for added permissions and minimize the effective capability set.

Description

To determine a Linux container’s effective capabilities, check capabilities.add, drop and the runtime defaults together.

Potential impact

Unnecessary capabilities expose more system functions. Removing one the service needs can prevent startup.

Remediation

Remove unnecessary security_context.capabilities.add entries. Where compatible, start with drop = ["ALL"] and grant only what each workload needs. Enforce permitted settings through Pod Security Admission or a policy engine and check the running process’s actual permissions.

Examples

The example removes an explicit NET_BIND_SERVICE addition, which may be needed for low-numbered ports. The capability can remain in the runtime default set. With a maintained image, test startup, port binding and file access.

Before

hcl
resource "kubernetes_pod" "pod" {
  metadata {
    name = "terraform-example"
  }

  spec {
    container {
      image = "nginx:1.7.9"
      name  = "example"

      security_context {
        capabilities {
          add = ["NET_BIND_SERVICE"]
        }
      }
    }
  }
}

After

hcl
resource "kubernetes_pod" "pod" {
  metadata {
    name = "terraform-example"
  }

  spec {
    container {
      image = "nginx:1.7.9"
      name  = "example"
    }
  }
}

References