1 // Copyright 2019 The Chromium 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 #ifndef TESTING_LIBFUZZER_FUZZERS_MACH_MACH_MESSAGE_CONVERTER_H_ 6 #define TESTING_LIBFUZZER_FUZZERS_MACH_MACH_MESSAGE_CONVERTER_H_ 7 8 #include <mach/mach.h> 9 #include <stdint.h> 10 11 #include <memory> 12 #include <vector> 13 14 #include "base/mac/scoped_mach_port.h" 15 #include "testing/libfuzzer/fuzzers/mach/mach_message.pb.h" 16 17 namespace mach_fuzzer { 18 19 // Container for a Mach port right that will be sent in a message. 20 struct SendablePort { 21 mach_port_t name = MACH_PORT_NULL; 22 mach_msg_type_name_t disposition = 0; 23 MachPortType proto_type = static_cast<MachPortType>(-1); 24 25 base::mac::ScopedMachSendRight send_right; 26 base::mac::ScopedMachReceiveRight receive_right; 27 }; 28 29 // Holds the buffer allocation and port references for a message to be sent. 30 struct SendableMessage { 31 // The message buffer. 32 std::unique_ptr<uint8_t[]> buffer; 33 34 // The |ports| are also encoded into the body of the message, but they are 35 // accessible here to allow for further manipulation. 36 std::vector<SendablePort> ports; 37 38 // Pointer to the header of the message stored in |buffer|. 39 mach_msg_header_t* header = nullptr; 40 }; 41 42 // Converts the given protobuf message into a live Mach message, including port 43 // rights. 44 SendableMessage ConvertProtoToMachMessage(const MachMessage& proto); 45 46 // Takes the protobuf |proto|, converts it to a Mach message using 47 // ConvertProtoToMachMessage(), and then sends it via |local_port|. The port 48 // named by |local_port| must have a send right, which will be copied. 49 struct SendResult { 50 // The return value from mach_msg_send(). 51 kern_return_t kr; 52 53 // The message that was sent, including its descriptors. This allows callers 54 // to control the lifetimes of any Mach rights after the message has been 55 // sent. 56 SendableMessage message; 57 }; 58 SendResult SendMessage(mach_port_t local_port, const MachMessage& proto); 59 60 } // namespace mach_fuzzer 61 62 #endif // TESTING_LIBFUZZER_FUZZERS_MACH_MACH_MESSAGE_CONVERTER_H_ 63