Review removal of NET_RAW from containers

Drop NET_RAW from containers that do not need raw sockets.

Description

NET_RAW lets a Linux container use raw and packet sockets. Check the effective runtime capabilities if the application does not need those sockets.

Potential impact

Retaining this capability lets a process create network packets outside the application’s intended use.

Remediation

Add NET_RAW to the container’s security_context.capabilities.drop and check add and privileged mode. Under the Restricted Pod Security Standard, drop ALL. The only capability you may add back is NET_BIND_SERVICE, if needed. Test user switching, port binding and file operations.

Examples

The first example leaves the capabilities block empty. The second drops only NET_RAW, so other default capabilities may remain. Use a maintained deployment image.

Before

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

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

      security_context {
        capabilities {}
      }
    }
  }
}

After

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

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

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

References