kyverno: [Bug] Segmentation fault in admission controller 1.13

Kyverno Version

1.13.0

Kubernetes Version

1.28.x

Kubernetes Platform

Other (specify in description)

Kyverno Rule Type

Validate

Description

After upgrading to Kyverno 1.13 (Helm chart v3.3.0), the admission controller panics when trying to iterate over a list that is not defined in the resource being validated.

Steps to reproduce

  1. Define validating policy trying to iterate over an optional list:
    apiVersion: kyverno.io/v1
    kind: ClusterPolicy
    metadata:
      name: restrict-storageclasses-by-namespace-annotation
    spec:
      validationFailureAction: Enforce
      background: false
      rules:
        - name: restrict-storageclasses-on-statefulset
          match:
            any:
              - resources:
                  kinds: [StatefulSet]
                  operations: [CREATE, UPDATE]
          context:
            - name: allowedStorageClasses
              apiCall:
                urlPath: /api/v1/namespaces/{{ request.namespace }}
                jmesPath: >-
                  split((metadata.annotations."storage/allowed-storageclasses" || ''), ',')
          validate:
            message: >-
              Attempt use of storageclass that is forbidden in this namespace. Please annotate the
              namespace as needed if this storageclass is required.
            foreach:
              - list: request.object.spec.volumeClaimTemplates # <-------- this might not always be defined
                deny:
                  conditions:
                    any:
                      - key: "{{ element.spec.storageClassName }}"
                        operator: NotIn
                        value: "{{ allowedStorageClasses }}"
    
  2. Create a resource without the target field, in this case a StatefulSet without .spec.volumeClaimTemplates defined

Expected behavior

Up to v1.12, if the key provided in foreach.list does not exist in the validated resource, Kyverno just skips the loop altogether (and thus does not deny the resource in this case), so it basically treats an undefined list just like an empty list.

One could also argue that it should be explicitly stated that only resources having the field defined should be evaluated. E.g. with a precondition or can we express this more elegantly in the foreach.list JMESPath expression?

Anyway, this behavior seems to have changed and it should clearly not lead to panicking admission controllers.

Screenshots

No response

Kyverno logs

2024-10-30T10:07:08Z TRC github.com/kyverno/kyverno/pkg/engine/handlers/validation/validate_resource.go:215 > failed to evaluate list error="JMESPath query failed: Unknown key \"volumeClaimTemplates\" in path" list=request.object.spec.volumeClaimTemplates logger=engine.validate new.kind=StatefulSet new.name=recruit-pg-00-jsdq new.namespace=recruit old.kind=StatefulSet old.name=recruit-pg-00-jsdq old.namespace=recruit policy.apply=All policy.name=restrict-storageclasses-by-namespace-annotation policy.namespace= rule.name=restrict-storageclasses-on-statefulset v=2
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x3521db6]

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
  • Reactions: 4
  • Comments: 21 (9 by maintainers)

Most upvoted comments

spoke too soon, problem still happens

