Skip to content

Truly independent web browser

LibCore: Seal received anonymous buffers against shrinking

Zaggy1024875210baadb3
diff --git a/Libraries/LibCompositing/DisplayList/DisplayList.cpp b/Libraries/LibCompositing/DisplayList/DisplayList.cpp
index 41832698bb9..2c2269dc572 100644
--- a/Libraries/LibCompositing/DisplayList/DisplayList.cpp
+++ b/Libraries/LibCompositing/DisplayList/DisplayList.cpp
@@ -138,7 +138,7 @@ ErrorOr<Core::AnonymousBuffer> DisplayList::copy_to_shared_buffer() const
if (size == 0)
return Core::AnonymousBuffer {};
// Sealed where the platform supports it, so the receiver cannot be faulted by the file shrinking.
- auto buffer = TRY(Core::AnonymousBuffer::create_with_size(size, Core::AnonymousBuffer::Sealability::Sealable));
+ auto buffer = TRY(Core::AnonymousBuffer::create_with_size(size));
RustFFI::display_list_storage_write_packed(m_storage, buffer.data<u8>());
return buffer;
}
diff --git a/Libraries/LibCore/AnonymousBuffer.cpp b/Libraries/LibCore/AnonymousBuffer.cpp
index 4e423beaf84..f2cd34ebc47 100644
--- a/Libraries/LibCore/AnonymousBuffer.cpp
+++ b/Libraries/LibCore/AnonymousBuffer.cpp
@@ -18,12 +18,9 @@
namespace Core {
-ErrorOr<AnonymousBuffer> AnonymousBuffer::create_with_size(size_t size, Sealability sealability)
+ErrorOr<AnonymousBuffer> AnonymousBuffer::create_with_size(size_t size)
{
- auto allow_sealing = sealability == Sealability::Sealable
- ? Core::System::AllowSealing::Yes
- : Core::System::AllowSealing::No;
- auto fd = TRY(Core::System::anon_create(size, O_CLOEXEC, allow_sealing));
+ auto fd = TRY(Core::System::anon_create(size, O_CLOEXEC, Core::System::AllowSealing::Yes));
return create_from_anon_fd(fd, size);
}
@@ -33,12 +30,12 @@ ErrorOr<AnonymousBuffer> AnonymousBuffer::create_from_anon_fd(int fd, size_t siz
return AnonymousBuffer(move(impl));
}
-ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot(Sealability sealability) const
+ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot() const
{
- return snapshot(0, size(), sealability);
+ return snapshot(0, size());
}
-ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot(size_t offset, size_t size, Sealability sealability) const
+ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot(size_t offset, size_t size) const
{
if (!is_valid())
return Error::from_string_literal("Cannot snapshot an invalid anonymous buffer");
@@ -63,7 +60,7 @@ ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot(size_t offset, size_t size, S
TRY(validate_backing_size());
- auto copy = TRY(create_with_size(size, sealability));
+ auto copy = TRY(create_with_size(size));
if (size == 0)
return copy;
@@ -110,6 +107,13 @@ ErrorOr<void> AnonymousBuffer::validate_backing_size() const
if (!is_valid())
return Error::from_string_literal("Cannot validate the backing store of an invalid anonymous buffer");
+#if defined(AK_OS_LINUX)
+ // Sealed first, or the holder could shrink it after the check.
+ auto seals = TRY(Core::System::fcntl(fd(), F_GET_SEALS, static_cast<uintptr_t>(0)));
+ if (!(seals & F_SEAL_SHRINK))
+ TRY(Core::System::fcntl(fd(), F_ADD_SEALS, F_SEAL_SHRINK));
+#endif
+
auto file_status = TRY(Core::System::fstat(fd()));
if (file_status.st_size < 0 || static_cast<u64>(file_status.st_size) < size())
return Error::from_string_literal("Anonymous buffer is smaller than its claimed size");
diff --git a/Libraries/LibCore/AnonymousBuffer.h b/Libraries/LibCore/AnonymousBuffer.h
index 13d1f9104b6..09dfe9b5d9f 100644
--- a/Libraries/LibCore/AnonymousBuffer.h
+++ b/Libraries/LibCore/AnonymousBuffer.h
@@ -37,23 +37,19 @@ private:
class CORE_API AnonymousBuffer {
public:
- enum class Sealability {
- Unsealable,
- Sealable,
- };
-
- static ErrorOr<AnonymousBuffer> create_with_size(size_t, Sealability = Sealability::Unsealable);
+ static ErrorOr<AnonymousBuffer> create_with_size(size_t);
static ErrorOr<AnonymousBuffer> create_from_anon_fd(int fd, size_t);
AnonymousBuffer() = default;
bool is_valid() const { return m_impl; }
- ErrorOr<AnonymousBuffer> snapshot(Sealability = Sealability::Unsealable) const;
- ErrorOr<AnonymousBuffer> snapshot(size_t offset, size_t size, Sealability = Sealability::Unsealable) const;
+ ErrorOr<AnonymousBuffer> snapshot() const;
+ ErrorOr<AnonymousBuffer> snapshot(size_t offset, size_t size) const;
// The size of a received buffer is whatever the peer claimed in the message. If the backing store is
- // smaller than that, reading the mapping past its end faults, so check before trusting the size.
+ // smaller than that, reading the mapping past its end faults, so check before trusting the size. This also seals
+ // the backing store against shrinking where the peer could still shrink it.
ErrorOr<void> validate_backing_size() const;
int fd() const { return m_impl ? m_impl->fd() : -1; }
diff --git a/Libraries/LibCore/AnonymousBufferWindows.cpp b/Libraries/LibCore/AnonymousBufferWindows.cpp
index a05f551e0fe..69a2a0e6697 100644
--- a/Libraries/LibCore/AnonymousBufferWindows.cpp
+++ b/Libraries/LibCore/AnonymousBufferWindows.cpp
@@ -55,9 +55,8 @@ ErrorOr<NonnullRefPtr<AnonymousBufferImpl>> AnonymousBufferImpl::create(int fd,
return adopt_ref(*new AnonymousBufferImpl(fd, size, ptr));
}
-ErrorOr<AnonymousBuffer> AnonymousBuffer::create_with_size(size_t size, Sealability)
+ErrorOr<AnonymousBuffer> AnonymousBuffer::create_with_size(size_t size)
{
- // FIXME: Support sealability on Windows.
auto impl = TRY(AnonymousBufferImpl::create(size));
return AnonymousBuffer(move(impl));
}
@@ -68,12 +67,12 @@ ErrorOr<AnonymousBuffer> AnonymousBuffer::create_from_anon_fd(int fd, size_t siz
return AnonymousBuffer(move(impl));
}
-ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot(Sealability sealability) const
+ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot() const
{
- return snapshot(0, size(), sealability);
+ return snapshot(0, size());
}
-ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot(size_t offset, size_t size, Sealability sealability) const
+ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot(size_t offset, size_t size) const
{
if (!is_valid())
return Error::from_string_literal("Cannot snapshot an invalid anonymous buffer");
@@ -81,7 +80,7 @@ ErrorOr<AnonymousBuffer> AnonymousBuffer::snapshot(size_t offset, size_t size, S
if (offset > this->size() || size > this->size() - offset)
return Error::from_string_literal("Anonymous buffer snapshot range is out of bounds");
- auto copy = TRY(create_with_size(size, sealability));
+ auto copy = TRY(create_with_size(size));
bytes().slice(offset, size).copy_to({ copy.data<u8>(), copy.size() });
return copy;
}
diff --git a/Libraries/LibCore/SharedCircularQueue.h b/Libraries/LibCore/SharedCircularQueue.h
index f77bb1da3df..c12868bcdb8 100644
--- a/Libraries/LibCore/SharedCircularQueue.h
+++ b/Libraries/LibCore/SharedCircularQueue.h
@@ -73,6 +73,7 @@ public:
static ErrorOr<SharedSingleProducerCircularQueue> create(int fd)
{
auto buffer = TRY(AnonymousBuffer::create_from_anon_fd(fd, sizeof(Data)));
+ TRY(buffer.validate_backing_size());
auto* data = reinterpret_cast<Data*>(buffer.data<void>());
return create_internal(move(buffer), data);
}
diff --git a/Libraries/LibGC/CAPI.cpp b/Libraries/LibGC/CAPI.cpp
index ba43f9d4fe1..95ea1a85945 100644
--- a/Libraries/LibGC/CAPI.cpp
+++ b/Libraries/LibGC/CAPI.cpp
@@ -504,7 +504,7 @@ uint8_t* gc_primitive_storage_data(GCPrimitiveStorageHandle handle)
bool gc_shared_memory_create(size_t size, int* out_fd)
{
*out_fd = -1;
- auto buffer = Core::AnonymousBuffer::create_with_size(size, Core::AnonymousBuffer::Sealability::Sealable);
+ auto buffer = Core::AnonymousBuffer::create_with_size(size);
if (buffer.is_error())
return false;
// NB: The buffer closes its descriptor and unmaps its own view of the memory when it goes away; the caller gets a
diff --git a/Libraries/LibWeb/HTML/StructuredSerialize.cpp b/Libraries/LibWeb/HTML/StructuredSerialize.cpp
index e76c9416453..b67350901e3 100644
--- a/Libraries/LibWeb/HTML/StructuredSerialize.cpp
+++ b/Libraries/LibWeb/HTML/StructuredSerialize.cpp
@@ -2268,7 +2268,7 @@ WebIDL::ExceptionOr<SerializedTransferRecord> structured_serialize_with_transfer
image_bitmap_pixels_size += ImageBitmap::shared_pixels_size(*image_bitmap->bitmap());
}
if (!image_bitmap_pixels_size.has_overflow() && image_bitmap_pixels_size.value() > 0) {
- if (auto buffer = Core::AnonymousBuffer::create_with_size(image_bitmap_pixels_size.value(), Core::AnonymousBuffer::Sealability::Sealable); !buffer.is_error()) {
+ if (auto buffer = Core::AnonymousBuffer::create_with_size(image_bitmap_pixels_size.value()); !buffer.is_error()) {
image_bitmap_pixels = ImageBitmapSharedPixels { .buffer = buffer.release_value(), .buffer_index = static_cast<u32>(serialized_writer.shared_buffers().size()) };
serialized_writer.shared_buffers().append(image_bitmap_pixels->buffer);
}
diff --git a/Libraries/LibWebView/FontService.cpp b/Libraries/LibWebView/FontService.cpp
index d089e3c1c9c..131422cba8e 100644
--- a/Libraries/LibWebView/FontService.cpp
+++ b/Libraries/LibWebView/FontService.cpp
@@ -163,7 +163,7 @@ ErrorOr<IPC::File> FontService::create_immutable_font_data(ReadonlyBytes bytes)
return Error::from_errno(errno);
return file;
#elif defined(AK_OS_WINDOWS)
- auto buffer = TRY(Core::AnonymousBuffer::create_with_size(bytes.size(), Core::AnonymousBuffer::Sealability::Sealable));
+ auto buffer = TRY(Core::AnonymousBuffer::create_with_size(bytes.size()));
bytes.copy_to({ buffer.data<u8>(), buffer.size() });
return IPC::File::clone_fd(buffer.fd());
#else
diff --git a/Libraries/LibWebView/GamepadManager.cpp b/Libraries/LibWebView/GamepadManager.cpp
index e3f2ced2dae..c40f1ab1a7a 100644
--- a/Libraries/LibWebView/GamepadManager.cpp
+++ b/Libraries/LibWebView/GamepadManager.cpp
@@ -170,7 +170,7 @@ GamepadManager::Consumer* GamepadManager::ensure_consumer(WebContentClient& clie
if (auto existing_consumer = m_consumers.get(&client); existing_consumer.has_value())
return *existing_consumer;
- auto state_buffer = Core::AnonymousBuffer::create_with_size(sizeof(Web::Gamepad::SharedGamepadStateBuffer), Core::AnonymousBuffer::Sealability::Sealable);
+ auto state_buffer = Core::AnonymousBuffer::create_with_size(sizeof(Web::Gamepad::SharedGamepadStateBuffer));
if (state_buffer.is_error()) {
dbgln("Failed to allocate a shared gamepad state buffer: {}", state_buffer.error());
return nullptr;
diff --git a/Tests/LibCore/TestLibCoreAnonymousBuffer.cpp b/Tests/LibCore/TestLibCoreAnonymousBuffer.cpp
index f04e5571808..76a511e935b 100644
--- a/Tests/LibCore/TestLibCoreAnonymousBuffer.cpp
+++ b/Tests/LibCore/TestLibCoreAnonymousBuffer.cpp
@@ -19,6 +19,7 @@
#else
# include <errno.h>
# include <fcntl.h>
+# include <sys/mman.h>
# include <unistd.h>
#endif
@@ -167,7 +168,7 @@ TEST_CASE(validate_backing_size_rejects_an_invalid_buffer)
#if (defined(AK_OS_LINUX) || defined(AK_OS_FREEBSD)) && defined(F_ADD_SEALS) && defined(F_GET_SEALS) && defined(F_SEAL_GROW) && defined(F_SEAL_SHRINK)
TEST_CASE(create_sealable_buffer)
{
- auto buffer = MUST(Core::AnonymousBuffer::create_with_size(128, Core::AnonymousBuffer::Sealability::Sealable));
+ auto buffer = MUST(Core::AnonymousBuffer::create_with_size(128));
MUST(Core::System::fcntl(buffer.fd(), F_ADD_SEALS, F_SEAL_GROW | F_SEAL_SHRINK));
auto seals = MUST(Core::System::fcntl(buffer.fd(), F_GET_SEALS, static_cast<uintptr_t>(0)));
EXPECT_EQ(seals & (F_SEAL_GROW | F_SEAL_SHRINK), F_SEAL_GROW | F_SEAL_SHRINK);
@@ -175,7 +176,7 @@ TEST_CASE(create_sealable_buffer)
TEST_CASE(snapshot_rejects_a_buffer_larger_than_its_backing_store)
{
- auto original = MUST(Core::AnonymousBuffer::create_with_size(64, Core::AnonymousBuffer::Sealability::Sealable));
+ auto original = MUST(Core::AnonymousBuffer::create_with_size(64));
auto fd = MUST(Core::System::dup(original.fd()));
auto oversized = MUST(Core::AnonymousBuffer::create_from_anon_fd(fd, 128));
EXPECT(oversized.snapshot().is_error());
@@ -213,7 +214,7 @@ TEST_CASE(failed_creation_from_a_handle_closes_the_handle)
#ifdef AK_OS_LINUX
TEST_CASE(create_with_size_seals_immutable_size)
{
- auto buffer = MUST(Core::AnonymousBuffer::create_with_size(8192, Core::AnonymousBuffer::Sealability::Sealable));
+ auto buffer = MUST(Core::AnonymousBuffer::create_with_size(8192));
EXPECT(buffer.is_valid());
int seals = fcntl(buffer.fd(), F_GET_SEALS);
@@ -232,11 +233,30 @@ TEST_CASE(create_with_size_seals_immutable_size)
EXPECT_EQ(data[0], static_cast<u8>(0x5a));
}
-TEST_CASE(create_with_size_unsealed_by_default)
+static int create_unsealed_memfd(size_t size, unsigned flags)
{
- auto buffer = MUST(Core::AnonymousBuffer::create_with_size(8192));
- // Without the seal, the fd can still be resized.
- EXPECT(ftruncate(buffer.fd(), 4096) == 0);
+ auto fd = memfd_create("", MFD_CLOEXEC | flags);
+ VERIFY(fd >= 0);
+ VERIFY(ftruncate(fd, static_cast<off_t>(size)) == 0);
+ return fd;
+}
+
+TEST_CASE(validate_backing_size_seals_a_received_buffer)
+{
+ auto const page_size = static_cast<size_t>(PAGE_SIZE);
+ auto buffer = MUST(Core::AnonymousBuffer::create_from_anon_fd(create_unsealed_memfd(2 * page_size, MFD_ALLOW_SEALING), 2 * page_size));
+ EXPECT(!buffer.validate_backing_size().is_error());
+
+ // The sender still holds the fd, but can no longer pull the pages out from under the mapping.
+ errno = 0;
+ EXPECT(ftruncate(buffer.fd(), static_cast<off_t>(page_size)) < 0);
+ EXPECT_EQ(errno, EPERM);
+}
+
+TEST_CASE(validate_backing_size_rejects_a_buffer_that_cannot_be_sealed)
+{
+ auto buffer = MUST(Core::AnonymousBuffer::create_from_anon_fd(create_unsealed_memfd(4096, 0), 4096));
+ EXPECT(buffer.validate_backing_size().is_error());
}
// These truncation tests are Linux-only: macOS refuses to resize a shared memory object once its
@@ -244,7 +264,7 @@ TEST_CASE(create_with_size_unsealed_by_default)
TEST_CASE(validate_backing_size_rejects_a_truncated_backing_file)
{
auto const page_size = static_cast<size_t>(PAGE_SIZE);
- auto buffer = MUST(Core::AnonymousBuffer::create_with_size(2 * page_size));
+ auto buffer = MUST(Core::AnonymousBuffer::create_from_anon_fd(create_unsealed_memfd(2 * page_size, MFD_ALLOW_SEALING), 2 * page_size));
EXPECT_EQ(ftruncate(buffer.fd(), static_cast<off_t>(page_size)), 0);
// The mapping is deliberately never touched from here on: its second page no longer has backing.
EXPECT(buffer.validate_backing_size().is_error());
@@ -261,7 +281,7 @@ TEST_CASE(validate_backing_size_rejects_a_claim_larger_than_the_backing_file)
TEST_CASE(a_sealed_buffer_cannot_be_truncated)
{
auto const page_size = static_cast<size_t>(PAGE_SIZE);
- auto buffer = MUST(Core::AnonymousBuffer::create_with_size(2 * page_size, Core::AnonymousBuffer::Sealability::Sealable));
+ auto buffer = MUST(Core::AnonymousBuffer::create_with_size(2 * page_size));
EXPECT(ftruncate(buffer.fd(), static_cast<off_t>(page_size)) < 0);
EXPECT(!buffer.validate_backing_size().is_error());
}
diff --git a/Tests/LibIPC/TestDecoder.cpp b/Tests/LibIPC/TestDecoder.cpp
index fcb4a4ad914..6d6dc518096 100644
--- a/Tests/LibIPC/TestDecoder.cpp
+++ b/Tests/LibIPC/TestDecoder.cpp
@@ -104,7 +104,7 @@ TEST_CASE(decode_byte_buffer_larger_than_64_mib)
TEST_CASE(decode_anonymous_buffer_refuses_a_size_larger_than_its_backing)
{
auto decode_with_claimed_size = [](u64 claimed_size) {
- auto fd = MUST(Core::System::anon_create(4096, O_CLOEXEC, Core::System::AllowSealing::No));
+ auto fd = MUST(Core::System::anon_create(4096, O_CLOEXEC, Core::System::AllowSealing::Yes));
Vector<u8> bytes;
bytes.append(1);
@@ -124,6 +124,25 @@ TEST_CASE(decode_anonymous_buffer_refuses_a_size_larger_than_its_backing)
}
#endif
+#if defined(AK_OS_LINUX)
+TEST_CASE(decode_anonymous_buffer_refuses_a_backing_that_cannot_be_sealed)
+{
+ // Without a seal, the sender could shrink the backing after the size check passed.
+ auto fd = MUST(Core::System::anon_create(4096, O_CLOEXEC, Core::System::AllowSealing::No));
+ u64 claimed_size = 4096;
+
+ Vector<u8> bytes;
+ bytes.append(1);
+ bytes.append(reinterpret_cast<u8 const*>(&claimed_size), sizeof(claimed_size));
+
+ FixedMemoryStream stream { bytes.span() };
+ Queue<IPC::Attachment> attachments;
+ attachments.enqueue(IPC::Attachment::from_fd(fd));
+ IPC::Decoder decoder { stream, attachments };
+ EXPECT(decoder.decode<Core::AnonymousBuffer>().is_error());
+}
+#endif
+
TEST_CASE(sizes_are_bounded_before_contents_and_only_within_the_value)
{
IPC::MessageBuffer buffer;
diff --git a/Tests/LibWeb/TestStructuredSerializeFormat.cpp b/Tests/LibWeb/TestStructuredSerializeFormat.cpp
index 3980e02667e..9b0f061fe5d 100644
--- a/Tests/LibWeb/TestStructuredSerializeFormat.cpp
+++ b/Tests/LibWeb/TestStructuredSerializeFormat.cpp
@@ -864,7 +864,7 @@ TEST_CASE(image_bitmap_transfer_rejects_invalid_shared_pixels)
{
auto& realm = test_realm();
Vector<Core::AnonymousBuffer> shared_buffers;
- shared_buffers.append(MUST(Core::AnonymousBuffer::create_with_size(64, Core::AnonymousBuffer::Sealability::Sealable)));
+ shared_buffers.append(MUST(Core::AnonymousBuffer::create_with_size(64)));
auto receive = [&](Vector<Core::AnonymousBuffer> const* buffers, u32 buffer_index, u64 offset, Gfx::BitmapFormat format, int width) {
Web::HTML::TransferDataEncoder holder;
MUST(holder.encode(true));
@@ -938,7 +938,8 @@ TEST_CASE(image_bitmap_transfer_rejects_unsealed_backing)
auto& realm = test_realm();
// Even a correctly sized descriptor with sealing permanently disabled must be rejected.
Vector<Core::AnonymousBuffer> shared_buffers;
- shared_buffers.append(MUST(Core::AnonymousBuffer::create_with_size(64)));
+ auto unsealable_fd = MUST(Core::System::anon_create(64, O_CLOEXEC, Core::System::AllowSealing::No));
+ shared_buffers.append(MUST(Core::AnonymousBuffer::create_from_anon_fd(unsealable_fd, 64)));
Web::HTML::TransferDataEncoder holder;
MUST(holder.encode(true));
MUST(holder.encode(0u));
diff --git a/Tests/LibWebCommon/TestGamepadStateBuffer.cpp b/Tests/LibWebCommon/TestGamepadStateBuffer.cpp
index 8540a2aec3c..0296a310678 100644
--- a/Tests/LibWebCommon/TestGamepadStateBuffer.cpp
+++ b/Tests/LibWebCommon/TestGamepadStateBuffer.cpp
@@ -20,7 +20,7 @@ using namespace Web::Gamepad;
static Core::AnonymousBuffer create_producer_buffer()
{
- auto buffer = MUST(Core::AnonymousBuffer::create_with_size(sizeof(SharedGamepadStateBuffer), Core::AnonymousBuffer::Sealability::Sealable));
+ auto buffer = MUST(Core::AnonymousBuffer::create_with_size(sizeof(SharedGamepadStateBuffer)));
new (buffer.data<void>()) SharedGamepadStateBuffer {};
return buffer;
}