• 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/http/http_stream_parser.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <fuzzer/FuzzedDataProvider.h>
11 
12 #include <algorithm>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 #include "base/check_op.h"
18 #include "base/memory/ref_counted.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/test_completion_callback.h"
22 #include "net/http/http_request_headers.h"
23 #include "net/http/http_response_info.h"
24 #include "net/log/net_log.h"
25 #include "net/log/test_net_log.h"
26 #include "net/socket/fuzzed_socket.h"
27 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
28 #include "url/gurl.h"
29 
30 // Fuzzer for HttpStreamParser.
31 //
32 // |data| is used to create a FuzzedSocket.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)33 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
34   net::TestCompletionCallback callback;
35   // Including an observer; even though the recorded results aren't currently
36   // used, it'll ensure the netlogging code is fuzzed as well.
37   net::RecordingNetLogObserver net_log_observer;
38   net::NetLogWithSource net_log_with_source =
39       net::NetLogWithSource::Make(net::NetLogSourceType::NONE);
40   FuzzedDataProvider data_provider(data, size);
41   net::FuzzedSocket fuzzed_socket(&data_provider, net::NetLog::Get());
42   CHECK_EQ(net::OK, fuzzed_socket.Connect(callback.callback()));
43 
44   scoped_refptr<net::GrowableIOBuffer> read_buffer =
45       base::MakeRefCounted<net::GrowableIOBuffer>();
46   // Use a NetLog that listens to events, to get coverage of logging
47   // callbacks.
48   net::HttpStreamParser parser(
49       &fuzzed_socket, false /* is_reused */, GURL("http://localhost/"), "GET",
50       /*upload_data_stream=*/nullptr, read_buffer.get(), net_log_with_source);
51 
52   net::HttpResponseInfo response_info;
53   int result = parser.SendRequest(
54       "GET / HTTP/1.1\r\n", net::HttpRequestHeaders(),
55       TRAFFIC_ANNOTATION_FOR_TESTS, &response_info, callback.callback());
56   result = callback.GetResult(result);
57   if (net::OK != result)
58     return 0;
59 
60   result = parser.ReadResponseHeaders(callback.callback());
61   result = callback.GetResult(result);
62 
63   if (result < 0)
64     return 0;
65 
66   while (true) {
67     scoped_refptr<net::IOBufferWithSize> io_buffer =
68         base::MakeRefCounted<net::IOBufferWithSize>(64);
69     result = parser.ReadResponseBody(io_buffer.get(), io_buffer->size(),
70                                      callback.callback());
71 
72     // Releasing the pointer to IOBuffer immediately is more likely to lead to a
73     // use-after-free.
74     io_buffer = nullptr;
75     if (callback.GetResult(result) <= 0)
76       break;
77   }
78 
79   return 0;
80 }
81