Phase 1 of 4 · Study weeks 1-4
The machine, the process and the service
This phase makes orders-api run as a proper service that restarts, logs and is sandboxed, so Phase 2 has something to limit. Read man 5 systemd.unit, man 5 systemd.service, man 5 systemd.exec and the kernel's Documentation/admin-guide as you go.
First day. Download the Debian 13 generic cloud image from cloud.debian.org (availability not checked), write a cloud-init user-data file that adds your SSH key and a user, boot it with QEMU and a forwarded port (-nic user,hostfwd=tcp::2222-:22,hostfwd=tcp::8080-:8080), and log in with ssh -p 2222. For load, use a tool such as hey (check its help: -q is a per-worker rate and -c the workers, so 20 workers at 30 each is 600 a second) or wrk. QEMU's user-mode networking adds latency and can cap throughput, so take latency numbers with the load generator inside the VM, and use the forwarded port only for the "from outside" firewall test. If your cloud kernel lacks sch_netem (Week 11), install the standard linux-image-amd64 kernel.
Topics every week
- Week 18 h
- Boot: firmware, bootloader, kernel and initramfs, systemd as process 1
- processes: fork, exec, signals, exit status,
/proc
Hand inA VM built by a script, plus a boot timeline (
systemd-analyze,dmesg) with each stage labelled and timed - Week 28 h
- Files and permissions: inodes, links, mounts,
/etcand/var, users and groups, mode bits, umask, setuid, ACLs, capabilities
Hand inA permissions report for
orders-api: service user, directory modes, and a table showing which capabilities it needs (none) and why - Files and permissions: inodes, links, mounts,
- Week 38 h
- systemd units: service types, ordering and dependencies,
Restart=, drop-ins, journald, timers
Hand in
orders-api.serviceand a timer, with a kill-and-restart table (20 trials, 10 seconds apart, because systemd's default start limit is 5 starts in 10 seconds; or setStartLimitIntervalSec=0and say why) - systemd units: service types, ordering and dependencies,
- Week 48 h
- Sandbox options (
ProtectSystem,NoNewPrivileges,PrivateTmp,CapabilityBoundingSet,StateDirectory) - scripted provisioning with
set -euo pipefailand ShellCheck
Hand inCheckpoint 1, built in four steps: the script; the unit; the hardening score; the disk-full fault (then the captures)
- Sandbox options (
Practice projects
- Write a unit for a second program of your own and make it fail in three different ways. Done when
systemctl statusandjournalctl -ushow each failure and you can say whichRestart=setting restarts which failure. - Walk a boot with
systemd-analyze critical-chainand remove one unneeded unit. Done when the boot is faster by a number you measured and you can show the service is not needed. - (Optional for a fresher) Write a user-space program that is stopped by
SIGSTOP, resumed withSIGCONTand cleans up onSIGTERM. Done whenstraceshows each signal arrive.
GitHub repos to study
| Repo | What to read | Week |
|---|---|---|
| systemd/systemd | man/ pages for service and exec settings, and src/analyze/analyze-security.c for what the score counts | 3 to 4 |
| strace/strace | src/ and the tests, for how a syscall is decoded | 1, 4 |
Milestone checkpoint
Build the VM and the service by script, run the kill test, apply the hardening and fill the disk. Done when the Checkpoint 1 row passes. Evidence: the script, the unit, the kill table (20 trials), the systemd-analyze security output, the disk-full capture, ADR-1 (which sandbox options you set and one you rejected, such as MemoryDenyWriteExecute, because your runtime needs writable and executable memory).
Check yourself
Answer each question in your head first, then open it to check.
What runs first after the kernel starts, and what happens when it exits?
The kernel starts process 1 (systemd). If it exits, the kernel panics.A file has mode
0640and ownerroot:orders. Who can read it?Root, the owner (read and write), and members of grouporders(read only).What is the difference between
Restart=on-failureandRestart=always?on-failurerestarts after a non-zero exit, an unclean signal such asSIGKILL, a timeout or a watchdog trip;alwaysalso restarts after a clean exit. A deliberatesystemctl stopis not restarted by either.Why does
systemd-analyze securitynot prove a service is safe?It scores which directives are set, not whether the service is exploitable; a flaw inside the allowed set still works.What does a full disk do to a process that writes a log line?
The write returnsENOSPC. If the program ignores the error or blocks on it, it fails; a program that handles it can degrade, which is what Checkpoint 1 tests.