1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/socket/fuzzed_socket.h"
#include <fuzzer/FuzzedDataProvider.h>
#include <algorithm>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.h"
#include "net/base/io_buffer.h"
#include "net/log/net_log_source_type.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace net {
namespace {
const int kMaxAsyncReadsAndWrites = 1000;
// Some of the socket errors that can be returned by normal socket connection
// attempts.
const Error kConnectErrors[] = {
ERR_CONNECTION_RESET, ERR_CONNECTION_CLOSED, ERR_FAILED,
ERR_CONNECTION_TIMED_OUT, ERR_ACCESS_DENIED, ERR_CONNECTION_REFUSED,
ERR_ADDRESS_UNREACHABLE};
// Some of the socket errors that can be returned by normal socket reads /
// writes. The first one is returned when no more input data remains, so it's
// one of the most common ones.
const Error kReadWriteErrors[] = {ERR_CONNECTION_CLOSED, ERR_FAILED,
ERR_TIMED_OUT, ERR_CONNECTION_RESET};
} // namespace
FuzzedSocket::FuzzedSocket(FuzzedDataProvider* data_provider,
net::NetLog* net_log)
: data_provider_(data_provider),
net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)),
remote_address_(IPEndPoint(IPAddress::IPv4Localhost(), 80)) {}
FuzzedSocket::~FuzzedSocket() = default;
int FuzzedSocket::Read(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) {
DCHECK(!connect_pending_);
DCHECK(!read_pending_);
bool sync;
int result;
if (net_error_ != OK) {
// If an error has already been generated, use it to determine what to do.
result = net_error_;
sync = !error_pending_;
} else {
// Otherwise, use |data_provider_|. Always consume a bool, even when
// ForceSync() is true, to behave more consistently against input mutations.
sync = data_provider_->ConsumeBool() || ForceSync();
num_async_reads_and_writes_ += static_cast<int>(!sync);
std::string data = data_provider_->ConsumeRandomLengthString(buf_len);
result = data.size();
if (!data.empty()) {
std::ranges::copy(data, buf->data());
} else {
result = ConsumeReadWriteErrorFromData();
net_error_ = result;
if (!sync)
error_pending_ = true;
}
}
// Graceful close of a socket returns OK, at least in theory. This doesn't
// perfectly reflect real socket behavior, but close enough.
if (result == ERR_CONNECTION_CLOSED)
result = 0;
if (sync) {
if (result > 0)
total_bytes_read_ += result;
return result;
}
read_pending_ = true;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&FuzzedSocket::OnReadComplete, weak_factory_.GetWeakPtr(),
std::move(callback), result));
return ERR_IO_PENDING;
}
int FuzzedSocket::Write(
IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback,
const NetworkTrafficAnnotationTag& /* traffic_annotation */) {
DCHECK(!connect_pending_);
DCHECK(!write_pending_);
bool sync;
int result;
if (net_error_ != OK) {
// If an error has already been generated, use it to determine what to do.
result = net_error_;
sync = !error_pending_;
} else {
// Otherwise, use |data_provider_|. Always consume a bool, even when
// ForceSync() is true, to behave more consistently against input mutations.
sync = data_provider_->ConsumeBool() || ForceSync();
num_async_reads_and_writes_ += static_cast<int>(!sync);
// Intentionally using smaller |result| size here.
result = data_provider_->ConsumeIntegralInRange<int>(0, 0xFF);
if (result > buf_len)
result = buf_len;
if (result == 0) {
net_error_ = ConsumeReadWriteErrorFromData();
result = net_error_;
if (!sync)
error_pending_ = true;
}
}
if (sync) {
if (result > 0)
total_bytes_written_ += result;
return result;
}
write_pending_ = true;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&FuzzedSocket::OnWriteComplete, weak_factory_.GetWeakPtr(),
std::move(callback), result));
return ERR_IO_PENDING;
}
int FuzzedSocket::SetReceiveBufferSize(int32_t size) {
return OK;
}
int FuzzedSocket::SetSendBufferSize(int32_t size) {
return OK;
}
int FuzzedSocket::Bind(const net::IPEndPoint& local_addr) {
NOTREACHED();
}
int FuzzedSocket::Connect(CompletionOnceCallback callback) {
// Sockets can normally be reused, but don't support it here.
DCHECK_NE(net_error_, OK);
DCHECK(!connect_pending_);
DCHECK(!read_pending_);
DCHECK(!write_pending_);
DCHECK(!error_pending_);
DCHECK(!total_bytes_read_);
DCHECK(!total_bytes_written_);
bool sync = true;
Error result = OK;
if (fuzz_connect_result_) {
// Decide if sync or async. Use async, if no data is left.
sync = data_provider_->ConsumeBool();
// Decide if the connect succeeds or not, and if so, pick an error code.
if (data_provider_->ConsumeBool())
result = data_provider_->PickValueInArray(kConnectErrors);
}
if (sync) {
net_error_ = result;
return result;
}
connect_pending_ = true;
if (result != OK)
error_pending_ = true;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&FuzzedSocket::OnConnectComplete,
weak_factory_.GetWeakPtr(), std::move(callback), result));
return ERR_IO_PENDING;
}
void FuzzedSocket::Disconnect() {
net_error_ = ERR_CONNECTION_CLOSED;
weak_factory_.InvalidateWeakPtrs();
connect_pending_ = false;
read_pending_ = false;
write_pending_ = false;
error_pending_ = false;
}
bool FuzzedSocket::IsConnected() const {
return net_error_ == OK && !error_pending_;
}
bool FuzzedSocket::IsConnectedAndIdle() const {
return IsConnected();
}
int FuzzedSocket::GetPeerAddress(IPEndPoint* address) const {
if (!IsConnected())
return ERR_SOCKET_NOT_CONNECTED;
*address = remote_address_;
return OK;
}
int FuzzedSocket::GetLocalAddress(IPEndPoint* address) const {
if (!IsConnected())
return ERR_SOCKET_NOT_CONNECTED;
*address = IPEndPoint(IPAddress(127, 0, 0, 1), 43434);
return OK;
}
const NetLogWithSource& FuzzedSocket::NetLog() const {
return net_log_;
}
bool FuzzedSocket::WasEverUsed() const {
return total_bytes_written_ != 0 || total_bytes_read_ != 0;
}
NextProto FuzzedSocket::GetNegotiatedProtocol() const {
return NextProto::kProtoUnknown;
}
bool FuzzedSocket::GetSSLInfo(SSLInfo* ssl_info) {
return false;
}
int64_t FuzzedSocket::GetTotalReceivedBytes() const {
return total_bytes_read_;
}
void FuzzedSocket::ApplySocketTag(const net::SocketTag& tag) {}
Error FuzzedSocket::ConsumeReadWriteErrorFromData() {
return data_provider_->PickValueInArray(kReadWriteErrors);
}
void FuzzedSocket::OnReadComplete(CompletionOnceCallback callback, int result) {
CHECK(read_pending_);
read_pending_ = false;
if (result <= 0) {
error_pending_ = false;
} else {
total_bytes_read_ += result;
}
std::move(callback).Run(result);
}
void FuzzedSocket::OnWriteComplete(CompletionOnceCallback callback,
int result) {
CHECK(write_pending_);
write_pending_ = false;
if (result <= 0) {
error_pending_ = false;
} else {
total_bytes_written_ += result;
}
std::move(callback).Run(result);
}
void FuzzedSocket::OnConnectComplete(CompletionOnceCallback callback,
int result) {
CHECK(connect_pending_);
connect_pending_ = false;
if (result < 0)
error_pending_ = false;
net_error_ = result;
std::move(callback).Run(result);
}
bool FuzzedSocket::ForceSync() const {
return (num_async_reads_and_writes_ >= kMaxAsyncReadsAndWrites);
}
} // namespace net
|