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 "base/notreached.h"
10 #include "net/base/address_list.h"
11 #include "net/base/ip_endpoint.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/network_change_notifier.h"
14 #include "net/log/net_log_with_source.h"
15 #include "net/socket/connection_attempts.h"
16 #include "net/socket/fuzzed_datagram_client_socket.h"
17 #include "net/socket/fuzzed_socket.h"
18 #include "net/socket/ssl_client_socket.h"
19 #include "net/traffic_annotation/network_traffic_annotation.h"
20
21 namespace net {
22
23 class NetLog;
24
25 namespace {
26
27 // SSLClientSocket implementation that always fails to connect.
28 class FailingSSLClientSocket : public SSLClientSocket {
29 public:
30 FailingSSLClientSocket() = default;
31
32 FailingSSLClientSocket(const FailingSSLClientSocket&) = delete;
33 FailingSSLClientSocket& operator=(const FailingSSLClientSocket&) = delete;
34
35 ~FailingSSLClientSocket() override = default;
36
37 // Socket implementation:
Read(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)38 int Read(IOBuffer* buf,
39 int buf_len,
40 CompletionOnceCallback callback) override {
41 NOTREACHED();
42 return ERR_UNEXPECTED;
43 }
44
Write(IOBuffer * buf,int buf_len,CompletionOnceCallback callback,const NetworkTrafficAnnotationTag & traffic_annotation)45 int Write(IOBuffer* buf,
46 int buf_len,
47 CompletionOnceCallback callback,
48 const NetworkTrafficAnnotationTag& traffic_annotation) override {
49 NOTREACHED();
50 return ERR_UNEXPECTED;
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(base::StringPiece label,bool has_context,base::StringPiece context,unsigned char * out,unsigned int outlen)86 int ExportKeyingMaterial(base::StringPiece label,
87 bool has_context,
88 base::StringPiece context,
89 unsigned char* out,
90 unsigned int outlen) override {
91 NOTREACHED();
92 return 0;
93 }
94
95 // SSLClientSocket implementation:
GetECHRetryConfigs()96 std::vector<uint8_t> GetECHRetryConfigs() override {
97 NOTREACHED();
98 return {};
99 }
100
101 private:
102 NetLogWithSource net_log_;
103 };
104
105 } // namespace
106
FuzzedSocketFactory(FuzzedDataProvider * data_provider)107 FuzzedSocketFactory::FuzzedSocketFactory(FuzzedDataProvider* data_provider)
108 : data_provider_(data_provider) {}
109
110 FuzzedSocketFactory::~FuzzedSocketFactory() = default;
111
112 std::unique_ptr<DatagramClientSocket>
CreateDatagramClientSocket(DatagramSocket::BindType bind_type,NetLog * net_log,const NetLogSource & source)113 FuzzedSocketFactory::CreateDatagramClientSocket(
114 DatagramSocket::BindType bind_type,
115 NetLog* net_log,
116 const NetLogSource& source) {
117 return std::make_unique<FuzzedDatagramClientSocket>(data_provider_);
118 }
119
120 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)121 FuzzedSocketFactory::CreateTransportClientSocket(
122 const AddressList& addresses,
123 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
124 NetworkQualityEstimator* network_quality_estimator,
125 NetLog* net_log,
126 const NetLogSource& source) {
127 auto socket = std::make_unique<FuzzedSocket>(data_provider_, net_log);
128 socket->set_fuzz_connect_result(fuzz_connect_result_);
129 // Just use the first address.
130 socket->set_remote_address(*addresses.begin());
131 return std::move(socket);
132 }
133
CreateSSLClientSocket(SSLClientContext * context,std::unique_ptr<StreamSocket> stream_socket,const HostPortPair & host_and_port,const SSLConfig & ssl_config)134 std::unique_ptr<SSLClientSocket> FuzzedSocketFactory::CreateSSLClientSocket(
135 SSLClientContext* context,
136 std::unique_ptr<StreamSocket> stream_socket,
137 const HostPortPair& host_and_port,
138 const SSLConfig& ssl_config) {
139 return std::make_unique<FailingSSLClientSocket>();
140 }
141
142 } // namespace net
143