blob: 491f044a435e6fbb389cc4bec95bece7cbf4eb32 [file] [log] [blame]
Xiaochu Liu61647282018-11-08 10:31:08 -08001// 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>
hschamea373112020-01-22 13:06:46 +090011#include <base/posix/unix_domain_socket.h>
Xiaochu Liu7a3509a2019-08-06 14:34:51 -070012#include <libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h>
13
14#include "imageloader/ipc.pb.h"
Xiaochu Liu61647282018-11-08 10:31:08 -080015
16namespace imageloader {
17
Xiaochu Liu7a3509a2019-08-06 14:34:51 -070018void helper_process_receiver_fuzzer_run(const char* data, size_t size) {
Xiaochu Liu61647282018-11-08 10:31:08 -080019 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 Abee2e90722019-08-21 05:45:55 +090035 helper_process_receiver.OnCommandReady();
Xiaochu Liu61647282018-11-08 10:31:08 -080036}
37
38} // namespace imageloader
39
Xiaochu Liu7a3509a2019-08-06 14:34:51 -070040DEFINE_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 Liu61647282018-11-08 10:31:08 -080046}