Repository navigation
security: validate and atomically install Tor config - #188
Conversation
f294f24 to
f86a276
Compare
RonDeBanc
left a comment
There was a problem hiding this comment.
Review summary
Thanks for tightening this path. The conservative service/port validation, staging beside torrc, Tor-side validation, atomic rename, root ownership, and systemd reload are all good directions.
I reviewed commit f86a276dd0a38ac8a8048da32b9ac551e2f6611e and exercised the change in a fixture using the system tor --verify-config: add, replace with a second port, remove, invalid service names, and invalid ports. The happy paths passed and the argument validation rejected the tested bad inputs. I also ran bash -n, git diff --check, and local ShellCheck.
I found two blocking issues before this should merge:
- Candidate generation can fail but still install an empty/truncated torrc because the pipelines do not fail on the earlier
sed/awkstages. HiddenServiceDirremains an unvalidated, sourced shell value and can inject additional Tor directives into the candidate even though the service name and ports are now validated.
See the inline comments for repro details and suggested fixes.
|
All three blocking findings addressed in 3bd619e. 1+2. Masked pipeline failures (add + removal paths): replaced both 3. HiddenServiceDir injection: the script no longer sources Verified locally: injection values rejected, legitimate paths accepted, empty-stage guard triggers. On the failing image-build checks: unrelated to this PR. Both |
RonDeBanc
left a comment
There was a problem hiding this comment.
Re-review of 3bd619e — previous blockers fixed, one new regression
Thanks for the update. Both blockers from my previous review are resolved:
- ✅ Candidate generation is now fail-safe — every stage of
generateTorrcCandidateis checked (if ! sudo sed …,[ ! -s "$stage" ], theawk | teepipeline,[ ! -s "$candidate" ]). A failed read can no longer yield an empty torrc. - ✅
HiddenServiceDiris no longer sourced shell —joininConfValuereads it as data andvalidateHiddenServiceDirrestricts it to/var/lib/toror/mnt/hdd/tor. The directive-injection path is closed. Atomic rename, root ownership,tor --verify-configgating, andsystemctl reloadall look right.
However, the new version has a regression that breaks both the add and remove paths at runtime:
stage=$(sudo mktemp) || exit 1
if ! sudo sed "…" /etc/tor/torrc > "$stage"; thensudo mktemp creates a root-owned 0600 file, but the > "$stage" redirection is opened by the unprivileged calling shell — the write fails with EACCES before sed even runs. Result: generateTorrcCandidate always exits 1 with "failed to read/process", so every invocation fails closed… permanently. All five callers (install.joinmarket-api.sh, info.importwallet.sh, menu.orderbook.sh, install.specter.sh ×2) invoke the script without sudo, so this hits every path.
Suggested fix (matches the pattern you already use two lines below):
if ! sudo sed "…" /etc/tor/torrc | sudo tee "$stage" >/dev/null; then—or drop sudo from the stage mktemp (a torrc copy isn't sensitive) and keep the user-owned redirect.
The PR's own test list (bash -n, git diff --check, grep checks) wouldn't catch this — it needs an actual add/remove run as the joinmarket user. Requesting changes until that path is exercised.
Minor notes (non-blocking):
- Restricting
HiddenServiceDirto two allowlisted roots refuses any pre-existing custom directory from older installs — intentional hardening, but worth a release-notes line. - Head-up for merge order: #195 also rewrites the
source joinin.confregion of this same file — one of the two will need a rebase.
Reviewed by Ron DeBanc (Hermes Agent)
|
Thanks @RonDeBanc — confirmed and fixed in cfb59a7. The regression: The fix: the write now stays inside the privileged pipeline — Functional testing (agreed,
Noted on the merge-order dependency with #195 — whichever lands second will need the rebase; happy to handle it on this side. |
cfb59a7 to
4ef0554
Compare
RonDeBanc
left a comment
There was a problem hiding this comment.
Re-review of 4ef055465 — request changes
The latest commit fixes the concrete EACCES regression from my last review: writes to the root-owned stage file now stay inside the privileged pipeline (sudo sed … | sudo tee "$stage"). The earlier HiddenServiceDir injection issue also remains fixed by reading it as data and enforcing the two-root allowlist.
One failure-detection issue remains before this meets the PR's promise to preserve the old torrc whenever generation fails:
if ! sudo sed "…" /etc/tor/torrc | sudo tee "$stage" >/dev/null; thenand later:
if ! sudo awk '…' "$stage" | sudo tee "$candidate" >/dev/null; thenThe script does not enable pipefail, so each conditional observes only tee's status. If sed or awk emits partial output and then exits nonzero, tee succeeds, the file is nonempty, and the code proceeds. If that partial configuration is still syntactically valid, tor --verify-config can accept and install a truncated torrc despite the source stage having failed.
I reproduced the shell behavior mechanically:
if ! (printf 'SocksPort 9050\n'; exit 7) | tee "$stage" >/dev/null; then
echo detected
else
echo NOT_DETECTED
fi
# => NOT_DETECTED, with nonempty stage contentPlease either:
- enable
set -o pipefailbefore these pipelines (and confirm it doesn't alter unrelated paths), or - capture and inspect both
PIPESTATUSentries immediately after each pipeline.
Once both producer and consumer statuses are checked, this is ready. The ownership, input allowlists, same-filesystem staging, Tor validation, atomic rename, cleanup trap, and systemd reload all look good.
Current checks: ShellCheck and spelling pass; image builds pending.
Reviewed by Ron DeBanc (Hermes Agent)
|
Fixed in b9bf42e — enabled Now both Checked the script's other pipelines for pipefail interactions: |
RonDeBanc
left a comment
There was a problem hiding this comment.
Re-review of b9bf42e72 — APPROVE
The remaining pipeline failure-detection blocker is fixed correctly. set -o pipefail is enabled before candidate generation, so both:
sudo sed … | sudo tee "$stage"
sudo awk … | sudo tee "$candidate"now fail when either the producer or tee fails. Partial producer output can no longer pass merely because the destination is nonempty. This completes the earlier fixes for the root-owned staging redirect and untrusted HiddenServiceDir.
The complete path now has the properties required by the PR: conservative input validation, root-owned same-filesystem staging, checked generation pipelines, nonempty checks, tor --verify-config, atomic replacement, cleanup traps, and systemd reload. ShellCheck, spelling, and arm64 image build pass; amd64 is still pending at review time.
Integration note: #195 also changes the config-loading section in this file. Preserve both #195's data-only sourceConf behavior and this PR's strict HiddenServiceDir allowlist when rebasing whichever lands second.
Reviewed by Ron DeBanc (Hermes Agent)
Address review blocking findings: - Replace sed|awk|tee pipelines with generateTorrcCandidate() which checks every stage explicitly and refuses empty/truncated candidates before installTorrc ever runs (both add and removal paths) - Stop sourcing joinin.conf; read values with a grep-based helper - Validate HiddenServiceDir against the expected Tor data roots (/var/lib/tor, /mnt/hdd/tor) so a crafted value cannot inject Tor directives into the candidate - Quote HiddenServiceDir path expansions
Addresses re-review: sudo mktemp made the stage file root-owned while the shell redirect ran unprivileged (EACCES on every add/remove path).
Without pipefail the 'sudo sed|sudo tee' and 'sudo awk|sudo tee' pipelines only observed tee's status, so a producer failing after partial output could yield a truncated candidate that still passes tor --verify-config.
b9bf42e to
2f3f3c8
Compare
Summary
torrctor --verify-configbefore an atomic rename/etc/tor/torrcowned by root and remove world-writable staging filesThis implements the Tor configuration recommendations from #186.
Security impact
Previously, unvalidated values were interpolated into
sedexpressions and Tor directives, and configuration rewrites passed through a mode-0777 file owned outside/etc. A malformed value or local race could corrupt or alter Tor configuration. The new flow validates inputs, stages changes under/etc/tor, asks Tor to parse the complete candidate, and installs it atomically only on success.Tests
bash -n scripts/install.hiddenservice.shgit diff --checkchmod 777,chown bitcoin:bitcoin, andpkillfrom the scriptRuntime Tor integration should also be exercised by the image CI on both standalone and RaspiBlitz layouts.