Highlights

Contents

Part XI
11

Protection, containers and Linux

Security begins with identity and page permissions, then extends to virtual machines, namespaces and cgroups. The host can show a process a restricted world and limit it to 50 ms of CPU in each 100 ms period. We will connect permissions, boot, introspection, limits, leaks, caches, zero-copy and io_uring.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1OS fundamentals2Processes3Fork, exec and threads4Synchronization5Deadlocks6CPU scheduling7Paging and memory8Copy-on-write9Filesystems10I/O and IPC11Protection and Linux12Complete server trace

#Section 45Permissions and privilege

Unix permission digits encode read as 4, write as 2 and execute as 1. 755 therefore gives the owner 7, group 5 and others 5. A privilege is authority to perform an operation such as changing protected mappings or binding a restricted resource. UID, GID and effective UID participate in permission checks; root has broad authority and therefore a large failure radius.

sudo changes the identity context for a command under policy. Setuid programs run with an effective identity that may differ from the invoking user, so their input handling must be strict. A backend should run with only the files, ports and devices it needs.

755 GRANTS READ, WRITE AND EXECUTE TO DIFFERENT SUBJECT CLASSES755 GRANTS READ, WRITE AND EXECUTE TO DIFFERENT SUBJECT CLASSESownerrwx4 + 2 + 1 = 7grouprnonex4 + 0 + 1 = 5otherrnonex4 + 0 + 1 = 5filethe kernel checks the caller against the file policy
Figure 3.58Illustrative three permission classes expand 755 into exact bit sums. Orange marks owner permissions because they are the first class checked for the file owner.
THE KERNEL CHECKS THE EFFECTIVE UID, NOT THE NAME ON THE TERMINALTHE KERNEL CHECKS THE EFFECTIVE UID, NOT THE NAME ON THE TERMINALprocess 4182uid1000gid1000euid0setuid binary: real 1000, effective 0bind(port 80)needs euid 0 orCAP_NET_BIND_SERVICEeuid 0a compromised root process can do almost anything
Figure 3.59Illustrative identity fields lead to a privileged operation decision. Orange marks effective UID because it is the permission-check identity.

Protection and access control

For a directory, execute permission means search or traversal rather than running its bytes as a program. Read lists names, and write participates in changing directory entries under the filesystem’s rules. File mode digits alone do not decide every access: credentials, mount policy and other access controls can also apply. The illustrated owner, group and other classes describe the declared basic permission model.

UID, GID and effective identity

Effective identity can grant more authority than the invoker has. Privileged helpers must constrain untrusted arguments. Reduce server authority while separately authorizing each user’s application operation.

#Section 46Virtual machines and containers

A virtual machine includes a guest OS above a hypervisor. A Type 1 hypervisor runs close to hardware, while a Type 2 runs through a host OS. Containers share the host kernel and isolate process views and resource budgets. Calling a container a lightweight VM hides the kernel-sharing difference.

Namespaces control what a process can see. Cgroups control how much it can use. Filesystem layers supply an initial tree, but the process still uses host kernel mechanisms for scheduling, memory and devices.

A VIRTUAL MACHINE RUNS A WHOLE GUEST KERNEL ON TOP OF A HYPERVISORA VIRTUAL MACHINE RUNS A WHOLE GUEST KERNEL ON TOP OF A HYPERVISORhardware: real CPU, RAM, diskhypervisor: traps privileged instructions, gives each guest virtual devicesVM 1guest kernel (Linux)appappVM 2guest kernel (Windows)appapp
Figure 3.60Illustrative VM path adds a guest kernel between application and hardware. Orange marks the hypervisor boundary.
A CONTAINER IS A PROCESS WITH A NARROWED VIEW, A BUDGET AND ITS OWN FILES, ON THE HOST KERNELA CONTAINER IS A PROCESS WITH A NARROWED VIEW, A BUDGET AND ITS OWN FILES, ON THE HOST KERNELone shared host kernelimage layersPID 1namespace viewcgroupimage layersPID 1namespace viewcgroupimage layersPID 1namespace viewcgroup
Figure 3.61Illustrative namespace visibility, cgroup budget, filesystem layer and host kernel are separate container mechanisms. Orange marks the namespace view; the host kernel remains shared.

Hypervisors and containers

