Truly independent web browser
LibMediaCodecs: Refuse video frames past the size limits before decoding
Zaggy10247ca0d4c7a7c0
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.cpp b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.cppindex 5e9507e1bb3..c902a6a9e52 100644--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.cpp+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.cpp@@ -12,6 +12,7 @@#include "FFmpegVideoDecoder.h"#include <LibMediaCodecs/FFmpeg/FFmpegFunctions.h>#include <LibMediaCodecs/FFmpeg/FFmpegHelpers.h>+#include <LibMediaCodecs/Limits.h>namespace Media::FFmpeg {@@ -47,6 +48,7 @@ DecoderErrorOr<NonnullOwnPtr<FFmpegVideoDecoder>> FFmpegVideoDecoder::try_createTRY(set_codec_initialization_data(functions, *codec_context, codec_id, codec_initialization_data));VERIFY(functions.av_opt_set_int(codec_context, "threads", min(Core::System::hardware_concurrency(), 4u), 0) == 0);+ codec_context->max_pixels = MAX_VIDEO_FRAME_AREA;if (functions.avcodec_open2(codec_context, codec, nullptr) < 0)return DecoderError::format(DecoderErrorCategory::Unknown, "Unknown error occurred when opening FFmpeg codec {}", codec_id);diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cppindex 4208456c90c..37f986f956a 100644--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp@@ -14,6 +14,7 @@#include <LibMediaCodecs/FFmpeg/FFmpegFunctions.h>#include <LibMediaCodecs/FFmpeg/FFmpegHelpers.h>#include <LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.h>+#include <LibMediaCodecs/Limits.h>#include <LibMediaCodecs/VideoFrame.h>#include <LibThreading/Thread.h>#include <string.h>@@ -400,6 +401,7 @@ DecoderErrorOr<NonnullOwnPtr<FFmpegVulkanVideoDecoder>> FFmpegVulkanVideoDecoderTRY(set_codec_initialization_data(functions, *codec_context, codec_id, codec_initialization_data));// The device already decodes several frames at a time, and frame threading would only hold frames back.VERIFY(functions.av_opt_set_int(codec_context, "threads", 1, 0) == 0);+ codec_context->max_pixels = MAX_VIDEO_FRAME_AREA;codec_context->hw_device_ctx = functions.av_buffer_ref(device_context);if (codec_context->hw_device_ctx == nullptr)return DecoderError::with_description(DecoderErrorCategory::Memory, "Failed to reference the FFmpeg Vulkan device context"sv);diff --git a/Libraries/LibMediaCodecs/Limits.h b/Libraries/LibMediaCodecs/Limits.hindex 5730b8ee524..22e4f247d99 100644--- a/Libraries/LibMediaCodecs/Limits.h+++ b/Libraries/LibMediaCodecs/Limits.h@@ -13,4 +13,9 @@ namespace Media {static constexpr u32 MAX_VIDEO_FRAME_DIMENSION = 32767;static constexpr u64 MAX_VIDEO_FRAME_AREA = 1ull << 29;+constexpr bool video_frame_exceeds_limits(u64 width, u64 height)+{+ return width > MAX_VIDEO_FRAME_DIMENSION || height > MAX_VIDEO_FRAME_DIMENSION || width * height > MAX_VIDEO_FRAME_AREA;+}+}diff --git a/Libraries/LibMediaCodecs/VideoFrameMetadata.cpp b/Libraries/LibMediaCodecs/VideoFrameMetadata.cppindex caf98dd9613..839f3656667 100644--- a/Libraries/LibMediaCodecs/VideoFrameMetadata.cpp+++ b/Libraries/LibMediaCodecs/VideoFrameMetadata.cpp@@ -21,6 +21,8 @@ ErrorOr<FramePlaneLayout> frame_plane_layout(Gfx::IntSize size, u8 bit_depth, Suconstexpr size_t PLANE_ALIGNMENT = 64;auto plane_sizes = TRY(Gfx::YUVData::plane_sizes(size, bit_depth, subsampling));+ if (video_frame_exceeds_limits(size.width(), size.height()))+ return Error::from_string_literal("Video frame is larger than the frame size limit");auto u_offset = align_up_to(plane_sizes.y, PLANE_ALIGNMENT);auto v_offset = align_up_to(u_offset + plane_sizes.u, PLANE_ALIGNMENT);@@ -47,9 +49,7 @@ bool VideoFrameMetadata::is_valid() constreturn 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)+ if (video_frame_exceeds_limits(size.width(), size.height()))return false;return cicp.is_valid_or_unspecified();}diff --git a/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm b/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mmindex 935f7353da2..90d4b2db58d 100644--- a/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm+++ b/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm@@ -13,6 +13,7 @@#include <LibMediaCodecs/Codecs/NALUnit.h>#include <LibMediaCodecs/Codecs/VP9.h>#include <LibMediaCodecs/CodedFrame.h>+#include <LibMediaCodecs/Limits.h>#include <LibMediaCodecs/VideoFrame.h>#include <LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.h>@@ -227,6 +228,10 @@ DecoderErrorOr<VTDecompressionSessionRef> create_decompression_session(CodecID cif (hardware_decoding_is_required(codec_id) && !hardware_is_usable)return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "VideoToolbox has no decoder for this format"sv);+ auto dimensions = CMVideoFormatDescriptionGetDimensions(format.description.ref());+ if (dimensions.width < 1 || dimensions.height < 1 || video_frame_exceeds_limits(dimensions.width, dimensions.height))+ return DecoderError::with_description(DecoderErrorCategory::Corrupted, "Video frame size is outside the supported limits"sv);+VTRegisterSupplementalVideoDecoderIfAvailable(codec_type_from_codec_id(codec_id));RetainedRef<CFMutableDictionaryRef> specification { CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) };diff --git a/Tests/LibMedia/TestVideoFramePool.cpp b/Tests/LibMedia/TestVideoFramePool.cppindex 8c0df2318fe..9c45b87e2dc 100644--- a/Tests/LibMedia/TestVideoFramePool.cpp+++ b/Tests/LibMedia/TestVideoFramePool.cpp@@ -159,6 +159,16 @@ TEST_CASE(excess_free_buffers_are_dropped_to_fit_the_budget)EXPECT(pool->allocated_byte_count() <= slot_buffer_size * Media::VideoFramePool::MIN_SLOT_COUNT);}+TEST_CASE(frame_layouts_beyond_the_size_limits_are_refused)+{+ Media::Subsampling subsampling { true, true };+ EXPECT(!Media::frame_plane_layout({ 32767, 16 }, 8, subsampling).is_error());+ EXPECT(Media::frame_plane_layout({ 32768, 16 }, 8, subsampling).is_error());+ EXPECT(Media::frame_plane_layout({ 16, 32768 }, 8, subsampling).is_error());+ EXPECT(!Media::frame_plane_layout({ 16384, 32767 }, 8, subsampling).is_error());+ EXPECT(Media::frame_plane_layout({ 23171, 23171 }, 8, subsampling).is_error());+}+TEST_CASE(shed_storage_frees_all_but_held_slots){auto pool = make_pool();