Skip to content

Truly independent web browser

LibGfx: Reject empty sizes when decoding a BitmapSequence

Shannon Booth5d35deceb38a
diff --git a/Libraries/LibGfx/BitmapSequence.cpp b/Libraries/LibGfx/BitmapSequence.cpp
index 47b4c0cad12..2195842d6ba 100644
--- a/Libraries/LibGfx/BitmapSequence.cpp
+++ b/Libraries/LibGfx/BitmapSequence.cpp
@@ -94,7 +94,7 @@ ErrorOr<Gfx::BitmapSequence> decode(Decoder& decoder)
return Error::from_string_literal("IPC: Invalid Gfx::BitmapSequence buffer data");
// NB: A buffer that is too small is turned down below. One larger than a shareable bitmap of this size would
// have is turned down here, as the receiver would keep all of it mapped for as long as the bitmap lives.
- if (Gfx::Bitmap::size_would_overflow(metadata->format, metadata->size))
+ if (metadata->size.is_empty() || Gfx::Bitmap::size_would_overflow(metadata->format, metadata->size))
return Error::from_string_literal("IPC: Invalid Gfx::BitmapSequence size");
auto data_size = Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(metadata->size.width(), metadata->format), metadata->size.height());
if (buffer.size() > round_up_to_power_of_two(data_size, PAGE_SIZE))
diff --git a/Tests/LibGfx/TestBitmapSequence.cpp b/Tests/LibGfx/TestBitmapSequence.cpp
index b11470d7745..ce503c9fca2 100644
--- a/Tests/LibGfx/TestBitmapSequence.cpp
+++ b/Tests/LibGfx/TestBitmapSequence.cpp
@@ -84,6 +84,25 @@ TEST_CASE(ipc_decode_rejects_oversized_backing)
EXPECT(round_trip(message_buffer).is_error());
}
+TEST_CASE(ipc_decode_rejects_empty_size)
+{
+ auto buffer = MUST(Core::AnonymousBuffer::create_with_size(0));
+
+ Vector<Optional<Gfx::BitmapMetadata>> metadata_list;
+ metadata_list.append(Gfx::BitmapMetadata {
+ .format = Gfx::BitmapFormat::BGRA8888,
+ .alpha_type = Gfx::AlphaType::Premultiplied,
+ .size = Gfx::IntSize { 1, 0 },
+ });
+
+ IPC::MessageBuffer message_buffer;
+ IPC::Encoder encoder { message_buffer };
+ MUST(encoder.encode(metadata_list));
+ MUST(encoder.encode(buffer));
+
+ EXPECT(round_trip(message_buffer).is_error());
+}
+
TEST_CASE(ipc_round_trip_maps_the_pixels_of_shareable_bitmaps)
{
auto first = MUST(Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 4, 4 }));