1 // Copyright 2016 The Chromium Authors
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 <stddef.h>
6 #include <stdint.h>
7
8 #include <fuzzer/FuzzedDataProvider.h>
9
10 #include <memory>
11
12 #include "base/check_op.h"
13 #include "net/base/address_list.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/network_anonymization_key.h"
16 #include "net/base/test_completion_callback.h"
17 #include "net/dns/host_resolver.h"
18 #include "net/dns/mock_host_resolver.h"
19 #include "net/dns/public/secure_dns_policy.h"
20 #include "net/log/net_log.h"
21 #include "net/log/test_net_log.h"
22 #include "net/socket/fuzzed_socket.h"
23 #include "net/socket/socks_client_socket.h"
24 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
25
26 // Fuzzer for SocksClientSocket. Only covers the SOCKS4 handshake.
27 //
28 // |data| is used to create a FuzzedSocket to fuzz reads and writes, see that
29 // class for details.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)30 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
31 // Including an observer; even though the recorded results aren't currently
32 // used, it'll ensure the netlogging code is fuzzed as well.
33 net::RecordingNetLogObserver net_log_observer;
34
35 FuzzedDataProvider data_provider(data, size);
36
37 // Determine if the DNS lookup returns synchronously or asynchronously,
38 // succeeds or fails. Only returning an IPv4 address is fine, as SOCKS only
39 // issues IPv4 requests.
40 net::MockHostResolver mock_host_resolver;
41 mock_host_resolver.set_synchronous_mode(data_provider.ConsumeBool());
42 if (data_provider.ConsumeBool()) {
43 mock_host_resolver.rules()->AddRule("*", "127.0.0.1");
44 } else {
45 mock_host_resolver.rules()->AddRule("*", net::ERR_NAME_NOT_RESOLVED);
46 }
47
48 net::TestCompletionCallback callback;
49 auto fuzzed_socket =
50 std::make_unique<net::FuzzedSocket>(&data_provider, net::NetLog::Get());
51 CHECK_EQ(net::OK, fuzzed_socket->Connect(callback.callback()));
52
53 net::SOCKSClientSocket socket(
54 std::move(fuzzed_socket), net::HostPortPair("foo", 80),
55 net::NetworkAnonymizationKey(), net::DEFAULT_PRIORITY,
56 &mock_host_resolver, net::SecureDnsPolicy::kAllow,
57 TRAFFIC_ANNOTATION_FOR_TESTS);
58 int result = socket.Connect(callback.callback());
59 callback.GetResult(result);
60 return 0;
61 }
62