Truly independent web browser
LibIPC: Bound decoded sizes by the rest of the message
Tim Ledbettera671dc5c37a1
diff --git a/Libraries/LibIPC/Decoder.cpp b/Libraries/LibIPC/Decoder.cppindex e81a4e406e3..a693632dd3e 100644--- a/Libraries/LibIPC/Decoder.cpp+++ b/Libraries/LibIPC/Decoder.cpp@@ -20,15 +20,13 @@namespace IPC {-// Maximum size for decoded containers (strings, buffers, vectors, etc.)-// This prevents a malicious peer from claiming huge sizes to cause OOM.-static constexpr size_t MAX_DECODED_SIZE = 64 * MiB;-ErrorOr<size_t> Decoder::decode_size(){auto size = static_cast<size_t>(TRY(decode<u32>()));- if (size > MAX_DECODED_SIZE)- return Error::from_string_literal("IPC decode: Size exceeds maximum allowed");++ // Every element of a string or container takes at least one byte or one attachment of the message.+ if (size > m_stream.remaining() + m_attachments.size())+ return Error::from_string_literal("IPC decode: Size exceeds the rest of the message");return size;}@@ -183,8 +181,8 @@ ErrorOr<Core::AnonymousBuffer> decode(Decoder& decoder)if (auto valid = TRY(decoder.decode<bool>()); !valid)return Core::AnonymousBuffer {};- // We don't use decode_size() here since AnonymousBuffer is backed by shared memory, not heap allocation. The- // MAX_DECODED_SIZE limit doesn't apply because the memory is already allocated by the sender.+ // We don't use decode_size() here since AnonymousBuffer is backed by shared memory, not heap allocation. Its+ // contents are not part of the message.auto encoded_size = TRY(decoder.decode<u64>());if (!AK::is_within_range<size_t>(encoded_size))return Error::from_string_literal("Anonymous buffer size does not fit on this platform");diff --git a/Libraries/LibIPC/Decoder.h b/Libraries/LibIPC/Decoder.hindex 389209d825a..796ed337ff8 100644--- a/Libraries/LibIPC/Decoder.h+++ b/Libraries/LibIPC/Decoder.h@@ -10,9 +10,9 @@#include <AK/ByteString.h>#include <AK/Concepts.h>#include <AK/Forward.h>+#include <AK/MemoryStream.h>#include <AK/Queue.h>#include <AK/StdLibExtras.h>-#include <AK/Stream.h>#include <AK/String.h>#include <AK/Time.h>#include <AK/Try.h>@@ -39,7 +39,7 @@ inline ErrorOr<T> decode(Decoder&)class Decoder {public:- Decoder(Stream& stream, Queue<Attachment>& attachments)+ Decoder(FixedMemoryStream& stream, Queue<Attachment>& attachments): m_stream(stream), m_attachments(attachments){@@ -63,11 +63,11 @@ public:ErrorOr<size_t> decode_size();- Stream& stream() { return m_stream; }+ FixedMemoryStream& stream() { return m_stream; }Queue<Attachment>& attachments() { return m_attachments; }private:- Stream& m_stream;+ FixedMemoryStream& m_stream;Queue<Attachment>& m_attachments;};diff --git a/Meta/Generators/generate_ipc_definitions.py b/Meta/Generators/generate_ipc_definitions.pyindex d25beebb93c..17c529c2419 100644--- a/Meta/Generators/generate_ipc_definitions.py+++ b/Meta/Generators/generate_ipc_definitions.py@@ -434,7 +434,7 @@ public:""")static i32 static_message_id() {{ return (int)MessageID::{pascal_name}; }}virtual StringView message_name() const override {{ return "{endpoint.name}::{pascal_name}"sv; }}- static ErrorOr<NonnullOwnPtr<{pascal_name}>> decode(Stream& stream, Queue<IPC::Attachment>& attachments)+ static ErrorOr<NonnullOwnPtr<{pascal_name}>> decode(FixedMemoryStream& stream, Queue<IPC::Attachment>& attachments){{IPC::Decoder decoder {{ stream, attachments }};""")diff --git a/Tests/LibIPC/TestDecoder.cpp b/Tests/LibIPC/TestDecoder.cppindex 0c2599131ea..68f819a4494 100644--- a/Tests/LibIPC/TestDecoder.cpp+++ b/Tests/LibIPC/TestDecoder.cpp@@ -4,13 +4,16 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/ByteBuffer.h>#include <AK/MemoryStream.h>+#include <AK/NumericLimits.h>#include <AK/Queue.h>#include <AK/Vector.h>#include <LibCore/AnonymousBuffer.h>#include <LibCore/System.h>#include <LibIPC/Attachment.h>#include <LibIPC/Decoder.h>+#include <LibIPC/File.h>#include <LibTest/TestCase.h>template<typename T>@@ -44,6 +47,56 @@ TEST_CASE(decode_vector_of_bool)EXPECT(decode_bytes<Vector<bool>>(vector_bytes({ 1, 2, 1 })).is_error());}+static Vector<u8> bytes_with_size_prefix(u32 size, ReadonlyBytes contents)+{+ Vector<u8> bytes;+ bytes.append(reinterpret_cast<u8 const*>(&size), sizeof(size));+ bytes.append(contents.data(), contents.size());+ return bytes;+}++TEST_CASE(decode_size_refuses_a_size_larger_than_the_rest_of_the_message)+{+ auto contents = "abcd"sv.bytes();++ EXPECT_EQ(MUST(decode_bytes<ByteString>(bytes_with_size_prefix(4, contents))), "abcd"sv);+ EXPECT(decode_bytes<ByteString>(bytes_with_size_prefix(5, contents)).is_error());++ EXPECT(decode_bytes<Vector<String>>(bytes_with_size_prefix(5, contents)).is_error());+ EXPECT(decode_bytes<Vector<String>>(bytes_with_size_prefix(NumericLimits<u32>::max(), contents)).is_error());+}++#if !defined(AK_OS_WINDOWS)+TEST_CASE(decode_size_counts_elements_that_are_only_attachments)+{+ auto decode_files = [](u32 claimed_size, size_t attachment_count) {+ auto bytes = bytes_with_size_prefix(claimed_size, {});+ FixedMemoryStream stream { bytes.span() };+ Queue<IPC::Attachment> attachments;+ for (size_t i = 0; i < attachment_count; ++i)+ attachments.enqueue(IPC::Attachment::from_fd(MUST(Core::System::open("/dev/null"sv, O_RDONLY))));+ IPC::Decoder decoder { stream, attachments };+ return decoder.decode<Vector<IPC::File>>();+ };++ EXPECT_EQ(MUST(decode_files(2, 2)).size(), 2u);+ EXPECT(decode_files(3, 2).is_error());+}+#endif++TEST_CASE(decode_byte_buffer_larger_than_64_mib)+{+ static constexpr size_t size = 65 * MiB;+ auto contents = MUST(ByteBuffer::create_uninitialized(size));+ contents.bytes().fill('A');+ contents[size - 1] = 'B';++ auto buffer = MUST(decode_bytes<ByteBuffer>(bytes_with_size_prefix(size, contents)));+ EXPECT_EQ(buffer.size(), size);+ EXPECT_EQ(buffer[0], static_cast<u8>('A'));+ EXPECT_EQ(buffer[size - 1], static_cast<u8>('B'));+}+#if !defined(AK_OS_WINDOWS)TEST_CASE(decode_anonymous_buffer_refuses_a_size_larger_than_its_backing){