Skip to content
Race condition Write-up Advanced

GKE Autopilot Node Compromise via Race Condition

A race condition during GKE Autopilot cluster provisioning allows an attacker to create a privileged pod before the OPA policies that enforce security controls are fully applied. This lets the attacker gain privileged access to the managed node, read all cluster secrets, and retrieve a service account token from the metadata service.

Anthony Weems Follow

Published on lf.lc

Bounty$1,337 View original

GKE Autopilot provides many default security controls to protect the node, including protections against privileged pods and limitations on hostPath volume mounts.

Anthony Weems, on lf.lc

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

GKE Autopilot is a mode of Google Kubernetes Engine in which Google manages the nodes on the customer's behalf. To make that management model safe, Autopilot ships a set of default security controls that protect the node, including protections against privileged pods and limitations on hostPath volume mounts. Those controls are enforced through OPA policies (Gatekeeper), which have to be provisioned into the cluster when it is created.

The report looks at what happens during that provisioning window. The claim is that a race condition exists while the Autopilot OPA policies are being installed, and that a user who can create resources in the cluster during that window can get a privileged pod onto a managed node, bypassing the intended controls.

Setup

The only precondition the report describes is access to create pods and persistent volumes in the Autopilot cluster. The tooling used is gcloud and kubectl, and the demonstration needs an Autopilot cluster that is in the process of being created. The malicious resource is a deployment manifest, deploy.yaml, which the report links to but does not reproduce.

Finding it

The Autopilot security controls are not present from the first moment of a cluster's life: the OPA policies are provisioned as part of cluster creation. A resource created before policy enforcement is active is therefore not evaluated against those policies. Because cluster creation takes time, there is a window in which a user can submit a resource that would normally be rejected.

Exploitation

Step 1: obtain the manifest

Download the deployment manifest referenced in the report:

Text
deploy.yaml

The report does not include the contents of this file; it is the privileged deployment that the later steps apply.

Step 2: create an Autopilot cluster

Create a new Autopilot cluster. The cluster does not need to be ready before the next step starts.

Step 3: race the credential fetch and the apply

While the cluster is being created, run the following commands, replacing $name with the cluster name:

Bash
while true; do
  gcloud container clusters get-credentials $name && break;
done
while true; do
  timeout 1 kubectl apply -f deploy.yaml && break;
done
kubectl exec -it deploy/priv -- nsenter --mount=/proc/1/ns/mnt -- /bin/bash

The first loop retries gcloud container clusters get-credentials until it succeeds, so the local kubeconfig points at the new cluster as soon as the API server is reachable. The second loop retries kubectl apply with a one second timeout until the apply succeeds. The final command execs into the priv deployment and uses nsenter with --mount=/proc/1/ns/mnt to enter the mount namespace of PID 1, which is the node's namespace rather than the container's.

Step 4: observe the failures, then the success

Initially the privileged deployment fails to create. The report lists the following observed errors:

  • The connection to the server [redacted] was refused - did you specify the right host or port?
  • GKEAutopilot authz: the request was sent before policy enforcement is enabled
  • Internal error occurred: failed calling webhook "validation.gatekeeper.sh": Post https://localhost:8787/v1/admit?timeout=5s: dial tcp [::1]:8787

The report lists these observed errors without stating the order in which they occurred. After a few minutes have passed, the deployment succeeds and the user can exec into the new privileged pod.

Root cause

The Autopilot OPA policies are provisioned after the cluster API becomes usable, so there is a period in which the admission path does not enforce them. A resource applied in that period is admitted without the privileged-pod and hostPath restrictions being evaluated. The GKEAutopilot authz message states that the request was sent before policy enforcement is enabled, and the Gatekeeper webhook error shows a call to the webhook failing. The existing defences did not stop the attack because they were not yet in place at the moment the resource was created; the race is against their installation, not against their logic.

Impact

A user with access to create pods and persistent volumes in the Autopilot cluster could bypass the security controls and gain privileged access to the managed Kubernetes nodes. With that access they could read all secrets in the cluster, including those outside their provisioned access, or explore the attack surface of the Autopilot control plane. The same user can also retrieve a service account token from the node metadata service for the default compute service account.

Fix

According to the timeline, the issue was reported fixed on 2021-06-07. On 2021-09-10 the researcher retested and verified that only variant "a" was fixed, and the issue was reopened the following day. The report does not describe the code or configuration change itself.

Variants to test

