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