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:
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)
I think the point is that this container from above example from @KRASSUSS
it has no defined
runAsNonRoot: falseand this is not even blocked/reported by Kyverno itself, as you pasted above:kyverno do not reports it, there is no
Forbidden reason: runAsNonRoot != trueforspec.containers[0]at all, onlyspec.initContainers[0]and I think it shoud be because there is nospec.securityContext.runAsNonRootat 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 notallowPrivilegeEscalation- @KRASSUSS correct me if I’m wrong:Exceptions can be applied at container level for
validate.podSecurityrules. 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:
runAsNonRootfield:The resource is blocked because
runAsNonRootfield is required since this field isn’t set under thespeclevel (expected behavior):runAsNonRootfield and an init container that sets the field totrue:The pod is blocked because the field must be set in the container since it isn’t set under the
speclevel (expected behavior):initContainers[0].runAsNonRootto false:The pod is blocked as follows:
The pod is blocked but it complains about setting the field to
falseonly! It should also state that this field is required forcontainers[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:
psp-restricted-limited
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!