socks_client_socket_fuzzer.cc 2.3 KB

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