Branch data Line data Source code
1 : : // Copyright (c) 2020-2022 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 <addrman.h>
6 : : #include <chainparams.h>
7 : : #include <common/args.h>
8 : : #include <net.h>
9 : : #include <net_processing.h>
10 : : #include <netaddress.h>
11 : : #include <protocol.h>
12 : : #include <test/fuzz/FuzzedDataProvider.h>
13 : : #include <test/fuzz/fuzz.h>
14 : : #include <test/fuzz/util.h>
15 : : #include <test/fuzz/util/net.h>
16 : : #include <test/fuzz/util/threadinterrupt.h>
17 : : #include <test/util/setup_common.h>
18 : : #include <util/translation.h>
19 : :
20 : : #include <cstdint>
21 : : #include <vector>
22 : :
23 : : namespace {
24 : : const TestingSetup* g_setup;
25 : :
26 : 3895 : int32_t GetCheckRatio()
27 : : {
28 [ + - + - ]: 7790 : return std::clamp<int32_t>(g_setup->m_node.args->GetIntArg("-checkaddrman", 0), 0, 1000000);
29 : : }
30 : :
31 : : } // namespace
32 : :
33 : 1 : void initialize_connman()
34 : : {
35 [ + - + - : 1 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
+ - ]
36 : 1 : g_setup = testing_setup.get();
37 : 1 : }
38 : :
39 [ + - ]: 3163 : FUZZ_TARGET(connman, .init = initialize_connman)
40 : : {
41 : 2707 : SeedRandomStateForTest(SeedRand::ZEROS);
42 : 2707 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
43 : 2707 : SetMockTime(ConsumeTime(fuzzed_data_provider));
44 : 2707 : auto netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
45 [ + - + - ]: 2707 : auto addr_man_ptr{std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio())};
46 [ + + ]: 2707 : if (fuzzed_data_provider.ConsumeBool()) {
47 : 1917 : const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
48 [ - + + - ]: 1917 : DataStream ds{serialized_data};
49 : 1917 : try {
50 [ + + ]: 3834 : ds >> *addr_man_ptr;
51 [ - + ]: 1188 : } catch (const std::ios_base::failure&) {
52 [ + - + - ]: 2376 : addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
53 : 1188 : }
54 : 1917 : }
55 [ + - ]: 2707 : AddrManDeterministic& addr_man{*addr_man_ptr};
56 [ + - ]: 2707 : auto net_events{ConsumeNetEvents(fuzzed_data_provider)};
57 : :
58 : : // Mock CreateSock() to create FuzzedSock.
59 [ + - ]: 2707 : auto CreateSockOrig = CreateSock;
60 : 11854 : CreateSock = [&fuzzed_data_provider](int, int, int) {
61 : 9147 : return std::make_unique<FuzzedSock>(fuzzed_data_provider);
62 : 2707 : };
63 : :
64 : : // Mock g_dns_lookup() to return a fuzzed address.
65 [ + - ]: 2707 : auto g_dns_lookup_orig = g_dns_lookup;
66 : 3050 : g_dns_lookup = [&fuzzed_data_provider](const std::string&, bool) {
67 [ + - + + : 1029 : return std::vector<CNetAddr>{ConsumeNetAddr(fuzzed_data_provider)};
- - ]
68 : 3050 : };
69 : :
70 : 2707 : ConnmanTestMsg connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
71 : : fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
72 : : addr_man,
73 : : netgroupman,
74 : : Params(),
75 : 2707 : fuzzed_data_provider.ConsumeBool(),
76 [ + - + - : 5414 : ConsumeThreadInterrupt(fuzzed_data_provider)};
+ - ]
77 : :
78 : 2707 : const uint64_t max_outbound_limit{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
79 : 2707 : CConnman::Options options;
80 : 2707 : options.m_msgproc = &net_events;
81 : 2707 : options.nMaxOutboundLimit = max_outbound_limit;
82 [ + - ]: 2707 : connman.Init(options);
83 : :
84 [ + - ]: 2707 : CNetAddr random_netaddr;
85 [ + - ]: 2707 : CAddress random_address;
86 : 2707 : CNode random_node = ConsumeNode(fuzzed_data_provider);
87 [ + - ]: 2707 : CSubNet random_subnet;
88 : 2707 : std::string random_string;
89 : :
90 [ + + + + ]: 24722 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
91 : 22015 : CNode& p2p_node{*ConsumeNodeAsUniquePtr(fuzzed_data_provider).release()};
92 [ + - ]: 22015 : connman.AddTestNode(p2p_node);
93 : : }
94 : :
95 [ + + + + ]: 107463 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
96 [ + - ]: 104756 : CallOneOf(
97 : : fuzzed_data_provider,
98 : 3341 : [&] {
99 : 3341 : random_netaddr = ConsumeNetAddr(fuzzed_data_provider);
100 : 3341 : },
101 : 8448 : [&] {
102 : 8448 : random_address = ConsumeAddress(fuzzed_data_provider);
103 : 8448 : },
104 : 4981 : [&] {
105 : 4981 : random_subnet = ConsumeSubNet(fuzzed_data_provider);
106 : 4981 : },
107 : 3618 : [&] {
108 : 3618 : random_string = fuzzed_data_provider.ConsumeRandomLengthString(64);
109 : 3618 : },
110 : 663 : [&] {
111 : 2652 : connman.AddNode({random_string, fuzzed_data_provider.ConsumeBool()});
112 [ - + + - ]: 1989 : },
113 : 1574 : [&] {
114 : 1574 : connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
115 : 1574 : },
116 : 2567 : [&] {
117 : 2567 : connman.DisconnectNode(fuzzed_data_provider.ConsumeIntegral<NodeId>());
118 : 2567 : },
119 : 805 : [&] {
120 : 805 : connman.DisconnectNode(random_netaddr);
121 : 805 : },
122 : 409 : [&] {
123 : 409 : connman.DisconnectNode(random_string);
124 : 409 : },
125 : 171 : [&] {
126 : 171 : connman.DisconnectNode(random_subnet);
127 : 171 : },
128 : 2518 : [&] {
129 [ + - ]: 2518 : connman.ForEachNode([](auto) {});
130 : 2518 : },
131 : 175 : [&] {
132 [ + - ]: 175 : (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
133 : 175 : },
134 : 2931 : [&] {
135 : 2931 : auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
136 : 2931 : auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
137 : 2931 : auto filtered = fuzzed_data_provider.ConsumeBool();
138 : 2931 : (void)connman.GetAddressesUnsafe(max_addresses, max_pct, /*network=*/std::nullopt, filtered);
139 : 2931 : },
140 : 544 : [&] {
141 : 544 : auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
142 : 544 : auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
143 : 544 : (void)connman.GetAddresses(/*requestor=*/random_node, max_addresses, max_pct);
144 : 544 : },
145 : 338 : [&] {
146 : 338 : (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
147 : 338 : },
148 : 2640 : [&] {
149 : 2640 : (void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
150 : 2640 : },
151 : 30003 : [&] {
152 : 30003 : (void)connman.OutboundTargetReached(fuzzed_data_provider.ConsumeBool());
153 : 30003 : },
154 : 643 : [&] {
155 [ + - ]: 643 : CSerializedNetMsg serialized_net_msg;
156 [ + - ]: 643 : serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::MESSAGE_TYPE_SIZE);
157 : 643 : serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
158 [ + - ]: 643 : connman.PushMessage(&random_node, std::move(serialized_net_msg));
159 : 643 : },
160 : 654 : [&] {
161 : 654 : connman.RemoveAddedNode(random_string);
162 : 654 : },
163 : 25208 : [&] {
164 : 25208 : connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
165 : 25208 : },
166 : 123 : [&] {
167 : 123 : connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
168 : 123 : },
169 : 2556 : [&] {
170 : 2556 : ConnectionType conn_type{
171 : 2556 : fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES)};
172 [ + + ]: 2556 : if (conn_type == ConnectionType::INBOUND) { // INBOUND is not allowed
173 : 10 : conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
174 : : }
175 : :
176 [ - + ]: 5112 : connman.OpenNetworkConnection(
177 : : /*addrConnect=*/random_address,
178 [ + - ]: 2556 : /*fCountFailure=*/fuzzed_data_provider.ConsumeBool(),
179 : : /*grant_outbound=*/{},
180 [ + + ]: 2556 : /*pszDest=*/fuzzed_data_provider.ConsumeBool() ? nullptr : random_string.c_str(),
181 : : /*conn_type=*/conn_type,
182 : 2556 : /*use_v2transport=*/fuzzed_data_provider.ConsumeBool());
183 : 2556 : },
184 : 8822 : [&] {
185 : 8822 : connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
186 : 8822 : const auto peer = ConsumeAddress(fuzzed_data_provider);
187 : 8822 : connman.CreateNodeFromAcceptedSocketPublic(
188 [ + - ]: 17644 : /*sock=*/CreateSock(AF_INET, SOCK_STREAM, IPPROTO_TCP),
189 : : /*permissions=*/ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS),
190 : 17644 : /*addr_bind=*/ConsumeAddress(fuzzed_data_provider),
191 : : /*addr_peer=*/peer);
192 : 8822 : },
193 : 251 : [&] {
194 : 251 : CConnman::Options options;
195 : :
196 : 251 : options.vBinds = ConsumeServiceVector(fuzzed_data_provider);
197 : :
198 : 251 : options.vWhiteBinds = std::vector<NetWhitebindPermissions>{
199 [ + - ]: 502 : fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 5)};
200 [ + + ]: 589 : for (auto& wb : options.vWhiteBinds) {
201 : 338 : wb.m_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
202 : 338 : wb.m_service = ConsumeService(fuzzed_data_provider);
203 : : }
204 : :
205 : 251 : options.onion_binds = ConsumeServiceVector(fuzzed_data_provider);
206 : :
207 [ + + + + ]: 251 : options.bind_on_any = options.vBinds.empty() && options.vWhiteBinds.empty() &&
208 [ - + ]: 3 : options.onion_binds.empty();
209 : :
210 [ + - ]: 251 : connman.InitBindsPublic(options);
211 : 251 : },
212 : 773 : [&] {
213 : 773 : connman.SocketHandlerPublic();
214 : 773 : });
215 : : }
216 [ + - ]: 2707 : (void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
217 [ + - ]: 2707 : (void)connman.GetExtraFullOutboundCount();
218 [ + - ]: 2707 : (void)connman.GetLocalServices();
219 [ + - - + ]: 2707 : assert(connman.GetMaxOutboundTarget() == max_outbound_limit);
220 [ + - ]: 2707 : (void)connman.GetMaxOutboundTimeframe();
221 [ + - ]: 2707 : (void)connman.GetMaxOutboundTimeLeftInCycle();
222 [ + - ]: 2707 : (void)connman.GetNetworkActive();
223 : 2707 : std::vector<CNodeStats> stats;
224 [ + - ]: 2707 : connman.GetNodeStats(stats);
225 [ + - ]: 2707 : (void)connman.GetOutboundTargetBytesLeft();
226 [ + - ]: 2707 : (void)connman.GetTotalBytesRecv();
227 [ + - ]: 2707 : (void)connman.GetTotalBytesSent();
228 [ + - ]: 2707 : (void)connman.GetTryNewOutboundPeer();
229 : 2707 : (void)connman.GetUseAddrmanOutgoing();
230 [ + - ]: 2707 : (void)connman.ASMapHealthCheck();
231 : :
232 [ + - ]: 2707 : connman.ClearTestNodes();
233 [ + - ]: 2707 : g_dns_lookup = g_dns_lookup_orig;
234 [ + - ]: 2707 : CreateSock = CreateSockOrig;
235 : 2707 : }
|