Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +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> |
| 14 | #include <base/message_loop/message_loop.h> |
| 15 | #include <base/run_loop.h> |
| 16 | #include <brillo/message_loops/base_message_loop.h> |
| 17 | #include <libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h> |
| 18 | |
| 19 | #include "bindings/worker_common.pb.h" |
| 20 | #include "system-proxy/protobuf_util.h" |
| 21 | #include "system-proxy/server_proxy.h" |
| 22 | |
| 23 | namespace { |
| 24 | void NullClosure() {} |
| 25 | } // namespace |
| 26 | |
| 27 | struct Environment { |
| 28 | Environment() { |
| 29 | logging::SetMinLogLevel(logging::LOG_FATAL); // Disable logging. |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | // ServerProxy implementation that receives input from a given file descriptor, |
| 34 | // instead of the default standard input file descriptor (STDIN_FILENO). |
| 35 | class FakeServerProxy : public system_proxy::ServerProxy { |
| 36 | public: |
| 37 | explicit FakeServerProxy(base::ScopedFD stdin_fd) |
| 38 | : system_proxy::ServerProxy(base::BindOnce(&NullClosure)), |
| 39 | stdin_fd_(std::move(stdin_fd)) {} |
| 40 | FakeServerProxy(const FakeServerProxy&) = delete; |
| 41 | FakeServerProxy& operator=(const FakeServerProxy&) = delete; |
| 42 | ~FakeServerProxy() override = default; |
| 43 | |
| 44 | int GetStdinPipe() override { return stdin_fd_.get(); } |
| 45 | |
| 46 | private: |
| 47 | base::ScopedFD stdin_fd_; |
| 48 | }; |
| 49 | |
| 50 | DEFINE_PROTO_FUZZER(const system_proxy::WorkerConfigs& configs) { |
| 51 | static Environment env; |
| 52 | |
| 53 | // Mock main task runner |
| 54 | base::MessageLoopForIO message_loop; |
| 55 | brillo::BaseMessageLoop brillo_loop(&message_loop); |
| 56 | brillo_loop.SetAsCurrent(); |
| 57 | |
| 58 | int fds[2]; |
| 59 | CHECK(base::CreateLocalNonBlockingPipe(fds)); |
| 60 | base::ScopedFD stdin_read_fd(fds[0]); |
| 61 | base::ScopedFD stdin_write_fd(fds[1]); |
| 62 | |
| 63 | auto server = std::make_unique<FakeServerProxy>(std::move(stdin_read_fd)); |
| 64 | |
| 65 | // Send the config to the worker's stdin input. |
| 66 | WriteProtobuf(stdin_write_fd.get(), configs); |
| 67 | base::RunLoop().RunUntilIdle(); |
| 68 | } |