Repository navigation
fix(security): harden key file writes and honor RSA key size - #372
Merged
Merged
Conversation
Private keys were written into the existing inode with create+truncate, so overwriting a 0644 file left the new key 0644. The software path checked output.exists(), which is false for a dangling symlink, so keys were written through links, and the public half went through std::fs::write after the private key was already on disk. The security-key staging helpers move from sk_files.rs to a shared keygen::files module (security_key re-exports unchanged). The ecdsa, ed25519 and rsa generators publish both halves through write_pair, and gen-host-key uses the new write_private: content is staged in a create_new 0600 file, synced, and renamed into place. bssh-keygen refuses symlinks and non-regular files at either path even with -y, the prompt names both files, and missing parent directories are created 0700. RSA generation now uses RsaKeypair::random with the requested size, since PrivateKey::random always produced 4096 bits. gen-host-key validates --bits with keygen::rsa::validate_bits. New unit tests in keygen and tests/keygen_file_safety_test.rs fail on the previous code and pass now. Closes #371
write_pair renamed the new private key into place before the public half. If the .pub rename then failed (a file locked on Windows, an entry owned by another user in a sticky directory, or a .pub replaced after the preflight check), the new private key stayed at <output> next to a stale public key, which #371 requires never to happen. When replacing, the public half is now published first, so a .pub that cannot be saved leaves no new private key behind. Creating a new pair (security-key no-overwrite mode) keeps the private-first hard_link order. A unit test drives the publish step with an unreplaceable .pub and fails on the previous order. Refs #371
Member
Author
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
|
Member
Author
|
Security and performance review of the key file write path (head 6e7d84f). No CRITICAL or HIGH findings, so no fixes were pushed. What holds up
MEDIUM / LOW (not fixed)
|
The review-driven change publishes the public half first when an existing pair is replaced, so a failure cannot leave a new private key beside an old public key. The bssh-keygen man page security notes and the CHANGELOG Security entry for #371 now state this. Validation: cargo fmt --check, cargo clippy --lib --tests --bins -D warnings, cargo test --lib keygen, cargo test --bin bssh-keygen, cargo test --test keygen_file_safety_test all pass. Refs #371
14 tasks done
A `.bssh-keygen-<hex>` file holding the private key stayed in the key directory when `bssh-keygen` or `bssh-server gen-host-key` was interrupted between staging and publishing. Named staged files are now tracked in a process-wide registry, and both binaries (only the gen-host-key subcommand of bssh-server) install a signal-hook thread that removes them on SIGINT, SIGTERM, and SIGHUP, then terminates with the signal's default action. Inherited ignored signals stay ignored. On Linux, staging uses `O_TMPFILE` and publishes with `linkat`, so no name exists until publish; it falls back to a named file where unsupported. The software generators published in overwrite mode even when `confirm_output` found nothing, so a file created in between was replaced. The CLI now passes that result through the new `keygen::generate_with_overwrite`; the existing `generate_*` functions keep replacing as before. No-replace publishing needed hard links, which FAT and exFAT lack. It now tries `renameat2(RENAME_NOREPLACE)` on Linux, `renamex_np(RENAME_EXCL)` on macOS, or `MoveFileExW` without replace on Windows, then `hard_link`, then, only on unsupported-operation errors, a checked rename with a documented race. The CHANGELOG Security entries and the bssh-keygen(1) and bssh-server(8) pages describe the cleanup, the no-replace publish, and the remaining SIGKILL and Windows Ctrl+C gaps. Tested with the keygen, security_key, bssh-keygen, bssh-server, and keygen_file_safety_test targets, a SIGTERM test that re-runs itself as a child, a FAT disk image, and a cross-target check of the files module for Linux and Windows. Refs #371
Member
Author
|
Security and performance review of 97438d7 (items 5-7 of #371): no CRITICAL or HIGH findings, so no fixes were pushed. Checked and found sound:
LOW (not fixed):
Refs #371 |
…sync The O_TMPFILE wording overstated the guarantee: a new key stays unnamed until published, but when replacing a key (and in the copy fallback) the inode is briefly linked to a registered .bssh-keygen-<hex> name before the rename, which SIGKILL can leave behind. The CHANGELOG, bssh-keygen(1), bssh-server(8), and the files module doc now say so. Interrupt cleanup also handles SIGQUIT, with the same removal and default action as SIGINT, SIGTERM, and SIGHUP. The destination directory is fsynced after every successful publish on Unix (errors from filesystems that reject directory fsync are ignored), so both bssh-keygen and gen-host-key get it. The SIGTERM cleanup test now wraps the child process in a guard that kills and reaps it on every failure path. Validation: - cargo fmt --check, cargo clippy --lib --tests --bins -- -D warnings: clean - cargo test --lib keygen (46), --bin bssh-keygen (17), --test keygen_file_safety_test (9): pass Refs #371
5 tasks
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
Hardens how
bssh-keygenandbssh-server gen-host-keywrite key files, and makes RSA-b/--bitstake effect. Covers items 1 to 4 of #371 plus the follow-up items 5 to 7 added in the issue thread.Key file publication (
keygen::files)keygen::filesmodule (security_keyre-exports kept). Every software generator andgen-host-keystages each file in a fresh 0600 file in the destination directory, syncs it, publishes it, and then fsyncs the directory. Overwriting a 0644 key now yields 0600.bssh-keygenchecks both the private and.pubpaths withsymlink_metadatabefore prompting. Symlinks (including dangling ones) and non-regular files are refused even with-y; the prompt names both files.gen-host-keyrefuses a non-regular output too..pubthat cannot be saved leaves no new private key behind. Missing parent directories of-fare created 0700.-yor a confirmed prompt still replaces. Publicgenerate_*signatures are unchanged; overwrite-awaregenerate_with_overwritevariants back the CLI.renameat2(RENAME_NOREPLACE)on Linux,renamex_np(RENAME_EXCL)on macOS,MoveFileExWwithout replace on Windows, thenhard_link, then a final check plus rename used only for unsupported-operation errors (race documented). Verified on a FAT32 volume, where the old code failed.O_TMPFILEinodes and published withlinkat, so they never appear under a temporary name; when replacing, the file is briefly named before the rename. SIGKILL in that window, and Windows Ctrl+C, are not covered and are documented.RSA key size
-b/--bitsnow takes effect: generation usesRsaKeypair::random(rng, bits)becausePrivateKey::randomalways produced 4096 bits.gen-host-keyvalidates withkeygen::rsa::validate_bits(2048 to 16384). Default stays 4096.Docs
[Unreleased]Security and Bug Fixes entries;bssh-keygen.1andbssh-server.8describe the write, refusal, signal cleanup, and exit status behavior.Changes during review
.pubpublication failed).O_TMPFILEkeeps new keys unnamed but replacement briefly names the file; SIGQUIT added to cleanup; directory fsync after publish; the SIGTERM test now always reaps its child.Test plan
src/keygen/mod.rsandsrc/keygen/files/tests.rs, newtests/keygen_file_safety_test.rsdriving both binaries with a temporary HOME, and a newbssh-keygenbinary test for a file created after confirmation. Tests that can run on the old code were shown to fail there (in amain/763c144worktree); the fallback-chain test was shown to fail with the fallback disabled; two tests are regression guards.bssh-keygen-*fileENOTSUP, new code publishes and refuses a second writecargo test --lib,cargo test --tests -- --skip integration_test,cargo clippy --all-targets --all-features -- -D warningskeygen::filesmodule; Linux paths execute in the ubuntu CI jobs; the Windows path is compile-checked onlyCloses #371