fuzz: Add a target that feeds arbitrary bytes to V2Transport #36635

pull fametrano wants to merge 1 commits into bitcoin:master from fametrano:fuzz-v2-transport-arbitrary-bytes changing 1 files +117 −0
  1. fametrano commented at 11:46 PM on October 23, 2026: none

    No fuzz target feeds V2Transport bytes that an honest peer would not send past the v2 key exchange. I add p2p_transport_v2_receive. It knows the transport's keys, builds the input from arbitrary bytes and from parts of a valid handshake (the peer key, garbage, the terminator, encrypted packets that may be corrupted, and runs of empty decoys that force a rekey), and feeds it in pieces of arbitrary size. Once the transport reports v2, it checks that its session id matches the peer's.

    It reaches the v2 receive states and their failures, except the fallback to v1 and the wrong-magic disconnect, which need a v1 version header at the start of the stream; p2p_transport_bidirectional_v1v2 reaches the first. 15 minutes with libFuzzer, ASan and UBSan found no crash.

  2. DrahtBot added the label Fuzzing on Oct 23, 2026
  3. DrahtBot commented at 11:46 PM on October 23, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36808 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36808.svg"></sub> (iwyu: Fix warnings in src/test/fuzz and treat them as errors by hebasto)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. in src/test/fuzz/p2p_transport_serialization.cpp:506 in 0b0fa362fe
     501 | +            assert(progress);
     502 | +        }
     503 | +        const auto& [bytes, more, msg_type] = transport.GetBytesToSend(/*have_next_message=*/false);
     504 | +        if (!bytes.empty()) transport.MarkBytesSent(provider.ConsumeIntegralInRange<size_t>(0, bytes.size()));
     505 | +    }
     506 | +    transport.GetInfo();
    


    uqlidi commented at 8:28 PM on October 31, 2026:

    The target calls transport.GetInfo() and drops the result.

    The transport reports v2 once it verifies the version packet: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/net.cpp#L1595-L1600

    At that point its session id should match the peer's. Would an assert on that work? I ran the check for 4 minutes and it didn't fail.

        const auto info{transport.GetInfo()};
        if (info.transport_type == TransportProtocolType::V2) {
            assert(info.session_id == uint256{MakeUCharSpan(peer.GetSessionID())});
        }
    
  5. in src/test/fuzz/p2p_transport_serialization.cpp:426 in 0b0fa362fe
     421 | +    auto peer_key = ConsumePrivateKey(provider);
     422 | +    if (!key.IsValid() || !peer_key.IsValid()) return;
     423 | +    auto ent = provider.ConsumeBytes<std::byte>(32);
     424 | +    ent.resize(32);
     425 | +    auto peer_ent = provider.ConsumeBytes<std::byte>(32);
     426 | +    peer_ent.resize(32);
    


    uqlidi commented at 8:29 PM on October 31, 2026:
        const auto ent{ConsumeFixedLengthByteVector<std::byte>(provider, 32)};
        const auto peer_ent{ConsumeFixedLengthByteVector<std::byte>(provider, 32)};
    

    nit: I think ConsumeFixedLengthByteVector<std::byte>(provider, 32) does the same consume-then-resize and lets these be const: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/test/fuzz/util.h#L280

    It should read the input the same way.

  6. in src/test/fuzz/p2p_transport_serialization.cpp:429 in 0b0fa362fe
     424 | +    ent.resize(32);
     425 | +    auto peer_ent = provider.ConsumeBytes<std::byte>(32);
     426 | +    peer_ent.resize(32);
     427 | +    V2Transport transport{NodeId{0}, initiating, key, ent, /*garbage=*/{}};
     428 | +    BIP324Cipher peer{peer_key, peer_ent};
     429 | +    peer.Initialize(BIP324Cipher{key, ent}.GetOurPubKey(), /*initiator=*/!initiating);
    


    uqlidi commented at 8:31 PM on October 31, 2026:

    nit: the BIP324Cipher(key, ent) constructor calls key.EllSwiftCreate(ent) and nothing else: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/bip324.cpp#L25-L29

    Maybe call that instead of building a second cipher? It gives the same public key.

        peer.Initialize(key.EllSwiftCreate(ent), /*initiator=*/!initiating);
    
  7. in src/test/fuzz/p2p_transport_serialization.cpp:474 in 0b0fa362fe
     469 | +                auto contents = provider.ConsumeBytes<std::byte>(provider.ConsumeIntegralInRange<size_t>(0, 1000));
     470 | +                const bool ignore = provider.ConsumeBool();
     471 | +                const bool corrupt = provider.ConsumeBool();
     472 | +                const auto start = stream.size();
     473 | +                stream.resize(start + contents.size() + BIP324Cipher::EXPANSION);
     474 | +                peer.Encrypt(contents, MakeByteSpan(aad), ignore, MakeWritableByteSpan(stream).subspan(start));
    


    uqlidi commented at 8:32 PM on October 31, 2026:

    nit: stream and aad hold uint8_t, but most bytes going in are std::byte. That forces casts both ways: UCharCast in append_fn, and MakeByteSpan / MakeWritableByteSpan for Encrypt.

    Maybe store std::byte and convert once with MakeUCharSpan(stream) before ReceivedBytes()? I think that also removes the duplicate insert in the "Arbitrary bytes" and "Garbage" branches, and it reads the input the same way.


    uqlidi commented at 8:33 PM on October 31, 2026:

    diff:

    diff --git a/src/test/fuzz/p2p_transport_serialization.cpp b/src/test/fuzz/p2p_transport_serialization.cpp
    index 852994afe..d6e0bd5c5 100644
    --- a/src/test/fuzz/p2p_transport_serialization.cpp
    +++ b/src/test/fuzz/p2p_transport_serialization.cpp
    @@ -429,34 +429,33 @@ FUZZ_TARGET(p2p_transport_v2_receive, .init = initialize_p2p_transport_serializa
         peer.Initialize(BIP324Cipher{key, ent}.GetOurPubKey(), /*initiator=*/!initiating);
     
         // Build the byte stream.
    -    std::vector<uint8_t> stream;
    +    std::vector<std::byte> stream;
         // Garbage appended since the last key, which the first packet after the terminator must
         // authenticate.
    -    std::vector<uint8_t> aad;
    +    std::vector<std::byte> aad;
         auto append_fn = [&](std::span<const std::byte> bytes) {
    -        stream.insert(stream.end(), UCharCast(bytes.data()), UCharCast(bytes.data() + bytes.size()));
    +        stream.insert(stream.end(), bytes.begin(), bytes.end());
         };
         // Flip a bit in stream, at or after position start.
         auto flip_fn = [&](size_t start) {
    -        stream[provider.ConsumeIntegralInRange<size_t>(start, stream.size() - 1)] ^= 1 << provider.ConsumeIntegralInRange<unsigned>(0, 7);
    +        stream[provider.ConsumeIntegralInRange<size_t>(start, stream.size() - 1)] ^= std::byte{1} << provider.ConsumeIntegralInRange<unsigned>(0, 7);
         };
         LIMITED_WHILE (provider.remaining_bytes(), 100) {
             CallOneOf(provider,
                 // Arbitrary bytes.
                 [&] {
    -                auto bytes = provider.ConsumeBytes<uint8_t>(provider.ConsumeIntegralInRange<size_t>(0, 256));
    -                stream.insert(stream.end(), bytes.begin(), bytes.end());
    +                append_fn(provider.ConsumeBytes<std::byte>(provider.ConsumeIntegralInRange<size_t>(0, 256)));
                 },
                 // The peer's key.
                 [&] {
    -                append_fn(std::span{peer.GetOurPubKey().data(), EllSwiftPubKey::size()});
    +                append_fn(peer.GetOurPubKey());
                     aad.clear();
                 },
                 // Garbage.
                 [&] {
    -                auto bytes = provider.ConsumeBytes<uint8_t>(provider.ConsumeIntegralInRange<size_t>(0, V2Transport::MAX_GARBAGE_LEN));
    -                stream.insert(stream.end(), bytes.begin(), bytes.end());
    -                aad.insert(aad.end(), bytes.begin(), bytes.end());
    +                const auto garbage{provider.ConsumeBytes<std::byte>(provider.ConsumeIntegralInRange<size_t>(0, V2Transport::MAX_GARBAGE_LEN))};
    +                append_fn(garbage);
    +                aad.insert(aad.end(), garbage.begin(), garbage.end());
                 },
                 // The garbage terminator, possibly corrupted.
                 [&] {
    @@ -471,7 +470,7 @@ FUZZ_TARGET(p2p_transport_v2_receive, .init = initialize_p2p_transport_serializa
                     const bool corrupt = provider.ConsumeBool();
                     const auto start = stream.size();
                     stream.resize(start + contents.size() + BIP324Cipher::EXPANSION);
    -                peer.Encrypt(contents, MakeByteSpan(aad), ignore, MakeWritableByteSpan(stream).subspan(start));
    +                peer.Encrypt(contents, aad, ignore, std::span{stream}.subspan(start));
                     aad.clear();
                     if (corrupt) flip_fn(start);
                 }
    @@ -479,7 +478,7 @@ FUZZ_TARGET(p2p_transport_v2_receive, .init = initialize_p2p_transport_serializa
         }
     
         // Receive the stream in pieces, and send what the transport wants to send in between.
    -    std::span<const uint8_t> to_recv{stream};
    +    std::span<const uint8_t> to_recv{MakeUCharSpan(stream)};
         while (!to_recv.empty()) {
             const size_t piece_len = provider.ConsumeIntegralInRange<size_t>(1, to_recv.size());
             std::span<const uint8_t> piece = to_recv.first(piece_len);
    
  8. in src/test/fuzz/p2p_transport_serialization.cpp:503 in 0b0fa362fe
     498 | +                progress = true;
     499 | +            }
     500 | +            // Bytes must be consumed, or a message returned.
     501 | +            assert(progress);
     502 | +        }
     503 | +        const auto& [bytes, more, msg_type] = transport.GetBytesToSend(/*have_next_message=*/false);
    


    uqlidi commented at 8:34 PM on October 31, 2026:

    nit: the code doesn't use more or msg_type.

            const auto& [bytes, _more, _msg_type] = transport.GetBytesToSend(/*have_next_message=*/false);
    
  9. in src/test/fuzz/p2p_transport_serialization.cpp:476 in 0b0fa362fe outdated
     471 | +                const bool corrupt = provider.ConsumeBool();
     472 | +                const auto start = stream.size();
     473 | +                stream.resize(start + contents.size() + BIP324Cipher::EXPANSION);
     474 | +                peer.Encrypt(contents, MakeByteSpan(aad), ignore, MakeWritableByteSpan(stream).subspan(start));
     475 | +                aad.clear();
     476 | +                if (corrupt) flip_fn(start);
    


    uqlidi commented at 8:36 PM on October 31, 2026:

    I think the transport never rekeys here. The loop stops at 100 actions, so a stream holds fewer than 100 packets, and the transport rekeys every 224: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/bip324.h#L26

    Maybe add an action that appends a run of empty decoys? With the diff below, libFuzzer's coverage report shows this rekey branch hit: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/crypto/chacha20poly1305.cpp#L106-L108

    diff --git a/src/test/fuzz/p2p_transport_serialization.cpp b/src/test/fuzz/p2p_transport_serialization.cpp
    index 852994afe..c823f669d 100644
    --- a/src/test/fuzz/p2p_transport_serialization.cpp
    +++ b/src/test/fuzz/p2p_transport_serialization.cpp
    @@ -474,6 +474,16 @@ FUZZ_TARGET(p2p_transport_v2_receive, .init = initialize_p2p_transport_serializa
                     peer.Encrypt(contents, MakeByteSpan(aad), ignore, MakeWritableByteSpan(stream).subspan(start));
                     aad.clear();
                     if (corrupt) flip_fn(start);
    +            },
    +            // A run of empty decoys, so the transport has to rekey (every REKEY_INTERVAL packets).
    +            [&] {
    +                const auto count{provider.ConsumeIntegralInRange<unsigned>(1, BIP324Cipher::REKEY_INTERVAL)};
    +                for (unsigned i{0}; i < count; ++i) {
    +                    const auto start = stream.size();
    +                    stream.resize(start + BIP324Cipher::EXPANSION);
    +                    peer.Encrypt({}, MakeByteSpan(aad), /*ignore=*/true, MakeWritableByteSpan(stream).subspan(start));
    +                    aad.clear();
    +                }
                 }
             );
         }
    
  10. fametrano commented at 6:51 PM on November 1, 2026: none

    @uqlidi thanks, all seven comments are applied in 984a01619e.

    Once the transport reports v2, the target asserts that its session id matches the peer's; with the check inverted it aborts on the new corpus, so the assert is reached. The old target never rekeyed: a new action appends a run of empty decoys, and replaying the corpora counted 0 rekeys before and 854 after, at about half the exec rate. Both entropies are const, from ConsumeFixedLengthByteVector. The peer is initialized from key.EllSwiftCreate(ent). stream and aad hold std::byte. The unused bindings are _more and _msg_type. The refactors read the input the same way: a hash of the stream, the entropy and the bytes left was identical before and after them on all 601 old-corpus inputs with valid keys.

    One note on Ralph's report on 0b0fa362fe: its verifier says the target reaches the wrong-magic disconnect. That branch's log line, net.cpp:1144 at 984a01619e, had 0 hits in the gist's 10-minute run of the new target and its 5-minute run of the final one (results.txt). This is why the description says the target does not reach it: that path needs a v1 version header at the start of the stream.

    I also rebased: master gave initialize_p2p_transport_serialization a reachability goal this target never observes, so it now uses initialize_p2p_transport. Commands and output: https://gist.github.com/fametrano/b1d9da3c3312cae5b1029679828ee023/e168d09e55328bc3b2955680154ea4b4687b8e5c

  11. DrahtBot added the label Needs rebase on Nov 10, 2026
  12. fuzz: Add a target that feeds arbitrary bytes to V2Transport
    No fuzz target feeds V2Transport bytes that an honest peer would not
    send past the v2 key exchange.
    
    Add p2p_transport_v2_receive. It knows the keys of the transport it
    tests, and builds a byte stream from arbitrary bytes and from the parts
    of a valid handshake: the peer key, garbage, the garbage terminator,
    encrypted packets, which may be corrupted, and runs of empty decoys,
    which make the transport rekey. The stream is received in pieces of
    arbitrary size.
    This reaches the v2 receive states and their failures, except two
    that need the 16-byte v1 prefix: the fallback to v1, which
    p2p_transport_bidirectional_v1v2 reaches, and the disconnect of a v1
    peer with the wrong magic. Once the transport reports v2, its session
    id must match the peer's.
    b2636036e6
  13. DrahtBot removed the label Needs rebase on Nov 11, 2026
Labels

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-20 15:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me