• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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 <fuzzer/FuzzedDataProvider.h>
6 
7 #include <algorithm>
8 
9 #include "base/logging.h"
10 #include "base/run_loop.h"
11 #include "net/base/host_port_pair.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/request_priority.h"
14 #include "net/cert/x509_certificate.h"
15 #include "net/dns/public/secure_dns_policy.h"
16 #include "net/log/net_log.h"
17 #include "net/log/net_log_source.h"
18 #include "net/log/test_net_log.h"
19 #include "net/socket/fuzzed_socket_factory.h"
20 #include "net/socket/socket_tag.h"
21 #include "net/socket/socket_test_util.h"
22 #include "net/socket/ssl_client_socket.h"
23 #include "net/spdy/spdy_test_util_common.h"
24 #include "net/ssl/ssl_config.h"
25 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
26 
27 namespace {
28 
29 const uint8_t kCertData[] = {
30 #include "net/data/ssl/certificates/spdy_pooling.inc"
31 };
32 
33 class FuzzerDelegate : public net::SpdyStream::Delegate {
34  public:
FuzzerDelegate(base::OnceClosure done_closure)35   explicit FuzzerDelegate(base::OnceClosure done_closure)
36       : done_closure_(std::move(done_closure)) {}
37 
38   FuzzerDelegate(const FuzzerDelegate&) = delete;
39   FuzzerDelegate& operator=(const FuzzerDelegate&) = delete;
40 
OnHeadersSent()41   void OnHeadersSent() override {}
OnEarlyHintsReceived(const spdy::Http2HeaderBlock & headers)42   void OnEarlyHintsReceived(const spdy::Http2HeaderBlock& headers) override {}
OnHeadersReceived(const spdy::Http2HeaderBlock & response_headers)43   void OnHeadersReceived(
44       const spdy::Http2HeaderBlock& response_headers) override {}
OnDataReceived(std::unique_ptr<net::SpdyBuffer> buffer)45   void OnDataReceived(std::unique_ptr<net::SpdyBuffer> buffer) override {}
OnDataSent()46   void OnDataSent() override {}
OnTrailers(const spdy::Http2HeaderBlock & trailers)47   void OnTrailers(const spdy::Http2HeaderBlock& trailers) override {}
OnClose(int status)48   void OnClose(int status) override { std::move(done_closure_).Run(); }
CanGreaseFrameType() const49   bool CanGreaseFrameType() const override { return false; }
50 
source_dependency() const51   net::NetLogSource source_dependency() const override {
52     return net::NetLogSource();
53   }
54 
55  private:
56   base::OnceClosure done_closure_;
57 };
58 
59 }  // namespace
60 
61 namespace net {
62 
63 namespace {
64 
65 class FuzzedSocketFactoryWithMockSSLData : public FuzzedSocketFactory {
66  public:
67   explicit FuzzedSocketFactoryWithMockSSLData(
68       FuzzedDataProvider* data_provider);
69 
70   void AddSSLSocketDataProvider(SSLSocketDataProvider* socket);
71 
72   std::unique_ptr<SSLClientSocket> CreateSSLClientSocket(
73       SSLClientContext* context,
74       std::unique_ptr<StreamSocket> nested_socket,
75       const HostPortPair& host_and_port,
76       const SSLConfig& ssl_config) override;
77 
78  private:
79   SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_;
80 };
81 
FuzzedSocketFactoryWithMockSSLData(FuzzedDataProvider * data_provider)82 FuzzedSocketFactoryWithMockSSLData::FuzzedSocketFactoryWithMockSSLData(
83     FuzzedDataProvider* data_provider)
84     : FuzzedSocketFactory(data_provider) {}
85 
AddSSLSocketDataProvider(SSLSocketDataProvider * data)86 void FuzzedSocketFactoryWithMockSSLData::AddSSLSocketDataProvider(
87     SSLSocketDataProvider* data) {
88   mock_ssl_data_.Add(data);
89 }
90 
91 std::unique_ptr<SSLClientSocket>
CreateSSLClientSocket(SSLClientContext * context,std::unique_ptr<StreamSocket> nested_socket,const HostPortPair & host_and_port,const SSLConfig & ssl_config)92 FuzzedSocketFactoryWithMockSSLData::CreateSSLClientSocket(
93     SSLClientContext* context,
94     std::unique_ptr<StreamSocket> nested_socket,
95     const HostPortPair& host_and_port,
96     const SSLConfig& ssl_config) {
97   return std::make_unique<MockSSLClientSocket>(std::move(nested_socket),
98                                                host_and_port, ssl_config,
99                                                mock_ssl_data_.GetNext());
100 }
101 
102 }  // namespace
103 
104 }  // namespace net
105 
106 // Fuzzer for SpdySession
107 //
108 // |data| is used to create a FuzzedServerSocket.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)109 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
110   // Including an observer; even though the recorded results aren't currently
111   // used, it'll ensure the netlogging code is fuzzed as well.
112   net::RecordingNetLogObserver net_log_observer;
113   net::NetLogWithSource net_log_with_source =
114       net::NetLogWithSource::Make(net::NetLogSourceType::NONE);
115   FuzzedDataProvider data_provider(data, size);
116   net::FuzzedSocketFactoryWithMockSSLData socket_factory(&data_provider);
117   socket_factory.set_fuzz_connect_result(false);
118 
119   net::SSLSocketDataProvider ssl_provider(net::ASYNC, net::OK);
120   ssl_provider.ssl_info.cert = net::X509Certificate::CreateFromBytes(kCertData);
121   CHECK(ssl_provider.ssl_info.cert);
122   socket_factory.AddSSLSocketDataProvider(&ssl_provider);
123 
124   net::SpdySessionDependencies deps;
125   std::unique_ptr<net::HttpNetworkSession> http_session(
126       net::SpdySessionDependencies::SpdyCreateSessionWithSocketFactory(
127           &deps, &socket_factory));
128 
129   net::ProxyChain direct_connect(net::ProxyChain::Direct());
130   net::SpdySessionKey session_key(
131       net::HostPortPair("127.0.0.1", 80), direct_connect,
132       net::PRIVACY_MODE_DISABLED, net::SpdySessionKey::IsProxySession::kFalse,
133       net::SocketTag(), net::NetworkAnonymizationKey(),
134       net::SecureDnsPolicy::kAllow);
135   base::WeakPtr<net::SpdySession> spdy_session(net::CreateSpdySession(
136       http_session.get(), session_key, net_log_with_source));
137 
138   net::SpdyStreamRequest stream_request;
139   base::WeakPtr<net::SpdyStream> stream;
140 
141   net::TestCompletionCallback wait_for_start;
142   int rv = stream_request.StartRequest(
143       net::SPDY_REQUEST_RESPONSE_STREAM, spdy_session,
144       GURL("http://www.example.invalid/"), false /* no early data */,
145       net::DEFAULT_PRIORITY, net::SocketTag(), net_log_with_source,
146       wait_for_start.callback(), TRAFFIC_ANNOTATION_FOR_TESTS);
147 
148   if (rv == net::ERR_IO_PENDING) {
149     rv = wait_for_start.WaitForResult();
150   }
151 
152   // Re-check the status after potential event loop.
153   if (rv != net::OK) {
154     LOG(WARNING) << "StartRequest failed with result=" << rv;
155     return 0;
156   }
157 
158   stream = stream_request.ReleaseStream();
159   stream->SendRequestHeaders(
160       net::SpdyTestUtil::ConstructGetHeaderBlock("http://www.example.invalid"),
161       net::NO_MORE_DATA_TO_SEND);
162 
163   base::RunLoop run_loop;
164   FuzzerDelegate delegate(run_loop.QuitClosure());
165   stream->SetDelegate(&delegate);
166   run_loop.Run();
167 
168   // Give a chance for GOING_AWAY sessions to wrap up.
169   base::RunLoop().RunUntilIdle();
170 
171   return 0;
172 }
173