A VM exposes virtual CPUs, memory and devices to its guest kernel. The hypervisor maps those virtual resources onto host resources and establishes the isolation boundary. A guest scheduler makes decisions among guest tasks while a host layer still schedules the virtual CPU. Being runnable inside the guest does not imply receiving physical CPU time immediately.

A container image supplies files, not another kernel. Namespaces and quotas change separate contracts, so hidden processes do not imply reserved CPU or memory.

#Section 47Namespaces, cgroups and boot

PID namespaces can make a process see itself as PID 1 while the host assigns another PID. Network namespaces split interfaces and routes, mount namespaces change filesystem views, user namespaces map identities, and IPC or UTS namespaces isolate other kernel views. A cgroup is the resource boundary, not the visibility boundary.

Finch’s container has a 50 ms quota in a 100 ms period. Once it consumes its quota, the controller throttles it until the period permits more CPU. Boot passes control from firmware to bootloader, kernel, early userspace and PID 1, which starts services. On a systemd-based installation, systemd supplies that init role; another init implementation can supply the same lifecycle responsibility.

THE SAME PROCESS IS PID 1 INSIDE AND PID 4182 ON THE HOSTTHE SAME PROCESS IS PID 1 INSIDE AND PID 4182 ON THE HOSTinside the container: psPID CMD1 nginx7 nginx workereth0 10.0.0.2/ image rooton the host: psPID CMD1 systemd812 containerd4182 nginx4189 nginx workereth0 192.168.1.20PIDprocess numbersnetinterfaces, routesmountfilesystem treeuserUID 0 inside maps to 100000
Figure 3.62Illustrative inside and host views map PID, network, mount and user namespaces to the view each changes. Orange marks PID identity because inside and outside can name one process differently.
QUOTA 50 MS PER 100 MS PERIOD: THE GROUP RUNS, THEN WAITS FOR THE NEXT PERIODQUOTA 50 MS PER 100 MS PERIOD: THE GROUP RUNS, THEN WAITS FOR THE NEXT PERIODrunsthrottled0 msrunsthrottled100 msrunsthrottled200 ms300 msquota hit at 50 ms50 / 100 = 0.5 of one core, on a 2-core hosttail latency jumps when a request lands in the hatched part
Figure 3.63Illustrative quota timeline shows 50 ms of CPU permitted in each 100 ms period on a 2-core host. Orange marks the quota boundary.
BOOT IS A RELAY: EACH STAGE LOADS THE NEXT AND HANDS OVER CONTROLBOOT IS A RELAY: EACH STAGE LOADS THE NEXT AND HANDS OVER CONTROLfirmwarepower-on, find diskbootloaderload kernel imagekerneldrivers, memory, CPUsinitramfsmount real rootinitPID 1serviceserver readyeach stage trusts the one before it
Figure 3.64Illustrative firmware, bootloader, kernel, init and service form the startup chain. Orange marks the kernel because it establishes the execution environment.

Namespaces and cgroups

CPU quota counts aggregate execution across the group. Several simultaneously running workers can exhaust it sooner in wall time; the allowance is not a fraction of every host core.

Boot process

An initramfs can supply early userspace needed to find and mount the intended root filesystem. The init system then starts services and collects adopted child status according to its policy. PID 1 has lifecycle duties distinct from an ordinary callback process. A container’s apparent PID 1 still needs deliberate signal forwarding and child collection; restricting its process view does not perform those duties automatically.

#Section 48Introspection, limits and leaks

/proc exposes process and kernel information as a pseudo-filesystem. /proc/<pid>/fd shows descriptor links, while strace displays system calls. ulimit and related limits cap resources such as open files. Finch has a descriptor limit of 1,024 and reserves 24, leaving 1,000 working slots in the declared model.

A memory leak retains useless allocations. A descriptor leak retains sockets or files until the process hits its limit. A thread leak retains execution resources and scheduling overhead. Diagnosis needs a count over time and a release path, not only a larger limit.

