Skip to content

secrets: don't pass the RPC password to rpcauth in argv - #44

Merged
seberm merged 2 commits into
bitcoinix:masterfrom
bobberb:rpcauth-stdin
Oct 20, 2026
Merged

seberm merged 2 commits into
bitcoinix:masterfrom
bobberb:rpcauth-stdin

Conversation

@bobberb

@bobberb bobberb commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

modules/secrets/secrets.nix ran

${rpcauth} $user $(cat "$file") | grep rpcauth | cut -d ':' -f 2 > "$HMACfile"

so the plaintext RPC password was an argv element of rpcauth (and the python it execs), readable by any local user via /proc/<pid>/cmdline while it ran. Fixes #24.

Change

rpcauth.py (pinned upstream file) has no way to read the password from a file or stdin: - uses getpass, which reads from the tty. So the rpcauth wrapper now loads rpcauth.py with runpy.run_path (its main() doesn't run) and calls its own generate_salt(16) and password_to_hmac(salt, password) on the password read from the file. Only the file path is in argv.

The wrapper prints <salt>$<hmac> directly, so the grep | cut step is gone. Output format is unchanged: 32 hex chars, $, 64 hex chars, newline (98 bytes), the value bitcoind expects after rpcauth=<user>:.

The password is read the way clients read it ("$(cat file)"): the file is opened with newline="", so CR bytes are kept, and only trailing newlines are stripped. This matters only for hand-placed password files (pwgen -s never writes CR, spaces or globs). Compared to master, passwords with spaces, glob characters or leading/trailing whitespace now get a matching HMAC; the old unquoted $(cat) split or mangled them. A password file that isn't valid UTF-8 makes the generator fail (rc 1); master handled it bytewise.

Also: no empty secret files. Passwords and HMACs were written with a plain redirect, which creates the file before the command runs. If pwgen or rpcauth failed, an empty file stayed behind:

  • An empty HMAC file is newer than its password, so [[ $file -nt $HMACfile ]] never regenerated it, and bitcoind got rpcauth=<user>: with an empty HMAC.
  • An empty password file passed the [[ -e $1 ]] check forever, and rpcauth hashed the empty string into a valid HMAC, so bitcoind would accept an empty RPC password for that user.

A new writeSecret helper writes to <file>.tmp and renames it only on success (removing the temp file on failure). makePasswordSecret now checks -s instead of -e, and the HMAC is also regenerated when its file is empty, so files left by earlier versions get fixed on the next run.

Testing

New test secrets-no-argv (enabled whenever bitcoind is): runs nix-bitcoin.generateSecretsScript in a fresh dir under strace -f -e trace=execve, then

  • fails if the content of any generated *password* file (16+ chars) appears in the trace,
  • checks each bitcoin-HMAC-<user> matches \A[0-9a-f]{32}\$[0-9a-f]{64}\n\Z and equals HMAC-SHA256(salt, password) computed in the test driver,
  • with bitcoin-rpcpassword-public placed by hand before generation as CrLfPassword12345\r\n, so the HMAC has to cover the \r like $(cat) does,
  • that a failed HMAC generation (password "file" replaced by a directory, IsADirectoryError) leaves neither an HMAC file nor a temp file,
  • that an empty HMAC file newer than its password is regenerated and matches the password,
  • that an empty password file is regenerated (20 chars from pwgen) together with a matching HMAC.

The existing bitcoind test still authenticates as user public with the generated rpcauth line (RPC User public not allowed to call method stop means auth succeeded).

What Result
Ad hoc scenario { services.bitcoind.enable = true; }, secrets.nix from origin/master (red) FAIL as expected: Secret 'bitcoin-rpcpassword-privileged' was passed as a command-line argument
Same scenario with the fix (green) PASS
The CRLF case Not red on master (the old $(cat) kept the CR too). It guards against a regression in the first version of this change, which used universal newlines
Scenario default PASS
Scenario netnsRegtest PASS (before the CR fix; the fix only adds newline="" and the CRLF case)
Out-of-VM harness: wrapper HMAC vs HMAC of "$(cat pw)" for plain, spaces/quotes/glob/$, leading and trailing whitespace, multiple trailing newlines, no newline, CRLF, lone CR, UTF-8, empty file, embedded newline all MATCH
Generator outside a VM, old vs new, same password file, under strace old: password in 2 execve calls; new: 0. Both write a 98 byte salt$hmac\n that verifies
shellcheck on the generated rpcauth wrapper clean
Empty-file checks, generator from the first version of this PR (plain redirect) FAIL as expected: the failed run leaves bitcoin-HMAC-public behind
Same, temp file but no regeneration of empty HMAC files FAIL as expected: empty HMAC stays (Pattern ... not found in '')
Same, everything but -s for passwords FAIL as expected: empty password file stays
All checks with the final code, ad hoc bitcoind scenario PASS
default, regtest, netnsRegtest, wireguard-lndconnect, clightning-replication on the final code (the last two generate secrets with wg genkey and ssh-keygen) PASS
./test/shellcheck.sh PASS
nix flake check --all-systems PASS
Separate attacker review: traced every generateSecretsCmds block for argv leaks, compared the HMAC with "$(cat file)" for 11 password shapes First round found that CRLF password files were hashed without the CR; fixed (newline=""), all 11 cases now match
Separate attacker review of the empty-file fix PASS. Found the empty password file case (now fixed and tested). Tried half-fixes, /dev/full, crash after mv, a leftover 0440 .tmp, symlinked HMAC file, two concurrent runs: all held

Not covered

  • HMAC regeneration still only compares mtimes. An HMAC that doesn't match its password but is non-empty and newer (clock skew, a restore with cp -p, equal mtimes) is kept, as before.
  • Every makePasswordSecret secret (lnd-wallet-password, backup-encryption-password, clightning-replication-password, rtl-password, liquid-rpcpassword) is now regenerated if its file is empty. None of the encryption passwords can have been in use while empty. Checked with the nixpkgs versions: lndinit init-wallet fails with private passphrase may not be empty, gocryptfs -init -passfile fails with could not read from "pw": EOF, and duplicity's gpg fails with Invalid passphrase and writes no backup. rtl-password and liquid-rpcpassword are read from the file by every consumer on each start.
  • Not fixed here, same as on master: the failed lndinit init-wallet run above still leaves a wallet.db (16 KiB, no wallet), so the [[ ! -f wallet.db ]] check in lnd's preStart skips wallet creation on later starts, also after the password is regenerated.
  • The password is still in the Python process memory and in the file, as before. This only removes it from argv.

🤖 Generated with Claude Code

@ostermayer ostermayer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping the CR is more correct than our .strip(); the clients read the password with $(cat file), which keeps it too. writeSecret and the empty-file regeneration fix a real failure mode we did not cover. Approving.

@seberm seberm added area: modules modules/ and pkgs/lib.nix type: security Hardening or vulnerability (public-safe only) labels Oct 16, 2026
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
`rtl-setup-config` spliced the RTL `multiPass` into a sed program with
`$(cat ...)`. A program argument is visible to every local user through
/proc/<pid>/cmdline for as long as sed runs, and the password guards an
lnd admin macaroon copy and the clnrest admin rune.

Let jq read the secret file itself (`--rawfile`) and set the field. No
process ever sees the password as an argument, and jq escapes it as
JSON, which sed did not (a password with `|`, `&` or `"` broke the
config before).

Same class as the rpcauth argv fix (bitcoinix#44) and the lndconnect fix (bitcoinix#77).
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
The backend's `preStart` spliced the public RPC password into a sed
program with `$(cat ...)`. A program argument is visible to every local
user through /proc/<pid>/cmdline for as long as sed runs.

Let jq read the secret file itself (`--rawfile`) and set
`CORE_RPC.PASSWORD`. No process sees the password as an argument, and jq
escapes it as JSON.

Same class as the rpcauth argv fix (bitcoinix#44) and the lndconnect fix (bitcoinix#77).
@seberm seberm mentioned this pull request Oct 17, 2026
prusnak
prusnak previously approved these changes Oct 18, 2026
seberm pushed a commit that referenced this pull request Oct 19, 2026
The clightning-rest and clnrest variants of the connect helper rewrote
the URL with sed, with the admin macaroon (hex) or the admin rune spliced
into the sed program. A program argument is visible to every local user
through /proc/<pid>/cmdline for as long as sed runs. Use bash parameter
substitution instead, which spawns no process.

Same class as the rpcauth argv fix (#44).
seberm pushed a commit to ostermayer/bitcoinix that referenced this pull request Oct 19, 2026
`rtl-setup-config` spliced the RTL `multiPass` into a sed program with
`$(cat ...)`. A program argument is visible to every local user through
/proc/<pid>/cmdline for as long as sed runs, and the password guards an
lnd admin macaroon copy and the clnrest admin rune.

Let jq read the secret file itself (`--rawfile`) and set the field. No
process ever sees the password as an argument, and jq escapes it as
JSON, which sed did not (a password with `|`, `&` or `"` broke the
config before).

Same class as the rpcauth argv fix (bitcoinix#44) and the lndconnect fix (bitcoinix#77).
@github-actions github-actions Bot added the area: ci .github/, test/, dev/ label Oct 20, 2026
bobberb and others added 2 commits October 20, 2026 12:55
makeBitcoinRPCPassword ran `rpcauth $user $(cat "$file")`, so the
plaintext password was readable by any local user via
/proc/<pid>/cmdline while rpcauth ran. rpcauth.py only takes the
password as an argument (`-` reads it via getpass from the tty).

Load rpcauth.py as a module instead and call its generate_salt and
password_to_hmac functions on the password read from the file. The
output is the same `<salt>$<hmac>` line that grep/cut extracted
before: 16 random bytes as hex, HMAC-SHA256 hex, trailing newline.

Clients send the password as `$(cat file)`, so read it the same way:
keep CR bytes (no newline translation) and strip trailing newlines
only. Unlike the old unquoted `$(cat)`, passwords with spaces, glob
characters or leading/trailing whitespace now also get a matching HMAC.

Also don't leave empty secret files behind. Passwords and HMACs were
written with a plain redirect, which creates the file before the command
runs. If pwgen or rpcauth failed, an empty file stayed, and neither the
`-e` check for passwords nor the `-nt` check for HMACs regenerated it.
An empty password file got the HMAC of a random password (rpcauth.py
generates one when it gets none), so nobody could log in as that RPC
user. A new `writeSecret` helper
writes to a temp file and renames it on success, and empty password and
HMAC files left by earlier versions are now regenerated.

Add test `secrets-no-argv`: run the secrets generator under strace,
check no generated password appears in any execve, and check each
bitcoin-HMAC-* file matches its password, including a hand-placed
password file with a CRLF line ending. It also checks that a failed
HMAC generation leaves no file behind, and that empty password and HMAC
files are regenerated.

Reported as a low finding in the ostermayer/nix-bitcoin security audit
(docs/security-audit-2026-08-14.md), fixed there in the secrets.nix part
of ostermayer/nix-bitcoin@e66c00a. This is a different implementation,
so it is not a cherry-pick.

Fixes bitcoinix#24

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A password file that holds only a newline isn't empty, so
makePasswordSecret keeps it. The rpcauth wrapper then stripped the
newline and wrote the HMAC of the empty string, and bitcoind would
accept an empty password for that RPC user.

Fail secret generation for such a file instead, so it leaves no HMAC
behind and setup-secrets reports the error.

Assisted-by: Claude Code (Claude Opus 5.5)
@seberm

seberm commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Rebased onto master and added a small follow-up commit: the rpcauth wrapper now refuses a password file that holds only a newline, because otherwise it would write the HMAC of the empty password. While at it I corrected one sentence in your commit message: on master an empty password file got the HMAC of a random password (rpcauth.py generates one when it gets none), not of the empty string. Thanks for the thorough testing table, it made this easy to review.

@seberm
seberm merged commit e90bebf into bitcoinix:master Oct 20, 2026
10 checks passed
prusnak pushed a commit to ostermayer/bitcoinix that referenced this pull request Oct 20, 2026
The backend's `preStart` spliced the public RPC password into a sed
program with `$(cat ...)`. A program argument is visible to every local
user through /proc/<pid>/cmdline for as long as sed runs.

Let jq read the secret file itself (`--rawfile`) and set
`CORE_RPC.PASSWORD`. No process sees the password as an argument, and jq
escapes it as JSON.

Same class as the rpcauth argv fix (bitcoinix#44) and the lndconnect fix (bitcoinix#77).
msgilligan pushed a commit to msgilligan/bitcoinix that referenced this pull request Nov 8, 2026
The clightning-rest and clnrest variants of the connect helper rewrote
the URL with sed, with the admin macaroon (hex) or the admin rune spliced
into the sed program. A program argument is visible to every local user
through /proc/<pid>/cmdline for as long as sed runs. Use bash parameter
substitution instead, which spawns no process.

Same class as the rpcauth argv fix (bitcoinix#44).
msgilligan pushed a commit to msgilligan/bitcoinix that referenced this pull request Nov 8, 2026
`rtl-setup-config` spliced the RTL `multiPass` into a sed program with
`$(cat ...)`. A program argument is visible to every local user through
/proc/<pid>/cmdline for as long as sed runs, and the password guards an
lnd admin macaroon copy and the clnrest admin rune.

Let jq read the secret file itself (`--rawfile`) and set the field. No
process ever sees the password as an argument, and jq escapes it as
JSON, which sed did not (a password with `|`, `&` or `"` broke the
config before).

Same class as the rpcauth argv fix (bitcoinix#44) and the lndconnect fix (bitcoinix#77).
msgilligan pushed a commit to msgilligan/bitcoinix that referenced this pull request Nov 8, 2026
The backend's `preStart` spliced the public RPC password into a sed
program with `$(cat ...)`. A program argument is visible to every local
user through /proc/<pid>/cmdline for as long as sed runs.

Let jq read the secret file itself (`--rawfile`) and set
`CORE_RPC.PASSWORD`. No process sees the password as an argument, and jq
escapes it as JSON.

Same class as the rpcauth argv fix (bitcoinix#44) and the lndconnect fix (bitcoinix#77).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ci .github/, test/, dev/ area: modules modules/ and pkgs/lib.nix type: security Hardening or vulnerability (public-safe only)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

secrets: RPC password is passed in argv to rpcauth during secret generation

4 participants