KCSA Compromised Containers, Network Attacks and Privilege Escalation
This Kubernetes threat-model topic covers what an attacker does after getting code execution in a container: escaping to the node, moving across the Pod network, reaching sensitive data and escalating privileges. This lesson follows the attacker through privileged containers, host namespaces and Linux capabilities, hostPath mounts and the container runtime socket, service account tokens and what a stolen one grants, Secrets at rest and in environment variables, the blast radius of a compromised node, lateral movement on the flat Pod network and how NetworkPolicy contains it, and the RBAC and Pod-creation paths to more privilege.
On this page10 sections
- What happens when an attacker runs code in a container
- Privileged containers and host namespaces: escape to the node
- Linux capabilities and allowPrivilegeEscalation
- hostPath mounts: reading the node's credentials
- Service account tokens: where they live and what a stolen one grants
- Secrets at rest and in use: base64 is not encryption
- A compromised node: blast radius, the Node authorizer and NodeRestriction
- Attackers on the Pod network: lateral movement
- Containing the attacker with NetworkPolicy
- Privilege escalation through RBAC and Pod creation
- Explain what an attacker inherits from a compromised container or a malicious image, and why privileged mode, host namespaces and CAP_SYS_ADMIN lead to the node
- Tell apart capabilities drop ALL, allowPrivilegeEscalation, runAsNonRoot, readOnlyRootFilesystem and seccomp by the attack each one blocks
- Identify what hostPath mounts of the node root, the kubelet directories and the container runtime socket expose
- Judge the blast radius of a stolen service account token, compare bound and legacy tokens, and explain why Secrets need encryption at rest with a KMS provider
- Describe lateral movement on the flat Pod network and contain it with ingress and egress NetworkPolicy, including DNS and the metadata endpoint
- Recognise RBAC and Pod-creation privilege-escalation paths, and the limits the Node authorizer and NodeRestriction place on a compromised node
What happens when an attacker runs code in a container
When an attacker gets code execution in a container, they inherit everything the Pod was given: its mounted service account token, its volumes and Secrets, its Linux capabilities and its reach on the Pod network. The threat model starts from that inheritance, because a compromised container is rarely the goal. It is a foothold for reaching the API server, other Pods, the node or the data the cluster holds.
Code execution arrives in two common ways:
- A compromised application: a vulnerability in the app (remote code execution, a deserialisation bug, a web shell) lets the attacker run commands as the container's process.
- Malicious code in the image: a third-party or tampered image ships a cryptominer, scanner or backdoor. Its entrypoint runs as soon as the container starts.
Either way, the code runs with the Pod's rights and nothing more. What it gets is listed below, and each item is a path covered later in this lesson.
| What the Pod holds | What the attacker can do with it | Main control |
|---|---|---|
| Automounted service account token | Call the API server as that service account, within its RBAC | Least-privilege RBAC; automountServiceAccountToken: false where the app never calls the API |
| Network reach | Connect to other Pods, ClusterIP Services, the node and external hosts | NetworkPolicy (ingress and egress) |
| Mounted Secrets and environment variables | Read credentials for databases and other systems | Mount only the Secrets a Pod needs; prefer files to env vars |
| Capabilities, privileged mode, host namespaces, hostPath | Break out of the container onto the node | securityContext hardening, enforced by Pod Security Admission |
Hardening the Pod therefore shrinks what any future compromise can do. Configuring those settings is taught in the Pod security and admission lessons; here they appear through the attacker's eyes.
Privileged containers and host namespaces: escape to the node
A container started with privileged: true is treated as equivalent to root on its worker node, because it runs with all Linux capabilities and access to the host's devices. An attacker inside it can mount the node's disks, load kernel modules and interfere with every other workload on that node. Privileged mode removes most of the confinement that normally keeps a container's process inside its namespaces.
The three host-namespace settings each share one of the node's namespaces with the container, and each exposes something different:
| Setting | Namespace shared | What a compromised container sees or reaches |
|---|---|---|
hostPID: true | The node's process ID namespace | Every process on the node, including other Pods' containers, through ps and /proc |
hostNetwork: true | The node's network namespace | The node's interfaces and every port the node's own services listen on, including services bound only to the node's localhost |
hostIPC: true | The node's IPC namespace | Shared memory segments other workloads on the node may use |
shareProcessNamespace: true | One PID namespace across the Pod's own containers | Only the processes of containers in the same Pod, not the node |
Running as UID 0 is a different matter. runAsUser: 0 makes the process root inside the container. With the default capability set and no privileged mode, host namespaces or host mounts, that root process is still confined to the container's namespaces. It is a weaker posture than running as non-root, but on its own it is not a path to the node. The escape paths are privileged mode, host namespaces, dangerous capabilities and host mounts.
Linux capabilities and allowPrivilegeEscalation
Linux capabilities split root's power into separate privileges, and the ones a container holds decide how far a compromised process can reach into the kernel; CAP_SYS_ADMIN is the broadest and the classic lever for container escape. It bundles a wide range of administrative operations, mounting filesystems among them, into one grant, which is why it is often described as "almost root".
Other capabilities are narrower and do not break isolation by themselves:
NET_BIND_SERVICElets a process bind ports below 1024.CHOWNlets a process change file ownership inside the container.KILLlets a process signal other processes in its own PID namespace.NET_RAWlets a process open raw sockets and craft its own packets, which matters for network attacks (see the section on attackers on the Pod network).
The container runtime grants a default capability set to every container unless the Pod changes it. Five securityContext settings are often confused, because each closes a different gap:
| Setting | What it does | What it does not do |
|---|---|---|
capabilities: drop: ["ALL"] (plus add for anything truly needed) | Removes the runtime's default capability set, leaving the attacker as little kernel privilege as the app allows | Does not filter syscalls or stop setuid gains on its own |
allowPrivilegeEscalation: false | Sets the kernel's no_new_privs flag, so running setuid binaries or binaries with file capabilities cannot raise a process above its parent's privileges | Does not remove the starting capability set, and does not change the UID the container starts as |
runAsNonRoot: true | Refuses to start the container if it would run as UID 0, for example when the image's USER is root | Does not drop capabilities |
readOnlyRootFilesystem: true | Stops the process writing tools or changes onto the container's root filesystem | Does not remove any capability |
seccompProfile: type: RuntimeDefault | Filters the system calls the process may make | Does not remove any capability |
The Baseline Pod Security Standard lets a Pod add only capabilities from a short default list (AUDIT_WRITE, CHOWN, DAC_OVERRIDE, FOWNER, FSETID, KILL, MKNOD, NET_BIND_SERVICE, SETFCAP, SETGID, SETPCAP, SETUID, SYS_CHROOT). SYS_ADMIN is not on it, so admission rejects a Pod that adds it. Restricted goes further and requires dropping ALL, adding back at most NET_BIND_SERVICE.
hostPath mounts: reading the node's credentials
A hostPath volume mounts a directory from the worker node into the Pod, so a compromised Pod with a sensitive hostPath can read or change whatever lives at that path on the node, including kubelet credentials and the tokens of other Pods. Kubernetes' own documentation warns that hostPath volumes carry many security risks and should be avoided; the Baseline Pod Security Standard forbids them.
Which path is mounted decides what is exposed:
| Node path | What it holds |
|---|---|
/ (the node root) | Everything below, including /var/lib/kubelet and /etc/kubernetes: the kubelet's client credentials and every co-located Pod's mounted tokens and Secrets |
/var/lib/kubelet/pods | The per-Pod directories where the kubelet stages each Pod's volumes, so neighbouring Pods' projected tokens and mounted Secrets |
/var/lib/kubelet/pki and the kubelet kubeconfig under /etc/kubernetes | The kubelet's own client certificate and key for authenticating to the API server (outside the pods directory) |
The container runtime socket, e.g. /run/containerd/containerd.sock | The runtime's control interface (see below) |
The rule of thumb: a worker node holds its own kubelet credentials and the material for the Pods scheduled on it. Control-plane data and keys live on control-plane hosts, and a Secret reaches a worker's disk only when a Pod there mounts it.
The container runtime socket
Mounting the runtime socket hands the node to whoever controls the Pod. The socket accepts the same create, start and exec calls the kubelet makes, so a client on it can start new containers on the node with any settings, privileged ones included. Those containers never go through the API server, so RBAC, admission control and Pod Security Admission never see them. Monitoring and logging DaemonSets that mount it are high-value targets for exactly this reason.
Service account tokens: where they live and what a stolen one grants
When automounting is enabled, Kubernetes mounts a Pod's service account token as a file at /var/run/secrets/kubernetes.io/serviceaccount/token, next to the cluster CA certificate (ca.crt) and the Pod's namespace, and any process in the container can read it. The token is delivered through a projected volume as a file.
A service account token is a bearer credential. The API server checks its signature and expiry, and for a bound token it checks that the Pod it was issued for still exists. It does not check where the request comes from. An attacker who copies the token out of the Pod can use it from anywhere that can reach the API server.
The token's power is exactly the RBAC bound to its service account, no more and no less. If the account's only RoleBinding grants read access to ConfigMaps in one namespace, the stolen token can read ConfigMaps in that namespace and nothing else. ConfigMaps and Secrets are separate resources, so a rule on one grants nothing on the other. A workload token is not cluster-admin unless someone bound cluster-admin to it, and it cannot impersonate users unless its RBAC includes the impersonate verb. Least-privilege service accounts are the main way to shrink what a stolen token is worth. For workloads that never call the API, automountServiceAccountToken: false removes the credential altogether.
Bound tokens versus legacy Secret-based tokens
| Property | Projected bound token (current default) | Legacy token stored in a Secret |
|---|---|---|
| Expiry | Time-bound; the kubelet refreshes it before it expires | Does not expire; no longer auto-created since v1.24 |
| Audience | Carries an audience claim; verifiers expecting another audience reject it | No per-use audience |
| Binding | Bound to its Pod object; deleting the Pod invalidates it | Valid until the Secret is deleted |
| Where it is stored | Requested through the TokenRequest API and mounted into the Pod; not stored as a Secret | A Secret in etcd, readable by anyone allowed to read Secrets in the namespace |
A bound token is limited by its expiry, its audience and the life of the Pod object it is bound to. A container restart keeps the same Pod object, so the binding still holds; deleting the Pod is what invalidates it.
Secrets at rest and in use: base64 is not encryption
On an upstream Kubernetes cluster with default settings, Secret values are stored in etcd base64-encoded, not encrypted, so anyone who can read etcd or an etcd backup can decode them. Base64 is an encoding that anyone can reverse. Secrets are persisted to etcd like any other object, and they must be stored reversibly because workloads need the original values. Encryption at rest stays off until an administrator gives the API server an EncryptionConfiguration file through --encryption-provider-config. Some managed Kubernetes services turn on envelope encryption for you, but that is the provider's setting, not the upstream default.
An EncryptionConfiguration lists providers for each resource. The first provider in the list encrypts new writes; the others are only tried when reading existing data. Putting identity (no encryption) first means new Secrets are written in plaintext, whatever follows it.
| Provider | Where the key lives | Does a stolen etcd backup plus the control-plane config files expose Secrets? |
|---|---|---|
identity | No key; data is not encrypted | Yes, the data was never encrypted |
aescbc, aesgcm, secretbox (local providers) | In the EncryptionConfiguration file on the control-plane host | Yes: the key travels with the configuration file, however strong the cipher |
kms v2 (envelope encryption) | The key-encryption key stays in an external key management service; etcd holds the encrypted data and the data-encryption keys only in wrapped (encrypted) form | No: the key that unwraps the data-encryption keys never leaves the KMS |
Local providers protect against someone who copies only the etcd data files. They do not help if the attacker also has the control-plane host's configuration, because the key is written in it. Kubernetes recommends a KMS v2 provider for that reason. Beyond encryption, restrict who can read etcd directly, protect etcd backups, and limit get, list and watch on Secrets in RBAC, because anyone allowed to read a Secret through the API sees it decrypted.
Secrets in environment variables versus mounted files
A Secret injected as an environment variable is exposed more widely than the same Secret mounted as a file, because the environment is inherited by every child process and is commonly printed in logs, error reports and crash dumps. Once an application is compromised, or simply logs its environment on a crash, an env-injected password spreads further than a file the app reads when it needs it.
Neither form is encrypted inside the container: a process that can read the file or the environment sees the value in clear. The difference is how many places the value ends up. The kubelet keeps mounted Secret volumes in memory-backed storage (tmpfs) on the node, and it updates mounted files when the Secret changes, whereas environment variables keep the value they had when the container started.
Good practice from the Kubernetes security checklist follows from this: mount only the Secrets each container needs, prefer volumes to environment variables, and give each application its own Secrets so one compromise does not expose another app's credentials. A node-level attacker can still read every Secret mounted into Pods on that node, which is covered next.
Attackers on the Pod network: lateral movement
Kubernetes networking is flat by default: once a CNI plugin is installed and no NetworkPolicy exists, every Pod can connect to every other Pod and every ClusterIP Service in the cluster, across nodes and namespaces. An attacker who compromises one front-end Pod can immediately reach databases, internal admin APIs and other teams' workloads. Policies are not needed to permit traffic; they exist to restrict it.
Two assumptions fail under this model:
- "Namespaces isolate teams." Namespaces scope names, RBAC and quotas. They do not separate network traffic. A Pod in
devcan resolve and connect topayments.prod.svc.cluster.local: cluster DNS resolves fully qualified Service names in any namespace (a short name defaults to the caller's namespace, but the full name works). RBAC bindings play no part, because RBAC governs requests to the Kubernetes API, not connections between Pods. - "ClusterIP means private." A ClusterIP Service has no external address, but every Pod in the cluster can route to it. It is not published to the internet, and it does not bypass the backend's own authentication; the risk is internal reach. An internal admin API on a ClusterIP is open to any compromised Pod until a NetworkPolicy forbids it.
Layer 2 attacks and NET_RAW
An attacker on the same node can try to forge packets and spoof ARP replies to intercept traffic between neighbouring Pods. Crafting raw packets needs the NET_RAW capability. containerd's default capability set includes NET_RAW; CRI-O's default set already omits it. Dropping it in the container's securityContext (for instance with drop: ["ALL"]) removes the ability. A NetworkPolicy does not help here: it filters connections to and from Pods, not the forging of packets at layer 2.
Containing the attacker with NetworkPolicy
NetworkPolicy is the native Kubernetes control for limiting lateral movement: it selects Pods and allows only the ingress and egress traffic its rules list, and once any policy selects a Pod for a direction, everything else in that direction is denied. It is enforced by the CNI plugin, so a plugin without NetworkPolicy support silently ignores the objects. Other controls do not substitute for it: Pod Security Admission limits what a Pod spec may request, RBAC authorizes API requests, and disabling the token removes an API credential, but none of them filters Pod-to-Pod traffic.
The usual pattern is a default-deny policy per namespace followed by narrow allows. Ingress and egress are independent, and that independence is where containment often fails:
- A namespace with default-deny ingress and an allow for frontend Pods from the ingress controller protects Pods receiving traffic. A compromised frontend Pod's outbound connections to other namespaces and to external hosts stay open, because no egress policy selects it. Connections to Pods in its own namespace fail only because the destination's ingress default-deny drops them.
- Containment of a compromised workload needs egress policies as well as ingress ones.
Egress rules, DNS and the metadata endpoint
Egress default-deny breaks name resolution, so policies typically allow UDP and TCP port 53 to the cluster DNS Pods. For internet egress, teams often allow ipBlock 0.0.0.0/0 and list the Pod CIDR and Service CIDR under except, so in-cluster destinations stay blocked: Pods in other namespaces and ClusterIP Services (whose backend Pod IPs are also in the Pod CIDR) are denied.
The gap in that pattern is the cloud instance metadata endpoint at the link-local address 169.254.169.254. It sits inside 0.0.0.0/0 and outside both excepted ranges, so the policy allows it. On cloud nodes the metadata service can hand out node or instance credentials, so a broad egress allow should add 169.254.169.254/32 to except explicitly.
Privilege escalation through RBAC and Pod creation
Kubernetes RBAC prevents a user from creating or editing a Role, or creating a binding, that grants more permissions than the user already holds, unless they have the escalate or bind verb; over-broad grants of those verbs, or of a few other powerful permissions, are the RBAC privilege-escalation paths.
| Permission | Why it escalates |
|---|---|
escalate on roles / clusterroles | Lets the holder create or update a Role or ClusterRole with rules beyond their own permissions |
bind on roles / clusterroles, plus create on rolebindings | Lifts the rule that you can only grant what you hold: the holder can bind an existing powerful role, even one they lack, to themselves |
impersonate on users, groups or service accounts | Lets the holder act as another identity and use its rights |
create on serviceaccounts/token | Lets the holder mint a fresh token for any service account in the namespace through the TokenRequest API |
create on pods (or on workloads that create Pods) | Lets the holder run a Pod as any service account in the namespace, or with host access (next section) |
get / list / watch on secrets | Reveals credentials, including any legacy service account tokens stored as Secrets |
escalate and bind are easy to confuse. escalate governs authoring Role and ClusterRole objects. bind governs binding an existing role. Someone with create on rolebindings in team-a and bind on clusterroles needs no escalate to grant themselves a powerful ClusterRole: a RoleBinding may reference a ClusterRole, and it then grants that role's permissions inside the RoleBinding's namespace.
A token minted through serviceaccounts/token authenticates as the service account and carries its full permissions, not an intersection with the caller's. No Secret is read: the TokenRequest API returns the new token directly. The kubelet uses the same API for Pods, but any caller authorized for that subresource can use it too, so minting a token for an account is becoming that account.
Privilege escalation through Pod creation
Permission to create Pods is a privilege-escalation path on its own, because the creator chooses the Pod's service account, securityContext and volumes. Kubernetes' RBAC good practices list it among the permissions to grant carefully. Two separate routes exist, and different controls close each one.
Route 1: host access. If the namespace enforces no Pod Security Standard, the user can create a privileged Pod that mounts the node's root filesystem through hostPath, read the node's credentials and escape to the host. The escape comes from the host-level settings, not from simply running as root inside an otherwise default container. Enforcing Baseline or Restricted through Pod Security Admission closes this route: Baseline rejects privileged mode, host namespaces, hostPath volumes and added capabilities outside its default list.
Route 2: borrowing a stronger service account. A Pod can run as any service account in its own namespace by setting serviceAccountName, and Pod Security Admission does not restrict that field. If the namespace holds an account bound to, say, cluster-wide get and list on Secrets, the user launches a Pod as that account and reads the token it mounts. Baseline does not stop this, because nothing in the Pod is privileged. A Pod cannot use or project tokens for a service account in another namespace such as kube-system, which is why powerful service accounts belong in namespaces that only administrators can create Pods in.
| Route | What the Pod asks for | Stopped by Pod Security Admission? | What closes it |
|---|---|---|---|
| Host access | Privileged mode, host namespaces, hostPath, extra capabilities | Yes, at Baseline or Restricted | Enforce a Pod Security Standard |
| Borrowed service account | An ordinary Pod with a powerful serviceAccountName | No | Keep powerful service accounts out of namespaces where others can create Pods; restrict create on Pods and workload controllers |
Tip. KCSA questions on this topic take the attacker's point of view: a container, Pod, token or node has been compromised, and you are asked what the attacker can now reach or which control limits them. Expect to identify what privileged mode, a host namespace setting or a capability exposes; what a particular hostPath mount reveals; where the service account token lives and how far a stolen one goes; why base64 Secrets need encryption at rest and which provider survives theft of the control-plane config; and why env-var Secrets spread further than files. Network questions ask how far a compromised Pod reaches with no policies, across namespaces or to a ClusterIP, and what an ingress-only or broad egress policy still leaves open. Escalation questions name an RBAC grant or Pod-creation right and ask what it enables. Distractors are real Kubernetes controls that act on the wrong layer: RBAC offered for a network problem, Pod Security Admission for a borrowed service account, an ingress policy for egress, or a securityContext field that closes a different gap.
- Code running in a container inherits exactly what its Pod holds: its service account token, volumes, Secrets, capabilities and network reach.
- privileged: true, host namespaces, CAP_SYS_ADMIN and sensitive hostPath mounts are the main container-escape paths; hostPID shows the node's processes, hostNetwork its network, hostIPC its shared memory.
- drop ALL removes default capabilities, allowPrivilegeEscalation: false sets no_new_privs against setuid gains, and neither replaces the other.
- Mounting the node root, /var/lib/kubelet/pods or the container runtime socket exposes node credentials, neighbours' tokens or the whole node, bypassing the API server.
- A service account token is a bearer credential worth exactly its RBAC; bound tokens limit theft through expiry, audience and Pod binding.
- Secrets are only base64 in etcd until encryption at rest is configured; local providers keep the key in the config file, KMS v2 keeps it outside the cluster.
- Without NetworkPolicy every Pod reaches every Pod and ClusterIP Service across namespaces; containment needs egress rules, and a 0.0.0.0/0 allow must except 169.254.169.254.
- escalate, bind, impersonate, serviceaccounts/token and create on Pods are privilege-escalation permissions; Pod Security Admission does not stop a Pod borrowing a powerful service account in its namespace.
- The Node authorizer confines a kubelet to its own node's Pods' objects and NodeRestriction limits what it can modify, so one compromised node exposes only its own Pods' Secrets.
Check yourself: 3 questions on this topic
Exam-style KCSA questions on Compromised containers, network attackers, sensitive data and privilege escalation, answered right here with an explanation after each. Want more? KCSA Kubernetes Threat Model practice questions.
Frequently asked questions
Why is a privileged container considered equivalent to root on the node?
A container with privileged: true runs with all Linux capabilities and access to the host's devices, so most of the isolation that keeps a container inside its namespaces is gone. An attacker inside it can mount the node's disks, load kernel modules and affect every other workload on the node, which is why Kubernetes threat models treat privileged mode as a node breakout.
What can an attacker do with a stolen Kubernetes service account token?
A stolen service account token lets the attacker call the Kubernetes API server as that service account, from anywhere that can reach the API, with exactly the permissions its RBAC bindings grant. A token whose account can only read ConfigMaps in one namespace is worth only that. Projected bound tokens also expire, carry an audience claim and stop working when their Pod is deleted; legacy tokens stored in Secrets do not expire.
Are Kubernetes Secrets encrypted in etcd?
Not by default on upstream Kubernetes. Secret values are stored in etcd base64-encoded, which anyone can decode, until an administrator configures encryption at rest with an EncryptionConfiguration file. Local providers such as aescbc, aesgcm and secretbox keep their key in that file on the control-plane host, so the KMS v2 provider, which keeps the key-encryption key in an external key management service, is the recommended choice. Some managed Kubernetes services enable envelope encryption for you.
Do Kubernetes namespaces isolate network traffic?
No. Kubernetes namespaces scope object names, RBAC and resource quotas, but the Pod network is flat: with no NetworkPolicy, a Pod in one namespace can connect to Pods and Services in any other namespace, for example by their fully qualified DNS names. Network isolation between namespaces needs NetworkPolicy objects enforced by a CNI plugin that supports them.
Why should an egress NetworkPolicy exclude 169.254.169.254?
An egress NetworkPolicy that allows ipBlock 0.0.0.0/0 also allows the cloud instance metadata endpoint at 169.254.169.254, because that link-local address is inside 0.0.0.0/0 and outside the usual Pod CIDR and Service CIDR exceptions. On cloud nodes the metadata service can hand out instance credentials, so a compromised Pod could fetch them. Adding 169.254.169.254/32 to the ipBlock's except list blocks it.
What is the difference between the escalate and bind verbs in Kubernetes RBAC?
In Kubernetes RBAC, escalate on roles or clusterroles lets a user create or edit a Role or ClusterRole containing permissions they do not hold themselves. bind lets a user create a RoleBinding or ClusterRoleBinding to an existing role whose permissions they do not hold. Both lift RBAC's privilege-escalation prevention, so both should be granted only to trusted administrators.
Why is permission to create Pods a privilege-escalation risk?
Whoever creates a Pod chooses its service account, securityContext and volumes. Without a Pod Security Standard they can run a privileged Pod with a hostPath to the node root and escape to the host. Even with Baseline or Restricted enforced, they can set serviceAccountName to any service account in the namespace and read its mounted token, inheriting that account's RBAC.
What do the Node authorizer and NodeRestriction protect against?
The Node authorizer limits each kubelet's API reads to the Secrets, ConfigMaps and volumes referenced by Pods bound to its own node, and the NodeRestriction admission plugin limits a kubelet to modifying its own Node object and its own Pods. Together they mean an attacker who steals one node's kubelet credential cannot read other nodes' Secrets or tamper with other nodes.
Source
This lesson covers the "Kubernetes Threat Model" domain of the official KCSA exam guide. Vendors revise their guides — check the source for the current version.
- KCSA certification page and curriculum — The Linux Foundation
Sign up free to mark lessons complete, bookmark topics and track your exam readiness.