The report records that additional variants were reported on 2021-03-05 (vrp/181521559b and vrp/181521559c) and on 2021-04-01 (vrp/181521559d), and that the reward was paid per variant. The report does not describe what distinguishes these variants. A positive result here looked like the privileged deployment being created and nsenter into the node mount namespace succeeding.

Timeline

  • 2021-03-01: Initial report to Google VRP
  • 2021-03-01: Issue triaged
  • 2021-03-04: Internal bug report filed
  • 2021-03-05: Additional variants reported (vrp/181521559b, vrp/181521559c)
  • 2021-03-09: Internal bug report updated
  • 2021-04-01: Additional variant reported (vrp/181521559d)
  • 2021-04-06: Internal bug report updated
  • 2021-04-08: VRP issued reward ($1337 per variant)
  • 2021-06-07: Issue reported fixed
  • 2021-09-10: Retested and verified only variant "a" is fixed
  • 2021-09-11: Issue reopened

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

What happened, in plain words

Google runs a service called GKE Autopilot. It lets you run your own programs (in containers) on computers that Google manages for you. Because Google manages those computers, it puts safety rules in place so that your programs cannot take over the machines. One of those rules stops programs from running with extra powers (privileged pods), and another limits how much of the machine's file system a program can see (hostPath mounts).

Those safety rules are not there from the very first second a new cluster is created. They are installed a little later, while the cluster is still being set up. The report shows that if you create your program during that gap, the safety rules are not yet active, so your program can get through. Once the privileged pod is running, the user can enter the node's mount namespace and reach the machine underneath.

The report calls this a race condition. A race condition is when two things happen at the same time and the result depends on which one finishes first. Here, the two things are: (1) you sending your program to the cluster, and (2) Google installing the safety rules. If you win the race, your program is accepted before the rules can stop it.

Context

GKE Autopilot is a mode of Google Kubernetes Engine (GKE). Kubernetes is a system for running and managing containers. A container is a lightweight, isolated box that holds a program and everything it needs to run. GKE is Google's hosted version of Kubernetes: Google runs the underlying computers (called nodes) for you.

In Autopilot, Google manages the nodes on your behalf. To make that safe, Autopilot ships with default security controls that protect the node. These include protections against privileged pods and limitations on hostPath volume mounts.

  • A pod is the smallest unit in Kubernetes: one or more containers that run together. A privileged pod is a pod that has been given extra powers, close to what the underlying machine itself has. That is dangerous because it can escape the container and affect the node.
  • A volume mount is a way to attach a folder from the machine into a container. hostPath means the folder comes from the node's own file system. If a container can mount arbitrary hostPath folders, it can read or change files on the node.

Those controls are enforced through OPA policies (Gatekeeper). OPA (Open Policy Agent) is a tool that checks requests against rules. Gatekeeper is the Kubernetes component that plugs OPA into the cluster's admission process. Admission is the checkpoint every request passes through before Kubernetes accepts it: if a policy says no, the request is rejected. These policies have to be provisioned (installed and configured) into the cluster when it is created.

The report looks at what happens during that provisioning window. The claim is that a race condition exists while the Autopilot OPA policies are being installed, and that a user who can create resources in the cluster during that window can get a privileged pod onto a managed node, bypassing the intended controls.

Setup

The only precondition the report describes is access to create pods and persistent volumes in the Autopilot cluster. A persistent volume is storage that survives beyond the life of a single pod, like an external hard drive you can attach to a container.

The tooling used is gcloud and kubectl.

  • gcloud is Google's command-line tool for managing Google Cloud resources. You type commands into a terminal (a text window where you type commands) and it talks to Google's servers.
  • kubectl is the standard command-line tool for talking to a Kubernetes cluster. It sends requests to the cluster's API server.
  • An API (Application Programming Interface) is a way for one program to talk to another. The API server is the front door of a Kubernetes cluster: every request to create, read, update or delete something goes through it.

The demonstration needs an Autopilot cluster that is in the process of being created. The malicious resource is a deployment manifest, deploy.yaml, which the report links to but does not reproduce. A manifest is a file (usually YAML) that describes what you want Kubernetes to create, for example a deployment. A deployment is a Kubernetes object that keeps a specified number of copies of a pod running.

Finding it

The Autopilot security controls are not present from the first moment of a cluster's life: the OPA policies are provisioned as part of cluster creation. A resource created before policy enforcement is active is therefore not evaluated against those policies. Because cluster creation takes time, there is a window in which a user can submit a resource that would normally be rejected.

In everyday terms: imagine a new building where the security guard at the door is hired and trained after the doors open. If you walk in during that gap, nobody checks your badge. The guard is not bad at their job; they simply are not there yet.

