GHSA-cfw7-6c5v-2wjq: Jupyter Enterprise Gateway: Kubernetes Manifest Injection in Jinja2 Template Rendering
SummaryThe environment variables used during the rendering of the Kubernetes manifest allow YAML injection, enabling attackers to overwrite existing keys like `securityContext` and inject multi-document YAML to create additional unintended Kubernetes resources.DetailsThe server interpolates untrusted environment variables (e.g., `KERNEL_XXX`) into Kubernetes manifests without YAML-aware escaping, enabling YAML injection attacks. Attackers can inject new fields, overwrite critical fields (e.g., duplicate `securityContext` keys, where the last one prevails), and inject document boundaries (`---` for new documents, `...` for end-of-document) to generate multiple resources, potentially creating arbitrary kinds like privileged pods.The Jinja2 template for the Kubernetes manifest contains several `kernel_xxx` variables, such as `kernel_working_dir` that are used when rendering the manifest and are all vectors for YAML injection.
https://github.com/jupyter-server/enterprise_gateway/blob/152c20f162f2fab700c04c8830ebf8c1e2e2217a/etc/kernel-launchers/kubernetes/scripts/kernel-pod.yaml.j2#L77These values come from the environment passed in the API call, where they were `KERNEL_XXX` before being converted to lowercase.https://github.com/jupyter-server/enterprise_gateway/blob/152c20f162f2fab700c04c8830ebf8c1e2e2217a/etc/kernel-launchers/kubernetes/scripts/launch_kubernetes.py#L130-L137PoCThese proof of concepts are injecting in the `KERNEL_WORKING_DIR` env var, but any of the env vars could have been used.
By default, the `KERNEL_WORKING_DIR` will be ignored unless `EG_MIRROR_WORKING_DIRS` is truthy for the `enterprise-gateway`. This is controlled by the `mirrorWorkingDirs` value in the Helm chart.Using `ducaale/xh`:`env-working-dir-exploit.yaml`:Resulting request:Curl equivalent command:The rendered Jinja2 template:Normally the container would run as `uid=1000(jovyan) gid=100(users) groups=100(users)`.
This injects a pod `securityContext` with `runAsUser: 0` and `runAsGroup: 0` (and `fsGroup: 100`).
The processing of the YAML results in the duplicate key clobbering the original.
Making the container run as `uid=0(root) gid=0(root) groups=0(root),100(users)`.In addition to injecting a pod level `securityContext` it is also possible to inject a container level `securityContext` which supports the `privileged` field.Injecting a PodBy injecting `...` and `---` it is possible to use multi-document YAML to inject Kubernetes resources.`env-working-dir-exploit-pod.yaml`:This is rendered as (skipping the beginning of the rendering before the inject):`kubectl get pods -n notebooks`The `injected-pod` has been created in addition to the `working-dir-root-pod`.`kubectl get pod/injected-pod -o yaml -n notebooks -o jsonpath='{.spec.containers[*].securityContext}'`:ImpactAn attacker can create pods running with arbitrary, `image`, `securityContext`, and `volumeMounts` including `hostPath` mounts. Privileged pods can be created.Arbitrary Kubernetes resources of kinds: `Pod`, `Secret`, `PersistentVolumeClaim`, `PersistentVolume`, `Service`, and `ConfigMap` can be created.Repeated exploitation can compromise all worker nodes, and thus the entire Kubernetes cluster. Multiple container escape vectors exist. It is possible to create privileged pods which could load kernel modules to compromise the host. It is also possible to specify volume mounts, so another vector for a container escape is to use a `hostPath` R/W volume mount, use the injected `securityContext` to run as `root`, and then gain code execution in the underlying worker node by creating a crontab entry in the mounted host file system.
CVSS v4.0: 10