Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Side by Side Diff: net/dns/fuzzed_host_resolver.cc

Issue 1946793002: net: Add fuzzer for HostResolverImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Suppress leak Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/dns/fuzzed_host_resolver.h"
6
7 #include <stdint.h>
8
9 #include <limits>
10 #include <string>
11
12 #include "base/bind.h"
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "net/base/address_list.h"
19 #include "net/base/fuzzed_data_provider.h"
20 #include "net/base/ip_address.h"
21 #include "net/base/ip_endpoint.h"
22 #include "net/base/net_errors.h"
23 #include "net/dns/dns_client.h"
24 #include "net/dns/dns_config_service.h"
25 #include "net/dns/dns_hosts.h"
26
27 namespace net {
28
29 namespace {
30
31 // HostResolverProc that returns a random set of results, and can succeed or
32 // fail. Must only be run on the thread it's created on.
33 class FuzzedHostResolverProc : public HostResolverProc {
34 public:
35 // Can safely be used after the destruction of |data_provider|. This can
36 // happen if a request is issued but the code never waits for the result
37 // before the test ends.
38 explicit FuzzedHostResolverProc(
39 base::WeakPtr<FuzzedDataProvider> data_provider)
40 : HostResolverProc(nullptr),
41 data_provider_(data_provider),
42 network_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
43
44 int Resolve(const std::string& host,
45 AddressFamily address_family,
46 HostResolverFlags host_resolver_flags,
47 AddressList* addrlist,
48 int* os_error) override {
49 DCHECK(network_task_runner_->BelongsToCurrentThread());
50
51 if (os_error)
52 *os_error = 0;
53
54 // If the data provider is no longer avaiable, just fail. The HostResolver
55 // has already been deleted by this point, anyways.
56 if (!data_provider_)
57 return ERR_FAILED;
58
59 AddressList result;
60
61 // Put IPv6 addresses before IPv4 ones. This code doesn't sort addresses
62 // correctly, but when sorted according to spec, IPv6 addresses are
63 // generally before IPv4 ones.
64 if (address_family == ADDRESS_FAMILY_UNSPECIFIED ||
65 address_family == ADDRESS_FAMILY_IPV6) {
66 while (data_provider_->ConsumeBool()) {
67 result.push_back(IPEndPoint(
68 IPAddress(
69 data_provider_->ConsumeUint8(), data_provider_->ConsumeUint8(),
70 data_provider_->ConsumeUint8(), data_provider_->ConsumeUint8(),
71 data_provider_->ConsumeUint8(), data_provider_->ConsumeUint8(),
72 data_provider_->ConsumeUint8(), data_provider_->ConsumeUint8(),
73 data_provider_->ConsumeUint8(), data_provider_->ConsumeUint8(),
74 data_provider_->ConsumeUint8(), data_provider_->ConsumeUint8(),
75 data_provider_->ConsumeUint8(), data_provider_->ConsumeUint8(),
76 data_provider_->ConsumeUint8(), data_provider_->ConsumeUint8()),
77 0));
78 }
79 }
80
81 if (address_family == ADDRESS_FAMILY_UNSPECIFIED ||
82 address_family == ADDRESS_FAMILY_IPV4) {
83 while (data_provider_->ConsumeBool()) {
84 result.push_back(IPEndPoint(IPAddress(data_provider_->ConsumeUint8(),
85 data_provider_->ConsumeUint8(),
86 data_provider_->ConsumeUint8(),
87 data_provider_->ConsumeUint8()),
88 0));
89 }
90 }
91
92 if (result.empty())
93 return ERR_NAME_NOT_RESOLVED;
94
95 if (host_resolver_flags & HOST_RESOLVER_CANONNAME) {
96 // Don't bother to fuzz this - almost nothing cares.
97 result.set_canonical_name("foo.com");
98 }
99
100 *addrlist = result;
101 return OK;
102 }
103
104 private:
105 ~FuzzedHostResolverProc() override {}
106
107 base::WeakPtr<FuzzedDataProvider> data_provider_;
108
109 // Just used for thread-safety checks.
110 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
111
112 DISALLOW_COPY_AND_ASSIGN(FuzzedHostResolverProc);
113 };
114
115 } // namespace
116
117 FuzzedHostResolver::FuzzedHostResolver(const Options& options,
118 NetLog* net_log,
119 FuzzedDataProvider* data_provider)
120 : HostResolverImpl(options, net_log, base::ThreadTaskRunnerHandle::Get()),
121 data_provider_(data_provider),
122 socket_factory_(data_provider),
123 is_ipv6_reachable_(data_provider->ConsumeBool()),
124 net_log_(net_log),
125 data_provider_weak_factory_(data_provider) {
126 HostResolverImpl::ProcTaskParams proc_task_params(
127 new FuzzedHostResolverProc(data_provider_weak_factory_.GetWeakPtr()),
128 // Retries are only used when the original request hangs, which this class
129 // currently can't simulate.
130 0 /* max_retry_attempts */);
131 set_proc_params_for_test(proc_task_params);
132 }
133
134 FuzzedHostResolver::~FuzzedHostResolver() {}
135
136 void FuzzedHostResolver::SetDnsClientEnabled(bool enabled) {
137 if (!enabled) {
138 HostResolverImpl::SetDnsClientEnabled(false);
139 return;
140 }
141
142 // Fuzz DNS configuration.
143
144 DnsConfig config;
145 // Determine the number of name servers. Each case deliberately falls through.
146 switch (data_provider_->ConsumeValueInRange(0, 3)) {
Julia Tuttle 2016/05/13 17:15:13 It bothers me slightly that this construction keep
mmenke 2016/05/17 19:50:36 Fuzzed this, and the IPs in the hosts file (And th
147 case 3:
148 config.nameservers.push_back(IPEndPoint(IPAddress(1, 2, 3, 6), 345));
149 case 2:
150 config.nameservers.push_back(IPEndPoint(IPAddress::IPv6Localhost(), 77));
151 case 1:
152 config.nameservers.push_back(IPEndPoint(IPAddress(1, 2, 3, 4), 53));
153 default:
154 break;
155 }
156
157 // Determine the number of suffixes to try. Each case deliberately falls
158 // through.
159 switch (data_provider_->ConsumeValueInRange(0, 3)) {
160 case 3:
161 config.search.push_back("foo.com");
162 case 2:
163 config.search.push_back("bar");
164 case 1:
165 config.search.push_back("com");
166 default:
167 break;
168 }
169
170 net::DnsHosts hosts;
171 // Fuzz hosts file. Each case deliberately falls through.
172 switch (data_provider_->ConsumeValueInRange(0, 4)) {
173 case 4:
174 config.hosts[net::DnsHostsKey("foo.com", net::ADDRESS_FAMILY_IPV4)] =
175 net::IPAddress(1, 2, 3, 4);
176 case 3:
177 config.hosts[net::DnsHostsKey("foo.com", net::ADDRESS_FAMILY_IPV6)] =
178 net::IPAddress(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
179 case 2:
180 config.hosts[net::DnsHostsKey("foo", net::ADDRESS_FAMILY_IPV4)] =
181 net::IPAddress(1, 2, 3, 5);
182 case 1:
183 config.hosts[net::DnsHostsKey("foo", net::ADDRESS_FAMILY_IPV4)] =
184 net::IPAddress(1, 2, 3, 4);
185 default:
186 break;
187 }
188
189 config.unhandled_options = data_provider_->ConsumeBool();
190 config.append_to_multi_label_name = data_provider_->ConsumeBool();
191 config.randomize_ports = data_provider_->ConsumeBool();
192 config.attempts = data_provider_->ConsumeValueInRange(1, 3);
193 config.ndots = data_provider_->ConsumeValueInRange(0, 3);
194
195 // Timeouts don't really work for fuzzing. Even a timeout of 0 milliseconds
Julia Tuttle 2016/05/13 17:15:13 I understand that fuzzing the timeout doesn't work
mmenke 2016/05/17 19:50:36 Because if it ever triggers, it causes non-determi
196 // will be increased after the first timeout, resulting in inconsistent
197 // behavior.
198 config.timeout = base::TimeDelta::FromDays(10);
199
200 config.rotate = data_provider_->ConsumeBool();
201
202 // Doesn't currently seem to do anything.
203 config.edns0 = false;
204
205 config.use_local_ipv6 = data_provider_->ConsumeBool();
206
207 std::unique_ptr<DnsClient> dns_client = DnsClient::CreateClientForTesting(
208 net_log_, &socket_factory_,
209 base::Bind(&FuzzedDataProvider::ConsumeValueInRangeInt,
210 base::Unretained(data_provider_)));
211 dns_client->SetConfig(config);
212 SetDnsClient(std::move(dns_client));
213 }
214
215 bool FuzzedHostResolver::IsIPv6Reachable(const BoundNetLog& net_log) {
216 return is_ipv6_reachable_;
217 }
218
219 void FuzzedHostResolver::RunLoopbackProbeJob() {
220 SetHaveOnlyLoopbackAddresses(data_provider_->ConsumeBool());
221 }
222
223 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698