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