Give each Reflash board its own persistent SSH host key - #102
Merged
Conversation
mkimage.sh installed openssh-server during the image build, which generates SSH host keys as a side effect - baked into that one build, they'd be identical across every image and every board flashed from it (#80). That's a real MITM/impersonation risk: the private key extracted from any single image works against every other board running the same build. Deleted the build-time keys and added ssh-keygen-boot.service, which restores (or generates once and saves) them against /mnt/usb instead - the one thing on this board that actually persists across reboots, since the root fs runs from initrd and doesn't. Ordered before both ssh.service and reflash.service so it has /mnt/usb to itself first; mount-unmount-usb has no locking of its own, so overlapping access from two processes at once isn't safe. On a freshly-flashed drive /mnt/usb's second partition doesn't exist yet - it's normally created by expand-usb, called from the Go server's own startup, which runs after this unit specifically so it doesn't race the server for the drive. ssh-keygen-boot calls expand-usb itself first; it's idempotent, so the Go server calling it again moments later is harmless. Live-tested on real hardware through the actual boot sequence (not just invoked directly): flashed a fresh image, confirmed keys are generated and saved to /mnt/usb/ssh_host_keys/ on first boot, then rebooted and confirmed the exact same key (verified by fingerprint) is restored rather than regenerated. Closes #80
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mkimage.shinstalledopenssh-serverduring the image build, which generates SSH host keys as a side effect - baked into that one build, they'd be identical across every image and every board flashed from it (#80). That's a real MITM/impersonation risk: the private key extracted from any single image works against every other board running the same build.Deleted the build-time keys and added
ssh-keygen-boot.service, which restores (or generates once and saves) them against/mnt/usbinstead - the one thing that actually persists across reboots on this board, since the root fs runs from initrd and doesn't. Ordered before bothssh.serviceandreflash.serviceso it has/mnt/usbto itself first -mount-unmount-usbhas no locking of its own, so overlapping access from two processes isn't safe.On a freshly-flashed drive,
/mnt/usb's second partition doesn't exist yet - it's normally created byexpand-usb, called from the Go server's own startup, which runs after this unit specifically so it doesn't race the server for the drive.ssh-keygen-bootcallsexpand-usbitself first; it's idempotent, so the Go server calling it again moments later is harmless.Testing
make test-bats/go test ./...- no regressions (this script's logic depends on real block-device/mount behavior that isn't meaningfully unit-testable the way the other bats-covered scripts are, so validation is live-hardware only, below).ssh-keygen-boot.servicecorrectly detects no/mnt/usbpartition yet, creates it viaexpand-usb, generates keys, and saves them to/mnt/usb/ssh_host_keys/.Closes #80