forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcp.cpp
80 lines (65 loc) · 3.25 KB
/
pcp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2024 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <test/fuzz/util/net.h>
#include <common/pcp.h>
#include <util/check.h>
using namespace std::literals;
//! Fixed nonce to use in PCP port mapping requests.
constexpr PCPMappingNonce PCP_NONCE{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
//! Number of attempts to request a NAT-PMP or PCP port mapping to the gateway.
constexpr int NUM_TRIES{5};
//! Timeout for each attempt to request a port mapping.
constexpr std::chrono::duration TIMEOUT{100ms};
void port_map_target_init()
{
LogInstance().DisableLogging();
}
FUZZ_TARGET(pcp_request_port_map, .init = port_map_target_init)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
// Create a mocked socket between random (and potentially invalid) client and gateway addresses.
CreateSock = [&](int domain, int type, int protocol) {
if ((domain == AF_INET || domain == AF_INET6) && type == SOCK_DGRAM && protocol == IPPROTO_UDP) {
return std::make_unique<FuzzedSock>(fuzzed_data_provider);
}
return std::unique_ptr<FuzzedSock>();
};
// Perform the port mapping request. The mocked socket will return fuzzer-provided data.
const auto gateway_addr{ConsumeNetAddr(fuzzed_data_provider)};
const auto local_addr{ConsumeNetAddr(fuzzed_data_provider)};
const auto port{fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
const auto lifetime{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
const auto res{PCPRequestPortMap(PCP_NONCE, gateway_addr, local_addr, port, lifetime, NUM_TRIES, TIMEOUT)};
// In case of success the mapping must be consistent with the request.
if (const MappingResult* mapping = std::get_if<MappingResult>(&res)) {
Assert(mapping);
Assert(mapping->internal.GetPort() == port);
mapping->ToString();
}
}
FUZZ_TARGET(natpmp_request_port_map, .init = port_map_target_init)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
// Create a mocked socket between random (and potentially invalid) client and gateway addresses.
CreateSock = [&](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) {
return std::make_unique<FuzzedSock>(fuzzed_data_provider);
}
return std::unique_ptr<FuzzedSock>();
};
// Perform the port mapping request. The mocked socket will return fuzzer-provided data.
const auto gateway_addr{ConsumeNetAddr(fuzzed_data_provider)};
const auto port{fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
const auto lifetime{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
const auto res{NATPMPRequestPortMap(gateway_addr, port, lifetime, NUM_TRIES, TIMEOUT)};
// In case of success the mapping must be consistent with the request.
if (const MappingResult* mapping = std::get_if<MappingResult>(&res)) {
Assert(mapping);
Assert(mapping->internal.GetPort() == port);
mapping->ToString();
}
}