kyverno: [Bug] PolicyException causes Kyverno to skip entire workload, not only the excepted parts

Kyverno Version

1.12.4

Description

There are 2 generic PolicyExceptions deployed on the cluster that except istio containers:

Polex 1:

spec:
  exceptions:
  - policyName: psp-baseline
    ruleNames:
    - baseline
  match:
    any:
    - resources:
        kinds:
        - Pod
  podSecurity:
  - controlName: Capabilities
    images:
    - '*/istio/proxyv2*'
    restrictedField: spec.initContainers[*].securityContext.capabilities.add
    values:
    - NET_ADMIN
    - NET_RAW

Polex 2:

spec:
  exceptions:
  - policyName: psp-restricted-limited
    ruleNames:
    - restricted
  match:
    any:
    - resources:
        kinds:
        - Pod
        selector:
          matchLabels:
            service.istio.io/canonical-revision: latest
  podSecurity:
  - controlName: Running as Non-root
    images:
    - '*/istio/proxyv2*'
    restrictedField: spec.initContainers[*].securityContext.runAsNonRoot
    values:
    - "false"
  - controlName: Running as Non-root user
    images:
    - '*/istio/proxyv2*'
    restrictedField: spec.initContainers[*].securityContext.runAsUser
    values:
    - "0"

However when a non-compliant workload with istio containers is deployed the 2 Polex’es above cause the entire pod to be skipped, not only the istio related config.

Below is an example of a workload Pod that is skipped, while container app-1 is not compliant:

apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  metadata:
    annotations:
      istio.io/rev: default
    labels:
      app: app-1
    name: app-1-67c8567d6d-xsxqb
    namespace: app-1-namespace
    ownerReferences:
    - apiVersion: apps/v1
      blockOwnerDeletion: true
      controller: true
      kind: ReplicaSet
      name: app-1-67c8567d6d
  spec:
    containers:
    - env:
      - name: CLOUD_OPTION
        value: csp
      image: container.registry/app-1:v.2.0.0
      imagePullPolicy: IfNotPresent
      name: app-1
      ports:
      - containerPort: 80
        protocol: TCP
      resources:
        limits:
          cpu: "2"
          memory: 4Gi
        requests:
          cpu: 50m
          memory: 256Mi
      securityContext:
        allowPrivilegeEscalation: false
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
    - args:
      - proxy
      - sidecar
      - --domain
      - $(POD_NAMESPACE).svc.cluster.local
      - --proxyLogLevel=warning
      - --proxyComponentLogLevel=misc:error
      - --log_output_level=default:info
      env:
      - name: JWT_POLICY
        value: Some envs
      image: us-east4-docker.pkg.dev/shared-gcr-c99a/mct-stack/istio/proxyv2:1.18.7
      imagePullPolicy: IfNotPresent
      name: istio-proxy
      ports:
      - containerPort: 15090
        name: http-envoy-prom
        protocol: TCP
      readinessProbe:
        failureThreshold: 30
        httpGet:
          path: /healthz/ready
          port: 15021
          scheme: HTTP
        initialDelaySeconds: 1
        periodSeconds: 2
        successThreshold: 1
        timeoutSeconds: 3
      resources:
        limits:
          cpu: "2"
          memory: 1Gi
        requests:
          cpu: 10m
          memory: 40Mi
      securityContext:
        allowPrivilegeEscalation: false
        capabilities:
          drop:
          - ALL
        privileged: false
        readOnlyRootFilesystem: true
        runAsGroup: 1337
        runAsNonRoot: true
        runAsUser: 1337
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
    dnsPolicy: ClusterFirst
    enableServiceLinks: true
    initContainers:
    - args:
      - istio-iptables
      env:
      - name: TERMINATION_DRAIN_DURATION_SECONDS
        value: "30"
      image: some.registry/istio/proxyv2:1.18.7
      imagePullPolicy: IfNotPresent
      name: istio-init
      resources:
        limits:
          cpu: "2"
          memory: 1Gi
        requests:
          cpu: 10m
          memory: 40Mi
      securityContext:
        allowPrivilegeEscalation: false
        capabilities:
          add:
          - NET_ADMIN
          - NET_RAW
          drop:
          - ALL
        privileged: false
        readOnlyRootFilesystem: false
        runAsGroup: 0
        runAsNonRoot: false
        runAsUser: 0