Exploitation

Step 1: obtain the manifest

Download the deployment manifest referenced in the report:

Text
deploy.yaml

The report does not include the contents of this file; it is the privileged deployment that the later steps apply. In plain words, this file is the description of the pod you want to create, and it is written in a way that asks for privileged access. Normally the Autopilot policies would reject it.

Step 2: create an Autopilot cluster

Create a new Autopilot cluster. The cluster does not need to be ready before the next step starts. The point is to start the clock: the moment creation begins, the provisioning window is open, and you want to be sending your manifest while it is still open.

Step 3: race the credential fetch and the apply

While the cluster is being created, run the following commands, replacing $name with the cluster name:

Bash
while true; do
  gcloud container clusters get-credentials $name && break;
done
while true; do
  timeout 1 kubectl apply -f deploy.yaml && break;
done
kubectl exec -it deploy/priv -- nsenter --mount=/proc/1/ns/mnt -- /bin/bash

Line by line:

  1. while true; do starts an endless loop. The commands inside will repeat until something tells them to stop.
  2. gcloud container clusters get-credentials $name && break; asks Google for the login details (credentials) of the cluster named $name and writes them into a local configuration file that kubectl reads. The && means "only if the previous command succeeded". So if fetching the credentials works, break exits the loop. If it fails (for example because the cluster is not ready yet), the loop tries again immediately. This is how the script waits until the cluster's API server can be reached.
  3. done closes the first loop.
  4. while true; do starts a second endless loop.
  5. timeout 1 kubectl apply -f deploy.yaml && break; tries to send the manifest deploy.yaml to the cluster with kubectl apply. The -f flag means "read this file". timeout 1 gives the command at most one second; if it takes longer, it is stopped. If the apply succeeds, break exits the loop. If it fails, the loop tries again. The short timeout is deliberate: you do not want to wait a long time on a request that may hang while the cluster is still coming up; you want to retry quickly and often, to hit the window.
  6. done closes the second loop.
  7. kubectl exec -it deploy/priv -- nsenter --mount=/proc/1/ns/mnt -- /bin/bash runs a command inside the pod created by the deployment named priv. kubectl exec is like opening a shell inside a running container. -it means interactive with a terminal. nsenter is a Linux tool that lets a process enter another process's namespaces. A namespace here is an isolation boundary in Linux: processes, mounts, network and so on can be kept separate. --mount=/proc/1/ns/mnt tells nsenter to enter the mount namespace of process ID 1. Inside a container, PID 1 is the container's first process, but because the pod is privileged, this reaches the node's mount namespace, meaning the node's file system rather than the container's. Finally -- /bin/bash starts a shell there. In plain words: this is the step that proves you have escaped the container and are now looking at the machine itself.

Step 4: observe the failures, then the success

Initially the privileged deployment fails to create. The report lists the following observed errors:

  • The connection to the server [redacted] was refused - did you specify the right host or port?
  • GKEAutopilot authz: the request was sent before policy enforcement is enabled
  • Internal error occurred: failed calling webhook "validation.gatekeeper.sh": Post https://localhost:8787/v1/admit?timeout=5s: dial tcp [::1]:8787

The report lists these observed errors:

  1. The API server is not yet reachable. The first message is the network saying "nobody is answering at that address yet".
  2. The API server is reachable but policy enforcement is not yet enabled. The second message comes from Autopilot's own authorisation layer and says, in effect, "you sent this before the rules were switched on".
  3. The Gatekeeper validating webhook is configured but not yet answering. A webhook is a callback: Kubernetes pauses the request and asks another service whether to allow it. Here that service is Gatekeeper, and the third message says the call to it failed because nothing was listening yet.

After a few minutes have passed, the deployment succeeds and the user can exec into the new privileged pod.

Root cause

The Autopilot OPA policies are provisioned after the cluster API becomes usable, so there is a period in which the admission path does not enforce them. A resource applied in that period is admitted without the privileged-pod and hostPath restrictions being evaluated. The GKEAutopilot authz message states that the request was sent before policy enforcement is enabled, and the Gatekeeper webhook error shows a call to the webhook failing. The existing defences did not stop the attack because they were not yet in place at the moment the resource was created; the race is against their installation, not against their logic.

A simple comparison: a shop with a strict no-returns policy is only safe if the returns desk is staffed when the shop opens. If the desk opens an hour later, a customer can return anything during that hour. The policy itself is fine; the timing is the problem.

Impact

