fuzz: torcontrol: drive the control connection over a mocked socket

bitcoin/bitcoin: PR #36822

1 major finding · 2 suggestions

Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive for their assessments.

Findings

Link

[major] · Backslash passwords abort the new torcontrol fuzz target

The new password assertion aborts on a pre-existing sender bug at this head; the supplied unchanged-head experiment reproduced the failure with backslash passwords.

Location: src/test/fuzz/torcontrol.cpp:246 (head).

Details and suggested fix

ConsumeBytesAsString() can generate a nonempty password such as pa\ss. A code-250 PROTOCOLINFO reply offering HASHEDPASSWORD makes protocolinfo_cb() send that password with only double quotes escaped. DecodeAuthArg() consequently produces pass, and the equality assertion fails. A lone trailing backslash instead consumes the closing quote and makes decoding fail. This leaves generative fuzzing hitting a known failure rather than running continuously through the new handshake paths. Rebase onto #36637's hex-encoded sender before landing this assertion, or defer cbd8dd4 until that fix is present while retaining the byte-equality check.


Optional improvements

Link

[suggestion] · The fuzz command checks lose framing and accept malformed verb prefixes

The command oracle can accept injected lines or AUTHENTICATE-x without enforcing its intended checks, although IsWellFormedPrivateKey() currently prevents malformed reply keys from reaching commands.

Location: src/test/fuzz/torcontrol.cpp:234 (head).

Details and suggested fix

ProcessCommands() splits at \r\n before checking for line breaks, so that assertion cannot identify an extra CRLF-delimited command. It then accepts any allowed verb prefix: AUTHENTICATE-x passes the whitelist but skips both authentication branches, and an extra GETINFO ... line receives no further validation. Return a checking DynSock subclass from the harness's CreateSock override and assert that each send has exactly one trailing CRLF and no earlier CR or LF. This preserves command boundaries because Command() uses one SendComplete() call and DynSock::Send() consumes the whole buffer. Also require the verb to end at the command boundary or be followed by a space. These checks preserve all current valid commands while distinguishing the invariant violations the harness intends to catch.


Link

[suggestion] · Offered() counts methods from discarded replies

The fuzz auth oracle can miss regressions that send the password without an effective offer because it counts discarded replies and superseded METHODS lists, although the current controller handles these cases correctly.

Location: src/test/fuzz/torcontrol.cpp:166 (head).

Details and suggested fix

RecordLine() saves every written AUTH line without its status or separator, and Offered() unions all METHODS lists on the connection. A generated non-250 PROTOCOLINFO answer offering HASHEDPASSWORD therefore satisfies the oracle, so a regression removing protocolinfo_cb()'s success-code guard could send the password and still pass this assertion. Async offers and earlier METHODS lists likewise remain eligible after the controller would discard the reply or replace the list. Buffer the authentication records per complete reply and retain AUTH and AUTHCHALLENGE data only when the closing space-separated line has code 250. Within each reply, select the last METHODS value before adding its effective offers to the connection-level set. This filtering does not require matching replies to commands.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

The historical requirement is that Tor receive the original password, including literal backslashes and double quotes. Escaping only double quotes does not achieve this: a backslash escapes the following character, and a trailing backslash can consume the closing quote. The predecessor proposal therefore escaped backslashes before escaping quotes.

This establishes the compatibility rule behind the new password-equality oracle. The predecessor gives concrete examples: pa\ss becomes pass under the old encoding, while pass\ leaves the quoted argument unterminated. Both pinned base and head still contain the quote-only sender, so this is directly relevant historical context rather than an assumption that #36637 is already present.

Source 1

#36637 chose unquoted hexadecimal encoding instead of repairing quoted-string escaping. Its stated rationale was that the Tor control protocol expressly permits hex to avoid escaping issues, as already used for SAFECOOKIE; the added functional test covered passwords containing backslashes and quotes.

The established contract is preservation of password bytes, not preservation of a particular quoted wire representation. DecodeAuthArg accepts either quoted or hexadecimal arguments, so its equality assertion expresses that contract without requiring the sender to retain the older representation.

Source 1

Ralph's inferences

For the escaping rule documented in the historical proposals, DecodeAuthArg implements the intended inverse: backslash consumes exactly the next byte. Thus escaped backslash produces one literal backslash, escaped quote produces one literal quote, and backslash followed by n produces n rather than a newline. It requires an unescaped closing quote at the end and rejects a missing terminator or trailing escape.

This is the specific decoding behavior needed to distinguish an unchanged password from the historical backslash-mangling cases. The conclusion is limited to the documented escape rule, rather than asserting complete conformance to every Tor lexical restriction.

Source 1

The oracle is byte-oriented rather than text-oriented: quoted decoding appends bytes to a length-aware string, hexadecimal decoding constructs a string from the decoded byte range, and comparison uses string equality. This can express exact preservation of embedded NUL and high-bit bytes without Unicode interpretation. That capability alone does not establish that every such byte is legal in Tor's quoted form.

The harness deliberately generates raw password bytes, but applies the password assertion only for nonempty passwords. It also checks command line breaks separately before decoding. Consequently, arbitrary-byte preservation and full QuotedString syntax acceptance are distinct questions; the inspected history firmly motivates the former but does not fully resolve the latter.