knaxuslearn · build · ship

Projects / Container runtime / Wrap up

Wrap up

You built the core of a container runtime from the kernel's own parts: new namespaces from clone, a root file system entered with pivot_root, a smaller set of capabilities, no_new_privs and resource limits, an overlay for each run, a private network and a small init. Container tools such as Docker and Podman do these same things, plus a lot more.

What this version leaves out

  • There are no cgroups. RLIMIT_AS and RLIMIT_CPU limit single processes, and a container with many processes gets the limit many times.
  • It needs real root. There is no user namespace, so root inside is root of the host, with fewer capabilities.
  • There is no seccomp filter, so every system call that the kept capabilities allow is available.
  • Sensitive paths under /proc and /sys are not hidden or made read-only.
  • The image is a directory built from the host's programs. There is no image format, no download and no layer cache.
  • The network is only a loopback. There is no connection to the outside, and no DNS.
  • If rt is killed with SIGKILL, it cannot clean up its scratch directory under /tmp.
  • We ran everything on Linux 6.18 as root. We did not run the guide on other kernels.

Keep going

  • Cgroups v2. Create a directory for each run under the cgroup file system, write numbers into memory.max, cpu.max and pids.max, and write the container's process number into cgroup.procs. This limits the whole group, which is what RLIMIT_AS could not do. It needs a machine with cgroup v2 mounted and root access to it.
  • User namespaces. Add CLONE_NEWUSER and map user 0 inside to an ordinary user outside, so that rt can run without sudo.
  • A real network. Create a virtual Ethernet pair with ip link, move one end into the container's namespace, give both ends addresses, and add a route and address translation on the host.
  • Seccomp. Install a filter that returns an error for system calls the container has no business making.
  • Run a command in a running container. Keep the namespace handles from /proc and join them with setns.
  • More layers. Unpack two or three directories as separate lower layers and list them in lowerdir.

What real runtimes do differently

  • They are driven by a configuration file. The Open Container Initiative runtime specification describes the container as one JSON document, with fields for the process and its capabilities, resource limits and noNewPrivileges, and a Linux section for namespaces, cgroups and seccomp. Many of our command-line flags are fields there.
  • They set up user, cgroup and IPC namespaces too, and mask sensitive paths in /proc.
  • Images come as stacked layers that are unpacked and mounted as many overlay lower directories, and are shared between containers.
  • They connect networking with virtual cables and bridges, set up addresses and DNS, and clean up even after a crash.

Check yourself

Answer each question in your head first, then open it to check.

  1. Why does ps inside the container show only two processes, even though the host runs hundreds?
    The container has its own PID namespace, so it only sees processes created inside it. And ps reads /proc, which we mounted again inside the new mount namespace after the clone, so it reflects the new PID namespace instead of the host's.
  2. Why do we bind mount the root directory onto itself before calling pivot_root?
    pivot_root needs the new root to be a mount point, and an ordinary directory is not one. A bind mount of a directory onto itself makes it a mount point without copying anything.
  3. We drop capabilities from the bounding set. What does no_new_privs stop that the dropped capabilities alone do not?
    A set-uid program, or a file with file capabilities, can raise the rights of the process during exec up to what the bounding set allows. With no_new_privs, exec can no longer give more rights than the caller had, so set-uid bits and file capabilities have no effect at all.
  4. A teammate says "--mem 64 guarantees the container never uses more than 64 MB". What do you answer?
    RLIMIT_AS limits the address space of each process. It counts reserved memory that was never used, and each extra process gets its own full allowance, so the total can be much higher than 64 MB. A limit on actual use by the whole container needs a cgroup, for example memory.max.
  5. Teach it back: someone tells you containers are lightweight virtual machines. What is wrong with that?
    A virtual machine runs its own kernel on virtual hardware. A container is a group of ordinary processes on the host kernel, with a restricted view and restricted rights. Everything they do is a system call to the host's kernel, so a kernel bug, or a limit we forgot to set, can reach the host.

Sources