diff --git a/.gitea/workflows/os.yml b/.gitea/workflows/os.yml index 82ecb36..110843a 100644 --- a/.gitea/workflows/os.yml +++ b/.gitea/workflows/os.yml @@ -19,11 +19,16 @@ jobs: AUTH: "Authorization: token ${{ github.token }}" steps: - name: Install tools - run: dnf install -yq mkosi + run: dnf install -yq mkosi sbsigntools - name: Check out uses: builtin:checkout + # Signs systemd-boot and the UKI for Secure Boot (mkosi.conf, [Validation]). - name: Build - run: mkosi build + env: + KEY: ${{ secrets.SECUREBOOT_KEY }} + run: | + (umask 077 && printf '%s\n' "$KEY" > mkosi.key) + mkosi build - name: Compress working-directory: mkosi.output run: | diff --git a/.gitignore b/.gitignore index 96100fc..5087531 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ # Build outputs. mkosi.output/ +# The Secure Boot private key (the SECUREBOOT_KEY secret); mkosi.crt is its certificate. +mkosi.key diff --git a/README.md b/README.md index 7b439ae..c193a79 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,13 @@ A VM that runs [Claude Code](https://claude.com/claude-code) in the background, - **Resets at reboot:** the OS is read-only, so Claude can't change it, and Claude's home, `/tmp` and all containers are wiped at every boot. Only the setup answers and Claude's login survive (`/var/lib/agent`). - **Claude as non-root, tools in containers:** Claude runs as the user `claude`, without root, and runs its toolchains in containers with rootless podman (also as `docker`). `/etc/claude-code/CLAUDE.md` tells it about the machine; `/etc/claude-code/managed-settings.json` lets it run without asking permission. The host has only git, ssh and basic command-line tools. -- **Self-updating:** like [git-runner-mkosi](../git-runner-mkosi): images are rebuilt weekly (also picking up the latest Claude Code) and on every push. Hosts download updates with systemd-sysupdate, check their signature (`SIGNING_KEY` secret), write them into a second root partition (A/B) and reboot into them daily at about 04:10. That reboot also resets Claude's work. +- **Self-updating:** like [git-runner-mkosi](../git-runner-mkosi): images are rebuilt weekly (also picking up the latest Claude Code) and on every push. Hosts download updates with systemd-sysupdate, check their signature (`SIGNING_KEY` secret), write them into a second root partition (A/B) and reboot into them daily at about 04:10. That reboot also resets Claude's work. A new version gets three tries to boot; if none gets through, systemd-boot goes back to the previous one. - **Read-only system:** systemd-boot starts a UKI that mounts the root filesystem, /etc and /usr included, from a read-only erofs partition checked by dm-verity. All state (Claude's home, its login, the root password, logs) lives on `/var`, its own partition. -- **Hardened:** a firewall that drops all incoming traffic but ssh, KSPP sysctls and kernel arguments, and systemd-oomd kills Claude's work, not the system, when it runs the VM out of memory. +- **Secure Boot:** systemd-boot and the UKIs are signed with our own key (`mkosi.crt`; the private key is the `SECUREBOOT_KEY` secret). systemd-boot enrolls it at first boot if the VM's firmware has no keys yet; the firmware then boots nothing else. ## Install -Download [the disk image](https://git.plabble.org/api/packages/Job79/generic/agent/latest/agent.qcow2) (built by the `os` workflow), resize it to the disk size you want (/var grows to fill it) and boot it with UEFI. The screen and the serial console ask for a root password, a hostname and an ssh public key for the user `claude`. +Download [the disk image](https://git.plabble.org/api/packages/Job79/generic/agent/latest/agent.qcow2) (built by the `os` workflow), resize it to the disk size you want (/var grows to fill it) and boot it with UEFI in Secure Boot setup mode (no keys enrolled; in Proxmox, an EFI disk with "Pre-Enroll keys" unchecked), so it enrolls our key. The screen and the serial console ask for a root password, a hostname and an ssh public key for the user `claude`. Then log Claude in once: @@ -29,11 +29,14 @@ As soon as Claude is logged in, `claude-remote.service` starts `claude remote-co mkosi build ``` -`mkosi.output/` then holds the disk image (`agent_.raw`), and the UKI and root partitions that the `os` workflow publishes as an update. To boot the disk image (needs `qemu-kvm-core edk2-ovmf`; ssh on port 2222): +The build signs with `mkosi.key`: the `SECUREBOOT_KEY` secret, or for a local test a key of your own (`mkosi genkey` writes `mkosi.key` and `mkosi.crt`; don't commit them). + +`mkosi.output/` then holds the disk image (`agent_.raw`), and the UKI and root partitions that the `os` workflow publishes as an update. To boot the disk image with Secure Boot (needs `qemu-kvm-core edk2-ovmf`; `OVMF_VARS.fd` has no keys, so it enrolls ours; ssh on port 2222): ```sh qemu-img convert -O qcow2 mkosi.output/agent_.raw test.qcow2 && qemu-img resize test.qcow2 40G cp /usr/share/edk2/ovmf/OVMF_VARS.fd vars.fd qemu-kvm -m 8G -smp 4 -cpu host -nic user,model=virtio-net-pci,hostfwd=tcp::2222-:22 -drive file=test.qcow2,if=virtio -device virtio-balloon-pci,free-page-reporting=on \ - -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/ovmf/OVMF_CODE.fd -drive if=pflash,format=raw,file=vars.fd + -machine q35,smm=on -global driver=cfi.pflash01,property=secure,value=on \ + -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/ovmf/OVMF_CODE.secboot.fd -drive if=pflash,format=raw,file=vars.fd ``` diff --git a/mkosi.conf b/mkosi.conf index fb24de6..c337cce 100644 --- a/mkosi.conf +++ b/mkosi.conf @@ -25,7 +25,6 @@ Bootloader=systemd-boot # - systemd-pam login sessions (pam_systemd): Claude's user manager and its # D-Bus, which rootless podman needs # - openssh-server ssh login for the user (keys only) -# - nftables-services the firewall # - zram-generator-defaults compressed swap in RAM # - podman, podman-docker containers, for all of Claude's toolchains (also as docker) # - git-core, openssh-clients Claude Code uses git; it pushes over ssh @@ -44,7 +43,6 @@ Packages= shadow-utils systemd-pam openssh-server - nftables-services zram-generator-defaults podman podman-docker @@ -67,7 +65,7 @@ UnifiedKernelImageFormat=%i_%v # (mkosi.extra/etc/machine-id is empty, so no boot counts as the first), no systemd-firstboot # questions (agent-setup asks its own), freeing memory unused for an hour (damon_reclaim, in µs; the # watermarks make it run whatever memory is free), so the host can take it back (virtio-balloon -# free page reporting), and KSPP hardening (the last four). +# free page reporting). KernelCommandLine= console=tty0 console=ttyS0,115200n8 @@ -79,10 +77,6 @@ KernelCommandLine= damon_reclaim.wmarks_high=1000 damon_reclaim.wmarks_mid=999 damon_reclaim.wmarks_low=0 - slab_nomerge - init_on_alloc=1 - randomize_kstack_offset=on - page_alloc.shuffle=1 # Kernel modules in the initrd: only what finds and mounts the disks of a VM. KernelInitrdModules= virtio_blk @@ -122,3 +116,12 @@ RemoveFiles= /usr/lib/systemd/system/systemd-userdbd.* /usr/lib/systemd/system/systemd-nsresourced.* /usr/lib/systemd/system/sshd-keygen* + +[Validation] +# Signs systemd-boot and the UKI for Secure Boot, and puts our keys on the ESP, where systemd-boot +# enrolls them at first boot if the firmware is in setup mode (no keys enrolled; in Proxmox, an EFI +# disk without pre-enrolled keys). mkosi.crt is the certificate; mkosi.key, its private key, is the +# SECUREBOOT_KEY secret of this repository, which the os workflow writes to mkosi.key. +SecureBoot=yes +SecureBootKey=mkosi.key +SecureBootCertificate=mkosi.crt diff --git a/mkosi.crt b/mkosi.crt new file mode 100644 index 0000000..8a64c11 --- /dev/null +++ b/mkosi.crt @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC+DCCAeCgAwIBAgIUCJimQMIhhTOEiKBuq+g3Z0MQIbwwDQYJKoZIhvcNAQEL +BQAwHDEaMBgGA1UEAwwRYWdlbnQgU2VjdXJlIEJvb3QwHhcNMjYxMTE1MTUxMTMy +WhcNMzYxMTEyMTUxMTMyWjAcMRowGAYDVQQDDBFhZ2VudCBTZWN1cmUgQm9vdDCC +ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALav5Tkfxsb9ENOKAmt+Txh4 +MoNeph437ynX4iQFEgD9Go15u9ir2133h/2zsXbkWp/rwMyZCOBb1nw5Xa0/fEGO +uYrbAa77lOv9GOAeDcnr+cN4EWDkskx7HMxa5KK9aTNuk8Ot3FnMaK4bw06Rx321 +fhuBPSK4/30mBjhXqsp8Atc0X/TkALwAe0+E3R+oMY0mO7WphDvWlzSZOSbFV9H1 +bh/HmXF3ZQm6KxySHR85n1NLqDFhp8iqw5RTN8R+33XAATn9Z+8zRrOK7AbMKZej +gkQCRRhkkQGIirNovAuCMLjVRfpA008lDtOOINBDT1vq+GIN+fY7sqgB8sNIOl8C +AwEAAaMyMDAwHQYDVR0OBBYEFDxgMVL9sKugoHCm9IesHp1EtwLfMA8GA1UdEwEB +/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAGalNAOgEwCli2XnhX4AMHZCggwU +b1tWPVWhZ+uIJUV9WuiBc2BsdHEFGdOEq5N6QOqxUxdC1LVqK7xAwElBeVXg5oeo +sue811+ialFSegdzt1Ig0FRtD+LvMNl+WlYsbPs+7LT/No5osxcAn25d0SyjNbRL +7gOzbWw9h6h5ew+/jxLgL4jfbeoG4E+2FKiA1kGnBt7uiMfp9kp0zRZEVDB11uFE +Datcj1Bs54nT8/qlMvVwqhwmMx2eIel9Sxqa5Wd344OUAJcOezoUjqgCPy0/bcC9 +JUBnx+z5kHE4kSLkYOwEiN1HK1JBcNKMII6D2INBbUiBq12RZZFwVDolOD0= +-----END CERTIFICATE----- diff --git a/mkosi.extra/etc/claude-code/CLAUDE.md b/mkosi.extra/etc/claude-code/CLAUDE.md index 3ed66c5..7f07e54 100644 --- a/mkosi.extra/etc/claude-code/CLAUDE.md +++ b/mkosi.extra/etc/claude-code/CLAUDE.md @@ -12,4 +12,3 @@ You run on a small Fedora VM made for you, as the user `claude`, without root. - **Everything resets at reboot**: your home (/var/home/claude), /tmp and all containers and images. Only your Claude login survives. Keep results somewhere else (push them to git, for example). The VM may reboot at about 04:10 to update. -- Incoming traffic is blocked except ssh; to serve something, the user needs a tunnel. diff --git a/mkosi.extra/etc/profile.d/ps1.sh b/mkosi.extra/etc/profile.d/ps1.sh deleted file mode 100644 index ad7af6d..0000000 --- a/mkosi.extra/etc/profile.d/ps1.sh +++ /dev/null @@ -1,2 +0,0 @@ -# The prompt: hostname and directory on a purple bar (the arrow needs a Powerline/Nerd font). -export PS1="\[\e[30;45m\] \h | \w \[\e[0;35m\]\[\e[m\] " diff --git a/mkosi.extra/usr/lib/agent/firewall.nft b/mkosi.extra/usr/lib/agent/firewall.nft deleted file mode 100644 index db9254d..0000000 --- a/mkosi.extra/usr/lib/agent/firewall.nft +++ /dev/null @@ -1,18 +0,0 @@ -# Firewall: drop all incoming traffic except replies, loopback, ICMP, DHCPv6 and ssh, and forward -# nothing (rootless podman's networks live in Claude's own network namespace). Loaded by -# nftables.service. -destroy table inet firewall -table inet firewall { - chain input { - type filter hook input priority filter; policy drop; - ct state established,related accept - ct state invalid drop - iif lo accept - meta l4proto { icmp, ipv6-icmp } accept - ip6 saddr fe80::/10 udp dport 546 accept - tcp dport 22 accept - } - chain forward { - type filter hook forward priority filter; policy drop; - } -} diff --git a/mkosi.extra/usr/lib/sysctl.d/50-agent.conf b/mkosi.extra/usr/lib/sysctl.d/50-agent.conf index f566b37..09ba833 100644 --- a/mkosi.extra/usr/lib/sysctl.d/50-agent.conf +++ b/mkosi.extra/usr/lib/sysctl.d/50-agent.conf @@ -1,31 +1,3 @@ -# Kernel hardening, mostly the KSPP recommendations (Talos sets these too), and one limit raised -# for containers. Left out on purpose: user.max_user_namespaces=0 breaks rootless podman, and -# kernel.modules_disabled=1 breaks loading the network modules podman needs. - -# Hide kernel addresses (they help exploits bypass KASLR), even from root. -kernel.kptr_restrict = 2 -# Only root may read the kernel log; it leaks addresses. -kernel.dmesg_restrict = 1 -# No perf events for non-root users. -kernel.perf_event_paranoid = 3 -# Harden the BPF JIT against JIT spraying. -net.core.bpf_jit_harden = 2 -# No BPF for non-root users, and no switching it back on at runtime. -kernel.unprivileged_bpf_disabled = 1 -# No kexec: no booting another kernel past Secure Boot. -kernel.kexec_load_disabled = 1 -# No core dumps of setuid programs; they can hold secrets. -fs.suid_dumpable = 0 -# No loading TTY line disciplines on demand; old ones had exploitable bugs. -dev.tty.ldisc_autoload = 0 -# userfaultfd only for root; it makes kernel race exploits reliable. -vm.unprivileged_userfaultfd = 0 -# No opening FIFOs and files owned by others in sticky directories like /tmp. -fs.protected_fifos = 2 -fs.protected_regular = 2 -# ptrace only on your own descendants. -kernel.yama.ptrace_scope = 1 - # Containers (file watchers, systemd in containers) run out of Fedora's 128 inotify instances # per user; 8192 is common on Kubernetes nodes. fs.inotify.max_user_instances = 8192 diff --git a/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset b/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset index b6f8094..917f117 100644 --- a/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset +++ b/mkosi.extra/usr/lib/systemd/system-preset/50-agent.preset @@ -2,8 +2,6 @@ # login on the screen (tty1, and tty2-6 through its autovt@ alias); this file sorts first, so it wins. enable systemd-networkd.service enable systemd-resolved.service -enable systemd-oomd.service -enable nftables.service enable sshd.service enable getty@.service enable agent-setup@.service tty1 ttyS0 diff --git a/mkosi.extra/usr/lib/systemd/system/claude-remote.service b/mkosi.extra/usr/lib/systemd/system/claude-remote.service index 5321ac6..5b9c52b 100644 --- a/mkosi.extra/usr/lib/systemd/system/claude-remote.service +++ b/mkosi.extra/usr/lib/systemd/system/claude-remote.service @@ -8,8 +8,6 @@ After=network-online.target user@1000.service agent-boot.service [Service] User=claude WorkingDirectory=~ -# In user.slice, so systemd-oomd kills it (or Claude's containers), not the system (user.slice.d). -Slice=user.slice Environment=CLAUDE_CONFIG_DIR=/var/lib/agent/claude XDG_RUNTIME_DIR=/run/user/1000 ExecStart=/usr/bin/claude remote-control --name %H Restart=always diff --git a/mkosi.extra/usr/lib/systemd/system/nftables.service.d/agent.conf b/mkosi.extra/usr/lib/systemd/system/nftables.service.d/agent.conf deleted file mode 100644 index d405ce5..0000000 --- a/mkosi.extra/usr/lib/systemd/system/nftables.service.d/agent.conf +++ /dev/null @@ -1,6 +0,0 @@ -# Load our firewall (/usr/lib/agent/firewall.nft) instead of /etc/sysconfig/nftables.conf. -[Service] -ExecStart= -ExecStart=/usr/sbin/nft -f /usr/lib/agent/firewall.nft -ExecReload= -ExecReload=/usr/sbin/nft -f /usr/lib/agent/firewall.nft diff --git a/mkosi.extra/usr/lib/systemd/system/user.slice.d/50-oomd.conf b/mkosi.extra/usr/lib/systemd/system/user.slice.d/50-oomd.conf deleted file mode 100644 index f762bd4..0000000 --- a/mkosi.extra/usr/lib/systemd/system/user.slice.d/50-oomd.conf +++ /dev/null @@ -1,7 +0,0 @@ -# systemd-oomd for Claude and its containers: when they run the VM out of memory, kill the cgroup -# responsible before the VM thrashes on zram swap or the kernel's OOM killer picks sshd. -[Slice] -# Memory pressure above 60% for 30s: kill the cgroup reclaiming the most memory. -ManagedOOMMemoryPressure=kill -# Swap over 90% full: kill the cgroup using the most swap. -ManagedOOMSwap=kill diff --git a/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer b/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer index f4b66cc..467f899 100644 --- a/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer +++ b/mkosi.extra/usr/lib/sysupdate.d/20-uki.transfer @@ -1,4 +1,8 @@ # Installs the next UKI, which systemd-boot boots by default. Verify=yes: see 10-root.transfer. +# It gets three tries (boot counting, the +@l-@d in its name; no Mode=0444, systemd-boot can't +# rename a read-only file): systemd-bless-boot marks it good once a boot gets through +# (boot-complete.target); if none does, systemd-boot goes back to the previous UKI, and with it +# the previous root partition (each UKI holds its root hash). [Transfer] ProtectVersion=%A Verify=yes @@ -12,6 +16,7 @@ MatchPattern=agent_@v.efi Type=regular-file Path=/EFI/Linux PathRelativeTo=boot -MatchPattern=agent_@v.efi -Mode=0444 +MatchPattern=agent_@v+@l-@d.efi agent_@v+@l.efi agent_@v.efi +TriesLeft=3 +TriesDone=0 InstancesMax=2