This is how it shows in the UI:

image

And policyreports on the cluster show the same result.

Slack discussion

No response

Troubleshooting

  • I have read and followed the documentation AND the troubleshooting guide.
  • I have searched other issues in this repository and mine is not recorded.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

My bad! Yes, the container istio-proxy is the one which sets the field runAsNonRoot to true. For the container app-1, I see that the allowPrivilegeEscalation is set to false which doesn’t violate the restricted profile.

I think the point is that this container from above example from @KRASSUSS

spec:
    containers:
    - env:
      - name: CLOUD_OPTION
        value: csp
      image: container.registry/app-1:v.2.0.0
      imagePullPolicy: IfNotPresent
      name: app-1
      ports:
      - containerPort: 80
        protocol: TCP
      resources:
        limits:
          cpu: "2"
          memory: 4Gi
        requests:
          cpu: 50m
          memory: 256Mi
      securityContext:
        allowPrivilegeEscalation: false
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File

it has no defined runAsNonRoot: false and this is not even blocked/reported by Kyverno itself, as you pasted above:

 Error from server: error when creating "pod.yaml": admission webhook "validate.kyverno.svc-ignore" denied the request: 

 resource Pod/default/test-pod was blocked due to the following policies 
 
 psp-baseline:
  baseline: 'Validation rule ''baseline'' failed. It violates PodSecurity "baseline:v1.29":
     (Forbidden reason: non-default capabilities, field error list: [spec.initContainers[0].securityContext.capabilities.add
    is forbidden, forbidden values found: [NET_ADMIN NET_RAW]])'
 psp-restricted-limited:
   restricted: 'Validation rule ''restricted'' failed. It violates PodSecurity "restricted:v1.29":
     (Forbidden reason: runAsNonRoot != true, field error list: [spec.initContainers[0].securityContext.runAsNonRoot
     is forbidden, forbidden values found: false])(Forbidden reason: runAsUser=0, field
     error list: [spec.initContainers[0].securityContext.runAsUser is forbidden, forbidden
     values found: 0])'

kyverno do not reports it, there is no Forbidden reason: runAsNonRoot != true for spec.containers[0] at all, only spec.initContainers[0] and I think it shoud be because there is no spec.securityContext.runAsNonRoot at POD level, only istio in init-container is setting this. So if there is PolicyException just for istio-init only, the container anyway can be deployed running as root because it is not blocked/reported by kyverno and I think that is the problem not allowPrivilegeEscalation - @KRASSUSS correct me if I’m wrong:

All in all, what we want to achieve is exactly what’s mentioned here: #8570

Exceptions can be applied at container level for validate.podSecurity rules. This enhancement is for other rule types.

For the sake of this issue, it is a bug as mentioned above and I pushed a fix in the upstream repo. Once it is merged, I will test it against Kyverno with/without exceptions.

@akardaspg - Yes, you are correct.

If either pod or container sets the field to false, It goes to the 1st condition and returns the check result directly without even checking the implicit bad containers that don’t set the field at all.

I run the following test cases to understand the current behavior:

  1. policy.yaml:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: psp-restricted-limited
  annotations:
    pod-policies.kyverno.io/autogen-controllers: none
spec:
  failurePolicy: Ignore
  background: true
  validationFailureAction: Enforce
  rules:
  - name: restricted
    match:
      any:
      - resources:
          kinds:
          - Pod
    validate:
      podSecurity:
        level: restricted
        version: v1.29
        exclude:
        - controlName: Volume Types
        - controlName: Seccomp
        - controlName: Seccomp
          images:
          - '*'
        - controlName: Capabilities
          images:
          - "*"
  1. Run a pod that has one container without setting the runAsNonRoot field:
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test-pod
  name: test-pod
spec:
  containers:
  - image: nginx
    name: test-pod
    resources:
      limits:
        cpu: "2"
        memory: 4Gi
      requests:
        cpu: 50m
        memory: 256Mi
    securityContext:
      allowPrivilegeEscalation: false

The resource is blocked because runAsNonRoot field is required since this field isn’t set under the spec level (expected behavior):

