I think the direct handler calls here (the CallOneOf over protocolinfo_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:
FakeTor records the verb of each command it sees in ProcessCommands(). A new operation answers the oldest unanswered command with a reply of the matching shape (code usually 250, sometimes one of CODES). It reuses the existing line generators, which are split out of ConsumeReplyLine(). All existing operations stay. The direct-call operation is also gated behind one extra ConsumeBool().
<details><summary>diff</summary>
diff --git a/src/test/fuzz/torcontrol.cpp b/src/test/fuzz/torcontrol.cpp
index 06b6a6faac..d7e4ff99f0 100644
--- a/src/test/fuzz/torcontrol.cpp
+++ b/src/test/fuzz/torcontrol.cpp
@@ -27,6 +27,7 @@
#include <cassert>
#include <chrono>
#include <cstdint>
+#include <deque>
#include <memory>
#include <string>
#include <string_view>
@@ -68,6 +69,10 @@ struct FakeTor {
std::string sent;
//! From the last AUTHCHALLENGE command, needed to answer with a matching server hash.
std::vector<uint8_t> client_nonce;
+ //! Verbs of the commands sent on this connection, oldest first, that Answer() hasn't replied to.
+ //! Mirrors the controller's reply handler queue unless other operations complete replies too;
+ //! then the answers are merely mismatched, which is still valid input.
+ std::deque<std::string> unanswered;
//! The controller keeps its SAFECOOKIE nonce across reconnects, so we do too.
void NewConnection()
@@ -76,6 +81,7 @@ struct FakeTor {
pending.clear();
written_tail.clear();
sent.clear();
+ unanswered.clear();
}
//! Writes the first `n` queued bytes to the socket.
@@ -103,6 +109,7 @@ struct FakeTor {
// 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(' ')));
if (cmd.starts_with("AUTHCHALLENGE SAFECOOKIE ")) {
client_nonce = ParseHex(cmd.substr(std::string_view{"AUTHCHALLENGE SAFECOOKIE "}.size()));
}
@@ -132,52 +139,67 @@ std::vector<uint8_t> SafeCookieHash(std::string_view key, std::span<const uint8_
return hash;
}
+std::string ConsumeAuthLine(FuzzedDataProvider& provider)
+{
+ static constexpr std::array<std::string_view, 4> METHODS{"NULL", "COOKIE", "SAFECOOKIE", "HASHEDPASSWORD"};
+ std::string methods;
+ LIMITED_WHILE(provider.ConsumeBool(), 4) {
+ if (!methods.empty()) methods += ',';
+ methods += provider.PickValueInArray(METHODS);
+ }
+ return "AUTH METHODS=" + methods + ' ' + provider.PickValueInArray(g_cookie_args);
+}
+
+std::string ConsumeServiceIdLine(FuzzedDataProvider& provider)
+{
+ // The fuzzer can't produce the SHA3 checksum of a v3 onion address on its own.
+ const std::string onion{OnionToString(ConsumeFixedLengthByteVector(provider, ADDR_TORV3_SIZE))};
+ return "ServiceID=" + onion.substr(0, onion.size() - std::string_view{".onion"}.size());
+}
+
+std::string ConsumePrivateKeyLine(FuzzedDataProvider& provider)
+{
+ return "PrivateKey=ED25519-V3:" + EncodeBase64(ConsumeFixedLengthByteVector(provider, 64));
+}
+
+std::string ConsumeSocksLine(FuzzedDataProvider& provider)
+{
+ std::string line{"net/listeners/socks="};
+ LIMITED_WHILE(provider.ConsumeBool(), 4) {
+ std::string addr{provider.ConsumeBool() ? strprintf("127.0.0.1:%u", provider.ConsumeIntegral<uint16_t>()) : provider.ConsumeRandomLengthString(32)};
+ if (provider.ConsumeBool()) addr = '"' + addr + '"';
+ line += addr + ' ';
+ }
+ return line;
+}
+
+std::string ConsumeAuthChallengeLine(FuzzedDataProvider& provider, const FakeTor& tor)
+{
+ const auto server_nonce{ConsumeFixedLengthByteVector(provider, 32)};
+ const auto server_hash{provider.ConsumeBool() ?
+ SafeCookieHash("Tor safe cookie authentication server-to-controller hash", tor.client_nonce, server_nonce) :
+ ConsumeFixedLengthByteVector(provider, 32)};
+ return "AUTHCHALLENGE SERVERHASH=" + HexStr(server_hash) + " SERVERNONCE=" + HexStr(server_nonce);
+}
+
std::string ConsumeReplyLine(FuzzedDataProvider& provider, const FakeTor& tor)
{
std::string line;
CallOneOf(
provider,
- [&] {
- line = provider.ConsumeRandomLengthString(MAX_LINE_LENGTH);
- },
- [&] {
- static constexpr std::array<std::string_view, 4> METHODS{"NULL", "COOKIE", "SAFECOOKIE", "HASHEDPASSWORD"};
- std::string methods;
- LIMITED_WHILE(provider.ConsumeBool(), 4) {
- if (!methods.empty()) methods += ',';
- methods += provider.PickValueInArray(METHODS);
- }
- line = "AUTH METHODS=" + methods + ' ' + provider.PickValueInArray(g_cookie_args);
- },
- [&] {
- // The fuzzer can't produce the SHA3 checksum of a v3 onion address on its own.
- const std::string onion{OnionToString(ConsumeFixedLengthByteVector(provider, ADDR_TORV3_SIZE))};
- line = "ServiceID=" + onion.substr(0, onion.size() - std::string_view{".onion"}.size());
- },
- [&] {
- line = "PrivateKey=ED25519-V3:" + EncodeBase64(ConsumeFixedLengthByteVector(provider, 64));
- },
- [&] {
- line = "net/listeners/socks=";
- LIMITED_WHILE(provider.ConsumeBool(), 4) {
- std::string addr{provider.ConsumeBool() ? strprintf("127.0.0.1:%u", provider.ConsumeIntegral<uint16_t>()) : provider.ConsumeRandomLengthString(32)};
- if (provider.ConsumeBool()) addr = '"' + addr + '"';
- line += addr + ' ';
- }
- },
- [&] {
- const auto server_nonce{ConsumeFixedLengthByteVector(provider, 32)};
- const auto server_hash{provider.ConsumeBool() ?
- SafeCookieHash("Tor safe cookie authentication server-to-controller hash", tor.client_nonce, server_nonce) :
- ConsumeFixedLengthByteVector(provider, 32)};
- line = "AUTHCHALLENGE SERVERHASH=" + HexStr(server_hash) + " SERVERNONCE=" + HexStr(server_nonce);
- });
+ [&] { line = provider.ConsumeRandomLengthString(MAX_LINE_LENGTH); },
+ [&] { line = ConsumeAuthLine(provider); },
+ [&] { line = ConsumeServiceIdLine(provider); },
+ [&] { line = ConsumePrivateKeyLine(provider); },
+ [&] { line = ConsumeSocksLine(provider); },
+ [&] { line = ConsumeAuthChallengeLine(provider, tor); });
return line;
}
+constexpr std::array<int, 4> CODES{TOR_REPLY_OK, TOR_REPLY_UNRECOGNIZED, TOR_REPLY_SYNTAX_ERROR, /*async event=*/650};
+
TorControlReply ConsumeReply(FuzzedDataProvider& provider, const FakeTor& tor)
{
- static constexpr std::array<int, 4> CODES{TOR_REPLY_OK, TOR_REPLY_UNRECOGNIZED, TOR_REPLY_SYNTAX_ERROR, /*async event=*/650};
TorControlReply reply;
reply.code = provider.ConsumeBool() ? provider.PickValueInArray(CODES) : provider.ConsumeIntegralInRange<int>(0, 999);
LIMITED_WHILE(provider.ConsumeBool(), 16) {
@@ -186,6 +208,28 @@ TorControlReply ConsumeReply(FuzzedDataProvider& provider, const FakeTor& tor)
return reply;
}
+//! The reply Tor would send to a `verb` command. Usually 250, but any code keeps error paths
+//! (like the 512 that triggers the ADD_ONION retry without PoW) reachable.
+TorControlReply ConsumeAnswer(FuzzedDataProvider& provider, const FakeTor& tor, std::string_view verb)
+{
+ TorControlReply reply;
+ reply.code = provider.ConsumeBool() ? provider.PickValueInArray(CODES) : TOR_REPLY_OK;
+ if (verb == "PROTOCOLINFO") {
+ reply.lines = {"PROTOCOLINFO 1", ConsumeAuthLine(provider), R"(VERSION Tor="0.4.8.10")", "OK"};
+ } else if (verb == "AUTHCHALLENGE") {
+ reply.lines = {ConsumeAuthChallengeLine(provider, tor)};
+ } else if (verb == "GETINFO") {
+ reply.lines = {ConsumeSocksLine(provider), "OK"};
+ } else if (verb == "ADD_ONION") {
+ reply.lines = {ConsumeServiceIdLine(provider)};
+ if (provider.ConsumeBool()) reply.lines.push_back(ConsumePrivateKeyLine(provider));
+ reply.lines.emplace_back("OK");
+ } else {
+ reply.lines = {"OK"};
+ }
+ return reply;
+}
+
//! Wire format: "<code>-<line>" for all lines but the last, which uses "<code> <line>".
std::string ConsumeWireReply(FuzzedDataProvider& provider, const TorControlReply& reply)
{
@@ -253,6 +297,15 @@ FUZZ_TARGET(torcontrol, .init = initialize_torcontrol)
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 1000) {
CallOneOf(
fuzzed_data_provider,
+ [&] {
+ // Answer the oldest unanswered command with a reply of the matching type.
+ if (tor.unanswered.empty()) return;
+ const std::string verb{std::move(tor.unanswered.front())};
+ tor.unanswered.pop_front();
+ tor.pending += ConsumeWireReply(fuzzed_data_provider, ConsumeAnswer(fuzzed_data_provider, tor, verb));
+ tor.Write(tor.pending.size());
+ receive();
+ },
[&] {
tor.pending += ConsumeWireReply(fuzzed_data_provider, ConsumeReply(fuzzed_data_provider, tor));
},
@@ -273,6 +326,7 @@ FUZZ_TARGET(torcontrol, .init = initialize_torcontrol)
},
[&] {
// Hand a reply straight to a handler, skipping the exchanges that normally precede it.
+ if (!fuzzed_data_provider.ConsumeBool()) return;
TorControlReply reply{ConsumeReply(fuzzed_data_provider, tor)};
for (auto& line : reply.lines) RestrictCookieFiles(line);
CallOneOf(
</details>