Sub topic 5
Blocking privilege gain with no-new-privileges
This chapter assumes you can run containers with docker run, know what a setuid file is, and have read the earlier chapters on capabilities and non-root users. Dropping capabilities and running as a non-root user both shrink what a process can do. This chapter covers the flag that stops the process from getting that power back.
The problem: privileges that return on exec
Linux can raise a process's privileges when it runs a new program with execve. Two mechanisms do this:
- A setuid binary runs with the file owner's user ID, often root, whatever user started it.
- A file capability attaches capabilities to an executable, and the process gains them when it runs that file.
Suppose you run as user 10001 and drop all capabilities. If the image contains a setuid-root binary, an attacker who gets code execution can run that binary. Any bug in it then becomes a route back to root. Removing every setuid file from an image is hard, so you also need a control at the kernel level.
What no-new-privileges does
no-new-privileges sets a per-process kernel flag named no_new_privs, set through the prctl system call. After the flag is set:
execveno longer grants extra privileges from setuid or setgid bits.- File capabilities are not added to the process.
- Every child process inherits the flag.
- The flag cannot be cleared again. Even root inside the container cannot unset it.
The flag does not remove privileges the process already has. A process running as root with capabilities keeps them. It only blocks gaining more. That is why it works as a complement to the other controls and not a replacement.
Enabling it in Docker
Pass it as a security option:
docker run --rm \
--security-opt no-new-privileges:true \
alpine grep NoNewPrivs /proc/self/statusThe output is NoNewPrivs: followed by 1. Run the same command without the --security-opt line and the value is 0. The /proc/self/status file is the quickest way to confirm the flag from inside the container.
A typical hardened command combines it with the controls from earlier chapters:
docker run --rm \
--cap-drop ALL \
--security-opt no-new-privileges:true \
--user 10001:10001 \
alpine idEnabling it in Kubernetes
Kubernetes has no field with the same name. The container-level securityContext field allowPrivilegeEscalation controls the flag:
securityContext:
allowPrivilegeEscalation: falseWhen this is false, the container runtime sets no_new_privs on the container process. Kubernetes ignores the setting in two cases. It is always effectively true if the container is privileged: true or if it adds CAP_SYS_ADMIN. Do not rely on false in those cases.
Common errors
- Assuming it replaces dropping capabilities. It does not remove capabilities the process already holds.
- Expecting setuid tools to work. With the flag on,
sudo, andsuthat rely on setuid cannot raise privileges. Programs that must escalate on purpose, for example a start script that usessudoto bind a low port, will fail. Fix the image so it starts as the right user, instead of turning the flag off. - Setting it in the wrong place. In Kubernetes,
allowPrivilegeEscalationgoes under a container'ssecurityContext. It is not valid in the pod-levelsecurityContext. - Skipping verification. Check
NoNewPrivsin/proc/self/statusafter every change. A missing flag fails silently.
Exercise
Exercise
A container runs as user 10001 with all capabilities dropped. The image contains a setuid-root binary. Which setting stops an attacker from using that binary to become root, and how can you confirm the setting from inside the container?
Show answer
Set --security-opt no-new-privileges:true in Docker, or allowPrivilegeEscalation: false in the Kubernetes container securityContext. Confirm it by running grep NoNewPrivs /proc/self/status inside the container. The value must be 1.