Skip to content

Truly independent web browser

LibIPC: Fail to decode a transport handle that lacks its port rights

sideshowbarkerf51167f002db
diff --git a/Libraries/LibIPC/TransportHandle.cpp b/Libraries/LibIPC/TransportHandle.cpp
index 50fbbfed9af..8bfa8f2c0d1 100644
--- a/Libraries/LibIPC/TransportHandle.cpp
+++ b/Libraries/LibIPC/TransportHandle.cpp
@@ -94,14 +94,23 @@ ErrorOr<void> encode(Encoder& encoder, TransportHandle const& handle)
template<>
ErrorOr<TransportHandle> decode(Decoder& decoder)
{
+ // The attachments come from the peer, so a message that lacks them, or carries other rights in their place, is
+ // a malformed message to reject, not an invariant of ours to assert.
auto& attachments = decoder.attachments();
- VERIFY(attachments.size() >= 2);
- auto recv_attachment = attachments.dequeue();
- auto send_attachment = attachments.dequeue();
- VERIFY(recv_attachment.message_right() == Core::MachPort::MessageRight::MoveReceive);
- VERIFY(send_attachment.message_right() == Core::MachPort::MessageRight::MoveSend);
+ auto recv_attachment = TRY(attachments.try_dequeue());
+ auto send_attachment = TRY(attachments.try_dequeue());
+ if (recv_attachment.message_right() != Core::MachPort::MessageRight::MoveReceive)
+ return Error::from_string_literal("TransportHandle::decode: Expected a receive right");
+ if (send_attachment.message_right() != Core::MachPort::MessageRight::MoveSend)
+ return Error::from_string_literal("TransportHandle::decode: Expected a send right");
auto receive_right = recv_attachment.release_mach_port();
auto send_right = send_attachment.release_mach_port();
+ // The kernel passes a dead name along with whatever disposition the sender gave it, but a receive right can't have
+ // died in flight the way a send right can.
+ if (!MACH_PORT_VALID(receive_right.port()))
+ return Error::from_string_literal("TransportHandle::decode: Expected a live receive right");
+ if (send_right.port() == MACH_PORT_NULL)
+ return Error::from_string_literal("TransportHandle::decode: Expected a send right");
TransportHandle handle { move(receive_right), move(send_right) };
TRY(decode_unprocessed(decoder, handle.m_unprocessed_bytes, handle.m_unprocessed_attachments));
return handle;
diff --git a/Tests/LibIPC/TestTransportMachPort.cpp b/Tests/LibIPC/TestTransportMachPort.cpp
index 8daa8ae06a4..e59fa11e8d0 100644
--- a/Tests/LibIPC/TestTransportMachPort.cpp
+++ b/Tests/LibIPC/TestTransportMachPort.cpp
@@ -416,3 +416,51 @@ TEST_CASE(a_flood_of_out_of_line_messages_in_both_directions_does_not_deadlock)
EXPECT_EQ(received_by_peer, message_count);
EXPECT_EQ(received_by_local, message_count);
}
+
+// Decodes a transport handle from the bytes an encoded one has, with the given attachments in place of its own.
+static ErrorOr<IPC::TransportHandle> decode_transport_handle_with(Vector<IPC::Attachment> (*make_attachments)(Vector<IPC::Attachment>))
+{
+ auto pair = MUST(IPC::TransportHandle::create_pair());
+ IPC::MessageBuffer buffer;
+ IPC::Encoder encoder(buffer);
+ MUST(encoder.encode(pair.first));
+ auto bytes = buffer.take_data();
+
+ Queue<IPC::Attachment> attachments;
+ for (auto& attachment : make_attachments(buffer.take_attachments()))
+ attachments.enqueue(move(attachment));
+ FixedMemoryStream stream { bytes.span() };
+ IPC::Decoder decoder { stream, attachments };
+ return decoder.decode<IPC::TransportHandle>();
+}
+
+TEST_CASE(transport_handle_decodes_from_its_own_attachments)
+{
+ auto handle = decode_transport_handle_with([](Vector<IPC::Attachment> attachments) { return attachments; });
+ EXPECT(!handle.is_error());
+}
+
+TEST_CASE(transport_handle_with_missing_attachments_is_a_decode_error)
+{
+ EXPECT(decode_transport_handle_with([](Vector<IPC::Attachment>) { return Vector<IPC::Attachment> {}; }).is_error());
+ EXPECT(decode_transport_handle_with([](Vector<IPC::Attachment> attachments) {
+ attachments.take_last();
+ return attachments;
+ }).is_error());
+}
+
+TEST_CASE(transport_handle_with_the_wrong_rights_is_a_decode_error)
+{
+ EXPECT(decode_transport_handle_with([](Vector<IPC::Attachment> attachments) {
+ swap(attachments[0], attachments[1]);
+ return attachments;
+ }).is_error());
+}
+
+TEST_CASE(transport_handle_with_a_dead_receive_right_is_a_decode_error)
+{
+ EXPECT(decode_transport_handle_with([](Vector<IPC::Attachment> attachments) {
+ attachments[0] = IPC::Attachment::from_mach_port(Core::MachPort::adopt_right(MACH_PORT_DEAD, Core::MachPort::PortRight::Receive), Core::MachPort::MessageRight::MoveReceive);
+ return attachments;
+ }).is_error());
+}