Skip to content

Conversation

@dergoegge
Copy link
Member

The chance that the fuzzer is able to guess a valid onion address is probably slim, as they are Base32 encoded and include a checksum. Right now, any target using ConsumeNetAddr would have a hard time uncovering bugs that require valid onion addresses as input.

This PR makes ConsumeNetAddr produce valid onion addresses by using the 32 bytes given by the fuzzer as the pubkey for the onion address and forming a valid address according to the torv3 spec.

@dergoegge dergoegge force-pushed the 2022-11-fuzz-valid-onion-addrs branch from 0d2fd4c to 4e1bc19 Compare November 14, 2022 14:14
@DrahtBot DrahtBot added the Tests label Nov 14, 2022
Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

Approach ACK 4e1bc1971245714735eff70700dfd0f336169db5

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

Good catch.

Nit: Maybe move it to a separate fuzz util helper? It is used in the following tests:

src/test/fuzz/addrman.cpp
src/test/fuzz/banman.cpp
src/test/fuzz/connman.cpp
src/test/fuzz/netaddress.cpp
src/test/fuzz/netbase_dns_lookup.cpp

and could be placed in a new src/test/fuzz/util/net.h?

@dergoegge dergoegge force-pushed the 2022-11-fuzz-valid-onion-addrs branch from 4e1bc19 to 6052380 Compare November 15, 2022 11:30
@dergoegge
Copy link
Member Author

Thanks for the reviews @vasild @MarcoFalke!

Addressed all the comments and added a new commit for moving ConsumeNetAddr to src/test/fuzz/util/net.h. There are more net related utils that could move there in a future PR (also happy to do that in this PR if preferred).

@dergoegge dergoegge changed the title fuzz: Make ConsumeNetAddr (sometimes) produce valid onion addresses fuzz: Make ConsumeNetAddr always produce valid onion addresses Nov 15, 2022
Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK 60523801db69efccbe5e997237dc41a9684bc3a2

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 16, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK vasild, brunoerg
Stale ACK MarcoFalke

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

tested at runtime that this SetSpecial always fails on master with this diff:

diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp
index d495a6bfe3..74cddd037d 100644
--- a/src/test/fuzz/util.cpp
+++ b/src/test/fuzz/util.cpp
@@ -524,7 +524,7 @@ CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept
     } else if (network == Network::NET_INTERNAL) {
         net_addr.SetInternal(fuzzed_data_provider.ConsumeBytesAsString(32));
     } else if (network == Network::NET_ONION) {
-        net_addr.SetSpecial(fuzzed_data_provider.ConsumeBytesAsString(32));
+        Assert(!net_addr.SetSpecial(fuzzed_data_provider.ConsumeBytesAsString(32)));
     }
     return net_addr;
 }

review ACK 60523801db69efccbe5e997237dc41a9684bc3a2 🍦

Show signature

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

review ACK 60523801db69efccbe5e997237dc41a9684bc3a2 🍦
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUjUcwv+LS3rIkpeUS3dDy6+pcvtTGUM5DUcyT+gYPxd+1vGujS1wk20fN5JqE+n
ILMK5kgtRcQe/EV8yW9coVjWwiMJ2F5SGuJnw0+1ZeO7B6sCAsgpg7ouLoDyedbv
H2HK3B8gtsu82SYaWP22Lh3lM5wASWeKdT3lrn+eBHTWBQd4A0+446h3cfM9h0sM
RFtDdwr21vb5zQoq3KgbN6l8+RQMzn29QnFWYxPkS08pyqVacib4m+htZxFo3XWs
Sez8d/YTBmg63YC6AdZ7Oz40icJsb75WJwNQxmRQLXpxlwkAaJYchtEKAJ0HRtZJ
Uqz+OAwnlAkOIypfTH3yDqIFE9I2KbE5kvMlVbUfEnSJ9tVSAjTCgKw8chPMm6iW
faFSf9fUc0ob0PiJlTXvplG444Sy4iT5C+m2+7LVfz2atVF6bpIUbs2rtGfZ3RWr
dgv6TeLPAzjmUemug9KBvog4P5DhUYQWghwa6NG75pEkv68ulciyadlN5jNQtZCG
AfEYffWE
=yuEE
-----END PGP SIGNATURE-----

