Skip to content

Truly independent web browser

LibGfx+LibMedia+LibMediaCodecs: Validate frame metadata from peers

Zaggy1024b788c3c86034
diff --git a/Libraries/LibGfx/YUVData.cpp b/Libraries/LibGfx/YUVData.cpp
index fb11b517b1e..52c815176a3 100644
--- a/Libraries/LibGfx/YUVData.cpp
+++ b/Libraries/LibGfx/YUVData.cpp
@@ -27,7 +27,7 @@ ErrorOr<YUVData::PlaneSizes> YUVData::plane_sizes(IntSize size, u8 bit_depth, Me
{
if (size.is_empty())
return Error::from_string_literal("YUVData size is empty");
- if (bit_depth == 0 || bit_depth > 16)
+ if (bit_depth == 0 || bit_depth > 12)
return Error::from_string_literal("Invalid YUVData bit depth");
auto component_size = bit_depth <= 8 ? 1 : 2;
diff --git a/Libraries/LibMedia/Producers/RemoteVideoProducer.cpp b/Libraries/LibMedia/Producers/RemoteVideoProducer.cpp
index e84652dc6d7..b63bd600550 100644
--- a/Libraries/LibMedia/Producers/RemoteVideoProducer.cpp
+++ b/Libraries/LibMedia/Producers/RemoteVideoProducer.cpp
@@ -85,7 +85,7 @@ void RemoteVideoProducer::discard_stale_ring_heads()
auto discarded_any_frames = false;
while (true) {
auto head = m_edge.peek();
- if (!head.has_value() || head->seek_id == m_expected_seek_id)
+ if (!head.has_value() || (head->seek_id == m_expected_seek_id && head->handle.is_valid()))
break;
discard_ring_head(*head);
discarded_any_frames = true;
@@ -148,7 +148,10 @@ bool RemoteVideoProducer::can_satisfy_seek_locally(AK::Duration timestamp) const
auto head = m_edge.peek();
if (!head.has_value() || head->seek_id != m_expected_seek_id || timestamp < head->handle.timestamp)
return false;
- if (timestamp < m_edge.peek_newest()->handle.conservative_end())
+ auto newest = m_edge.peek_newest();
+ if (!newest.has_value() || !newest->handle.is_valid())
+ return false;
+ if (timestamp < newest->handle.conservative_end())
return true;
auto status = m_edge.status();
return status.has_value() && status->seek_id == m_expected_seek_id && status->status == PipelineStatus::EndOfStream;
diff --git a/Libraries/LibMedia/VideoEdgeQueue.h b/Libraries/LibMedia/VideoEdgeQueue.h
index 58b39748c5d..210963d3ded 100644
--- a/Libraries/LibMedia/VideoEdgeQueue.h
+++ b/Libraries/LibMedia/VideoEdgeQueue.h
@@ -85,7 +85,13 @@ public:
Optional<VideoEdgeItem> peek() const { return m_ring.peek(); }
Optional<VideoEdgeItem> peek_newest() const { return m_ring.peek_newest(); }
void consume() { (void)m_ring.dequeue(); }
- Optional<VideoEdgeStatus> status() const { return m_header->status.read(); }
+ Optional<VideoEdgeStatus> status() const
+ {
+ auto status = m_header->status.read();
+ if (status.has_value() && to_underlying(status->status) > to_underlying(PipelineStatus::Error))
+ status->status = PipelineStatus::Error;
+ return status;
+ }
void set_requested_seek_id(u32 seek_id) { m_header->requested_seek_id.store(seek_id, AK::MemoryOrder::memory_order_relaxed); }
private:
diff --git a/Libraries/LibMediaCodecs/AudioBlockTiming.h b/Libraries/LibMediaCodecs/AudioBlockTiming.h
index 9e4816d4055..921aa59a3db 100644
--- a/Libraries/LibMediaCodecs/AudioBlockTiming.h
+++ b/Libraries/LibMediaCodecs/AudioBlockTiming.h
@@ -8,6 +8,7 @@
#include <AK/Math.h>
#include <AK/SaturatingMath.h>
+#include <AK/SharedMemoryValue.h>
#include <AK/Time.h>
namespace Media {
@@ -16,7 +17,7 @@ class AudioBlockTiming {
public:
AK::Duration media_time_start() const { return m_media_time_start; }
AK::Duration media_time_duration() const { return m_media_time_duration; }
- AK::Duration media_time_end() const { return m_media_time_start + m_media_time_duration; }
+ AK::Duration media_time_end() const { return media_time_start() + media_time_duration(); }
i64 first_frame_index() const { return m_first_frame_index; }
i64 frame_count() const { return m_frame_count; }
@@ -94,8 +95,8 @@ private:
m_media_time_duration = AK::Duration::from_time_units(m_frame_count, 1, sample_rate);
}
- AK::Duration m_media_time_start;
- AK::Duration m_media_time_duration;
+ SharedMemoryValue<AK::Duration> m_media_time_start;
+ SharedMemoryValue<AK::Duration> m_media_time_duration;
i64 m_first_frame_index { 0 };
i64 m_frame_count { 0 };
};
diff --git a/Libraries/LibMediaCodecs/Limits.h b/Libraries/LibMediaCodecs/Limits.h
new file mode 100644
index 00000000000..5730b8ee524
--- /dev/null
+++ b/Libraries/LibMediaCodecs/Limits.h
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Types.h>
+
+namespace Media {
+
+static constexpr u32 MAX_VIDEO_FRAME_DIMENSION = 32767;
+static constexpr u64 MAX_VIDEO_FRAME_AREA = 1ull << 29;
+
+}
diff --git a/Libraries/LibMediaCodecs/Subsampling.h b/Libraries/LibMediaCodecs/Subsampling.h
index aef80791fb9..86b73577ea7 100644
--- a/Libraries/LibMediaCodecs/Subsampling.h
+++ b/Libraries/LibMediaCodecs/Subsampling.h
@@ -6,6 +6,8 @@
#pragma once
+#include <AK/Optional.h>
+#include <AK/SharedMemoryValue.h>
#include <LibGfx/Size.h>
namespace Media {
@@ -50,3 +52,19 @@ private:
};
}
+
+template<>
+struct AK::SharedMemoryCodec<Media::Subsampling> {
+ struct Representation {
+ u8 x { 0 };
+ u8 y { 0 };
+ };
+
+ static constexpr Representation encode(Media::Subsampling value) { return { value.x(), value.y() }; }
+ static constexpr Optional<Media::Subsampling> decode(Representation representation)
+ {
+ if (representation.x > 1 || representation.y > 1)
+ return {};
+ return Media::Subsampling { representation.x == 1, representation.y == 1 };
+ }
+};
diff --git a/Libraries/LibMediaCodecs/VideoFrameHandle.cpp b/Libraries/LibMediaCodecs/VideoFrameHandle.cpp
index ae773fea73e..cc37ba67fa0 100644
--- a/Libraries/LibMediaCodecs/VideoFrameHandle.cpp
+++ b/Libraries/LibMediaCodecs/VideoFrameHandle.cpp
@@ -8,6 +8,7 @@
#include <LibGfx/YUVData.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
+#include <LibMediaCodecs/Limits.h>
#include <LibMediaCodecs/VideoFrame.h>
#include <LibMediaCodecs/VideoFramePool.h>
@@ -38,6 +39,21 @@ ErrorOr<FramePlaneLayout> frame_plane_layout(Gfx::IntSize size, u8 bit_depth, Su
};
}
+bool VideoFrameHandle::is_valid() const
+{
+ if (duration.value().is_negative())
+ return false;
+ if (bit_depth != 8 && bit_depth != 10 && bit_depth != 12)
+ return false;
+ if (size.width() < 1 || size.height() < 1)
+ return false;
+ if (static_cast<u32>(size.width()) > MAX_VIDEO_FRAME_DIMENSION || static_cast<u32>(size.height()) > MAX_VIDEO_FRAME_DIMENSION)
+ return false;
+ if (static_cast<u64>(size.width()) * static_cast<u64>(size.height()) > MAX_VIDEO_FRAME_AREA)
+ return false;
+ return cicp.is_valid_or_unspecified();
+}
+
VideoFrameHandle VideoFrameHandle::for_frame(VideoFrame const& frame)
{
// Owner-backed (a held pool slot) or consumer-backed (resolved from a handle); both carry the slot identity.
@@ -79,12 +95,12 @@ ErrorOr<void> encode(Encoder& encoder, Media::VideoFrameHandle const& handle)
TRY(encoder.encode(handle.pool_id));
TRY(encoder.encode(handle.slot_index));
TRY(encoder.encode(handle.slot_acquisition_id));
- TRY(encoder.encode(handle.timestamp));
- TRY(encoder.encode(handle.duration));
+ TRY(encoder.encode(handle.timestamp.value()));
+ TRY(encoder.encode(handle.duration.value()));
TRY(encoder.encode(handle.size));
TRY(encoder.encode(handle.bit_depth));
- TRY(encoder.encode(handle.subsampling.x()));
- TRY(encoder.encode(handle.subsampling.y()));
+ TRY(encoder.encode(handle.subsampling.value().x()));
+ TRY(encoder.encode(handle.subsampling.value().y()));
TRY(encoder.encode(handle.cicp));
return {};
}
@@ -105,6 +121,8 @@ ErrorOr<Media::VideoFrameHandle> decode(Decoder& decoder)
TRY(decoder.decode<bool>()),
};
handle.cicp = TRY(decoder.decode<Media::CodingIndependentCodePoints>());
+ if (!handle.is_valid())
+ return Error::from_string_literal("Video frame handle describes a frame that could not have been decoded");
return handle;
}
diff --git a/Libraries/LibMediaCodecs/VideoFrameHandle.h b/Libraries/LibMediaCodecs/VideoFrameHandle.h
index e7936a2e947..610140fc187 100644
--- a/Libraries/LibMediaCodecs/VideoFrameHandle.h
+++ b/Libraries/LibMediaCodecs/VideoFrameHandle.h
@@ -7,6 +7,7 @@
#pragma once
#include <AK/DistinctNumeric.h>
+#include <AK/SharedMemoryValue.h>
#include <AK/Time.h>
#include <LibGfx/Size.h>
#include <LibIPC/Forward.h>
@@ -26,16 +27,18 @@ AK_TYPEDEF_DISTINCT_ORDERED_ID(u64, VideoFramePoolID);
struct MEDIACODECS_API VideoFrameHandle {
static VideoFrameHandle for_frame(VideoFrame const&);
+ bool is_valid() const;
+
AK::Duration conservative_end() const { return VideoFrame::conservative_end_of(timestamp, duration); }
VideoFramePoolID pool_id { 0 };
u32 slot_index { 0 };
u64 slot_acquisition_id { 0 };
- AK::Duration timestamp;
- AK::Duration duration;
+ SharedMemoryValue<AK::Duration> timestamp;
+ SharedMemoryValue<AK::Duration> duration;
Gfx::IntSize size;
u8 bit_depth { 0 };
- Subsampling subsampling;
+ SharedMemoryValue<Subsampling> subsampling;
CodingIndependentCodePoints cicp;
};
diff --git a/Libraries/LibMediaCodecs/VideoFramePool.cpp b/Libraries/LibMediaCodecs/VideoFramePool.cpp
index 0a319130f64..830e1bb46dd 100644
--- a/Libraries/LibMediaCodecs/VideoFramePool.cpp
+++ b/Libraries/LibMediaCodecs/VideoFramePool.cpp
@@ -468,6 +468,8 @@ ErrorOr<NonnullRefPtr<VideoFrame>> resolve_frame_from_slot(Core::AnonymousBuffer
{
if (!slot_buffer.is_valid() || slot_buffer.size() < slot_data_offset())
return Error::from_string_literal("Invalid video frame slot buffer");
+ if (!handle.is_valid())
+ return Error::from_string_literal("Video frame handle describes a frame that could not have been decoded");
// A surface holds the pixels itself, so only an inline framebuffer has a plane layout to validate.
if (surface == nullptr)
diff --git a/Tests/LibMedia/TestRemoteVideoNode.cpp b/Tests/LibMedia/TestRemoteVideoNode.cpp
index 8fa7b646e09..f60a1b40225 100644
--- a/Tests/LibMedia/TestRemoteVideoNode.cpp
+++ b/Tests/LibMedia/TestRemoteVideoNode.cpp
@@ -186,6 +186,15 @@ void wire_full_node(VideoEdgeTestHarness& harness, NonnullRefPtr<VideoProducer>
harness.consumer->start();
}
+// The consumer drops handles that describe a frame no decoder produces, so give the edge's items a real frame's format.
+VideoFrameHandle make_decodable_handle()
+{
+ VideoFrameHandle handle;
+ handle.size = { 2, 2 };
+ handle.bit_depth = 8;
+ return handle;
+}
+
// Builds a consumer over a controlled edge pre-filled with four frames at 1.0s..1.3s under seek_id 0,
// to exercise the seek fast path deterministically.
NonnullRefPtr<RemoteVideoProducer> make_lookahead_consumer(VideoEdgeQueue& producer_edge, size_t& upstream_seeks)
@@ -200,7 +209,7 @@ NonnullRefPtr<RemoteVideoProducer> make_lookahead_consumer(VideoEdgeQueue& produ
auto consumer = RemoteVideoProducer::create(move(consumer_edge), VideoFrameSlotDirectory::create(), move(delegates));
for (int i = 0; i < 4; i++) {
- VideoFrameHandle handle;
+ auto handle = make_decodable_handle();
handle.timestamp = AK::Duration::from_milliseconds(1000 + (i * 100));
handle.duration = AK::Duration::from_milliseconds(100);
MUST(producer_edge.enqueue(handle, 0));
@@ -409,7 +418,7 @@ TEST_CASE(consumer_requests_seeks_before_releasing_stale_frames)
delegates.request_start = [] { };
delegates.request_seek = [&](AK::Duration, InvalidateHeldData) {
events.append("seek"sv);
- VideoFrameHandle handle;
+ auto handle = make_decodable_handle();
handle.timestamp = AK::Duration::from_milliseconds(500);
handle.duration = AK::Duration::from_milliseconds(100);
handle.slot_index = 4;
@@ -423,7 +432,7 @@ TEST_CASE(consumer_requests_seeks_before_releasing_stale_frames)
auto consumer = RemoteVideoProducer::create(move(consumer_edge), VideoFrameSlotDirectory::create(), move(delegates));
for (u32 i = 0; i < 4; i++) {
- VideoFrameHandle handle;
+ auto handle = make_decodable_handle();
handle.timestamp = AK::Duration::from_milliseconds(1000 + (i * 100));
handle.duration = AK::Duration::from_milliseconds(100);
handle.slot_index = i;
@@ -472,7 +481,7 @@ TEST_CASE(consumer_releases_ring_contents_on_suspension)
auto consumer = RemoteVideoProducer::create(move(consumer_edge), VideoFrameSlotDirectory::create(), move(delegates));
for (u32 i = 0; i < 4; i++) {
- VideoFrameHandle handle;
+ auto handle = make_decodable_handle();
handle.timestamp = AK::Duration::from_milliseconds(1000 + (i * 100));
handle.duration = AK::Duration::from_milliseconds(100);
handle.slot_index = i;
@@ -489,6 +498,24 @@ TEST_CASE(consumer_releases_ring_contents_on_suspension)
EXPECT_EQ(output.status, PipelineStatus::Suspended);
}
+// A ring item's times are raw words the peer wrote; one that is not a canonical Duration must not reach Duration
+// arithmetic, which would abort the consumer's process.
+TEST_CASE(consumer_reads_forged_frame_times_without_aborting)
+{
+ auto producer_edge = MUST(VideoEdgeQueue::create());
+ size_t upstream_seeks = 0;
+ auto consumer = make_lookahead_consumer(producer_edge, upstream_seeks);
+
+ auto handle = make_decodable_handle();
+ handle.duration = AK::Duration::from_milliseconds(100);
+ SharedMemoryCodec<AK::Duration>::Representation forged_timestamp { 1, 2'000'000'000 };
+ __builtin_memcpy(&handle.timestamp, &forged_timestamp, sizeof(forged_timestamp));
+ MUST(producer_edge.enqueue(handle, 0));
+
+ consumer->seek(AK::Duration::from_milliseconds(1450));
+ EXPECT_EQ(upstream_seeks, 1u);
+}
+
namespace {
NonnullRefPtr<VideoFrame> create_pooled_frame(VideoFramePool& pool, AK::Duration timestamp)
diff --git a/Tests/LibMedia/TestVideoFramePool.cpp b/Tests/LibMedia/TestVideoFramePool.cpp
index 4ece61f5660..d41fb2504cc 100644
--- a/Tests/LibMedia/TestVideoFramePool.cpp
+++ b/Tests/LibMedia/TestVideoFramePool.cpp
@@ -5,10 +5,14 @@
*/
#include <AK/ConditionVariable.h>
+#include <AK/MemoryStream.h>
#include <AK/Mutex.h>
#include <AK/Queue.h>
#include <AK/Time.h>
#include <LibCore/System.h>
+#include <LibIPC/Decoder.h>
+#include <LibIPC/Encoder.h>
+#include <LibMediaCodecs/Limits.h>
#include <LibMediaCodecs/VideoFrame.h>
#include <LibMediaCodecs/VideoFrameHandle.h>
#include <LibMediaCodecs/VideoFramePool.h>
@@ -475,6 +479,49 @@ TEST_CASE(resolve_frame_rejects_stale_and_oversized_handles)
EXPECT(directory->resolve_frame(handle, [] { }) == nullptr);
}
+// Handles come from another process, so one describing a frame no decoder produces must not resolve, even when the
+// slot's buffer could hold it.
+TEST_CASE(resolve_frame_rejects_handles_that_could_not_have_been_decoded)
+{
+ auto pool = make_pool();
+ auto slot = pool->try_acquire(MUST(Media::frame_plane_layout(RESOLVED_FRAME_SIZE, 12, Media::Subsampling::yuv444())).total_byte_count).release_value();
+ auto directory = Media::VideoFrameSlotDirectory::create();
+ directory->notify_slot_announced(pool->id(), slot.index, pool->slot_buffer(slot.index), nullptr);
+ EXPECT(directory->resolve_frame(make_handle(pool, slot), [] { }) != nullptr);
+
+ auto thirteen_bit = make_handle(pool, slot);
+ thirteen_bit.bit_depth = 13;
+ EXPECT(directory->resolve_frame(thirteen_bit, [] { }) == nullptr);
+
+ auto negative_duration = make_handle(pool, slot);
+ negative_duration.duration = AK::Duration::from_milliseconds(-20);
+ EXPECT(directory->resolve_frame(negative_duration, [] { }) == nullptr);
+
+ auto unknown_primaries = make_handle(pool, slot);
+ unknown_primaries.cicp.set_color_primaries(static_cast<Media::ColorPrimaries>(200));
+ EXPECT(directory->resolve_frame(unknown_primaries, [] { }) == nullptr);
+
+ auto too_wide = make_handle(pool, slot);
+ too_wide.size = { static_cast<int>(Media::MAX_VIDEO_FRAME_DIMENSION) + 1, 1 };
+ EXPECT(!too_wide.is_valid());
+}
+
+TEST_CASE(handles_that_could_not_have_been_decoded_are_refused_over_ipc)
+{
+ auto pool = make_pool();
+ auto slot = pool->try_acquire(resolved_frame_byte_count()).release_value();
+ auto handle = make_handle(pool, slot);
+ handle.bit_depth = 13;
+
+ IPC::MessageBuffer message;
+ IPC::Encoder encoder { message };
+ MUST(encoder.encode(handle));
+ Queue<IPC::Attachment> attachments;
+ FixedMemoryStream stream { message.data().span() };
+ IPC::Decoder decoder { stream, attachments };
+ EXPECT(decoder.decode<Media::VideoFrameHandle>().is_error());
+}
+
TEST_CASE(directory_resolves_announced_slots)
{
auto pool = make_pool();