One bug, and then what?
A bug in a web page lets an attacker run one command inside the container. What happens next is not decided by the bug.
It is decided by the container's settings: who it runs as, what it may write, and which kernel calls it may make.
The one rule
Warning
Everything in this module happens in the cluster of your own lab. Testing a cluster you do not own needs the owner's written permission. In the UK, without it, it can break the Computer Misuse Act 1990.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can run a pod with a read-only root, no capabilities and a seccomp filter.
Key words
| Word | What it means | Example |
|---|---|---|
| Security context | the security settings of a pod or container | |
| Capability | one piece of root's power | CAP_CHOWN |
| Seccomp | a filter on the kernel calls a process may make | |
| Read-only root | an image's files that nothing can change | |
| Privilege escalation | a process gaining more power than it started with | |
| emptyDir | a writable folder that lives as long as the pod |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsFour settings close four doors
Each setting answers one question about the attacker's command. Who am I? runAsNonRoot. Can I change the program? readOnlyRootFilesystem.
Can I gain power? allowPrivilegeEscalation: false and dropping every capability. Which kernel calls can I make? A seccompProfile of RuntimeDefault, the runtime's own list.
What each setting stops
| Setting | Without it | With it |
|---|---|---|
| runAsNonRoot: true | The command runs as root | The pod will not start as root |
| readOnlyRootFilesystem: true | It can change the program's files | Writes fail |
| capabilities: drop ALL | Root keeps 14 powers | No powers left |
| allowPrivilegeEscalation: false | A setuid file can add power | NoNewPrivs is set |
| seccompProfile: RuntimeDefault | Every kernel call is allowed | Risky calls are refused |
The cells students fill in are left blank in this preview.
Try it: count capabilities
mask = "00000000a80425fb"
number = int(mask, 16)
print(bin(number).count("1"), "capabilities")
# Change mask to "0000000000000000", the hardened pod. How many now?
Make a free account to save your work and track your progress.
First, what your cluster gives by default
Never assume a default is safe: measure it. You start a pod with no settings at all and read its process status from /proc/1/status.
CapEff lists its capabilities in hex. NoNewPrivs is 1 when it cannot gain power. Seccomp is 0 for no filter and 2 for a filter.
A pod with no settings, and its status
kubectl create namespace runtime; kubectl run plain -n runtime --image=busybox:1.37.0 -- sleep 3600; kubectl wait -n runtime --for=condition=Ready pod/plain --timeout=120s && kubectl exec -n runtime plain -- sh -c 'id; grep -E "^(CapEff|NoNewPrivs|Seccomp):" /proc/1/status'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the hardened pod
mkdir -p ~/ch && printf '%s\n' 'apiVersion: v1' 'kind: Pod' 'metadata:' ' name: hard' ' namespace: runtime' 'spec:' ' automountServiceAccountToken: false' ' securityContext:' ' runAsNonRoot: true' ' runAsUser: 10001' ' runAsGroup: 10001' ' seccompProfile:' ' type: RuntimeDefault' ' containers:' ' - name: main' ' image: busybox:1.37.0' ' command: ["sleep", "3600"]' ' securityContext:' ' readOnlyRootFilesystem: true' ' allowPrivilegeEscalation: false' ' capabilities:' ' drop: ["ALL"]' > ~/ch/hard.yamlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Start it and read its status
cd ~/ch && kubectl apply -f hard.yaml && kubectl wait -n runtime --for=condition=Ready pod/hard --timeout=120s && kubectl exec -n runtime hard -- sh -c 'id; grep -E "^(CapEff|NoNewPrivs|Seccomp):" /proc/1/status'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsTry a write and a namespace in each
for p in plain hard; do echo "== $p"; kubectl exec -n runtime $p -- sh -c "touch /x && echo wrote /x; unshare -U true && echo unshare worked"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think Kubernetes defaults are safe because Kubernetes chose them. Your plain pod ran as root with 14 capabilities and no seccomp filter. Set the security context on every pod, because the defaults favour running anything.
Give the app one writable folder
Some programs write at start, such as a page, a cache or a lock file. Under a read-only root they fail.
Do not turn the setting off. Mount an emptyDir where the program writes. Only that folder is writable, and it goes when the pod goes.
Write a pod that writes at start
mkdir -p ~/ch && printf '%s\n' 'apiVersion: v1' 'kind: Pod' 'metadata:' ' name: greet' ' namespace: runtime' 'spec:' ' securityContext:' ' runAsNonRoot: true' ' runAsUser: 10001' ' seccompProfile:' ' type: RuntimeDefault' ' containers:' ' - name: web' ' image: busybox:1.37.0' ' command: ["sh", "-c", "mkdir -p /www && echo hello > /www/index.html && exec httpd -f -p 8080 -h /www"]' ' securityContext:' ' readOnlyRootFilesystem: true' ' allowPrivilegeEscalation: false' ' capabilities:' ' drop: ["ALL"]' > ~/ch/greet.yamlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Start it and read why it stops
cd ~/ch && kubectl apply -f greet.yaml && sleep 15 && kubectl get pod -n runtime greet && kubectl logs -n runtime greetShown here, not run. Press Start lab to type it into your own lab’s terminal.
Add an emptyDir at /www and retry
cd ~/ch && kubectl delete pod -n runtime greet --now && printf "%s\n" " volumeMounts:" " - name: www" " mountPath: /www" " volumes:" " - name: www" " emptyDir: {}" >> greet.yaml && kubectl apply -f greet.yaml && kubectl wait -n runtime --for=condition=Ready pod/greet --timeout=120s && kubectl exec -n runtime greet -- wget -qO- http://127.0.0.1:8080Shown here, not run. Press Start lab to type it into your own lab’s terminal.
List the pods that lack the settings
A cluster drifts: somebody always adds a pod in a hurry. So list every pod's settings and look for the gaps, or let lesson 4's admission rule refuse them.
The columns below read straight from the pod specs.
Show each pod's settings
kubectl get pods -n runtime -o custom-columns=NAME:.metadata.name,NONROOT:.spec.securityContext.runAsNonRoot,SECCOMP:.spec.securityContext.seccompProfile.type,READONLY:.spec.containers[0].securityContext.readOnlyRootFilesystemShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: find the missing settings
Write a check of a pod's runtime settings.
missing(pod) gives, for each container by name, the settings in ORDER it lacks: runAsNonRoot true, readOnlyRootFilesystem true, allowPrivilegeEscalation false, "ALL" in capabilities drop, and a seccompProfile type of RuntimeDefault or Localhost. A setting on the pod counts unless the container sets its own. count_caps(mask) counts the capabilities in a hex mask from /proc.
ORDER = ["runAsNonRoot", "readOnlyRootFilesystem", "allowPrivilegeEscalation",
"capabilities", "seccompProfile"]
def missing(pod):
# {container name: [the settings in ORDER it lacks]}.
# runAsNonRoot and seccompProfile may be set on the pod; the container's wins.
return {}
def count_caps(mask):
# mask is hex text from /proc, such as "00000000a80425fb".
# The number of capabilities it holds.
return 0
Make a free account to save your work and track your progress.
Exit quiz
Have a go