Skip to content

fuzz: torcontrol: drive the control connection over a mocked socket - #36822

Open
erickcestari wants to merge 8 commits into
bitcoin:masterfrom
erickcestari:fuzz-torcontrol-socket
Open

erickcestari wants to merge 8 commits into
bitcoin:masterfrom
erickcestari:fuzz-torcontrol-socket

Conversation

@erickcestari

@erickcestari erickcestari commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

The torcontrol fuzz target hands made-up replies straight to the TorController callbacks, 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 through DynSock. 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.cpp goes 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 that DynSock can't produce.

The input format changes, so the corpus in qa-assets needs a refresh.

@DrahtBot

DrahtBot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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.

Type Reviewers
ACK winterrdog
Concept ACK brunoerg
Stale ACK jpk68

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.

@jpk68

jpk68 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

ACK 14b276e

@winterrdog winterrdog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

concept ACK

@brunoerg

Copy link
Copy Markdown
Contributor

Concept ACK

Comment thread src/test/fuzz/torcontrol.cpp Resolved

@frankomosh frankomosh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed 345ffe0

Comment thread src/test/util/net.cpp Resolved
@erickcestari
erickcestari force-pushed the fuzz-torcontrol-socket branch from 14b276e to e9528f6 Compare November 17, 2026 12:55
Comment thread src/test/fuzz/torcontrol.cpp Resolved
Comment thread src/test/util/net.cpp Resolved
@erickcestari
erickcestari force-pushed the fuzz-torcontrol-socket branch 2 times, most recently from 9601d13 to a0a7c77 Compare November 19, 2026 17:54
Comment thread src/test/fuzz/torcontrol.cpp Resolved
Comment thread src/test/fuzz/torcontrol.cpp Resolved
Comment thread src/test/fuzz/torcontrol.cpp Resolved
Comment thread src/test/fuzz/torcontrol.cpp Resolved
@erickcestari
erickcestari force-pushed the fuzz-torcontrol-socket branch from a0a7c77 to cbd8dd4 Compare November 25, 2026 17:37
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.
@erickcestari
erickcestari force-pushed the fuzz-torcontrol-socket branch from cbd8dd4 to a0a20b2 Compare December 2, 2026 17:43
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.
@erickcestari
erickcestari force-pushed the fuzz-torcontrol-socket branch from a0a20b2 to fdc08cb Compare December 2, 2026 18:14

@winterrdog winterrdog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +234 to +262
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())));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+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\n splitting; 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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants