Review Linux capability reduction for containers

Remove Linux capabilities the application does not need.

Description

When security_context.capabilities.drop is omitted, a Linux container can retain the runtime’s default capabilities and any explicit additions.

Potential impact

Extra capabilities expose more system functions. Dropping a capability the application relies on can prevent startup.

Remediation

Start application and init containers with drop = ["ALL"]. Use add only for capabilities the actual runtime user needs. Retain other security-context and host isolation controls.

Examples

The first excerpt adds NET_BIND_SERVICE. The second drops all capabilities and adds none back. Test startup, file access and port binding with a maintained deployment image.

Before

hcl
resource "kubernetes_pod" "example" {
  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" "example" {
  metadata {
    name = "terraform-example"
  }

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

      security_context {
        capabilities {
          drop = ["ALL"]
        }
      }
    }
  }
}

References