Containers Running as Root
Containers run their processes as root, so compromising the application gives an attacker root inside the container and makes an escape to the node easier.
| Severity | Medium |
| CVSS 3.1 | 5.3 CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:C/C:L/I:L/A:L Open in the calculator |
| CVSS 4.0 | 2 CVSS:4.0/AV:L/AC:L/AT:P/PR:L/UI:N/VC:L/VI:L/VA:L/SC:L/SI:L/SA:L Open in the calculator |
| CWE | CWE-250: Execution with Unnecessary Privileges |
| MITRE ATT&CK | T1611 Escape to Host |
| Category | Security Misconfiguration |
Description
One or more containers run their processes as root (user ID 0). A container runs as the user its image specifies, and many images set no USER instruction and so run as root; Kubernetes keeps that user unless runAsUser or runAsNonRoot is set in the pod's or container's securityContext.
Unless user namespaces are in use, root in a container is the same user ID as root on the node. Running as root does not by itself break the container's isolation, since the container's capability set, seccomp profile and other runtime controls still restrict it, but it removes a layer of defence. An attacker who compromises the application gains full control of the container, including every file in it, and can install tools or change the application.
Files the node shares with the container, such as mounted volumes, are accessed as root, and many container escape techniques and runtime vulnerabilities, for example the runc flaw CVE-2019-5736, require root inside the container. Where the container also has added capabilities or a writable host mount, or runs on an unpatched kernel or container runtime, root in the container makes an escape to the node considerably more likely.
The issue can be confirmed by running id in the container, by reviewing the image's USER instruction and the pod's securityContext, or from configuration scanners and Kubernetes benchmark tools, which report containers that may run as root.
Remediation
Run every container as a dedicated non-root user:
- Create an unprivileged user in the image and select it with a
USERinstruction, using a numeric user ID so that Kubernetes can verify it is not root. - Set
runAsNonRoot: trueand explicitrunAsUserandrunAsGroupvalues in the pod or containersecurityContext, so that the kubelet refuses to start a container that would run as root. - Combine this with
allowPrivilegeEscalation: falseand dropping all capabilities, so the process cannot regain root through setuid binaries. Where the application listens on a port below 1024, have it listen on a higher port and map the Service port to it. - Enforce the Restricted Pod Security Standard on application namespaces so that new workloads must run as non-root.
- Where a workload must run as root, use user namespaces (
hostUsers: false) on clusters that support them, or Docker's rootless mode or user namespace remapping, so that root in the container is not root on the node.
Confirm the fix by running id in each affected container, and by checking that a test pod running as root is rejected in namespaces that enforce the Restricted standard.
References
Add This Finding to Your Library
In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.
Free trialYour trial starts when your portal goes live, not when you sign up, so the time you spend setting up does not count. · No card required
Only need the reporting? Our reporting tool is free and open source, with the same library.