Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Safe runtime defaults for a pod

After this lesson: I can run a pod with a read-only root, no capabilities and a seccomp filter.

65 min lesson8 commandsKubernetes lab: a one-node k3s cluster for each student on a private Ubuntu VM

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By the end of this lesson

Note

I can run a pod with a read-only root, no capabilities and a seccomp filter.

Key words

WordWhat it meansExample
Security contextthe security settings of a pod or container
Capabilityone piece of root's powerCAP_CHOWN
Seccompa filter on the kernel calls a process may make
Read-only rootan image's files that nothing can change
Privilege escalationa process gaining more power than it started with
emptyDira 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Four 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

SettingWithout itWith it
runAsNonRoot: trueThe command runs as rootThe pod will not start as root
readOnlyRootFilesystem: trueIt can change the program's filesWrites fail
capabilities: drop ALLRoot keeps 14 powersNo powers left
allowPrivilegeEscalation: falseA setuid file can add powerNoNewPrivs is set
seccompProfile: RuntimeDefaultEvery kernel call is allowedRisky calls are refused

The cells students fill in are left blank in this preview.

Try it: count capabilities

Code: python
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

Command
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

Command
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.yaml

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Start it and read its status

Command
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Try a write and a namespace in each

Command
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"; done

Shown 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

Command
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.yaml

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Start it and read why it stops

Command
cd ~/ch && kubectl apply -f greet.yaml && sleep 15 && kubectl get pod -n runtime greet && kubectl logs -n runtime greet

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Add an emptyDir at /www and retry

Command
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:8080

Shown 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

Command
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.readOnlyRootFilesystem

Shown 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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.