Review container root filesystem write protection

Keep the root filesystem read-only and provide separate volumes only for required writes.

Description

Set read_only_root_filesystem = true in the container’s security_context. Configure permissions separately on mounted volumes.

Potential impact

With a writable root filesystem, a process can change application files or settings within its permissions. Removing write access from a required path can prevent startup.

Remediation

Mount a volume only where the app needs to write, such as a cache, log or temporary directory. Restrict access to the emptyDir or PVC and test startup and normal operation.

Examples

For deployment, replace the example’s nginx:1.7.9 with a maintained image and provide writable volumes for the nginx cache and runtime directories it uses.

Before

hcl
resource "kubernetes_pod" "example" {
  metadata {
    name = "app"
  }

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

      security_context {
        read_only_root_filesystem = false
      }
    }
  }
}

After

hcl
resource "kubernetes_pod" "example" {
  metadata {
    name = "app"
  }

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

      security_context {
        read_only_root_filesystem  = true
        allow_privilege_escalation = false
      }
    }
  }
}

The second example makes the root filesystem read-only and also restricts process privilege escalation with allow_privilege_escalation = false.

References