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 "net/socket/fuzzed_socket_factory.h"
6
7 #include <fuzzer/FuzzedDataProvider.h>
8
9 #include <string_view>
10
11 #include "base/notreached.h"
12 #include "net/base/address_list.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/network_change_notifier.h"
16 #include "net/log/net_log_with_source.h"
17 #include "net/socket/connection_attempts.h"
18 #include "net/socket/fuzzed_datagram_client_socket.h"
19 #include "net/socket/fuzzed_socket.h"
20 #include "net/socket/ssl_client_socket.h"
21 #include "net/traffic_annotation/network_traffic_annotation.h"
22
23 namespace net {
24
25 class NetLog;
26
27 namespace {
28
29 // SSLClientSocket implementation that always fails to connect.
30 class FailingSSLClientSocket : public SSLClientSocket {
31 public:
32 FailingSSLClientSocket() = default;
33
34 FailingSSLClientSocket(const FailingSSLClientSocket&) = delete;
35 FailingSSLClientSocket& operator=(const FailingSSLClientSocket&) = delete;
36
37 ~FailingSSLClientSocket() override = default;
38
39 // Socket implementation:
Read(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)40 int Read(IOBuffer* buf,
41 int buf_len,
42 CompletionOnceCallback callback) override {
43 NOTREACHED();
44 }
45
Write(IOBuffer * buf,int buf_len,CompletionOnceCallback callback,const NetworkTrafficAnnotationTag & traffic_annotation)46 int Write(IOBuffer* buf,
47 int buf_len,
48 CompletionOnceCallback callback,
49 const NetworkTrafficAnnotationTag& traffic_annotation) override {
50 NOTREACHED();
51 }
52
SetReceiveBufferSize(int32_t size)53 int SetReceiveBufferSize(int32_t size) override { return OK; }
SetSendBufferSize(int32_t size)54 int SetSendBufferSize(int32_t size) override { return OK; }
55
56 // StreamSocket implementation:
Connect(CompletionOnceCallback callback)57 int Connect(CompletionOnceCallback callback) override { return ERR_FAILED; }
58
Disconnect()59 void Disconnect() override {}
IsConnected() const60 bool IsConnected() const override { return false; }
IsConnectedAndIdle() const61 bool IsConnectedAndIdle() const override { return false; }
62
GetPeerAddress(IPEndPoint * address) const63 int GetPeerAddress(IPEndPoint* address) const override {
64 return ERR_SOCKET_NOT_CONNECTED;
65 }
GetLocalAddress(IPEndPoint * address) const66 int GetLocalAddress(IPEndPoint* address) const override {
67 return ERR_SOCKET_NOT_CONNECTED;
68 }
69
NetLog() const70 const NetLogWithSource& NetLog() const override { return net_log_; }
71
WasEverUsed() const72 bool WasEverUsed() const override { return false; }
73
GetNegotiatedProtocol() const74 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; }
75
GetSSLInfo(SSLInfo * ssl_info)76 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; }
77
GetTotalReceivedBytes() const78 int64_t GetTotalReceivedBytes() const override { return 0; }
79
GetSSLCertRequestInfo(SSLCertRequestInfo * cert_request_info) const80 void GetSSLCertRequestInfo(
81 SSLCertRequestInfo* cert_request_info) const override {}
82
ApplySocketTag(const net::SocketTag & tag)83 void ApplySocketTag(const net::SocketTag& tag) override {}
84
85 // SSLSocket implementation:
ExportKeyingMaterial(std::string_view label,std::optional<base::span<const uint8_t>> context,base::span<uint8_t> out)86 int ExportKeyingMaterial(std::string_view label,
87 std::optional<base::span<const uint8_t>> context,
88 base::span<uint8_t> out) override {
89 NOTREACHED();
90 }
91
92 // SSLClientSocket implementation:
GetECHRetryConfigs()93 std::vector<uint8_t> GetECHRetryConfigs() override { NOTREACHED(); }
94
95 private:
96 NetLogWithSource net_log_;
97 };
98
99 } // namespace
100
FuzzedSocketFactory(FuzzedDataProvider * data_provider)101 FuzzedSocketFactory::FuzzedSocketFactory(FuzzedDataProvider* data_provider)
102 : data_provider_(data_provider) {}
103
104 FuzzedSocketFactory::~FuzzedSocketFactory() = default;
105
106 std::unique_ptr<DatagramClientSocket>
CreateDatagramClientSocket(DatagramSocket::BindType bind_type,NetLog * net_log,const NetLogSource & source)107 FuzzedSocketFactory::CreateDatagramClientSocket(
108 DatagramSocket::BindType bind_type,
109 NetLog* net_log,
110 const NetLogSource& source) {
111 return std::make_unique<FuzzedDatagramClientSocket>(data_provider_);
112 }
113
114 std::unique_ptr<TransportClientSocket>
CreateTransportClientSocket(const AddressList & addresses,std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,NetworkQualityEstimator * network_quality_estimator,NetLog * net_log,const NetLogSource & source)115 FuzzedSocketFactory::CreateTransportClientSocket(
116 const AddressList& addresses,
117 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
118 NetworkQualityEstimator* network_quality_estimator,
119 NetLog* net_log,
120 const NetLogSource& source) {
121 auto socket = std::make_unique<FuzzedSocket>(data_provider_, net_log);
122 socket->set_fuzz_connect_result(fuzz_connect_result_);
123 // Just use the first address.
124 socket->set_remote_address(*addresses.begin());
125 return std::move(socket);
126 }
127
CreateSSLClientSocket(SSLClientContext * context,std::unique_ptr<StreamSocket> stream_socket,const HostPortPair & host_and_port,const SSLConfig & ssl_config)128 std::unique_ptr<SSLClientSocket> FuzzedSocketFactory::CreateSSLClientSocket(
129 SSLClientContext* context,
130 std::unique_ptr<StreamSocket> stream_socket,
131 const HostPortPair& host_and_port,
132 const SSLConfig& ssl_config) {
133 return std::make_unique<FailingSSLClientSocket>();
134 }
135
136 } // namespace net
137