From 4dd6ea85895430c1ef231c0f5ec1437bcb7ff0e9 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 21 Nov 2026 15:12:13 +0000 Subject: run-firecracker-vm: the SSH host key a secret, no --ssh-host-key The SSH host key had a way of its own into the guest: --ssh-host-key FILE (by default ~/.local/share/run-firecracker-vm/ssh_host_ed25519_key, created if it didn't exist), served via 9P on vsock port 567, and fc-ssh-host-key. Now it's a secret like any other: the base system declares it (fc.secrets.ssh_host_ed25519_key, at /run/ssh/ssh_host_ed25519_key, 0600, with sshd), so the run script requires --secret ssh_host_ed25519_key=FILE, and checks it as before (adding a missing final newline; no passphrase). There's no --ssh-host-key any more, nor a default key, and the secrets take over port 567. --- README.md | 16 +++---- pkgs/run-firecracker-vm/default.nix | 83 ++++++++++++------------------------- 2 files changed, 36 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 303f2a7..3ee09f0 100644 --- a/README.md +++ b/README.md @@ -138,13 +138,15 @@ faster, but changes may not show up while the guest runs). Other modes: The guest gets 2 CPUs and 1024 MiB unless told otherwise (`--vcpus N`, 1 to 32; `--mem-size MIB`). -The guests run sshd. Its host key comes from the host at boot, so it's -not in the store: `--ssh-host-key FILE`, by default -`~/.local/share/run-firecracker-vm/ssh_host_ed25519_key`, created on first -use, so all your guests have the same one, and one known_hosts line does: +The guests run sshd. Its host key is a secret (see below), so it's not +in the store, and the run script requires it: `--secret +ssh_host_ed25519_key=FILE` (an ed25519 private key without passphrase). +With the same one for all your guests, one known_hosts line does: ```sh +ssh-keygen -t ed25519 -N "" -f ~/.local/share/run-firecracker-vm/ssh_host_ed25519_key echo "172.16.0.*,fdfc:fc::* $(cut -d' ' -f1,2 ~/.local/share/run-firecracker-vm/ssh_host_ed25519_key.pub)" >> ~/.ssh/known_hosts +./result/bin/run-firecracker-vm --secret ssh_host_ed25519_key=$HOME/.local/share/run-firecracker-vm/ssh_host_ed25519_key ``` Who may log in goes in a layer, e.g.: @@ -156,9 +158,9 @@ Who may log in goes in a layer, e.g.: A guest's address is its VM's, by name (`vms`, see host setup), or else its tap's own (see `fc-egress vms`). -Secrets, like API keys, come from the host at boot, too, never through -the store or `/xchg` (which other VMs started in the same directory -see): a layer declares what it needs, where it goes (default: +Secrets, like API keys (and that host key), come from the host at boot, +never through the store or `/xchg` (which other VMs started in the same +directory see): a layer declares what it needs, where it goes (default: `/run/secrets/NAME`), and whose it is (default: root's, 0400): ```nix diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index f78aaa5..106ec05 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -215,9 +215,9 @@ let environment.systemPackages = [ fc-egress ]; # sshd, started on demand (a layer may turn that off), with the host - # key the run script serves via 9P on vsock port 567 (--ssh-host-key; - # not in the store), copied to /run/ssh before sshd (or its socket) - # starts, and never one generated instead. Who may log in: + # key a secret (--secret ssh_host_ed25519_key=FILE; see fc-secrets), + # in place before sshd (or its socket) starts, and never one + # generated instead. Who may log in: # users.users..openssh.authorizedKeys, e.g. in a layer. services.openssh = { enable = true; @@ -225,29 +225,13 @@ let generateHostKeys = false; hostKeys = [{ path = "/run/ssh/ssh_host_ed25519_key"; type = "ed25519"; }]; }; - systemd.services.fc-ssh-host-key = let - sshd = if config.services.openssh.startWhenNeeded then [ "sshd.socket" ] else [ "sshd.service" ]; - in { - requiredBy = sshd; - # Early, without the default dependencies: sshd.socket is before - # sockets.target, which is before basic.target, which a service is - # after by default, so it can't be before sshd.socket. - before = sshd ++ [ "shutdown.target" ]; - conflicts = [ "shutdown.target" ]; - unitConfig.DefaultDependencies = false; - serviceConfig.Type = "oneshot"; - serviceConfig.RemainAfterExit = true; - script = '' - share=$(mktemp -d -p /run) - ${mountVsock9p} 567 "$share" -o ro - install -D -m 600 "$share"/ssh_host_ed25519_key /run/ssh/ssh_host_ed25519_key - ${pkgs.util-linux}/bin/umount "$share" - rmdir "$share" - ''; + fc.secrets.ssh_host_ed25519_key = lib.mkIf config.services.openssh.enable { + path = "/run/ssh/ssh_host_ed25519_key"; + mode = "0600"; }; # The secrets (fc.secrets), which the run script serves via 9P on - # vsock port 568 (--secret NAME=FILE; never in the store), copied + # vsock port 567 (--secret NAME=FILE; never in the store), copied # into place, each with its owner and mode, before sysinit.target # (so before any service with the default dependencies), but after # the users get created, for the owners; any other secret the run @@ -263,7 +247,7 @@ let serviceConfig.RemainAfterExit = true; script = '' share=$(mktemp -d -p /run) - ${mountVsock9p} 568 "$share" -o ro + ${mountVsock9p} 567 "$share" -o ro trap '${pkgs.util-linux}/bin/umount "$share"; rmdir "$share"' EXIT install -d -m 0755 /run/secrets ${lib.concatStrings (lib.mapAttrsToList (name: s: '' @@ -514,7 +498,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # [--mem-size MIB] # [--nixpkgs DIR] [--nixpkgs-cache MODE] # [--egress LIST] [--name NAME] [--vcpus N] - # [--ssh-host-key FILE] [--secret NAME=FILE]... + # [--secret NAME=FILE]... # The guest gets the directory xchg (in the current working directory) # at /xchg, via 9P over vsock. VMs started in the same directory share # it. Its store is read-only, unless it has nix (layers/nix.nix). @@ -539,9 +523,6 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # --name names the VM, for fc-egress and as the guest's host name; only # one running VM of the user's can have a given name. --vcpus: the # guest's number of CPUs (default 2; Firecracker takes 1 to 32). - # --ssh-host-key: the guest's sshd's (ed25519) host key, by default - # ~/.local/share/run-firecracker-vm/ssh_host_ed25519_key, created if it - # doesn't exist, so all of the user's guests have the same one. # --secret NAME=FILE: a secret for the guest, NAME letters, digits, '.', # '_' and '-', FILE read once (it may be a pipe, like <(...)), and handed # to the guest only, never in the store (see fc-secrets); a guest @@ -550,7 +531,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.openssh pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} usage() { - echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--ssh-host-key FILE] [--secret NAME=FILE]..." + echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--secret NAME=FILE]..." } args= rw_store= @@ -561,7 +542,6 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' egress= name= vcpus=2 - ssh_host_key=''${XDG_DATA_HOME:-$HOME/.local/share}/run-firecracker-vm/ssh_host_ed25519_key nixpkgs=${lib.escapeShellArg (toString pkgs.path)} nix_opt= # the last option that's only for a guest with nix secrets=() # NAME=FILE, as given @@ -578,7 +558,6 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' --egress) egress=$2; shift 2 ;; --name) name=$2; shift 2 ;; --vcpus) vcpus=$2; shift 2 ;; - --ssh-host-key) ssh_host_key=$2; shift 2 ;; --secret) secrets+=("$2"); shift 2 ;; -h|--help) usage; exit ;; *) usage >&2; exit 1 @@ -614,28 +593,9 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT - # The SSH host key, a copy, in a directory only the user can read, for - # the guest to copy (see fc-ssh-host-key). - if ! test -e "$ssh_host_key"; then - mkdir -p "$(dirname "$ssh_host_key")" - ssh-keygen -q -t ed25519 -N "" -C run-firecracker-vm -f "$ssh_host_key" - fi - # (FILE may be a pipe, e.g. <(...), so it's only read once, for the copy.) - # sshd takes no key without its final newline, which, e.g., $(...) loses. - mkdir "$tmp"/ssh - key=$tmp/ssh/ssh_host_ed25519_key - cp "$ssh_host_key" "$key" - chmod 600 "$key" - if test -n "$(tail -c 1 "$key")"; then - echo >> "$key" - fi - if ! ssh-keygen -y -f "$key" > /dev/null; then - echo "$0: $ssh_host_key: not an SSH private key without passphrase" >&2 - exit 1 - fi - - # The secrets, copies, likewise, for the guest to copy (see fc-secrets), - # each FILE read once. The guest's own (fc.secrets) are required. + # The secrets, copies, in a directory only the user can read, for the + # guest to copy (see fc-secrets), each FILE read once. The guest's own + # (fc.secrets) are required. mkdir -m 700 "$tmp"/secrets for secret in ''${secrets[@]+"''${secrets[@]}"}; do s=''${secret%%=*} @@ -654,6 +614,18 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' fi chmod 600 "$tmp/secrets/$s" done + # sshd takes no host key without its final newline, which, e.g., $(...) + # loses, and none with a passphrase. + key=$tmp/secrets/ssh_host_ed25519_key + if test -e "$key"; then + if test -n "$(tail -c 1 "$key")"; then + echo >> "$key" + fi + if ! ssh-keygen -y -f "$key" > /dev/null; then + echo "$0: --secret ssh_host_ed25519_key: not an SSH private key without passphrase" >&2 + exit 1 + fi + fi for s in ${lib.escapeShellArgs (lib.attrNames nixos.config.fc.secrets)}; do if ! test -e "$tmp/secrets/$s"; then echo "$0: the guest needs the secret $s: --secret $s=FILE is required" >&2 @@ -795,7 +767,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' vm= fi - # The 9P servers, for /xchg, the SSH host key, the secrets, and with nix, the store + # The 9P servers, for /xchg, the secrets, and with nix, the store # and nixpkgs, one each per VM. They run as the user running this # script: xchg's files end up owned by the user, and the store is # read-only to them. When the guest connects to vsock port PORT on the @@ -815,8 +787,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' } mkdir -p xchg serve 564 xchg - serve 567 "$tmp"/ssh - serve 568 "$tmp"/secrets + serve 567 "$tmp"/secrets ${lib.optionalString nix '' serve 565 /nix/store serve 566 "$nixpkgs" -- cgit v1.3.1