@vasild
Copy link
Contributor

vasild commented Nov 16, 2022

tested at runtime that this always fails on master ...

@MarcoFalke, do you mean that it always succeeds? I mean, SetSpecial() will always return false, so !SetSpecial() will always be true, so the assert will never fail, right? Edit: or maybe you mean "this always fails" --> "SetSpecial() always fails".

@dergoegge dergoegge force-pushed the 2022-11-fuzz-valid-onion-addrs branch from 6052380 to 78b6372 Compare November 17, 2022 12:56
Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK 78b6372863a4b22023a7d141ea5c854a67d41363

Copy link
Contributor

@brunoerg brunoerg left a comment

Choose a reason for hiding this comment

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

crACK 78b6372863a4b22023a7d141ea5c854a67d41363

@dergoegge dergoegge force-pushed the 2022-11-fuzz-valid-onion-addrs branch from 78b6372 to 0eeb9b0 Compare November 17, 2022 17:58
@dergoegge
Copy link
Member Author

Changed the approach a bit to use OnionToString. Thanks @brunoerg, @vasild and @MarcoFalke for the reviews!

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK 0eeb9b0

/// SAM 3.1 and earlier do not support specifying ports and force the port to 0.
static constexpr uint16_t I2P_SAM31_PORT{0};

std::string OnionToString(Span<const uint8_t> addr);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is now public, it would be nice to document it. Something like:

/**
 * Generate the ".onion" address which corresponds to a public key.
 * @param[in] addr public key bytes, must be 32.
 * @returns for example 5g72ppm3krkorsfopcm2bi7wlv4ohhs4u4mlseymasn7g7zhdcyjpfid.onion
 */

Copy link
Contributor

@brunoerg brunoerg left a comment

Choose a reason for hiding this comment

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

ACK 0eeb9b0

@maflcko maflcko merged commit 0968c51 into bitcoin:master Nov 21, 2022
@maflcko
Copy link
Member

maflcko commented Nov 21, 2022

Btw, if there is value in breaking the inputs (#26497 (comment)), it can be done, because fuzzing is running 24/7 anyway, so any "lost" inputs should be re-discovered eventually.

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Nov 21, 2022
…nion addresses

0eeb9b0 [fuzz] Move ConsumeNetAddr to fuzz/util/net.h (dergoegge)
291c869 [fuzz] Make ConsumeNetAddr produce valid onion addresses (dergoegge)
c9ba3f8 [netaddress] Make OnionToString public (dergoegge)

Pull request description:

  The chance that the fuzzer is able to guess a valid onion address is probably slim, as they are Base32 encoded and include a checksum.  Right now, any target using `ConsumeNetAddr` would have a hard time uncovering bugs that require valid onion addresses as input.

  This PR makes `ConsumeNetAddr` produce valid onion addresses by using the 32 bytes given by the fuzzer as the pubkey for the onion address and forming a valid address according to the torv3 spec.

ACKs for top commit:
  vasild:
    ACK 0eeb9b0
  brunoerg:
    ACK 0eeb9b0

Tree-SHA512: 7c687a4d12f9659559be8f0c3cd4265167d1261d419cfd3d503fd7c7f207cc0db745220f02fb1737e4a5700ea7429311cfc0b42e6c15968ce6a85f8813c7e1d8
@vasild
Copy link
Contributor

vasild commented Nov 21, 2022

A natural followup to this would be to also generate I2P and CJDNS addresses. That needs to modify the network array anyway. At the same time some dummy value can be added to it to return an invalid address. So, invalidate the inputs just once.

@vasild
Copy link
Contributor

vasild commented Jan 9, 2023

A natural followup to this would be to also generate I2P and CJDNS addresses.

Done in #26859.

@bitcoin bitcoin locked and limited conversation to collaborators Jan 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants