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