Skip to content

Commit c0c1ee9

Browse files
committed
fuzz: Provide correct length to assist fuzzer for v2 transport
before commit: 121493 REDUCE cov: 1744 ft: 2328 corp: 30/9789b lim: 976 exec/s: 979 rss: 443Mb L: 733/779 after commit: 121218 REDUCE cov: 1889 ft: 2574 corp: 36/2305b lim: 877 exec/s: 939 rss: 442Mb L: 345/345
1 parent 7255324 commit c0c1ee9

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/test/fuzz/p2p_v2_transport_serialization.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <compat/endian.h>
56
#include <crypto/chacha_poly_aead.h>
67
#include <key.h>
78
#include <net.h>
89
#include <netmessagemaker.h>
10+
#include <test/fuzz/FuzzedDataProvider.h>
911
#include <test/fuzz/fuzz.h>
1012

1113
#include <cassert>
@@ -18,9 +20,20 @@ FUZZ_TARGET(p2p_v2_transport_serialization)
1820
// Construct deserializer, with a dummy NodeId
1921
V2TransportDeserializer deserializer{(NodeId)0, k1, k2};
2022
V2TransportSerializer serializer{k1, k2};
23+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2124

22-
while (buffer.size() > 0) {
23-
const int handled = deserializer.Read(buffer);
25+
bool length_assist = fuzzed_data_provider.ConsumeBool();
26+
auto payload_bytes = fuzzed_data_provider.ConsumeRemainingBytes<uint8_t>();
27+
28+
if (length_assist && payload_bytes.size() >= CHACHA20_POLY1305_AEAD_AAD_LEN + CHACHA20_POLY1305_AEAD_TAG_LEN) {
29+
uint32_t packet_length = payload_bytes.size() - CHACHA20_POLY1305_AEAD_AAD_LEN - CHACHA20_POLY1305_AEAD_TAG_LEN;
30+
packet_length = htole32(packet_length);
31+
memcpy(payload_bytes.data(), &packet_length, 3);
32+
}
33+
34+
Span<const uint8_t> msg_bytes{payload_bytes};
35+
while (msg_bytes.size() > 0) {
36+
const int handled = deserializer.Read(msg_bytes);
2437
if (handled < 0) {
2538
break;
2639
}

0 commit comments

Comments
 (0)