Andreea Costinas | c65f99b | 2020-04-27 12:54:14 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 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 <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #include <sys/socket.h> |
| 9 | #include <sys/types.h> |
| 10 | |
| 11 | #include <base/bind.h> |
| 12 | #include <base/files/file_util.h> |
| 13 | #include <base/logging.h> |
Andreea Costinas | c65f99b | 2020-04-27 12:54:14 +0200 | [diff] [blame] | 14 | #include <base/run_loop.h> |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame^] | 15 | #include <base/task/single_thread_task_executor.h> |
Andreea Costinas | c65f99b | 2020-04-27 12:54:14 +0200 | [diff] [blame] | 16 | #include <brillo/message_loops/base_message_loop.h> |
| 17 | #include <chromeos/patchpanel/socket.h> |
| 18 | #include <chromeos/patchpanel/socket_forwarder.h> |
| 19 | |
| 20 | #include "system-proxy/proxy_connect_job.h" |
| 21 | |
| 22 | namespace { |
Andreea Costinas | eea01f9 | 2020-07-01 13:29:22 +0200 | [diff] [blame] | 23 | void ResolveProxyCallback( |
| 24 | base::OnceClosure quit_task, |
Andreea Costinas | c65f99b | 2020-04-27 12:54:14 +0200 | [diff] [blame] | 25 | const std::string&, |
Andreea Costinas | eea01f9 | 2020-07-01 13:29:22 +0200 | [diff] [blame] | 26 | base::OnceCallback<void(const std::list<std::string>&)> |
| 27 | on_proxy_resolution_callback) { |
| 28 | // Exit the fuzzer if the input fed to the test is a valid HTTP CONNECT |
| 29 | // request. |
| 30 | std::move(quit_task).Run(); |
| 31 | } |
| 32 | void NullAuthenticationRequiredCallback( |
| 33 | const std::string& proxy_url, |
| 34 | const std::string& scheme, |
| 35 | const std::string& realm, |
| 36 | base::OnceCallback<void(const std::string& credentials)> |
| 37 | on_auth_acquired_callback) {} |
Andreea Costinas | c65f99b | 2020-04-27 12:54:14 +0200 | [diff] [blame] | 38 | |
| 39 | void OnConnectionSetupFinished(base::OnceClosure quit_task, |
| 40 | std::unique_ptr<patchpanel::SocketForwarder>, |
| 41 | system_proxy::ProxyConnectJob*) { |
| 42 | std::move(quit_task).Run(); |
| 43 | } |
| 44 | } // namespace |
| 45 | |
| 46 | struct Environment { |
| 47 | Environment() { |
Andreea Costinas | eea01f9 | 2020-07-01 13:29:22 +0200 | [diff] [blame] | 48 | logging::SetMinLogLevel(logging::LOG_FATAL); // Disable logging. |
Andreea Costinas | c65f99b | 2020-04-27 12:54:14 +0200 | [diff] [blame] | 49 | } |
| 50 | }; |
| 51 | |
| 52 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 53 | static Environment env; |
| 54 | |
| 55 | // Mock main task runner |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame^] | 56 | base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO); |
| 57 | brillo::BaseMessageLoop brillo_loop(task_executor.task_runner()); |
Andreea Costinas | c65f99b | 2020-04-27 12:54:14 +0200 | [diff] [blame] | 58 | brillo_loop.SetAsCurrent(); |
| 59 | |
| 60 | base::RunLoop run_loop; |
| 61 | |
| 62 | int socket_pair[2]; |
| 63 | socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, socket_pair); |
| 64 | base::ScopedFD reader_fd(socket_pair[0]); |
| 65 | base::ScopedFD writer_fd(socket_pair[1]); |
| 66 | int fds[2]; |
| 67 | |
| 68 | socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, |
| 69 | 0 /* protocol */, fds); |
| 70 | patchpanel::Socket cros_client_socket((base::ScopedFD(fds[1]))); |
| 71 | |
| 72 | auto connect_job = std::make_unique<system_proxy::ProxyConnectJob>( |
| 73 | std::make_unique<patchpanel::Socket>(base::ScopedFD(fds[0])), "", |
Andreea Costinas | eea01f9 | 2020-07-01 13:29:22 +0200 | [diff] [blame] | 74 | base::BindOnce(&ResolveProxyCallback, run_loop.QuitClosure()), |
| 75 | base::BindOnce(&NullAuthenticationRequiredCallback), |
Andreea Costinas | c65f99b | 2020-04-27 12:54:14 +0200 | [diff] [blame] | 76 | base::BindOnce(&OnConnectionSetupFinished, run_loop.QuitClosure())); |
| 77 | connect_job->Start(); |
| 78 | cros_client_socket.SendTo(data, size); |
| 79 | |
| 80 | run_loop.Run(); |
| 81 | return 0; |
| 82 | } |