Skip to content

Undefined runAsNonRoot doesn't trigger violation #658

@wedge-jarrad

Description

@wedge-jarrad

The Pod Security Standard Restricted policy requires that runAsNonRoot is set to true. https://kubernetes.io/docs/concepts/security/pod-security-standards/#restricted

Containers must be required to run as non-root users.

Restricted Fields

  • spec.securityContext.runAsNonRoot
  • spec.containers[*].securityContext.runAsNonRoot
  • spec.initContainers[*].securityContext.runAsNonRoot
  • spec.ephemeralContainers[*].securityContext.runAsNonRoot

Allowed Values

  • true

The container fields may be undefined/nil if the pod-level spec.securityContext.runAsNonRoot is set to true.

Therefore, a manifest such as this should violate the policy.

apiVersion: v1
kind: Pod
metadata:
  name: no-nonroot
  labels:
    app: nginx-users
spec:
  securityContext:
    supplementalGroups:
      - 199
    fsGroup: 199
  containers:
    - name: nginx
      image: nginx
      securityContext:
        runAsUser: 1
        runAsGroup: 199
        allowPrivilegeEscalation: false
        seccompProfile:
          type: RuntimeDefault
        capabilities:
          drop:
            - ALL

The above manifest is blocked by the Pod Security admission controller in restricted mode.

$ k apply -n yes-pss -f reject-unset-runasnonroot.yaml
Error from server (Forbidden): error when creating "reject-unset-runasnonroot.yaml": pods "no-nonroot" is forbidden: violates PodSecurity "restricted:v1.33": runAsNonRoot != true (pod or container "nginx" must set securityContext.runAsNonRoot=true)

But OPA does not show a violation:

$ gator verify -v suite.yaml
=== RUN   allowed-users
    === RUN   reject-unset-runasnonroot
    --- FAIL: reject-unset-runasnonroot (0.007s)
        unexpected number of violations: got 0 violations but want exactly 1: got messages []
--- FAIL: allowed-users (0.015s)
FAIL    suite.yaml      0.015s
FAIL

Error: FAIL

suite.yaml:

kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
tests:
- name: allowed-users
  template: constrainttemplate.yaml
  constraint: constraint.yaml
  cases:
  - name: reject-unset-runasnonroot
    object: reject-unset-runasnonroot.yaml
    assertions:
    - violations: 1

constraint.yaml

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPAllowedUsers
metadata:
  name: psp-pods-allowed-user-ranges
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    runAsUser:
      rule: MustRunAsNonRoot # MustRunAsNonRoot # RunAsAny
    runAsGroup:
      rule: MustRunAs # MayRunAs # RunAsAny
      ranges:
        - min: 1
          max: 65535
    supplementalGroups:
      rule: MayRunAs # MayRunAs # RunAsAny
      ranges:
        - min: 1
          max: 65535
    fsGroup:
      rule: MayRunAs # MayRunAs # RunAsAny
      ranges:
        - min: 1
          max: 65535

The template is https://github.com/open-policy-agent/gatekeeper-library/blob/master/library/pod-security-policy/users/template.yaml as it was in version 1.0.2.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions