Xiaochu Liu | 6164728 | 2018-11-08 10:31:08 -0800 | [diff] [blame] | 1 | // Copyright 2018 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 "imageloader/helper_process_receiver.h" |
| 6 | |
| 7 | #include <sys/socket.h> |
| 8 | #include <sys/types.h> |
| 9 | |
| 10 | #include <base/logging.h> |
hscham | ea37311 | 2020-01-22 13:06:46 +0900 | [diff] [blame^] | 11 | #include <base/posix/unix_domain_socket.h> |
Xiaochu Liu | 7a3509a | 2019-08-06 14:34:51 -0700 | [diff] [blame] | 12 | #include <libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h> |
| 13 | |
| 14 | #include "imageloader/ipc.pb.h" |
Xiaochu Liu | 6164728 | 2018-11-08 10:31:08 -0800 | [diff] [blame] | 15 | |
| 16 | namespace imageloader { |
| 17 | |
Xiaochu Liu | 7a3509a | 2019-08-06 14:34:51 -0700 | [diff] [blame] | 18 | void helper_process_receiver_fuzzer_run(const char* data, size_t size) { |
Xiaochu Liu | 6164728 | 2018-11-08 10:31:08 -0800 | [diff] [blame] | 19 | int socket_pair[2]; |
| 20 | socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, socket_pair); |
| 21 | base::ScopedFD reader_fd(socket_pair[0]); |
| 22 | base::ScopedFD writer_fd(socket_pair[1]); |
| 23 | |
| 24 | imageloader::HelperProcessReceiver helper_process_receiver( |
| 25 | std::move(reader_fd)); |
| 26 | |
| 27 | if (size == 0) { |
| 28 | // Per recvmsg(2), the return value will be 0 when the peer has performed an |
| 29 | // orderly shutdown. |
| 30 | // This causes current fuzzer process to exit permanently. |
| 31 | return; |
| 32 | } |
| 33 | base::UnixDomainSocket::SendMsg(writer_fd.get(), data, size, |
| 34 | std::vector<int>()); |
Hidehiko Abe | e2e9072 | 2019-08-21 05:45:55 +0900 | [diff] [blame] | 35 | helper_process_receiver.OnCommandReady(); |
Xiaochu Liu | 6164728 | 2018-11-08 10:31:08 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | } // namespace imageloader |
| 39 | |
Xiaochu Liu | 7a3509a | 2019-08-06 14:34:51 -0700 | [diff] [blame] | 40 | DEFINE_PROTO_FUZZER(const imageloader::ImageCommand& input) { |
| 41 | std::vector<char> msg_buf(input.ByteSizeLong()); |
| 42 | if (!input.SerializeToArray(msg_buf.data(), msg_buf.size())) |
| 43 | LOG(FATAL) << "error serializing protobuf"; |
| 44 | imageloader::helper_process_receiver_fuzzer_run(msg_buf.data(), |
| 45 | msg_buf.size()); |
Xiaochu Liu | 6164728 | 2018-11-08 10:31:08 -0800 | [diff] [blame] | 46 | } |