A user with access to create pods and persistent volumes in the Autopilot cluster could bypass the security controls and gain privileged access to the managed Kubernetes nodes. With that access they could read all secrets in the cluster, including those outside their provisioned access, or explore the attack surface of the Autopilot control plane.

  • A secret in Kubernetes is an object that holds sensitive data such as passwords, tokens or keys. Reading all secrets means reading credentials that belong to other people or services.
  • The control plane is the part of Kubernetes that makes decisions: the API server, the scheduler and so on. Exploring its attack surface means looking for further weaknesses in that management layer.

The same user can also retrieve a service account token from the node metadata service for the default compute service account. A service account is an identity used by a program rather than a person. A token is a piece of data that proves that identity, like a temporary ID card. The metadata service is a small internal service that cloud machines expose so that programs running on them can learn about their environment and obtain credentials. Getting that token means the attacker can act as that service account.

Fix

According to the timeline, the issue was reported fixed on 2021-06-07. On 2021-09-10 the researcher retested and verified that only variant "a" was fixed, and the issue was reopened the following day. The report does not describe the code or configuration change itself.

Variants to test

The report records that additional variants were reported on 2021-03-05 (vrp/181521559b and vrp/181521559c) and on 2021-04-01 (vrp/181521559d), and that the reward was paid per variant. The report does not describe what distinguishes these variants.

Asynchronously means "not at the same time": the API opens first, and the controls arrive later. The approach is:

A positive result here looked like the privileged deployment being created and nsenter into the node mount namespace succeeding.

Timeline

  • 2021-03-01: Initial report to Google VRP
  • 2021-03-01: Issue triaged
  • 2021-03-05: Additional variants reported (vrp/181521559b, vrp/181521559c)
  • 2021-03-04: Internal bug report filed
  • 2021-03-09: Internal bug report updated
  • 2021-04-01: Additional variant reported (vrp/181521559d)
  • 2021-04-06: Internal bug report updated
  • 2021-04-08: VRP issued reward ($1337 per variant)
  • 2021-06-07: Issue reported fixed
  • 2021-09-10: Retested and verified only variant "a" is fixed
  • 2021-09-11: Issue reopened

Glossary

  • API: a way for one program to talk to another, usually over the network.
  • API server: the front door of a Kubernetes cluster; every request to create, read, update or delete something goes through it.
  • Admission: the checkpoint in Kubernetes where a request is checked before being accepted.
  • Asynchronous: happening at different times, not together; here, the API becomes reachable before the controls are installed.
  • Autopilot: a mode of GKE where Google manages the nodes for you.
  • Container: a lightweight, isolated box holding a program and its dependencies.
  • Control plane: the part of Kubernetes that makes decisions, such as the API server and scheduler.
  • Credentials: the details a tool uses to prove who it is and get access.
  • Deployment: a Kubernetes object that keeps a specified number of copies of a pod running.
  • Gatekeeper: the Kubernetes component that plugs OPA policies into admission.
  • GKE: Google Kubernetes Engine, Google's hosted Kubernetes service.
  • gcloud: Google's command-line tool for managing Google Cloud resources.
  • hostPath: a volume mount that attaches a folder from the node's own file system into a container.
  • kubectl: the standard command-line tool for talking to a Kubernetes cluster.
  • Kubernetes: a system for running and managing containers.
  • Manifest: a file (usually YAML) describing what you want Kubernetes to create.
  • Metadata service: an internal service on a cloud machine that programs can query for information and credentials.
  • Namespace: an isolation boundary in Linux that keeps processes, mounts, network and so on separate.
  • Node: a machine (physical or virtual) that runs pods in a Kubernetes cluster.
  • nsenter: a Linux tool that lets a process enter another process's namespaces.
  • OPA: Open Policy Agent, a tool that checks requests against rules.
  • Persistent volume: storage that survives beyond the life of a single pod.
  • Pod: the smallest unit in Kubernetes; one or more containers that run together.
  • Privileged pod: a pod given extra powers, close to those of the underlying machine.
  • Provisioning: installing and configuring something, here the OPA policies, as part of cluster creation.
  • Race condition: a situation where two things happen at the same time and the result depends on which finishes first.
  • Secret: a Kubernetes object holding sensitive data such as passwords, tokens or keys.
  • Service account: an identity used by a program rather than a person.
  • Token: a piece of data that proves an identity, like a temporary ID card.
  • Volume mount: a way to attach a folder from the machine into a container.
  • Webhook: a callback; Kubernetes pauses a request and asks another service whether to allow it.
  • YAML: a human-readable text format often used for configuration files.

More by Anthony Weems

View all

9 more disclosed reports, best first.

More from Google

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.