Truly independent web browser
LibIPC: Disconnect a Mach peer that sends a malformed message
sideshowbarker0bf73f160799
diff --git a/Libraries/LibIPC/TransportMachPort.cpp b/Libraries/LibIPC/TransportMachPort.cppindex 5574885a0a3..71346040cd9 100644--- a/Libraries/LibIPC/TransportMachPort.cpp+++ b/Libraries/LibIPC/TransportMachPort.cpp@@ -74,6 +74,62 @@ static void set_mach_port_queue_limit(mach_port_t port)reinterpret_cast<mach_port_info_t>(&limits), MACH_PORT_LIMITS_INFO_COUNT);}+static bool is_well_formed_port_descriptor(mach_msg_port_descriptor_t const& descriptor)+{+ if (descriptor.type != MACH_MSG_PORT_DESCRIPTOR || descriptor.name == MACH_PORT_NULL)+ return false;+ switch (descriptor.disposition) {+ case MACH_MSG_TYPE_MOVE_SEND:+ case MACH_MSG_TYPE_MOVE_RECEIVE:+ case MACH_MSG_TYPE_MOVE_SEND_ONCE:+ return true;+ default:+ return false;+ }+}++// Checks the shape of a data message against what send_mach_message() builds: port descriptors, then for an+// out-of-line message the descriptor of its payload, or for an inline one the payload itself. The message comes from+// the peer, so anything else is a malformed message to reject, not an invariant of ours to assert.+static bool is_well_formed_data_message(mach_msg_header_t const* header, bool is_out_of_line)+{+ auto const* message = reinterpret_cast<u8 const*>(header);+ size_t const message_size = header->msgh_size;+ size_t offset = sizeof(mach_msg_header_t);++ size_t descriptor_count = 0;+ if (header->msgh_bits & MACH_MSGH_BITS_COMPLEX) {+ if (message_size < offset + sizeof(mach_msg_body_t))+ return false;+ descriptor_count = reinterpret_cast<mach_msg_body_t const*>(message + offset)->msgh_descriptor_count;+ offset += sizeof(mach_msg_body_t);+ }++ if (is_out_of_line && descriptor_count == 0)+ return false;+ size_t const port_count = is_out_of_line ? descriptor_count - 1 : descriptor_count;+ if (message_size < offset + (port_count * sizeof(mach_msg_port_descriptor_t)))+ return false;+ auto const* port_descriptors = reinterpret_cast<mach_msg_port_descriptor_t const*>(message + offset);+ for (size_t i = 0; i < port_count; ++i) {+ if (!is_well_formed_port_descriptor(port_descriptors[i]))+ return false;+ }+ offset += port_count * sizeof(mach_msg_port_descriptor_t);++ if (is_out_of_line) {+ if (message_size < offset + sizeof(mach_msg_ool_descriptor_t))+ return false;+ return reinterpret_cast<mach_msg_ool_descriptor_t const*>(message + offset)->type == MACH_MSG_OOL_DESCRIPTOR;+ }++ if (message_size < offset + sizeof(InlinePayloadHeader))+ return false;+ InlinePayloadHeader payload_header;+ __builtin_memcpy(&payload_header, message + offset, sizeof(payload_header));+ return payload_header.payload_size <= message_size - offset - sizeof(InlinePayloadHeader);+}+static Attachment attachment_from_descriptor(mach_msg_port_descriptor_t const& descriptor){VERIFY(descriptor.type == MACH_MSG_PORT_DESCRIPTOR);@@ -410,10 +466,15 @@ intptr_t TransportMachPort::io_thread_loop()continue;case IPC_DATA_MESSAGE_ID:case IPC_INLINE_DATA_MESSAGE_ID:- process_received_message(buffer.data());+ if (process_received_message(buffer.data()) == IterationDecision::Break)+ m_io_thread_state = IOThreadState::Stopped;continue;default:- VERIFY_NOT_REACHED();+ dbgln("TransportMachPort: Rejecting message with unknown id {:#x}", header->msgh_id);+ mach_msg_destroy(header);+ mark_peer_eof();+ m_io_thread_state = IOThreadState::Stopped;+ continue;}}@@ -534,9 +595,16 @@ TransportMachPort::SendOutcome TransportMachPort::send_mach_message(PendingMessareturn SendOutcome::Sent;}-void TransportMachPort::process_received_message(u8* buffer)+IterationDecision TransportMachPort::process_received_message(u8* buffer){auto* header = reinterpret_cast<mach_msg_header_t*>(buffer);+ if (!is_well_formed_data_message(header, header->msgh_id == IPC_DATA_MESSAGE_ID)) {+ dbgln("TransportMachPort: Rejecting malformed message");+ mach_msg_destroy(header);+ mark_peer_eof();+ return IterationDecision::Break;+ }+auto message = make<Message>();if (header->msgh_id == IPC_INLINE_DATA_MESSAGE_ID) {@@ -595,11 +663,11 @@ void TransportMachPort::process_received_message(u8* buffer)if (message->attachments.size() > MAX_MESSAGE_FD_COUNT) {dbgln("TransportMachPort: Rejecting message with {} attachments exceeding limit {}", message->attachments.size(), MAX_MESSAGE_FD_COUNT);mark_peer_eof();- return;+ return IterationDecision::Break;}if (message->bytes.is_empty() && message->attachments.is_empty())- return;+ return IterationDecision::Continue;notify_incoming_message_observer(message->bytes.bytes());@@ -614,6 +682,7 @@ void TransportMachPort::process_received_message(u8* buffer)m_incoming_cv.signal();if (should_write_notification)write_read_notification_byte();+ return IterationDecision::Continue;}void TransportMachPort::set_incoming_message_observer(Function<void(ReadonlyBytes)> observer)diff --git a/Libraries/LibIPC/TransportMachPort.h b/Libraries/LibIPC/TransportMachPort.hindex 25dccc9abbb..ea92c955898 100644--- a/Libraries/LibIPC/TransportMachPort.h+++ b/Libraries/LibIPC/TransportMachPort.h@@ -14,6 +14,7 @@#include <AK/Atomic.h>#include <AK/ConditionVariable.h>+#include <AK/IterationDecision.h>#include <AK/Mutex.h>#include <AK/Optional.h>#include <AK/Queue.h>@@ -124,7 +125,8 @@ private:WouldBlock,};SendOutcome send_mach_message(PendingMessage&);- void process_received_message(u8* buffer);+ // Breaks for a message the peer had no business sending, after which the transport takes nothing more from it.+ IterationDecision process_received_message(u8* buffer);Core::MachPort m_receive_port;Core::MachPort m_send_port;diff --git a/Tests/LibIPC/TestTransportMachPort.cpp b/Tests/LibIPC/TestTransportMachPort.cppindex e59fa11e8d0..1e4c74da04f 100644--- a/Tests/LibIPC/TestTransportMachPort.cpp+++ b/Tests/LibIPC/TestTransportMachPort.cpp@@ -464,3 +464,225 @@ TEST_CASE(transport_handle_with_a_dead_receive_right_is_a_decode_error)return attachments;}).is_error());}++// The transport's own data message ids, for building messages no transport would send.+static constexpr mach_msg_id_t IPC_DATA_MESSAGE_ID = 0x4950C001;+static constexpr mach_msg_id_t IPC_INLINE_DATA_MESSAGE_ID = 0x4950C002;++// A transport that this test also holds a send right to, so it can send it raw Mach messages.+struct TransportWithRawSender {+ NonnullOwnPtr<IPC::TransportMachPort> transport;+ Core::MachPort send_right;+ Core::MachPort transport_peer_receive_right;+};++static TransportWithRawSender make_transport_with_raw_sender()+{+ auto receive_right = MUST(Core::MachPort::create_with_right(Core::MachPort::PortRight::Receive));+ auto send_right = MUST(receive_right.insert_right(Core::MachPort::MessageRight::MakeSend));+ auto peer_receive_right = MUST(Core::MachPort::create_with_right(Core::MachPort::PortRight::Receive));+ auto send_right_to_peer = MUST(peer_receive_right.insert_right(Core::MachPort::MessageRight::MakeSend));+ auto transport = make<IPC::TransportMachPort>(move(receive_right), move(send_right_to_peer));+ return { move(transport), move(send_right), move(peer_receive_right) };+}++template<typename T>+static void append_bytes_of(Vector<u8>& bytes, T const& value)+{+ bytes.append(reinterpret_cast<u8 const*>(&value), sizeof(T));+}++static void append_inline_payload(Vector<u8>& bytes, u32 claimed_size, ReadonlyBytes payload)+{+ append_bytes_of(bytes, claimed_size);+ bytes.append(payload.data(), payload.size());+}++static mach_msg_port_descriptor_t port_descriptor(mach_port_t name)+{+ mach_msg_port_descriptor_t descriptor {};+ descriptor.name = name;+ descriptor.disposition = MACH_MSG_TYPE_COPY_SEND;+ descriptor.type = MACH_MSG_PORT_DESCRIPTOR;+ return descriptor;+}++static mach_msg_ool_descriptor_t out_of_line_descriptor(ReadonlyBytes payload)+{+ mach_msg_ool_descriptor_t descriptor {};+ descriptor.address = const_cast<u8*>(payload.data());+ descriptor.size = payload.size();+ descriptor.copy = MACH_MSG_VIRTUAL_COPY;+ descriptor.deallocate = false;+ descriptor.type = MACH_MSG_OOL_DESCRIPTOR;+ return descriptor;+}++static void send_raw_message(Core::MachPort const& destination, mach_msg_id_t id, bool is_complex, ReadonlyBytes contents)+{+ Vector<u8> message;+ message.resize(sizeof(mach_msg_header_t));+ message.append(contents.data(), contents.size());+ while (message.size() % sizeof(natural_t) != 0)+ message.append(0);++ auto* header = reinterpret_cast<mach_msg_header_t*>(message.data());+ header->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | (is_complex ? MACH_MSGH_BITS_COMPLEX : 0);+ header->msgh_size = message.size();+ header->msgh_remote_port = destination.port();+ header->msgh_local_port = MACH_PORT_NULL;+ header->msgh_id = id;+ auto result = mach_msg(header, MACH_SEND_MSG, message.size(), 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);+ VERIFY(result == KERN_SUCCESS);+}++static void expect_disconnected_without_a_message(IPC::TransportMachPort& transport)+{+ size_t received = 0;+ auto should_shutdown = IPC::TransportMachPort::ShouldShutdown::No;+ while (should_shutdown == IPC::TransportMachPort::ShouldShutdown::No) {+ transport.wait_until_readable();+ should_shutdown = transport.read_as_many_messages_as_possible_without_blocking([&](auto&&) { ++received; });+ }+ EXPECT_EQ(received, 0uz);+ EXPECT(!transport.is_open());+}++TEST_CASE(raw_message_built_like_the_transport_builds_one_arrives)+{+ auto raw = make_transport_with_raw_sender();+ Vector<u8> contents;+ append_bytes_of(contents, mach_msg_body_t { 1 });+ append_bytes_of(contents, port_descriptor(raw.send_right.port()));+ append_inline_payload(contents, 1, Array<u8, 1> { 42 });+ send_raw_message(raw.send_right, IPC_INLINE_DATA_MESSAGE_ID, true, contents);++ size_t received = 0;+ raw.transport->wait_until_readable();+ auto should_shutdown = raw.transport->read_as_many_messages_as_possible_without_blocking([&](IPC::TransportMachPort::Message&& message) {+ ++received;+ EXPECT_EQ(message.bytes.bytes().size(), 1uz);+ EXPECT_EQ(message.bytes.bytes()[0], 42u);+ EXPECT_EQ(message.attachments.size(), 1uz);+ });+ EXPECT_EQ(received, 1uz);+ EXPECT_EQ(should_shutdown, IPC::TransportMachPort::ShouldShutdown::No);+}++TEST_CASE(message_with_an_unknown_id_disconnects_the_peer)+{+ auto raw = make_transport_with_raw_sender();+ Vector<u8> contents;+ append_inline_payload(contents, 1, Array<u8, 1> { 42 });+ send_raw_message(raw.send_right, 0x1234, false, contents);+ expect_disconnected_without_a_message(*raw.transport);+}++TEST_CASE(inline_message_claiming_more_payload_than_it_has_disconnects_the_peer)+{+ auto raw = make_transport_with_raw_sender();+ Vector<u8> contents;+ append_inline_payload(contents, 1000, Array<u8, 4> { 1, 2, 3, 4 });+ send_raw_message(raw.send_right, IPC_INLINE_DATA_MESSAGE_ID, false, contents);+ expect_disconnected_without_a_message(*raw.transport);+}++TEST_CASE(inline_message_without_a_payload_header_disconnects_the_peer)+{+ auto raw = make_transport_with_raw_sender();+ send_raw_message(raw.send_right, IPC_INLINE_DATA_MESSAGE_ID, false, {});+ expect_disconnected_without_a_message(*raw.transport);+}++TEST_CASE(message_with_a_null_port_disconnects_the_peer)+{+ auto raw = make_transport_with_raw_sender();+ Vector<u8> contents;+ append_bytes_of(contents, mach_msg_body_t { 1 });+ append_bytes_of(contents, port_descriptor(MACH_PORT_NULL));+ append_inline_payload(contents, 1, Array<u8, 1> { 42 });+ send_raw_message(raw.send_right, IPC_INLINE_DATA_MESSAGE_ID, true, contents);+ expect_disconnected_without_a_message(*raw.transport);+}++TEST_CASE(inline_message_with_out_of_line_memory_disconnects_the_peer)+{+ auto raw = make_transport_with_raw_sender();+ Array<u8, 4> memory { 1, 2, 3, 4 };+ Vector<u8> contents;+ append_bytes_of(contents, mach_msg_body_t { 1 });+ append_bytes_of(contents, out_of_line_descriptor(memory));+ append_inline_payload(contents, 1, Array<u8, 1> { 42 });+ send_raw_message(raw.send_right, IPC_INLINE_DATA_MESSAGE_ID, true, contents);+ expect_disconnected_without_a_message(*raw.transport);+}++TEST_CASE(out_of_line_message_without_descriptors_disconnects_the_peer)+{+ auto raw = make_transport_with_raw_sender();+ Vector<u8> contents;+ append_inline_payload(contents, 1, Array<u8, 1> { 42 });+ send_raw_message(raw.send_right, IPC_DATA_MESSAGE_ID, false, contents);+ expect_disconnected_without_a_message(*raw.transport);+}++TEST_CASE(out_of_line_message_without_its_payload_descriptor_disconnects_the_peer)+{+ auto raw = make_transport_with_raw_sender();+ Vector<u8> contents;+ append_bytes_of(contents, mach_msg_body_t { 1 });+ append_bytes_of(contents, port_descriptor(raw.send_right.port()));+ send_raw_message(raw.send_right, IPC_DATA_MESSAGE_ID, true, contents);+ expect_disconnected_without_a_message(*raw.transport);+}++TEST_CASE(transport_handle_whose_receive_right_was_sent_dead_is_a_decode_error)+{+ auto raw = make_transport_with_raw_sender();+ auto pair = MUST(IPC::TransportHandle::create_pair());+ IPC::MessageBuffer buffer;+ IPC::Encoder encoder(buffer);+ MUST(encoder.encode(pair.first));+ auto handle_bytes = buffer.take_data();++ auto receive_right_descriptor = port_descriptor(MACH_PORT_DEAD);+ receive_right_descriptor.disposition = MACH_MSG_TYPE_MOVE_RECEIVE;+ Vector<u8> contents;+ append_bytes_of(contents, mach_msg_body_t { 2 });+ append_bytes_of(contents, receive_right_descriptor);+ append_bytes_of(contents, port_descriptor(raw.send_right.port()));+ append_inline_payload(contents, handle_bytes.size(), handle_bytes.span());+ send_raw_message(raw.send_right, IPC_INLINE_DATA_MESSAGE_ID, true, contents);++ size_t received = 0;+ raw.transport->wait_until_readable();+ (void)raw.transport->read_as_many_messages_as_possible_without_blocking([&](IPC::TransportMachPort::Message&& message) {+ ++received;+ FixedMemoryStream stream { message.bytes.bytes() };+ IPC::Decoder decoder { stream, message.attachments };+ EXPECT(decoder.decode<IPC::TransportHandle>().is_error());+ });+ EXPECT_EQ(received, 1uz);+}++TEST_CASE(nothing_after_a_malformed_message_arrives)+{+ auto raw = make_transport_with_raw_sender();+ for (u8 value : { 1, 2 }) {+ Vector<u8> contents;+ append_inline_payload(contents, 1, Array<u8, 1> { value });+ send_raw_message(raw.send_right, IPC_INLINE_DATA_MESSAGE_ID, false, contents);+ if (value == 1)+ send_raw_message(raw.send_right, 0x1234, false, contents);+ }++ Vector<u8> received;+ auto should_shutdown = IPC::TransportMachPort::ShouldShutdown::No;+ while (should_shutdown == IPC::TransportMachPort::ShouldShutdown::No) {+ raw.transport->wait_until_readable();+ should_shutdown = raw.transport->read_as_many_messages_as_possible_without_blocking([&](IPC::TransportMachPort::Message&& message) {+ received.append(message.bytes.bytes()[0]);+ });+ }+ EXPECT_EQ(received, (Vector<u8> { 1 }));+}