Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_stream_parser.h" | 5 #include "net/http/http_stream_parser.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 19 #include "base/numerics/safe_conversions.h" | |
| 20 #include "net/base/address_list.h" | |
| 21 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 22 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 23 #include "net/base/test_completion_callback.h" | 21 #include "net/base/test_completion_callback.h" |
| 24 #include "net/http/http_request_headers.h" | 22 #include "net/http/http_request_headers.h" |
| 25 #include "net/http/http_request_info.h" | 23 #include "net/http/http_request_info.h" |
| 26 #include "net/http/http_response_info.h" | 24 #include "net/http/http_response_info.h" |
| 27 #include "net/log/net_log.h" | 25 #include "net/log/net_log.h" |
| 28 #include "net/log/test_net_log.h" | 26 #include "net/log/test_net_log.h" |
| 29 #include "net/socket/client_socket_handle.h" | 27 #include "net/socket/client_socket_handle.h" |
| 30 #include "net/socket/socket_test_util.h" | 28 #include "net/socket/fuzzed_socket.h" |
| 31 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 32 | 30 |
| 33 // Fuzzer for HttpStreamParser. | 31 // Fuzzer for HttpStreamParser. |
| 34 // | 32 // |
| 35 // |data| is the data received over a mock HTTP connection through one or more | 33 // |data| is used to create a FuzzedSocket. |
| 36 // reads, along with metadata about the size of each read, and whether or not | |
| 37 // the read completely synchronously. | |
| 38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 34 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 39 // Needed for thread checks and waits. | 35 // Needed for thread checks and waits. |
| 40 base::MessageLoopForIO message_loop; | 36 base::MessageLoopForIO message_loop; |
| 41 | 37 |
| 42 net::MockWrite writes[] = { | |
| 43 net::MockWrite(net::ASYNC, 0, "GET / HTTP/1.1\r\n\r\n"), | |
| 44 }; | |
| 45 | |
| 46 // Break the buffer into a sequence of variable sized sync and async | |
| 47 // reads. Use the last bytes of |data| exclusively for determining | |
| 48 // the size and type of each read. | |
| 49 std::vector<net::MockRead> reads; | |
| 50 // Sequence number for socket operations. | |
| 51 int last_sequence_number = 0; | |
| 52 | |
| 53 // IoMode for the final read, where the server closes the mock socket. | |
| 54 net::IoMode close_socket_io_mode = net::ASYNC; | |
| 55 | |
| 56 // Break |data| up into reads. The test may or may not make to the final | |
| 57 // read, where the server closes the socket. | |
| 58 | |
| 59 // Each read needs a one byte seed to determine read size and whether it | |
| 60 // should be sync or async, so if there's only one byte left unused, can't use | |
| 61 // it here. The bytes used to get this metadata are not used as over-the-wire | |
| 62 // bytes. | |
| 63 while (size > 0) { | |
| 64 size_t read_seed = data[size - 1]; | |
| 65 size--; | |
| 66 net::IoMode io_mode = net::ASYNC; | |
| 67 // Low order bit determines IoMode. | |
| 68 if (read_seed & 0x1) | |
| 69 io_mode = net::SYNCHRONOUS; | |
| 70 | |
| 71 // Use second bit to determine if the last read, when the socket is closed, | |
| 72 // is synchronous. The second bit only matters the last time this loop is | |
| 73 // runs. | |
| 74 if (read_seed & 0x2) { | |
| 75 close_socket_io_mode = net::SYNCHRONOUS; | |
| 76 } else { | |
| 77 close_socket_io_mode = net::ASYNC; | |
| 78 } | |
| 79 | |
| 80 // If there are no more bytes in |data|, next read is the connection close. | |
| 81 if (size == 0) | |
| 82 break; | |
| 83 | |
| 84 read_seed >>= 2; | |
| 85 | |
| 86 // Last 6 bits determine how many bytes are returned by the read. | |
| 87 int read_size = static_cast<int>(std::min(1 + read_seed, size)); | |
| 88 reads.push_back(net::MockRead(io_mode, reinterpret_cast<const char*>(data), | |
| 89 read_size, ++last_sequence_number)); | |
| 90 | |
| 91 data += read_size; | |
| 92 size -= read_size; | |
| 93 } | |
| 94 | |
| 95 // Server closes the socket. | |
| 96 reads.push_back(net::MockRead(close_socket_io_mode, | |
| 97 net::ERR_CONNECTION_CLOSED, | |
| 98 ++last_sequence_number)); | |
| 99 net::SequencedSocketData socket_data(reads.data(), reads.size(), writes, | |
| 100 arraysize(writes)); | |
| 101 socket_data.set_connect_data(net::MockConnect(net::SYNCHRONOUS, net::OK)); | |
| 102 | |
| 103 scoped_ptr<net::MockTCPClientSocket> socket( | |
| 104 new net::MockTCPClientSocket(net::AddressList(), nullptr, &socket_data)); | |
| 105 | |
| 106 net::TestCompletionCallback callback; | 38 net::TestCompletionCallback callback; |
| 107 CHECK_EQ(net::OK, socket->Connect(callback.callback())); | 39 net::BoundTestNetLog bound_test_net_log; |
| 40 scoped_ptr<net::FuzzedSocket> fuzzed_socket( | |
| 41 new net::FuzzedSocket(data, size, bound_test_net_log.bound())); | |
|
eroman
2016/04/15 19:09:52
same comment about std::unique_ptr<>
| |
| 42 CHECK_EQ(net::OK, fuzzed_socket->Connect(callback.callback())); | |
| 108 | 43 |
| 109 net::ClientSocketHandle socket_handle; | 44 net::ClientSocketHandle socket_handle; |
| 110 socket_handle.SetSocket(std::move(socket)); | 45 socket_handle.SetSocket(std::move(fuzzed_socket)); |
| 111 | 46 |
| 112 net::HttpRequestInfo request_info; | 47 net::HttpRequestInfo request_info; |
| 113 request_info.method = "GET"; | 48 request_info.method = "GET"; |
| 114 request_info.url = GURL("http://localhost/"); | 49 request_info.url = GURL("http://localhost/"); |
| 115 | 50 |
| 116 scoped_refptr<net::GrowableIOBuffer> read_buffer(new net::GrowableIOBuffer()); | 51 scoped_refptr<net::GrowableIOBuffer> read_buffer(new net::GrowableIOBuffer()); |
| 117 // Use a NetLog that listens to events, to get coverage of logging | 52 // Use a NetLog that listens to events, to get coverage of logging |
| 118 // callbacks. | 53 // callbacks. |
| 119 net::BoundTestNetLog net_log; | |
| 120 net::HttpStreamParser parser(&socket_handle, &request_info, read_buffer.get(), | 54 net::HttpStreamParser parser(&socket_handle, &request_info, read_buffer.get(), |
| 121 net_log.bound()); | 55 bound_test_net_log.bound()); |
| 122 | 56 |
| 123 net::HttpResponseInfo response_info; | 57 net::HttpResponseInfo response_info; |
| 124 int result = | 58 int result = |
| 125 parser.SendRequest("GET / HTTP/1.1\r\n", net::HttpRequestHeaders(), | 59 parser.SendRequest("GET / HTTP/1.1\r\n", net::HttpRequestHeaders(), |
| 126 &response_info, callback.callback()); | 60 &response_info, callback.callback()); |
| 127 CHECK_EQ(net::OK, callback.GetResult(result)); | 61 result = callback.GetResult(result); |
| 62 if (net::OK != result) | |
| 63 return 0; | |
| 128 | 64 |
| 129 result = parser.ReadResponseHeaders(callback.callback()); | 65 result = parser.ReadResponseHeaders(callback.callback()); |
| 130 result = callback.GetResult(result); | 66 result = callback.GetResult(result); |
| 131 | 67 |
| 132 if (result < 0) | 68 while (result > 0) { |
| 133 return 0; | |
| 134 | |
| 135 while (true) { | |
| 136 // 64 exactly matches the maximum amount of data returned by a single | |
| 137 // MockRead, as created above. | |
| 138 scoped_refptr<net::IOBufferWithSize> io_buffer( | 69 scoped_refptr<net::IOBufferWithSize> io_buffer( |
| 139 new net::IOBufferWithSize(64)); | 70 new net::IOBufferWithSize(64)); |
| 140 result = parser.ReadResponseBody(io_buffer.get(), io_buffer->size(), | 71 result = parser.ReadResponseBody(io_buffer.get(), io_buffer->size(), |
| 141 callback.callback()); | 72 callback.callback()); |
| 142 | 73 |
| 143 // Releasing the pointer to IOBuffer immediately is more likely to lead to a | 74 // Releasing the pointer to IOBuffer immediately is more likely to lead to a |
| 144 // use-after-free. | 75 // use-after-free. |
| 145 io_buffer = nullptr; | 76 io_buffer = nullptr; |
| 146 | 77 |
| 147 if (callback.GetResult(result) <= 0) | 78 result = callback.GetResult(result); |
| 148 break; | |
| 149 } | 79 } |
| 150 | 80 |
| 151 return 0; | 81 return 0; |
| 152 } | 82 } |
| OLD | NEW |