knaxuslearn · build · ship

Roadmaps / Linux / Phase 1

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

  1. 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

  2. Week 28 h
    • Files and permissions: inodes, links, mounts, /etc and /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

  3. Week 38 h
    • systemd units: service types, ordering and dependencies, Restart=, drop-ins, journald, timers

    Hand inorders-api.service and 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 set StartLimitIntervalSec=0 and say why)

  4. Week 48 h
    • Sandbox options (ProtectSystem, NoNewPrivileges, PrivateTmp, CapabilityBoundingSet, StateDirectory)
    • scripted provisioning with set -euo pipefail and ShellCheck

    Hand inCheckpoint 1, built in four steps: the script; the unit; the hardening score; the disk-full fault (then the captures)

Practice projects

  1. Write a unit for a second program of your own and make it fail in three different ways. Done when systemctl status and journalctl -u show each failure and you can say which Restart= setting restarts which failure.
  2. Walk a boot with systemd-analyze critical-chain and remove one unneeded unit. Done when the boot is faster by a number you measured and you can show the service is not needed.
  3. (Optional for a fresher) Write a user-space program that is stopped by SIGSTOP, resumed with SIGCONT and cleans up on SIGTERM. Done when strace shows each signal arrive.

GitHub repos to study

RepoWhat to readWeek
systemd/systemdman/ pages for service and exec settings, and src/analyze/analyze-security.c for what the score counts3 to 4
strace/stracesrc/ and the tests, for how a syscall is decoded1, 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.

  1. 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.
  2. A file has mode 0640 and owner root:orders. Who can read it?
    Root, the owner (read and write), and members of group orders (read only).
  3. What is the difference between Restart=on-failure and Restart=always?
    on-failure restarts after a non-zero exit, an unclean signal such as SIGKILL, a timeout or a watchdog trip; always also restarts after a clean exit. A deliberate systemctl stop is not restarted by either.
  4. Why does systemd-analyze security not 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.
  5. What does a full disk do to a process that writes a log line?
    The write returns ENOSPC. 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.