Repository navigation
fuzz: torcontrol: drive the control connection over a mocked socket - #36822
erickcestari wants to merge 8 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsNo conflicts as of last run. |
|
ACK 14b276e |
|
Concept ACK |
5aebf49 to
14b276e
Compare
14b276e to
e9528f6
Compare
9601d13 to
a0a7c77
Compare
a0a7c77 to
cbd8dd4
Compare
Wait() wrapped `this` in an owning shared_ptr to call WaitMany(), so the socket was deleted once that temporary went out of scope. Delegate to Sock::Wait(), which wraps it without taking ownership.
cbd8dd4 to
a0a20b2
Compare
Replies used to be handed straight to the TorController callbacks, with lines capped at 16 chars, so most success paths and the control port parser were never reached. Drive a real TorControlConnection over a DynSock instead, and generate the values the fuzzer can't produce.
Random replies almost never got past PROTOCOLINFO. Answer the oldest unanswered command with a reply of the matching shape, including the SAFECOOKIE server hash, so inputs can walk the whole handshake. Also assert that reply data never splits or injects a command.
Above all, the password must not go to a Tor that didn't ask for it.
Finds the escaping bug fixed in bitcoin#36637 within a few hundred runs.
a0a20b2 to
fdc08cb
Compare
winterrdog
left a comment
There was a problem hiding this comment.
ACK fdc08cb. code reviewed and tested
successfully built and tested with clang++-20 on Debian, targeting x86_64. also, i ran the target for 29 minutes on a fresh corpus, with no assertion failures
i left one suggestion.
| assert(std::ranges::any_of(VERBS, [&](std::string_view verb) { return cmd.starts_with(verb); })); | ||
| unanswered.emplace_back(cmd.substr(0, cmd.find(' '))); | ||
| // Each authentication method may only be used once Tor offered it on this connection. | ||
| if (cmd == "AUTHENTICATE") { | ||
| assert(Offered("NULL")); | ||
| } else if (cmd.starts_with("AUTHENTICATE ")) { | ||
| const std::string_view arg{cmd.substr(std::string_view{"AUTHENTICATE "}.size())}; | ||
| if (password.empty()) { | ||
| // SAFECOOKIE: send the proof only after Tor proved it knows the cookie. | ||
| assert(ProvedCookie(arg)); | ||
| } else { | ||
| assert(Offered("HASHEDPASSWORD")); | ||
| assert(DecodeAuthArg(arg) == password); | ||
| } | ||
| } else if (cmd.starts_with("AUTHCHALLENGE SAFECOOKIE ")) { | ||
| assert(Offered("SAFECOOKIE")); | ||
| client_nonce = ParseHex(cmd.substr(std::string_view{"AUTHCHALLENGE SAFECOOKIE "}.size())); | ||
| // A repeated nonce would let a recorded server hash prove the cookie again. | ||
| assert(client_nonce.size() == 32 && client_nonces.insert(client_nonce).second); | ||
| } else if (cmd.starts_with("ADD_ONION ")) { | ||
| // One port mapping, always from the default virtual port, so the service doesn't | ||
| // reveal a node using another port. | ||
| std::vector<std::string> ports; | ||
| for (auto& arg : util::SplitString(cmd, ' ')) { | ||
| if (arg.starts_with("Port=")) ports.push_back(std::move(arg)); | ||
| } | ||
| assert(ports.size() == 1); | ||
| assert(ports.front().starts_with(strprintf("Port=%u,", Params().GetDefaultPort()))); | ||
| } |
There was a problem hiding this comment.
+1 on Ralph's second suggestion: https://git.fish.foo/bitcoin/bitcoin/pulls/36822#author-ralph
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.
the command check only looks at how a command starts, so something like AUTHENTICATE-x passes as valid and then slips past every check below it
matching 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 loudly
patch
diff --git a/src/test/fuzz/torcontrol.cpp b/src/test/fuzz/torcontrol.cpp
index 2299ace8cb..41a71462b8 100644
--- a/src/test/fuzz/torcontrol.cpp
+++ b/src/test/fuzz/torcontrol.cpp
@@ -231,26 +231,36 @@ struct FakeTor {
const std::string_view cmd{std::string_view{sent}.substr(0, end)};
// Reply data must never break a command apart or inject a new one.
assert(cmd.find_first_of("\r\n") == std::string_view::npos);
- assert(std::ranges::any_of(VERBS, [&](std::string_view verb) { return cmd.starts_with(verb); }));
- unanswered.emplace_back(cmd.substr(0, cmd.find(' ')));
+ // do an exact match so that wrong commands like AUTHENTICATE-x do not make it through
+ const std::string_view verb{cmd.substr(0, cmd.find(' '))};
+ assert(std::ranges::find(VERBS, verb) != VERBS.end());
+ unanswered.emplace_back(verb);
// Each authentication method may only be used once Tor offered it on this connection.
- if (cmd == "AUTHENTICATE") {
- assert(Offered("NULL"));
- } else if (cmd.starts_with("AUTHENTICATE ")) {
- const std::string_view arg{cmd.substr(std::string_view{"AUTHENTICATE "}.size())};
- if (password.empty()) {
- // SAFECOOKIE: send the proof only after Tor proved it knows the cookie.
- assert(ProvedCookie(arg));
- } else {
- assert(Offered("HASHEDPASSWORD"));
- assert(DecodeAuthArg(arg) == password);
- }
- } else if (cmd.starts_with("AUTHCHALLENGE SAFECOOKIE ")) {
+ if (verb == "PROTOCOLINFO") {
+ assert(cmd == "PROTOCOLINFO 1");
+ } else if (verb == "GETINFO") {
+ assert(cmd == "GETINFO net/listeners/socks");
+ } else if (verb == "AUTHCHALLENGE") {
+ assert(cmd.starts_with("AUTHCHALLENGE SAFECOOKIE "));
assert(Offered("SAFECOOKIE"));
client_nonce = ParseHex(cmd.substr(std::string_view{"AUTHCHALLENGE SAFECOOKIE "}.size()));
// A repeated nonce would let a recorded server hash prove the cookie again.
assert(client_nonce.size() == 32 && client_nonces.insert(client_nonce).second);
- } else if (cmd.starts_with("ADD_ONION ")) {
+ } else if (verb == "AUTHENTICATE") {
+ if (cmd == "AUTHENTICATE") {
+ assert(Offered("NULL"));
+ } else {
+ const std::string_view arg{cmd.substr(std::string_view{"AUTHENTICATE "}.size())};
+ if (password.empty()) {
+ // SAFECOOKIE: send the proof only after Tor proved it knows the cookie.
+ assert(ProvedCookie(arg));
+ } else {
+ assert(Offered("HASHEDPASSWORD"));
+ assert(DecodeAuthArg(arg) == password);
+ }
+ }
+ } else {
+ assert(verb == "ADD_ONION");
// One port mapping, always from the default virtual port, so the service doesn't
// reveal a node using another port.
std::vector<std::string> ports;
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.