mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
fuzz: torcontrol: drive the control connection over a mocked socket #36822
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36822
Loading…
Reference in a new issue
No description provided.
Delete branch "fuzz-torcontrol-socket"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The
torcontrolfuzz target hands made-up replies straight to theTorControllercallbacks, and caps reply lines at 16 characters. As a result, most success paths can't be reached, and the code that parses the control port (ProcessBuffer(),ReceiveAndProcess()) never runs.This rewrites the target around a fake Tor on the other end of a real
TorControlConnection, connected throughDynSock. Replies go over the wire. The fake Tor answers each command the controller sends with a reply of the matching shape, so inputs can complete the whole handshake: PROTOCOLINFO, authentication (including SAFECOOKIE), GETINFO and ADD_ONION. The harness also checks that reply data never splits a command or injects a new one.The first commit fixes
DynSock::Wait(), which deleted its own socket. The bug has been there since #30205; this target is the first caller.Testing
Line coverage of
src/torcontrol.cppgoes from 52% on master to 81% after a 10-minute local run. The master number is from the qa-assets corpus. Most of what remains is the controller thread itself, which the harness replaces, and socket errors thatDynSockcan't produce.The input format changes, so the corpus in qa-assets needs a refresh.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
No conflicts as of last run.
Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.
🔵 Suggestion ·
Offered()counts authentication methods from discarded repliesThe fuzz oracle accepts methods advertised only in discarded replies or superseded
METHODSlists, so it can miss regressions that authenticate using data the controller should ignore.Location:
src/test/fuzz/torcontrol.cpp:190(head).🔵 Suggestion · Command splitting hides embedded delimiters from the framing check
The fuzz target's framing assertions can accept an injected command beginning with an allowed verb because they run after
\r\nsplitting; no current production injection is established.Location:
src/test/fuzz/torcontrol.cpp:233(head).Review coverage was incomplete.
Reviewed:
fdc08cbc96af· Review report · Previous reportspermalink
ACK
14b276e7c9concept ACK
Concept ACK
I think the direct handler calls here (the
CallOneOfoverprotocolinfo_cb…add_onion_cb) hide the paths that need the wire. As far as I tested, no input got past PROTOCOLINFO over the wire.Suggestion from my clanker:
FakeTorrecords the verb of each command it sees inProcessCommands(). A new operation answers the oldest unanswered command with a reply of the matching shape (code usually 250, sometimes one ofCODES). It reuses the existing line generators, which are split out ofConsumeReplyLine(). All existing operations stay. The direct-call operation is also gated behind one extraConsumeBool().diff
Thanks! I've applied the diff and removed the direct handler entirely since it's not needed anymore, because the fuzzer should be able to reach those state over the wire.
Reviewed
345ffe0daeCould we drop
DynSock::Wait()entirely instead? The baseSock::Wait()already handles this without taking ownership of the socket, andDynSock::WaitMany()always returnstrue, so the behavior should remain unchanged.Leaving this to the base
Sockimplementation might also make things simpler for anyone working in this area in the future.If you agree, perhaps something like this would work:
diff
Thanks for the review! Makes sense for me, but
DynSockinherits fromZeroSock, whoseWait()returns all requested events immediately, so removing the override would makeWait()resolve toZeroSock::Wait()rather thanSock::Wait(). I've instead madeDynSock::Wait()delegate directly toSock::Wait(), which reuses its non-owning wrap.In SAFECOOKIE authentication, the controller should only send its
AUTHENTICATEproof after verifying Tor'sSERVERHASH(torcontrol.cpp:639). Currently, flipping that check either way still passes this target as well as other tests I have looked at.FakeToralready knows when it has sent a correct server hash, so it could record the corresponding expected client hash and assert that everyAUTHENTICATE <hash>matches one of those recorded hashes.Patch if useful
Yes, that looks good, sorry I missed the
ZeroSock, thanks.As a possible follow-up, a small unit test that
DynSock::Wait()reports not-ready on an empty pipe and ready once data arrives.., would lock in both this fix and theZeroSockdistinction.Nice! I've added the oracle to verify if the bitcoin node is sending the correct server hash, along others such as: each
SAFECOOKIEclient nonce is 32 bytes and never repeats. I've also added an assert that tor receives-torpasswordexactly as configured, which catch the same issue being fixed by #36637.I'll let the
-torpasswordoracle as a follow-up after this PR and that one get merged.non-blocking style nit: just wondering why a
structwas chosen over aclassheregenerally, most readers will see a
structand expect a plain bundle of data, where each member can be touched on its own. this one has more going on:Write()keepspendingandwritten_tailconsistent, andNewConnection()has to reset everything exceptclient_nonce.for this use case a
classkind of looks like the more readable option. what do you think ?nit:
argis used in theif (password.empty()), it can be moved into that scope since that's where its only usage isi think it is safe to leave out the
COOKIEauthentication method. the controller never uses it (it only acts onNULL,SAFECOOKIEandHASHEDPASSWORD) so offering it does not reach any new code, and the Tor control spec discourages it anywayCreateSockis restored by hand at the end of the target. if anything exits early (an exception, for instance), the restoration is skipped andCreateSockis left usingtoraftertoris goneit can be restored with a minimal RAII guard instead, so it is put back on every exit path
diff
now that #36637 is merged, the only thing the
-torpasswordoracle is waiting on is this PR itself, and it is a change to this same file. could it go in here after a rebase ?i do not see any harm in adding it here; it avoids a follow-up PR. but, if there is a reason to keep it separate (e.g. not wanting to restart review or keeping the diff smaller), that is fine, just curious what it is
Updated!
Updated!
I think is worth as a regression test. Let's say if a day bitcoin core makes a change and for some reason start parsing
COOKIEmessages. It's also a one line change that shouldn't decrease much the efficiency of the fuzz target.The harness drives
FakeTor's state directly (pending, unanswered, pipes and others), so as a class everything would still be public. Hiding it would mean adding accessors to a test fake, which doesn't seem worth it. Statefulstructswith methods are common in the fuzz tests,SimTxGraphfor example, so I'll keep it as is.👍understood! this can be resolved
fair point that it is cheap. my concern is more about the confusion it could cause: the controller does not handleCOOKIE, and the spec marks it as deprecated and unsafe, so it seems very unlikely to ever be supportedoffering it in the fuzzer could give the impression that there is some code handling it, and someone might go out looking for itif you would rather keep it as a regression guard, could we add a short comment explaining that it is intentionally offered to check that it stays ignored ?I agree with you. Since it's already merged I've add it in this PR as you suggested.
i am still hitting the
-torpasswordassertion oncbd8dd443bwith a password containing a backslash:the check makes sure Tor gets the password exactly as configured. With
pa\ss, the controller sends an unescaped password,"pa\ss", and Tor drops the backslash and ends up withpass. so, authentication fails even though the user typed the right passwordto reproduce, pin the password to
pa\ssand answerPROTOCOLINFOwith aHASHEDPASSWORDoffer:diff
protocolinfo_cb()on this commit still only escapes quotes, so the backslash case is not covered. is #36637 in this branch? if your fix is local, could you push it ?Indeed I forgot to rebase against master. Now it should be fine. Thanks for letting me know!
A rebase would work I think
ACK
fdc08cbc96. code reviewed and testedsuccessfully built and tested with
clang++-20on Debian, targetingx86_64. also, i ran the target for 29 minutes on a fresh corpus, with no assertion failuresi left one suggestion.
+1 on Ralph's second suggestion: #36822
the command check only looks at how a command starts, so something like
AUTHENTICATE-xpasses as valid and then slips past every check below itmatching the verb exactly (just like in
ConsumeAnswer()), and pinning down the full shape of each command, means reply data cannot alter a command's body behind a valid first word. also, any new verb without a check now fails loudlypatch