Can't Be Contained: Finding a Command Injection Vulnerability in Kubernetes
A command injection vulnerability in Kubernetes allowed an attacker with apply privileges to execute arbitrary commands with SYSTEM privileges on Windows nodes by supplying a malicious subPath value in a pod's YAML file. This could lead to remote code execution and control over all Windows nodes in the cluster.
Published on akamai.com
Jump to PoC
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.
Context
Kubernetes uses YAML files for almost everything: configuring the Container Network Interface, managing pods, and even handling secrets. This write-up looks at what happens when user-controlled values from a pod's YAML reach a shell on a Windows node.
When a pod is created, the user can create a shared directory between the pod and the host. This feature is called volumes. It is useful, for example, when serving a website from a container in a pod: the site files can live in a directory shared with the host, so the layout can be changed without rebuilding an image each time.
Volumes are enabled by including the volume parameter in the pod's YAML file. The mount locations are listed in mountPath (the location in the container) and hostPath (the location on the host). The subPath subproperty lets you mount a shared directory or file at a chosen location.
The component that parses the YAML file is kubelet, the central service responsible for running containerized applications on a node.
Setup
The vulnerability can be exploited on default installations of Kubernetes and was tested against both on-prem deployments and Azure Kubernetes Service. The attacker needs access to a node and apply privileges, the privileges required to interact with the Kubernetes API. The target must include Windows nodes, because the vulnerability is restricted to Windows nodes.
Finding it
Earlier YAML-related vulnerabilities shaped the direction of the research. In 2022, CVE-2022-1471 was found inside the constructor of SnakeYAML, a known YAML parser; it allowed the creation of unsafe objects, which could lead to code execution on vulnerable applications using it. The Kubernetes security team's advisory for CVE-2021-25749 describes a vulnerability that lets attackers bypass the verification of who is allowed to run as root by misspelling their name in the YAML file.
The researchers also came across CVE-2017-1002101 and CVE-2021-25741, which showed how attackers can use race conditions and symlinks together with the subPath subproperty in a YAML file to reach privileged data outside the container. These inspired the team to pursue path processing in the Kubernetes codebase.
As part of the patch for CVE-2021-25741, kubelet validates every parameter in the YAML file and ensures that no symlinks are created as a result of using the subPath parameter, by calling an inner function named isLinkPath. That function takes the subPath supplied by the user in the YAML file and uses it to build a PowerShell command that determines the path type, that is, whether it is a symlink or not. The formatted PowerShell command is then immediately invoked by the exec.Command function call.
The presence of exec.Command combined with unsanitized user-supplied input strongly hinted at a command injection opportunity.
Exploitation
Step 1: Understand PowerShell string evaluation
PowerShell evaluates values inside strings before they are used. This is done by adding $(<experssion_to_be_evaluated>) to the string:
PS> echo “the value of 1+1 is $(1+1)
Output:
the value of 1+1 is 2
This example is simple, but any PowerShell command can be inserted between the parentheses and will be evaluated, such as $(Start-Process cmd), $(Invoke-Expression exp) and other PowerShell treats.
Step 2: Put the expression in subPath
An attacker can abuse this subPath evaluation to reach the vulnerable code and execute any command with SYSTEM privileges (kubelet's own context) from remote nodes, gaining control over all Windows nodes in the cluster. A malicious subPath value is what triggers it.
Step 3: Apply the malicious YAML
The proof of concept is a YAML file containing the evaluation of the PowerShell command. Applying it to the cluster is enough to exploit the vulnerability. The repository also holds a video showing the terminal being started.
This CVE led to the discovery and fix of more command injection vulnerabilities, collectively numbered CVE-2023-3955 and CVE-2023-3893.
Root cause
The isLinkPath function takes the subPath supplied by the user in the YAML file and uses it to build a PowerShell command. The formatted command is then immediately invoked by exec.Command. User input reaches a shell command without sanitisation, so the $(...) subexpression syntax is evaluated by PowerShell before the command runs.
The validation added for CVE-2021-25741 checks for symlinks by constructing and running a PowerShell command from the very value it is meant to validate.
Impact
The vulnerability allows remote code execution with SYSTEM privileges on all Windows endpoints within a Kubernetes cluster. The attacker needs to apply a malicious YAML file on the cluster. CVE-2023-3676 was rated high severity with a CVSS score of 8.8.
CVE-2023-3676 requires low privileges, so the bar for attackers is low: access to a node and apply privileges is all that is needed. Successful exploitation leads to remote code execution on any Windows node on the machine with SYSTEM privileges. High impact combined with ease of exploitation usually means a higher chance of seeing this attack, and similar attacks, against organizations. The only limiting factor is scope: the vulnerability is restricted to Windows nodes, which are not very popular today.
Fix
The Kubernetes team patched this class of vulnerabilities by passing parameters from environment variables instead of from user input. Values passed this way are treated as strings and are therefore not evaluated as expressions by PowerShell.
All Kubernetes versions below 1.28 are vulnerable to this CVE. Patching is the most reliable protection. Where patching is not an option, the following measures were outlined.
Disable Volume.Subpath
For CVE-2023-3676, admins can disable the use of Volume.Subpath. This keeps the cluster safe against the vulnerability but disables a mechanism that is sometimes important for production clusters.
OPA
Open Policy Agent (OPA) is an open source agent that allows users to receive data on traffic going in and out of nodes and take policy-based actions on it. OPA uses Rego as its engine language, in which admins can write rules to block certain YAML files. The following rule denies creation of pods with a malicious subPath:
package kubernetes.admission
deny[msg] {
input.request.kind.kind == "Pod"
path := input.request.object.spec.containers.volumeMounts.subPath
not startswith(path, "$(")
msg := sprintf("malicious path: %v was found", [path])
}
RBAC
Role-based access control (RBAC) segments user operations according to who the user is. For example, each user can only create pods in their own namespace, or can only view information for allowed namespaces. This can limit the number of users who can act on a cluster and help focus attention on those who do have the relevant privileges for exploitation.
Checking for Windows nodes
One method to determine whether a node in your cluster is a Windows node is the following command:
kubectl get nodes -o wide --show-labels | grep “os=windows”
Output:
akswin000000 Ready agent 4d17h v1.26.6 agentpool=win,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=windows…
akswin000001 Ready agent 4d17h v1.26.6 agentpool=win,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=windows… |
- Look for other places where kubelet builds a shell command from a YAML-supplied value on Windows nodes. A positive result is a value containing
$(...)that is evaluated when the pod is created. - Test other subproperties that end up in a PowerShell command, not just subPath. A positive result is command output or a spawned process appearing on the node.
- Check whether the OPA rule above can be bypassed with alternative PowerShell subexpression syntax or whitespace. A positive result is a pod created with a subPath that still evaluates.
- Review other Kubernetes components that shell out to PowerShell on Windows. A positive result is any user-controlled field reaching exec.Command without sanitisation.
Disclosure timeline
- 07/13/2023: vulnerability disclosed to the Kubernetes team.
- 07/19/2023: CVEs assigned by the Kubernetes team.
- 08/23/2023: Kubernetes released the CVE fixes.
- 09/13/2023: blog post published.
The researchers thanked the Kubernetes team for their very quick response and smooth communication.
What happened, in plain words
A security researcher named Tomer Peled found a serious flaw in Kubernetes, the software many companies use to run their applications in containers. The flaw was given the identifier CVE-2023-3676 and a severity score of 8.8 out of 10, which counts as high severity. In simple terms: someone who is allowed to send instructions to a Kubernetes cluster could send a specially crafted configuration file, and that file would make the cluster run commands of the attacker's choosing on its Windows machines, with the highest level of permission those machines have (an account called SYSTEM). The attacker does not need to be sitting at the machine; the commands run remotely.
The same mistake turned out to exist in more than one place, so two further identifiers were assigned: CVE-2023-3955 and CVE-2023-3893. The researcher published a proof of concept (a small demonstration file) and a rule that administrators can use to block the attack. The Kubernetes team released the CVE fixes on 23 August 2023.


First, the words you need
Before the details, here is the vocabulary this story uses. Each term is explained from zero.
- Kubernetes: a program that manages lots of small, isolated pieces of software, called containers, across one or more computers. Think of it as a building manager: you tell it what apartments (containers) you want and where, and it makes sure they exist and keep running.
- Container: a packaged application plus everything it needs to run, kept separate from the rest of the machine. Like a shipping container: standard outside, whatever you like inside, and it does not interfere with the other containers.
- Node: one physical or virtual machine that is part of a Kubernetes cluster. A cluster is a group of nodes working together.
- Cluster: the whole group of nodes that Kubernetes manages as one unit.
- YAML: a plain-text format for writing configuration, similar to JSON. It is indentation-based and meant to be readable by humans. In Kubernetes, YAML files are how you describe what you want: which containers to run, what storage to attach, and so on.
- Pod: the smallest unit Kubernetes runs. A pod usually holds one container (sometimes more) and is described in a YAML file.
- API: an interface that programs use to talk to each other. The Kubernetes API is the set of instructions you can send to the cluster, for example "create this pod". You send them with a command-line tool called
kubectlor with other software. - Privilege: permission to do something. "Apply privileges" means permission to send a YAML file to the cluster and have it take effect.
- kubelet: the Kubernetes program that runs on every node. It receives instructions about pods and makes them happen on that machine. It is the local worker, not the central brain.
- Volume: storage that a pod can use, which can be shared between the container and the machine it runs on (the host).
- hostPath: the location of a directory or file on the host machine.
- mountPath: the location inside the container where that host directory or file appears.
- subPath: a sub-property that lets you mount one specific file or directory from a volume at a chosen place, rather than the whole volume.
- PowerShell: the command-line shell and scripting language on Windows. A shell is a program that takes typed commands and runs them.
- Command injection: a class of vulnerability where untrusted input (something a user can control) is pasted into a command that the system then runs. If the input contains extra command syntax, the system runs that too. Think of a form that asks for a name and then prints "Hello, NAME": if you type a name containing instructions, and the program obeys them, that is injection.
- Remote code execution (RCE): the attacker can make the target machine run code of their choosing, from elsewhere.
- SYSTEM: on Windows, the built-in account with the highest privileges. Code running as SYSTEM can do essentially anything on that machine.
- CVE: Common Vulnerabilities and Exposures, a public numbering system for known security flaws. Each flaw gets an identifier such as CVE-2023-3676 so everyone can refer to the same thing.
- CVSS: a scoring system for the severity of a vulnerability, from 0 to 10. Higher is worse.
- Symlink: a shortcut file that points to another file or directory. Following a symlink can lead somewhere you did not expect, which is why software that handles paths often checks for them.
- Race condition: a bug that appears when two operations happen at nearly the same time and the order matters.
- Sanitisation: cleaning or checking input so that special characters cannot be interpreted as instructions.
- OPA (Open Policy Agent): an open source tool that inspects requests going into a system and can allow or deny them according to rules you write.
- Rego: the language in which OPA rules are written.
- RBAC (role-based access control): giving permissions based on a user's role, so each person can only do what their role allows.
- Namespace: a way of dividing a cluster into separate areas, so users and objects in one area do not mix with another.
- Proof of concept (PoC): a small demonstration that shows a vulnerability is real, usually the minimum needed to trigger it.
Introduction: why YAML matters here
YAML is a data serialisation language, similar to JSON, mainly used for configuration files. Kubernetes uses YAML files for basically everything: configuring the Container Network Interface (the part that gives containers network access), managing pods, and even handling secrets. Because the whole cluster is configured through YAML, the way Kubernetes reads and acts on YAML is an interesting place to look for mistakes.
How YAML and Kubernetes have been exploited before
Earlier flaws shaped the direction of this research.
- In 2022, CVE-2022-1471 was found inside the constructor of SnakeYAML, a well-known YAML parser (a parser is the piece of software that reads YAML text and turns it into data the program can use). The flaw allowed the creation of unsafe objects, which could lead to code execution on applications that used the parser.
- The Kubernetes security team wrote an advisory about CVE-2021-25749, a flaw that lets attackers bypass the check on who is allowed to run as root by misspelling their name in the YAML file.
- The researchers also came across CVE-2017-1002101 and CVE-2021-25741. These showed how attackers can use race conditions and symlinks together with the subPath sub-property in a YAML file to reach privileged data outside the container.
Those earlier bugs pointed the team towards path handling in the Kubernetes code, and that is where today's vulnerability was found. The key sentence from the research is this: an attacker with apply privileges, the privileges needed to interact with the Kubernetes API, can inject code that will be executed on remote Windows machines with SYSTEM privileges.
Vulnerability details
When a pod is created, the user can create a shared directory between the pod and the host. This feature is called volumes. It is useful, for example, when serving a website from a container in a pod: the site files can live in a directory shared with the host, so you can change the site layout without rebuilding the container image each time. (An image is the packaged template a container is started from.)
Volumes are enabled by including the volume parameter in the pod's YAML file. The mount locations are listed in mountPath (the location in the container) and hostPath (the location on the host). The subPath sub-property lets you mount a shared directory or file at a chosen location.
The component that parses the YAML file is kubelet, the central service responsible for running containerised applications on a node.
As part of the patch for CVE-2021-25741, kubelet validates every parameter in the YAML file, and also ensures that no symlinks are created as a result of using the subPath parameter, by calling an inner function named isLinkPath. That function takes the subPath supplied by the user in the YAML file and uses it to build a PowerShell command meant to determine the path type, that is, whether it is a symlink or not. The formatted PowerShell command is then immediately invoked by the exec.Command function call.
In plain words: kubelet wants to check whether the path you gave is a shortcut (symlink). To check, it asks Windows, through PowerShell, "what kind of thing is this path?". It builds that question as text, inserting your path into it, and runs it. If your path contains characters that PowerShell treats as instructions, PowerShell will follow those instructions too. The presence of exec.Command combined with unsanitised user-supplied input strongly hinted at a command injection opportunity.
Exploitation
Step 1: Understand how PowerShell evaluates text inside strings
PowerShell evaluates values inside strings before they are used. You do this by adding $(<experssion_to_be_evaluated>) to the string. The researcher's example:
PS> echo “the value of 1+1 is $(1+1)
Output:
the value of 1+1 is 2
Line by line:
PS>is the PowerShell prompt, the sign that PowerShell is waiting for a command.echois a command that prints text.- The text to print is
the value of 1+1 is $(1+1). The part inside$( ... )is not printed literally: PowerShell first works out the result of1+1, which is 2, and puts that result in place of the expression. - The output line shows the result:
the value of 1+1 is 2.
This example is simple, but in fact any PowerShell command can be inserted between the parentheses and will be evaluated, such as $(Start-Process cmd), $(Invoke-Expression exp) and other PowerShell treats. That is the heart of the bug: the parentheses are a door, and whatever is behind the door gets run.
Step 2: Put the expression in subPath
An attacker can abuse this subPath evaluation to reach the vulnerable code and execute any command they want with SYSTEM privileges (kubelet's own context) from remote nodes, and gain control over all Windows nodes in the cluster. A malicious subPath value is what triggers it.
Why this works: kubelet builds its PowerShell check using the subPath value from the YAML file. If that value contains $( ... ), PowerShell runs the contents before doing the check. Because kubelet itself runs with high privileges on the node, the injected command runs with those same privileges.
Step 3: Apply the malicious YAML
The proof of concept is simply a YAML file that contains the evaluation of the PowerShell command. You can find this file, as well as a video showcasing the terminal being started, in the researcher's GitHub repository. Applying the malicious file to the cluster is how the vulnerability is exploited.
This CVE led to the discovery and fix of more command injection vulnerabilities. They were collectively given the numbers CVE-2023-3955 and CVE-2023-3893.
Patch analysis
The Kubernetes team chose to patch this class of vulnerabilities by passing parameters from environment variables instead of from user input. An environment variable is a named value that the operating system hands to a running program, separate from the program's command text. By passing the values down in this manner, the parameters are treated as strings, so they will not be evaluated as expressions by PowerShell. In everyday terms: instead of writing your name into the middle of a sentence and hoping it is read as a name, the program is handed your name on a separate card and told "this is data, not instructions".
Am I vulnerable?
All Kubernetes versions below 1.28 are vulnerable to this CVE. There are several methods to determine whether one of the nodes in your cluster is a Windows node. One method is to run this command:
kubectl get nodes -o wide --show-labels | grep “os=windows”
Line by line:
kubectlis the command-line tool for talking to a Kubernetes cluster.get nodesasks the cluster for the list of nodes.-o wideasks for a wider output, with more columns of detail.--show-labelsasks for the labels attached to each node. A label is a small key/value tag, such asos=windows, used to describe a node.|is a pipe: it takes the output of the command on the left and hands it to the command on the right.grep “os=windows”filters the output, keeping only lines that contain the textos=windows.
So the whole line means: list all nodes with their labels, then show only the ones labelled as Windows. The output in the report looks like this:
akswin000000 Ready agent 4d17h v1.26.6 agentpool=win,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=windows…
akswin000001 Ready agent 4d17h v1.26.6 agentpool=win,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=windows… |
Reading the columns: the node name, its status (Ready), its role (agent), how long it has been up (4d17h), its Kubernetes version (v1.26.6), and then the labels, which include beta.kubernetes.io/os=windows. If lines like these appear, your cluster has Windows nodes, which is where this vulnerability applies.
Mitigation
Patching is the most reliable way to protect yourself from this vulnerability. In cases where patching is not an option, the researcher outlined a number of potential ways to protect against it, and you can choose the one that suits your environment best.
Previous solutions
For CVE-2023-3676, Kubernetes admins can disable the use of Volume.Subpath. This makes sure the cluster is safe against this vulnerability, but it disables a mechanism that is sometimes important for clusters in production.
OPA
Open Policy Agent (OPA) is an open source agent that allows users to receive data on traffic going in and out of nodes and take policy-based actions on the received data. OPA uses Rego as the language for its engine, in which admins can create rules to block certain YAML files from being implemented. The following rule denies creation of pods with a malicious subPath:
package kubernetes.admission
deny[msg] {
input.request.kind.kind == "Pod"
path := input.request.object.spec.containers.volumeMounts.subPath
not startswith(path, "$(")
msg := sprintf("malicious path: %v was found", [path])
}
Line by line:
package kubernetes.admissionnames the rule set. "Admission" refers to the moment a request is admitted into the cluster, before anything is created.deny[msg] { ... }declares a rule calleddenythat produces a messagemsgwhen the conditions inside the braces are all true.input.request.kind.kind == "Pod"checks that the request is about creating a Pod.inputis the incoming request that OPA is inspecting.path := input.request.object.spec.containers.volumeMounts.subPathtakes the subPath value out of the request and stores it in a variable calledpath.not startswith(path, "$(")is the condition that matters: it is true when the path does not start with$(. In other words, the rule fires for paths that do start with$(.msg := sprintf("malicious path: %v was found", [path])builds the message that will be returned, inserting the offending path.
So the rule blocks any pod whose subPath begins with $(, which is the shape of the attack. Note that this is a blocklist of one pattern, not a general fix.
RBAC
Role-based access control (RBAC) is a method that segments user operations according to who the user is. For example, each user can only create pods in their own namespace, or can only view information for allowed namespaces. This may help limit the number of users who can perform actions on a cluster and help focus attention on the ones who do have the relevant privileges for exploitation. In plain terms: fewer people with the keys means fewer people who can turn them the wrong way.
Conclusion
CVE-2023-3676 requires low privileges and therefore sets a low bar for attackers: all they need is access to a node and apply privileges. Successful exploitation leads to remote code execution on any Windows node on the machine with SYSTEM privileges.
High impact coupled with ease of exploitation usually means a higher chance of seeing this attack, and similar attacks, against organisations. The only limiting factor with this vulnerability is its scope: it is restricted to Windows nodes, which are not very popular today.
It is important that admins patch their Kubernetes clusters to the latest version available to prevent exploitation of these vulnerabilities. If patching is not available to you immediately, the researcher recommends using one of the methods described above to help defend against this vulnerability.
The researcher thanked the Kubernetes team for their very quick response and smooth communication.
Disclosure timeline
- 07/13/2023: vulnerability disclosed to the Kubernetes team.
- 07/19/2023: CVEs assigned by the Kubernetes team.
- 08/23/2023: Kubernetes released the CVE fixes.
- 09/13/2023: blog post published.

Glossary
- API: an interface that programs use to talk to each other; the Kubernetes API is the set of instructions you can send to a cluster.
- Apply privileges: permission to send a YAML file to the cluster and have it take effect.
- Command injection: a vulnerability where untrusted input is pasted into a command that the system runs, so the input can add extra commands.
- Container Network Interface: the part of Kubernetes that gives containers network access.
- Environment variable: a named value the operating system hands to a running program, separate from the program's command text.
- exec.Command: the function call in the code that runs an external command; here it runs the PowerShell check.
- Image: the packaged template a container is started from.
- isLinkPath: the kubelet function that checks whether a path is a symlink, by building and running a PowerShell command.
- kubelet: the Kubernetes program that runs on every node and makes pods happen on that machine.
- kubectl: the command-line tool for talking to a Kubernetes cluster.
- Kubernetes: software that manages containers across one or more machines.
- Label: a small key/value tag attached to an object such as a node, for example
os=windows. - mountPath: the location inside the container where a host directory or file appears.
- Namespace: a way of dividing a cluster into separate areas.
- OPA (Open Policy Agent): an open source tool that inspects requests going into a system and allows or denies them according to rules.
- Parser: the piece of software that reads text in a format such as YAML and turns it into data the program can use.
- Pod: the smallest unit Kubernetes runs, usually one container, described in a YAML file.
- Privilege: permission to do something.
- Proof of concept (PoC): a small demonstration that shows a vulnerability is real.
- RBAC (role-based access control): giving permissions based on a user's role.
- Sanitisation: cleaning or checking input so special characters cannot be interpreted as instructions.
- subPath: a sub-property that lets you mount one specific file or directory from a volume at a chosen place.
- Volume: storage a pod can use, which can be shared between the container and the host.
- YAML: a plain-text, indentation-based format for configuration files, similar to JSON.