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