OnCallReadyStart learning

Security and secrets Lesson 23.15 · Kubernetes Security (CKS) · 7 min read

AppArmor: confine what a container may touch

In plain words

AppArmor is a list of rules taped to a program: you may read these folders, you may not write anywhere. The kernel of the machine enforces the list, so even a program running as root inside the container cannot tear it off. The list has to be pinned up on the machine itself, which is why the profile must be loaded on the node before the pod arrives.

Limits the container cannot lift

AppArmor is a Linux security module: a profile lists what a program may read, write or execute, and the kernel enforces it - even for root inside the container. Kubernetes picks the profile per pod or container:

spec:
  securityContext:
    appArmorProfile:
      type: Localhost              # RuntimeDefault = the container runtime's default profile
      localhostProfile: k8s-deny-write

Localhost means "a profile already loaded on the node that runs the pod" - the kubelet does not ship it for you. Loading is done on the node:

sudo apparmor_parser -q /etc/apparmor.d/k8s-deny-write   # load (or -r to replace)
sudo aa-status                                         # what is loaded, in which mode

A small profile that refuses every write:

#include <tunables/global>
profile k8s-deny-write flags=(attach_disconnected) {
  #include <abstractions/base>
  file,
  deny /** w,
}

If the profile is not loaded on that node, the container is not created: kubectl describe pod shows "apparmor profile not found k8s-deny-write". With it loaded, touch /tmp/x inside the container answers Permission denied. (simulator) only deny <path> w rules are evaluated, and the event reason reads CreateContainerConfigError.

Reading aa-status

learner@worker-1:~$ sudo aa-status
apparmor module is loaded.
118 profiles are loaded.
25 profiles are in enforce mode.
   /usr/bin/man
   ...
   cri-containerd.apparmor.d
   k8s-deny-write
   ...
4 profiles are in complain mode.
   ...
89 profiles are in unconfined mode.
   ...
4 processes have profiles defined.
4 processes are in enforce mode.
   /coredns (2005) cri-containerd.apparmor.d
   /metrics-server (2011) cri-containerd.apparmor.d
   /usr/bin/sleep (2017) k8s-deny-write
   /usr/sbin/rsyslogd (612) rsyslogd
0 processes are in complain mode.
...
  • The profiles: what Ubuntu 24.04 loads from /etc/apparmor.d (the 89 "unconfined" ones are AppArmor 4's user-namespace profiles for desktop apps, loaded on servers too), plus cri-containerd.apparmor.d - containerd's default, which every container gets when the pod says nothing (= RuntimeDefault) unless it is privileged - and yours.
  • The processes: PID and executable per profile. A container under your profile shows up here; that line, not the pod spec, is the proof the kernel confines it. Privileged pods (kube-proxy, calico-node), Unconfined and the pause sandboxes have no line.
  • The executable is what aa-status resolves on the node: busybox's /bin/sleep reads /usr/bin/sleep, because the same path exists on the host. sudo aa-status --json gives the same data for a script; --enforced only the count. (simulator) one process per container.

In an interview: "the profile must exist on every node the pod can land on - load it with a DaemonSet or your node image, and pin the pod (nodeSelector) while you roll it out."

On the exam: domain "System hardening": load a given profile on a node and run a pod with it.

Why it helps

It limits what an attacker can do after breaking into a container, even with root inside it. The exam and real hardening both use it, and both trip over the same point: the profile must be loaded on the node that runs the pod, not created as an object in the cluster. Knowing how to verify the profile from inside the container completes the skill.

FAQ

What does appArmorProfile Localhost mean?

Use a profile that is already loaded on the node that runs the pod, named by localhostProfile. The kubelet does not ship or load profiles for you, so they must be on every node the pod can land on.

How do I load a profile?

On the node, as root: sudo apparmor_parser -q with the profile file, usually kept in /etc/apparmor.d. Then sudo aa-status lists the loaded profiles and shows whether each is in enforce or complain mode. Load it again with -r after you edit the file.

What happens if the profile is not loaded?

The container is not created: kubectl describe pod shows that the AppArmor profile was not found. The pod stays stuck until the profile is loaded on that node or the pod moves to a node that has it.

What does deny /** w, do?

It refuses every write anywhere in the filesystem, so touch or a log write inside the container fails with Permission denied. Real profiles are more selective, allowing writes only where the application needs them. Test the rule from inside the container to be sure it holds.

How do I get the profile onto every node?

With the node image or a DaemonSet that loads it on every node. While you roll it out, pin the pod with a nodeSelector to the nodes that already have the profile so it never lands on one without it.

In an interview Mid

How would you stop a container from writing to its filesystem even as root?

Load an AppArmor profile with a deny /** w, rule on the nodes (apparmor_parser -q, check with aa-status) and run the pod with securityContext.appArmorProfile type Localhost naming it; the kernel refuses the writes with Permission denied. A read-only root filesystem is the simpler first step; AppArmor also covers mounted paths and other actions.

Also asked: What happens when a Localhost profile is missing on the node? · How do you distribute profiles to all nodes? · How is AppArmor different from seccomp?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.