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 return ERR_UNEXPECTED;
45 }
46
Write(IOBuffer * buf,int buf_len,CompletionOnceCallback callback,const NetworkTrafficAnnotationTag & traffic_annotation)47 int Write(IOBuffer* buf,
48 int buf_len,
49 CompletionOnceCallback callback,
50 const NetworkTrafficAnnotationTag& traffic_annotation) override {
51 NOTREACHED();
52 return ERR_UNEXPECTED;
53 }
54
SetReceiveBufferSize(int32_t size)55 int SetReceiveBufferSize(int32_t size) override { return OK; }
SetSendBufferSize(int32_t size)56 int SetSendBufferSize(int32_t size) override { return OK; }
57
58 // StreamSocket implementation:
Connect(CompletionOnceCallback callback)59 int Connect(CompletionOnceCallback callback) override { return ERR_FAILED; }
60
Disconnect()61 void Disconnect() override {}
IsConnected() const62 bool IsConnected() const override { return false; }
IsConnectedAndIdle() const63 bool IsConnectedAndIdle() const override { return false; }
64
GetPeerAddress(IPEndPoint * address) const65 int GetPeerAddress(IPEndPoint* address) const override {
66 return ERR_SOCKET_NOT_CONNECTED;
67 }
GetLocalAddress(IPEndPoint * address) const68 int GetLocalAddress(IPEndPoint* address) const override {
69 return ERR_SOCKET_NOT_CONNECTED;
70 }
71
NetLog() const72 const NetLogWithSource& NetLog() const override { return net_log_; }
73
WasEverUsed() const74 bool WasEverUsed() const override { return false; }
75
GetNegotiatedProtocol() const76 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; }
77
GetSSLInfo(SSLInfo * ssl_info)78 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; }
79
GetTotalReceivedBytes() const80 int64_t GetTotalReceivedBytes() const override { return 0; }
81
GetSSLCertRequestInfo(SSLCertRequestInfo * cert_request_info) const82 void GetSSLCertRequestInfo(
83 SSLCertRequestInfo* cert_request_info) const override {}
84
ApplySocketTag(const net::SocketTag & tag)85 void ApplySocketTag(const net::SocketTag& tag) override {}
86
87 // SSLSocket implementation:
ExportKeyingMaterial(std::string_view label,bool has_context,std::string_view context,unsigned char * out,unsigned int outlen)88 int ExportKeyingMaterial(std::string_view label,
89 bool has_context,
90 std::string_view context,
91 unsigned char* out,
92 unsigned int outlen) override {
93 NOTREACHED();
94 return 0;
95 }
96
97 // SSLClientSocket implementation:
GetECHRetryConfigs()98 std::vector<uint8_t> GetECHRetryConfigs() override {
99 NOTREACHED();
100 return {};
101 }
102
103 private:
104 NetLogWithSource net_log_;
105 };
106
107 } // namespace
108
FuzzedSocketFactory(FuzzedDataProvider * data_provider)109 FuzzedSocketFactory::FuzzedSocketFactory(FuzzedDataProvider* data_provider)
110 : data_provider_(data_provider) {}
111
112 FuzzedSocketFactory::~FuzzedSocketFactory() = default;
113
114 std::unique_ptr<DatagramClientSocket>
CreateDatagramClientSocket(DatagramSocket::BindType bind_type,NetLog * net_log,const NetLogSource & source)115 FuzzedSocketFactory::CreateDatagramClientSocket(
116 DatagramSocket::BindType bind_type,
117 NetLog* net_log,
118 const NetLogSource& source) {
119 return std::make_unique<FuzzedDatagramClientSocket>(data_provider_);
120 }
121
122 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)123 FuzzedSocketFactory::CreateTransportClientSocket(
124 const AddressList& addresses,
125 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
126 NetworkQualityEstimator* network_quality_estimator,
127 NetLog* net_log,
128 const NetLogSource& source) {
129 auto socket = std::make_unique<FuzzedSocket>(data_provider_, net_log);
130 socket->set_fuzz_connect_result(fuzz_connect_result_);
131 // Just use the first address.
132 socket->set_remote_address(*addresses.begin());
133 return std::move(socket);
134 }
135
CreateSSLClientSocket(SSLClientContext * context,std::unique_ptr<StreamSocket> stream_socket,const HostPortPair & host_and_port,const SSLConfig & ssl_config)136 std::unique_ptr<SSLClientSocket> FuzzedSocketFactory::CreateSSLClientSocket(
137 SSLClientContext* context,
138 std::unique_ptr<StreamSocket> stream_socket,
139 const HostPortPair& host_and_port,
140 const SSLConfig& ssl_config) {
141 return std::make_unique<FailingSSLClientSocket>();
142 }
143
144 } // namespace net
145