/PROC SHOWS THE DESCRIPTORS; ULIMIT CAPS THEM AT 1024/PROC SHOWS THE DESCRIPTORS; ULIMIT CAPS THEM AT 1024$ ls -l /proc/4182/fd0 -> /dev/null1 -> /var/log/app.log2 -> /var/log/app.log3 -> socket:[88213]4 -> socket:[88219]...$ ls /proc/4182/fd | wc -l1024$ strace -e accept4 -p 4182ulimit -n 10241000 for sockets24 for files,logs, stdioconnection 1001 gets EMFILE
Figure 3.65Illustrative terminal and gauge connect /proc, descriptor inspection, limits and syscall tracing. Orange marks the limit because it turns a leak into a hard failure.
A LEAK CLIMBS UNTIL IT HITS A LIMIT; THE DESCRIPTOR LIMIT FAILS FIRST HEREA LEAK CLIMBS UNTIL IT HITS A LIMIT; THE DESCRIPTOR LIMIT FAILS FIRST HEREhoursheldfd limit 1024memorythreadsaccept() failsunclosed socketsillustrative rates; CPU can be idle while the server refuses connections
Figure 3.66Illustrative each leak holds a finite resource until a distinct failure appears. Orange marks the descriptor leak because it can block a server before CPU is full.

Linux process introspection and /proc

ps, top and htop expose different views of task activity; free and vmstat help distinguish memory and paging behavior. lsof connects open resources to processes. A syscall trace can reveal repeated would-block returns or an unexpected blocking operation, but its own collection cost and access permissions remain relevant. No single display converts a symptom into a cause without a timeline.

Resource limits and leaks

Compare resource counts at the same workload phase. Retained allocator capacity is not automatically a leak. Fix ownership and cleanup paths; raising a limit only postpones an unbounded lifetime problem.

#Section 49Caches, false sharing and fast I/O

Registers, caches, RAM and SSD trade capacity for latency. A cache line is the unit moved by cache coherence; Finch’s model uses 64 bytes. False sharing occurs when two threads update separate variables on one cache line, causing invalidations even though the variables are logically independent.

User-space networking can reduce copies or transitions through mechanisms such as sendfile, which can send file-backed data without copying it through an application buffer. io_uring uses submission and completion queues to submit operations and receive results. These interfaces reduce a particular dispatch cost; they do not remove storage latency or make blocking work safe in an event loop.

THE SAME REQUEST TRIES SUCCESSIVELY LARGER STORAGE REGIONSTHE SAME REQUEST TRIES SUCCESSIVELY LARGER STORAGE REGIONScoreL1L2L3RAMcapacity increases; access and miss costs depend on the machine
Figure 3.67Illustrative storage hierarchy. Larger regions hold more data; smaller cache levels offer shorter access paths. Shapes show relative roles rather than measured capacity or latency ratios.
TWO COUNTERS, ONE 64-BYTE LINE: EVERY WRITE STEALS THE LINE FROM THE OTHER CORETWO COUNTERS, ONE 64-BYTE LINE: EVERY WRITE STEALS THE LINE FROM THE OTHER COREcore 0thread A: a++private cache copyabcore 1thread B: b++private cache copyabone line = 8 × 8-byte countersinvalidateinvalidatepad a and b onto separate lines and the traffic stops
Figure 3.68Illustrative two threads write separate words inside one 64-byte line. Orange marks coherence traffic caused by false sharing.
READ + WRITE COPIES THROUGH USER SPACE; SENDFILE KEEPS THE BYTES IN THE KERNELREAD + WRITE COPIES THROUGH USER SPACE; SENDFILE KEEPS THE BYTES IN THE KERNELuser spacekerneldiskpage cacheuser buffersocket bufferNICread() then write(): two extra copiessendfile()
Figure 3.69Illustrative file bytes move from page cache toward the NIC without an application buffer hop. Orange marks the kernel-owned path.
IO_URING: TWO SHARED RINGS, ONE FOR REQUESTS IN AND ONE FOR RESULTS OUTIO_URING: TWO SHARED RINGS, ONE FOR REQUESTS IN AND ONE FOR RESULTS OUTsubmissioncompletionapp writes SQEskernelapp reads CQEsshared memory: no syscall per requestres < 0 is still an error
Figure 3.70Illustrative submission and completion queues separate request posting from result consumption. Orange marks completion because it is the application-visible event.

Cache hierarchy and false sharing

Temporal locality reuses recently accessed data; spatial locality accesses nearby bytes likely to share a fetched line. Finch’s illustrative 64-byte line can contain eight 8-byte counters. Placing different writers’ counters there can cause coherence invalidations despite logically independent variables. Padding or separating ownership can remove that sharing at a memory-footprint cost; it does not correct an actual race on the same counter.

User-space networking, zero-copy and io_uring

sendfile changes a copy path but still uses the kernel. io_uring changes submission and completion; DPDK-style paths change packet ownership. None removes every I/O cost or recovery duty.