← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Adds a Capability Outside the Baseline Set

container-capabilities-add

severityModerate resourceContainers productKubernetes bundles2

Linux capabilities beyond the baseline set, such as SYS_ADMIN, SYS_MODULE, SYS_PTRACE or NET_RAW, hand a container kernel level privileges it can use to mount filesystems, load modules and break out onto the node.

Rejects unless

(has(object.spec.os) &&
 has(object.spec.os.name) &&
 object.spec.os.name.lowerAscii() == "windows") ||
object.spec.containers.all(c,
  !has(c.securityContext) ||
  !has(c.securityContext.capabilities) ||
  !has(c.securityContext.capabilities.add) ||
  c.securityContext.capabilities.add.all(cap,
    variables.allowedCaps.exists(a, a == cap)
  )
)

Only the baseline capabilities may be added: AUDIT_WRITE, CHOWN, DAC_OVERRIDE, FOWNER, FSETID, KILL, MKNOD, NET_BIND_SERVICE, SETFCAP, SETGID, SETPCAP, SETUID, SYS_CHROOT.

Variables

allowedCaps

["AUDIT_WRITE","CHOWN","DAC_OVERRIDE","FOWNER","FSETID","KILL","MKNOD","NET_BIND_SERVICE","SETFCAP","SETGID","SETPCAP","SETUID","SYS_CHROOT"]

Remediation

Drop the non-baseline entries from the added capability list and keep only baseline names such as CHOWN, KILL, SETGID, SETUID and NET_BIND_SERVICE. Field: spec.containers[*].securityContext.capabilities.add.

Applies to

  • pods · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: container-capabilities-add
  annotations:
    kubeapt.io/uuid: "57487b22-099b-46ad-b669-844e1e392a65"
    security.kubeapt.io/displayName: "Adds a Capability Outside the Baseline Set"
    security.kubeapt.io/description: "Linux capabilities beyond the baseline set, such as SYS_ADMIN, SYS_MODULE, SYS_PTRACE or NET_RAW, hand a container kernel level privileges it can use to mount filesystems, load modules and break out onto the node."
    security.kubeapt.io/resource: "Containers"
    security.kubeapt.io/severity: "Moderate"
    security.kubeapt.io/remediation: "Drop the non-baseline entries from the added capability list and keep only baseline names such as CHOWN, KILL, SETGID, SETUID and NET_BIND_SERVICE. Field: spec.containers[*].securityContext.capabilities.add."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - ''
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - pods
  variables:
  - name: allowedCaps
    expression: |
      ["AUDIT_WRITE","CHOWN","DAC_OVERRIDE","FOWNER","FSETID","KILL","MKNOD","NET_BIND_SERVICE","SETFCAP","SETGID","SETPCAP","SETUID","SYS_CHROOT"]
  validations:
  - expression: |
      (has(object.spec.os) &&
       has(object.spec.os.name) &&
       object.spec.os.name.lowerAscii() == "windows") ||
      object.spec.containers.all(c,
        !has(c.securityContext) ||
        !has(c.securityContext.capabilities) ||
        !has(c.securityContext.capabilities.add) ||
        c.securityContext.capabilities.add.all(cap,
          variables.allowedCaps.exists(a, a == cap)
        )
      )
    message: |
      Only the baseline capabilities may be added: AUDIT_WRITE, CHOWN, DAC_OVERRIDE, FOWNER, FSETID, KILL, MKNOD, NET_BIND_SERVICE, SETFCAP, SETGID, SETPCAP, SETUID, SYS_CHROOT.

Save it as container-capabilities-add.yaml — the commands below assume that name.

Validate with kubeapt

Check a workload against this one policy, before anything is installed on a cluster.

$ kubeapt validate -p ./container-capabilities-add.yaml -r ./workload.yaml

Or without the file, from the published policy set — -r takes a directory too:

$ kubeapt policies download && kubeapt validate --policy-name container-capabilities-add -r ./manifests

Or against every namespace in the cluster your kubeconfig points at:

$ kubeapt validate --policy-name container-capabilities-add -A