Kubernetes privileged Pods and securityContext: red vs blue

Vinnie, on the red team, shows why a privileged Pod running as root with host access is barely a container at all; Space Llama, on the blue team, closes it with a tight securityContext and the Pod Security Standards' restricted level.

GazeekKUBERNETES SECURITY: RED VS BLUE·EPISODE 2
EPISODE 2

Kubernetes privileged Pods and securityContext: red vs blue

Vinnie, on the red team, shows why a privileged Pod running as root with host access is barely a container at all; Space Llama, on the blue team, closes it with a tight securityContext and the Pod Security Standards' restricted level.

0:00 / 3:58

Diagrams in this video · 1Free in archhy

Copy and edit in archhy: How a privileged Pod reaches the node (new tab)

Now try it on a real cluster

The videos are free. Gazeek's hands-on labs give you a real Kubernetes cluster in your browser for every lab, with checks that tell you when you've got it right: the practice the CKA and CKAD exams need.

The diagrams, full size

Copy any of them into archhy, free, and change it for your own system.

After this lesson you can

  • Explain why privileged mode, host namespaces and hostPath weaken container isolation
  • Write a securityContext that passes the restricted Pod Security Standard
  • Find Pods in your cluster that would fail the restricted level
Transcript

Kubernetes privileged Pods and securityContext: red vs blue

Space Llama. Blue team. Round two. Vinnie. Red team. And today, I'm getting out of my container. Privileged Pods. And Pods that share too much with their node. A container's a wall. Some people build theirs out of paper.

How a privileged Pod reaches the node

Here's what my nose finds, from the red side. One. The app runs as root. Without a user namespace, if anything breaks out of that container, it's root on the node. Two. Privileged, true. That hands the container every Linux capability, and switches off seccomp, AppArmor and SELinux for it. Three. Host access. A hostPath mount puts the node's files inside. Host PID, the node's process ID namespace, shows every process on the node. Host network shares the node's network. Four. The node. Its files hold the kubelet's credentials and the container runtime's socket. The Kubernetes docs say it plainly: those can be used for container escape. And a privileged container can reach every Secret used on that node. So the wall was a door. Here's how blue shuts it. Propped open. With a welcome mat.

Blue closes it: the securityContext

Blue's turn. Run as non-root, true. The kubelet won't start a container whose image would run as root. Seccomp profile, runtime default. That's the container runtime's own filter on system calls. Per container: no privilege escalation, and drop all capabilities. A read-only root filesystem isn't required, but it locks the image's own files. Writable volumes stay writable. And what's not here matters too. No privileged. No host namespaces. No hostPath. Leave them out, and they're off.

Blue closes it: make it the rule

Good habits drift. So make it the rule. Label the namespace: pod security, enforce, restricted. Restricted is the strictest of the three Pod Security Standards. It refuses privileged Pods, host namespaces and hostPath, and it requires most of what you just saw. Episode six goes deeper.

Red asks, blue answers

Question for blue. What if my app really needs root? Usually it needs one thing root gives it, like a port below 1024. Drop all, and add back only net bind service. Restricted allows that one. Or run it in a user namespace. Host users, false. Root inside the container, an ordinary user on the node. Stable since Kubernetes 1.36. Question for red. What do you sniff out first? Any hostPath mount. Especially one that reaches the container runtime's socket. That's not a volume. That's a door handle.

Check your own cluster

Now check your own cluster. Kubectl label, with dry run equals server, on all namespaces: pod security enforce equals restricted. It changes nothing. It warns you about every running Pod that wouldn't pass. Then go through the list. Privileged Pods, hostPath mounts, any host namespace, network, process or inter-process, and anything running as root. Each one needs a reason. Most don't have one.

Who won?

Round two. I'll give it to blue. On points. On points? Somebody always keeps one privileged Pod. With a very good excuse. Then it gets its own namespace. And a long conversation.

The full article, and what's next

Next time, secrets in plain sight. Bring your base64. I'll bring my nose.

Hands-on labs

Now try it on a real cluster

A real Kubernetes cluster in your browser for every lab, checked as you go.

See the labs

Read the article

Pod Security Standards: the whole idea in writing, free.

Read it ↗