kyverno panic: runtime error: invalid memory address or nil pointer dereference
kyverno [signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x3521504]
kyverno 
kyverno goroutine 17772 [running]:
kyverno github.com/kyverno/kyverno/pkg/engine/api.(*RuleResponse).Status(...)
kyverno     github.com/kyverno/kyverno/pkg/engine/api/ruleresponse.go:177
kyverno github.com/kyverno/kyverno/pkg/engine/handlers/validation.(*validator).validate(0xc002a6f208, {0x47f98b8, 0xc0015d0750})
kyverno     github.com/kyverno/kyverno/pkg/engine/handlers/validation/validate_resource.go:167 +0x2e4
kyverno github.com/kyverno/kyverno/pkg/engine/handlers/validation.validateResourceHandler.Process({}, {_, _}, {{_, _}, _}, {_, _}, {_}, {{0xc001c42440, ...}, ...}, ...)
kyverno     github.com/kyverno/kyverno/pkg/engine/handlers/validation/validate_resource.go:60 +0x15d
kyverno github.com/kyverno/kyverno/pkg/engine.(*engine).invokeRuleHandler.func1({0x47f98b8, 0xc0015d0750}, {0x4141388?, 0xa?})
kyverno     github.com/kyverno/kyverno/pkg/engine/engine.go:294 +0x7a6
kyverno github.com/kyverno/kyverno/pkg/tracing.ChildSpan2[...]({0x47f98b8?, 0xc0015d0750?}, {0x4141388?, 0x271a40e?}, {0xc001cf6bc0?, 0x414f255?}, 0xc000c96728?, {0x0, 0x0, 0x0})
kyverno     github.com/kyverno/kyverno/pkg/tracing/childspan.go:59 +0xd3
kyverno github.com/kyverno/kyverno/pkg/engine.(*engine).invokeRuleHandler(_, {_, _}, {{_, _}, _}, _, {_, _}, {0xc0015fb980}, ...)
kyverno     github.com/kyverno/kyverno/pkg/engine/engine.go:240 +0x1e5
kyverno github.com/kyverno/kyverno/pkg/engine.(*engine).validate(0xc002efaaa0, {0x47f98b8, 0xc0015d0750}, {{0x4808810?, 0xc0015f8800?}, 0x482f500?}, {0x482f500, 0xc003910c80})
kyverno     github.com/kyverno/kyverno/pkg/engine/validation.go:68 +0x477
kyverno github.com/kyverno/kyverno/pkg/engine.(*engine).Validate(0xc002efaaa0, {0x47f98b8, 0xc0015d0750}, {0x482f500, 0xc003910c80})
kyverno     github.com/kyverno/kyverno/pkg/engine/engine.go:92 +0x18d
kyverno github.com/kyverno/kyverno/pkg/webhooks/resource/validation.(*validationHandler).HandleValidationEnforce.func1({0x47f98b8, 0xc0015d0750}, {0x4188c75?, 0x1e?})
kyverno     github.com/kyverno/kyverno/pkg/webhooks/resource/validation/validation.go:116 +0x1df
kyverno github.com/kyverno/kyverno/pkg/tracing.ChildSpan({0x47f98b8?, 0xc0015d0750?}, {0x4188c75?, 0xc0023d9cc2?}, {0xc001cf6b80?, 0x0?}, 0xc000c97c50, {0x0, 0x0, 0x0})
kyverno     github.com/kyverno/kyverno/pkg/tracing/childspan.go:33 +0x9a
kyverno github.com/kyverno/kyverno/pkg/webhooks/resource/validation.(*validationHandler).HandleValidationEnforce(_, {_, _}, {{{0xc001d39710, 0x24}, {{0x0, 0x0}, {0xc0023d9cc0, 0x2}, {0xc0023d9cc2, ...}}, ...}, ...}, ...)
kyverno     github.com/kyverno/kyverno/pkg/webhooks/resource/validation/validation.go:106 +0x63e
kyverno github.com/kyverno/kyverno/pkg/webhooks/resource.(*resourceHandlers).Validate.func1()
kyverno     github.com/kyverno/kyverno/pkg/webhooks/resource/handlers.go:156 +0x165
kyverno created by github.com/kyverno/kyverno/pkg/webhooks/resource.(*resourceHandlers).Validate in goroutine 17671
kyverno     github.com/kyverno/kyverno/pkg/webhooks/resource/handlers.go:154 +0xb0d
Stream closed EOF for kyverno/kyverno-admission-controller-7cb798b947-49l7v (kyverno-pre)
Stream closed EOF for kyverno/kyverno-admission-controller-7cb798b947-49l7v (kyverno)

Thank you @realshuting. Unfortunately, the error still persists with ghcr.io/kyverno/kyverno:v1.13.1-rc.1:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x35210c4]
goroutine 24183 [running]:
github.com/kyverno/kyverno/pkg/engine/api.(*RuleResponse).Status(...)
	github.com/kyverno/kyverno/pkg/engine/api/ruleresponse.go:177

Thanks @bauerjs1 for digging into it! I added it to 1.13.1, we will investigate further and fix the issue.

Running into the same issue.