resource Pod/default/test-pod was blocked due to the following policies 

psp-restricted-limited:
  restricted: 'Validation rule ''restricted'' failed. It violates PodSecurity "restricted:v1.29":
    (Forbidden reason: runAsNonRoot != true, field error list: [spec.containers[0].securityContext.runAsNonRoot:
    Required value])'
  1. Run a pod that has a container that doesn’t set the runAsNonRoot field and an init container that sets the field to true:
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test-pod
  name: test-pod
spec:
  containers:
  - image: nginx
    name: test-pod
    resources:
      limits:
        cpu: "2"
        memory: 4Gi
      requests:
        cpu: 50m
        memory: 256Mi
    securityContext:
      allowPrivilegeEscalation: false
  initContainers:
  - args:
    - istio-iptables
    env:
    - name: TERMINATION_DRAIN_DURATION_SECONDS
      value: "30"
    image: some.registry/istio/proxyv2:1.18.7
    imagePullPolicy: IfNotPresent
    name: istio-init
    resources:
      limits:
        cpu: "2"
        memory: 1Gi
      requests:
        cpu: 10m
        memory: 40Mi
    securityContext:
      allowPrivilegeEscalation: false
      runAsNonRoot: true

The pod is blocked because the field must be set in the container since it isn’t set under the spec level (expected behavior):

psp-restricted-limited:
  restricted: 'Validation rule ''restricted'' failed. It violates PodSecurity "restricted:v1.29":
    (Forbidden reason: runAsNonRoot != true, field error list: [spec.containers[0].securityContext.runAsNonRoot:
    Required value])'
  1. Run the same pod but set the initContainers[0].runAsNonRoot to false:
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test-pod
  name: test-pod
spec:
  containers:
  - image: nginx
    name: test-pod
    resources:
      limits:
        cpu: "2"
        memory: 4Gi
      requests:
        cpu: 50m
        memory: 256Mi
    securityContext:
      allowPrivilegeEscalation: false
  initContainers:
  - args:
    - istio-iptables
    env:
    - name: TERMINATION_DRAIN_DURATION_SECONDS
      value: "30"
    image: some.registry/istio/proxyv2:1.18.7
    imagePullPolicy: IfNotPresent
    name: istio-init
    resources:
      limits:
        cpu: "2"
        memory: 1Gi
      requests:
        cpu: 10m
        memory: 40Mi
    securityContext:
      allowPrivilegeEscalation: false
      runAsNonRoot: false

The pod is blocked as follows:

resource Pod/default/test-pod was blocked due to the following policies 

psp-restricted-limited:
  restricted: 'Validation rule ''restricted'' failed. It violates PodSecurity "restricted:v1.29":
    (Forbidden reason: runAsNonRoot != true, field error list: [spec.initContainers[0].securityContext.runAsNonRoot
    is forbidden, forbidden values found: false])'

The pod is blocked but it complains about setting the field to false only! It should also state that this field is required for containers[0].

I think this is the real bug we want to solve and if it is solved, it will work as expected when deploying exceptions.

Sure, here are the policies:

psp-baseline:

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: psp-baseline
  annotations:
    pod-policies.kyverno.io/autogen-controllers: none
spec:
  failurePolicy: Ignore
  background: true
  validationFailureAction: Audit
  rules:
  - name: baseline
    match:
      any:
      - resources:
          kinds:
          - Pod
    validate:
      podSecurity:
        level: baseline
        version: v1.29

psp-restricted-limited

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: psp-restricted-limited
  annotations:
    pod-policies.kyverno.io/autogen-controllers: none
spec:
  failurePolicy: Ignore
  background: true
  validationFailureAction: Audit
  rules:
  - name: restricted
    match:
      any:
      - resources:
          kinds:
          - Pod
    validate:
      podSecurity:
        level: restricted
        version: v1.29
        exclude:
        - controlName: Volume Types
        - controlName: Seccomp
        - controlName: Seccomp
          images:
          - '*'
        - controlName: Capabilities
          images:
          - "*"

Regarding the other issue I have submitted, they’re both related to Polexes but the behaviour in both is different - this one excepts the resources too much, while the other one doesn’t except resources at all while it should. If you think the root cause for both might be the same I can close one of them!