• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
WasAlpnNegotiated() const74   bool WasAlpnNegotiated() 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(base::StringPiece label,bool has_context,base::StringPiece context,unsigned char * out,unsigned int outlen)88   int ExportKeyingMaterial(base::StringPiece label,
89                            bool has_context,
90                            base::StringPiece 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