12 KiB
Plan: Non-interactive mnemonic input at startup (--mnemonic-stdin, --mnemonic-fd)
Status: ready to implement.
Related: documents/SECURITY.md, src/main.c, src/mnemonic.c.
Companion plan (future work): plans/embedded_nsigner_spawn.md.
1. Goal
Allow n_signer to receive its mnemonic from a parent process at startup, without the human re-typing it, while preserving the project's existing security posture:
- nothing on the filesystem
- nothing in argv (no
--mnemonic <phrase>flag) - nothing in environment (no
NSIGNER_MNEMONICenv var) for production use - the mnemonic source is wiped from process memory immediately after a successful load
Two new CLI flags are added:
--mnemonic-stdin— read one line from stdin during the unlock phase, treat that line as the mnemonic, then continue to the normal status display. Stdin is closed (or replaced with/dev/null) before the running phase begins so the listener loop and TUI hotkeys keep working.--mnemonic-fd N— read one line from file descriptorN, then close it. Intended to be wired by parent processes to apipe2(O_CLOEXEC)write end or amemfd_create(2)region. No filesystem path is involved —Nis an inherited FD number, equivalent to stdin in lifecycle but separable so it can coexist with--listen stdio.
The existing interactive TUI prompt remains the default when neither flag is given.
2. Why no --mnemonic <phrase> argv flag
Listed only to make the rejection explicit and discoverable in git log:
- visible to every same-uid process via
/proc/<pid>/cmdline - captured by
ps auxww - captured by shell history (
bash/zshHISTFILE) - captured by audit subsystems (
auditd, sysmon-like agents) - captured by container/VM platform telemetry that records launch commands
Stdin and inherited FDs do not appear in any of those surfaces. They are the right primitive.
Likewise no NSIGNER_MNEMONIC environment variable, because envp is exposed via /proc/<pid>/environ to same-uid readers. Any future env-based override should be opt-in for testing only and named NSIGNER_TEST_* like the existing test hooks.
3. CLI surface changes
3.1 New flags (in src/main.c)
| Flag | Argument | Behavior |
|---|---|---|
--mnemonic-stdin |
none | read mnemonic from FD 0, then close FD 0 and reopen as /dev/null |
--mnemonic-fd <N> |
int | read mnemonic from FD N, then close FD N |
Both flags are mutually exclusive with each other.
3.2 Compatibility with --listen modes
| Listen mode | --mnemonic-stdin |
--mnemonic-fd |
|---|---|---|
unix (default) |
OK | OK |
tcp:HOST:PORT |
OK | OK |
stdio |
rejected (stdin is the request transport) | OK (FD must not be 0/1) |
qrexec |
rejected (stdio is the request transport) | OK (FD must not be 0/1) |
If both --mnemonic-stdin and --listen stdio/qrexec are passed, n_signer exits with a clear error before doing anything else.
--mnemonic-fd rejects N == 0 if --listen stdio/qrexec is also passed, and rejects N == 1 always (stdout is reserved for response framing in stdio/qrexec modes and for the TUI in unix/tcp modes).
3.3 Updated print_usage()
Two new lines added under the existing options section:
--mnemonic-stdin Read mnemonic from stdin (one line). Default unlock prompt is skipped.
--mnemonic-fd N Read mnemonic from inherited FD N (anonymous, no filesystem). Same-line semantics.
3.4 --help discoverability
The current print_usage() block in src/main.c already lists the other flags in tabular form; the two new lines slot in next to --auth. No structural change.
4. Behavioral spec
4.1 prompt_load_mnemonic becomes load_mnemonic
The existing function prompt_load_mnemonic() is renamed/refactored to load_mnemonic(mnemonic_state_t*, mnemonic_source_t) where:
typedef enum {
MNEMONIC_SRC_TUI_PROMPT = 0, /* current default behavior */
MNEMONIC_SRC_STDIN, /* --mnemonic-stdin */
MNEMONIC_SRC_FD, /* --mnemonic-fd N */
} mnemonic_source_kind_t;
typedef struct {
mnemonic_source_kind_t kind;
int fd; /* used for SRC_STDIN (=0) and SRC_FD */
} mnemonic_source_t;
For SRC_STDIN and SRC_FD:
- Read up to
MNEMONIC_MAX_LENbytes from the FD using a small bounded read loop (read(2)), stopping at the first\nor EOF. - Trim a single trailing
\nand any single trailing\r(handle Windows-style line endings just in case). - Reject input containing any embedded
\0(defensive; mnemonic is text). - Reject input that ends without seeing EOF or
\nafterMNEMONIC_MAX_LENbytes (caller is misbehaving). - Pass the buffer to
mnemonic_load()which already validates word count. - Zeroize the local read buffer with
secure_memzeroregardless of success/failure. - On success, close the source FD. For
SRC_STDIN, alsodup2()/dev/nullonto FD 0 so subsequentread()s on FD 0 don't accidentally consume bytes meant for nothing. - On failure (read error, validation error), exit non-zero. There is no retry loop in non-interactive mode — the parent should fix its input and re-spawn; silently retrying could mask a serious bug like leaked mnemonic-passing logic.
For SRC_TUI_PROMPT: unchanged, current behavior preserved verbatim, including the existing 10-attempt retry loop.
4.2 Lock/unlock keystroke (l hotkey)
Currently the l hotkey calls prompt_load_mnemonic() again interactively. After this change:
- If the original startup used the TUI prompt:
lre-runs the TUI prompt (unchanged). - If the original startup used
--mnemonic-stdinor--mnemonic-fd: the source FD has already been closed and zeroized, so re-locking would have no source to re-read from. Two options:- Option A (chosen): the
lhotkey falls back to the TUI prompt on re-unlock, regardless of how the original unlock happened. The user is at the terminal, so the TUI prompt is always available. - Option B: disable
lentirely if non-interactive source was used. Rejected — too restrictive.
- Option A (chosen): the
Implementation: track the original source kind in a static in main.c; when the user presses l after non-interactive startup, log a one-line notice ("re-unlock via terminal prompt") and call the TUI variant.
4.3 Trim/normalize rules
The mnemonic line is treated as ASCII text. Allowed transformations before validation:
- strip a single trailing
\r\nor\n - strip leading and trailing ASCII whitespace (space, tab)
- collapse internal runs of whitespace? No —
mnemonic_load()already tokenizes on whitespace; passing the trimmed string through unchanged is simpler and matches typed-mnemonic behavior.
4.4 Error messages
All errors go to stderr (not the TUI), prefixed nsigner:, and exit with status 1:
nsigner: --mnemonic-stdin and --mnemonic-fd are mutually exclusivensigner: --mnemonic-stdin requires --listen unix or --listen tcp:...nsigner: --mnemonic-fd N: invalid FD valuensigner: --mnemonic-fd N: read failed: <strerror>nsigner: mnemonic input exceeded maximum lengthnsigner: mnemonic validation failed
The actual mnemonic content is never printed in any error message. The generic "validation failed" message is intentional.
5. Security review checklist
Items the implementer should verify before merging:
- The stack buffer used for the read is wiped with
secure_memzeroon every exit path, including early-error returns. - If an
mlock-backed temporary buffer is used instead of a stack buffer, it must besecure_buf_alloc'd andsecure_buf_free'd. - The source FD is
close(2)'d after read, beforemnemonic_loadreturns control tomain(). Close happens even on failure paths. - For
--mnemonic-stdin, FD 0 is replaced with/dev/null(open("/dev/null", O_RDONLY)thendup2) so later code paths cannot read leftover bytes. - The new flags are documented in
README.md§3.1 (Startup phase) with an explicit note that they are intended for parent-process spawning and that the parent is responsible for FD hygiene. - The new flags are documented in
documents/SECURITY.mdunder §2 ("trust anchors") — the parent process now joins the trust chain when these flags are used. - No new path is added that prints the mnemonic to logs, the activity buffer, or any debug output.
man-style banner / help output never echoes the mnemonic.
6. Testing plan
New tests:
tests/test_mnemonic_input.c(new file) covering:--mnemonic-stdinhappy path: spawnnsignerwith a pipe, write a valid 12-word mnemonic +\n, expect successful unlock.--mnemonic-stdininvalid mnemonic: write garbage, expect non-zero exit.--mnemonic-stdinno newline before EOF: write valid mnemonic without\n, close pipe, expect success.--mnemonic-stdinline too long: write 1024 bytes, expect non-zero exit with the length error.--mnemonic-fd 3happy path: parent opens anO_CLOEXEC=0pipe, dups its write end as FD 3 in the child viaposix_spawn_file_actions_*, writes mnemonic, expects success.- Mutually-exclusive flag combinations:
--mnemonic-stdin --mnemonic-fd 3,--mnemonic-stdin --listen stdio,--mnemonic-fd 0 --listen stdio— all expected to exit 1 with the matching error string. - Lock-then-reunlock after
--mnemonic-stdinstartup: verify the TUI prompt path still works (usingNSIGNER_TEST_HOTKEYSandNSIGNER_TEST_NONINTERACTIVE_PROMPTtest hooks).
Existing tests must continue to pass:
tests/test_mnemonic.c(mnemonic library unit tests — unchanged surface)tests/test_integration.c(default TUI-prompt path)tests/test.shwrapper.
Add a row to Makefile test target for the new test binary.
7. Documentation updates
README.md§3.1: add a paragraph at the end noting the two new flags and pointing to this plan.documents/SECURITY.md§2.3: add a row to the trust-anchors table for "the parent process when--mnemonic-stdin/--mnemonic-fdis used".documents/CLIENT_IMPLEMENTATION.md: no changes (client wire protocol unaffected).- New section in
README.mdunder "Operating modes" pointing readers to the future spawning plan once that lands.
8. Out of scope (handled by companion plan)
- Embedding the
nsignerbinary into a host program viaobjcopyblob +memfd_create+fexecve. Seeplans/embedded_nsigner_spawn.md. - Reusable C client helper that picks a terminal emulator and wires the pipe. Same companion plan.
examples/spawn_and_sign.c. Same companion plan.
9. Acceptance criteria
nsigner --mnemonic-stdinreads one line, validates, and proceeds to status display without prompting.nsigner --mnemonic-fd 3(with FD 3 inherited from a parent test harness) does the same.- Invalid mnemonics exit non-zero with the generic validation message; mnemonic content is never printed.
- Mutually exclusive flag combinations are rejected at argument-parse time.
--listen stdioand--mnemonic-stdinare mutually exclusive.- After non-interactive unlock, pressing
lfalls back to the TUI prompt for re-unlock and the running phase otherwise behaves identically. - The mnemonic source FD is closed and the source buffer is zeroized on every path.
- All existing tests still pass; new tests in
tests/test_mnemonic_input.cpass. README.mdanddocuments/SECURITY.mdare updated.