Skip to content

Truly independent web browser

LibCore+LibGfx+LibMediaCodecs: Check video surface planes on receipt

Zaggy102414aae68d4254
diff --git a/Libraries/LibCore/IOSurface.cpp b/Libraries/LibCore/IOSurface.cpp
index 866fa7ecb5d..12fee8a88f6 100644
--- a/Libraries/LibCore/IOSurface.cpp
+++ b/Libraries/LibCore/IOSurface.cpp
@@ -131,6 +131,11 @@ ErrorOr<IOSurfaceHandle> IOSurfaceHandle::from_mach_port(MachPort const& port)
return IOSurfaceHandle(make<IOSurfaceRefWrapper>(ref));
}
+size_t IOSurfaceHandle::alloc_size() const
+{
+ return IOSurfaceGetAllocSize(m_ref_wrapper->ref);
+}
+
size_t IOSurfaceHandle::width() const
{
return IOSurfaceGetWidth(m_ref_wrapper->ref);
diff --git a/Libraries/LibCore/IOSurface.h b/Libraries/LibCore/IOSurface.h
index 1b62f7537f3..e9cd64524e5 100644
--- a/Libraries/LibCore/IOSurface.h
+++ b/Libraries/LibCore/IOSurface.h
@@ -39,6 +39,7 @@ public:
// The four character code the surface was allocated with, such as '420v' for biplanar 8-bit YCbCr.
u32 pixel_format() const;
size_t plane_count() const;
+ size_t alloc_size() const;
size_t plane_width(size_t plane) const;
size_t plane_height(size_t plane) const;
size_t bytes_per_element() const;
diff --git a/Libraries/LibGfx/VideoSurfaceImage.h b/Libraries/LibGfx/VideoSurfaceImage.h
index d665acb9c42..b28906847dc 100644
--- a/Libraries/LibGfx/VideoSurfaceImage.h
+++ b/Libraries/LibGfx/VideoSurfaceImage.h
@@ -21,6 +21,8 @@ namespace Gfx {
// interleaved in the second. Returns the bit depth of a surface in one of these formats.
Optional<u8> biplanar_bit_depth_for_pixel_format(u32 pixel_format);
+ErrorOr<void> validate_biplanar_video_surface(Core::IOSurfaceHandle const&);
+
// Reads a decoder's surface back into RGBA pixels, for the paths that need them in memory rather than on the GPU.
ErrorOr<NonnullRefPtr<Bitmap>> bitmap_from_video_surface(Core::IOSurfaceHandle const&, Media::CodingIndependentCodePoints);
diff --git a/Libraries/LibGfx/VideoSurfaceImage.mm b/Libraries/LibGfx/VideoSurfaceImage.mm
index c83d5cea610..c1b9ee35452 100644
--- a/Libraries/LibGfx/VideoSurfaceImage.mm
+++ b/Libraries/LibGfx/VideoSurfaceImage.mm
@@ -4,11 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Checked.h>
#include <AK/Optional.h>
#include <AK/ScopeGuard.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/VideoSurfaceImage.h>
#include <LibGfx/YUVData.h>
+#include <LibMediaCodecs/Limits.h>
#include <CoreVideo/CoreVideo.h>
@@ -28,6 +30,51 @@ Optional<u8> biplanar_bit_depth_for_pixel_format(u32 pixel_format)
}
}
+ErrorOr<void> validate_biplanar_video_surface(Core::IOSurfaceHandle const& io_surface)
+{
+ auto bit_depth = biplanar_bit_depth_for_pixel_format(io_surface.pixel_format());
+ if (!bit_depth.has_value() || io_surface.plane_count() != 2)
+ return Error::from_string_literal("Video surface is not in a supported bi-planar format");
+
+ auto width = io_surface.plane_width(0);
+ auto height = io_surface.plane_height(0);
+ if (width == 0 || height == 0 || width > Media::MAX_VIDEO_FRAME_DIMENSION || height > Media::MAX_VIDEO_FRAME_DIMENSION)
+ return Error::from_string_literal("Video surface has an unsupported size");
+
+ size_t bytes_per_sample = *bit_depth > 8 ? 2 : 1;
+ auto chroma_width = (width + 1) / 2;
+ auto chroma_height = (height + 1) / 2;
+ struct PlaneExtent {
+ size_t width;
+ size_t height;
+ size_t row_byte_count;
+ };
+ Array<PlaneExtent, 2> required_extents {
+ PlaneExtent { width, height, width * bytes_per_sample },
+ PlaneExtent { chroma_width, chroma_height, chroma_width * 2 * bytes_per_sample },
+ };
+
+ auto const* base = static_cast<u8 const*>(io_surface.data());
+ for (size_t plane = 0; plane < required_extents.size(); plane++) {
+ auto const& required = required_extents[plane];
+ auto plane_height = io_surface.plane_height(plane);
+ auto stride = io_surface.bytes_per_row_of_plane(plane);
+ if (io_surface.plane_width(plane) < required.width || plane_height < required.height || stride < required.row_byte_count)
+ return Error::from_string_literal("Video surface plane is smaller than its format requires");
+
+ auto const* plane_base = static_cast<u8 const*>(io_surface.data_of_plane(plane));
+ if (plane_base < base)
+ return Error::from_string_literal("Video surface plane starts outside the surface");
+ Checked<size_t> plane_byte_count = stride;
+ plane_byte_count *= plane_height;
+ Checked<size_t> plane_end = static_cast<size_t>(plane_base - base);
+ plane_end += plane_byte_count;
+ if (plane_end.has_overflow() || plane_end.value() > io_surface.alloc_size())
+ return Error::from_string_literal("Video surface plane extends past the surface");
+ }
+ return {};
+}
+
ErrorOr<NonnullRefPtr<Bitmap>> bitmap_from_video_surface(Core::IOSurfaceHandle const& io_surface, Media::CodingIndependentCodePoints cicp)
{
auto bit_depth = biplanar_bit_depth_for_pixel_format(io_surface.pixel_format());
diff --git a/Libraries/LibGfx/YUVData.cpp b/Libraries/LibGfx/YUVData.cpp
index 52c815176a3..34203009800 100644
--- a/Libraries/LibGfx/YUVData.cpp
+++ b/Libraries/LibGfx/YUVData.cpp
@@ -192,6 +192,22 @@ ErrorOr<NonnullRefPtr<Bitmap>> biplanar_yuv_to_bitmap(IntSize size, u8 bit_depth
{
if (cicp.matrix_coefficients() == Media::MatrixCoefficients::Identity)
return Error::from_string_literal("Subsampled RGB is unsupported");
+ if (size.is_empty())
+ return Error::from_string_literal("Bi-planar YUV size is empty");
+
+ size_t bytes_per_sample = bit_depth > 8 ? 2 : 1;
+ auto plane_covers_rows = [](BiplanarYUVPlane const& plane, size_t row_byte_count, size_t row_count) {
+ if (plane.stride < row_byte_count)
+ return false;
+ Checked<size_t> required_size = plane.stride;
+ required_size *= row_count - 1;
+ required_size += row_byte_count;
+ return !required_size.has_overflow() && plane.data.size() >= required_size.value();
+ };
+ auto chroma_width = (static_cast<size_t>(size.width()) + 1) / 2;
+ auto chroma_height = (static_cast<size_t>(size.height()) + 1) / 2;
+ if (!plane_covers_rows(luma, size.width() * bytes_per_sample, size.height()) || !plane_covers_rows(chroma, chroma_width * 2 * bytes_per_sample, chroma_height))
+ return Error::from_string_literal("Bi-planar YUV planes are smaller than their size requires");
auto bitmap = TRY(Bitmap::create(BitmapFormat::RGBA8888, AlphaType::Premultiplied, size));
auto* destination = reinterpret_cast<u8*>(bitmap->scanline(0));
diff --git a/Libraries/LibMediaCodecs/VideoFramePool.cpp b/Libraries/LibMediaCodecs/VideoFramePool.cpp
index 830e1bb46dd..170573a20b7 100644
--- a/Libraries/LibMediaCodecs/VideoFramePool.cpp
+++ b/Libraries/LibMediaCodecs/VideoFramePool.cpp
@@ -472,8 +472,13 @@ ErrorOr<NonnullRefPtr<VideoFrame>> resolve_frame_from_slot(Core::AnonymousBuffer
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)
+ if (surface == nullptr) {
TRY(yuv_data_in_slot_buffer(slot_buffer, handle.size, handle.bit_depth, handle.subsampling, handle.cicp));
+ } else {
+ auto surface_size = surface->size();
+ if (handle.subsampling.value() != Subsampling::yuv420() || handle.size.width() > surface_size.width() || handle.size.height() > surface_size.height())
+ return Error::from_string_literal("Video frame handle does not match its surface");
+ }
if (slot_header(slot_buffer).slot_acquisition_id.load(AK::MemoryOrder::memory_order_acquire) != handle.slot_acquisition_id)
return Error::from_string_literal("VideoFrameHandle refers to a recycled slot");
diff --git a/Libraries/LibMediaCodecs/VideoSurface.cpp b/Libraries/LibMediaCodecs/VideoSurface.cpp
index 09fb6f0b4ae..74ac026a6b4 100644
--- a/Libraries/LibMediaCodecs/VideoSurface.cpp
+++ b/Libraries/LibMediaCodecs/VideoSurface.cpp
@@ -17,6 +17,17 @@ namespace Media {
VideoSurface::~VideoSurface() = default;
+Gfx::IntSize VideoSurface::size() const
+{
+#if defined(AK_OS_MACOS)
+ return { static_cast<int>(m_io_surface.plane_width(0)), static_cast<int>(m_io_surface.plane_height(0)) };
+#elif defined(AK_OS_LINUX)
+ return m_dma_buf.size;
+#else
+ return {};
+#endif
+}
+
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> VideoSurface::read_back_bitmap([[maybe_unused]] CodingIndependentCodePoints const& cicp) const
{
#ifdef AK_OS_MACOS
@@ -36,7 +47,9 @@ ErrorOr<NonnullRefPtr<VideoSurface>> VideoSurface::create(Core::IOSurfaceHandle
ErrorOr<NonnullRefPtr<VideoSurface>> VideoSurface::create_from_mach_port(Core::MachPort const& port)
{
- return create(TRY(Core::IOSurfaceHandle::from_mach_port(port)));
+ auto io_surface = TRY(Core::IOSurfaceHandle::from_mach_port(port));
+ TRY(Gfx::validate_biplanar_video_surface(io_surface));
+ return create(move(io_surface));
}
#endif
diff --git a/Libraries/LibMediaCodecs/VideoSurface.h b/Libraries/LibMediaCodecs/VideoSurface.h
index 2f35459d9de..4ae89c118fa 100644
--- a/Libraries/LibMediaCodecs/VideoSurface.h
+++ b/Libraries/LibMediaCodecs/VideoSurface.h
@@ -11,6 +11,7 @@
#include <AK/NonnullRefPtr.h>
#include <AK/Platform.h>
#include <LibGfx/Forward.h>
+#include <LibGfx/Size.h>
#include <LibIPC/Forward.h>
#include <LibMediaCodecs/Color/CodingIndependentCodePoints.h>
#include <LibMediaCodecs/Export.h>
@@ -39,6 +40,7 @@ public:
~VideoSurface();
u32 id() const { return m_id; }
+ Gfx::IntSize size() const;
// Reads the pixels back into memory, which works only where the platform lets this process map the surface.
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> read_back_bitmap(CodingIndependentCodePoints const&) const;
diff --git a/Tests/Compositor/TestVideoSurfaceImage.mm b/Tests/Compositor/TestVideoSurfaceImage.mm
index 62ed3f02301..6e42f743f28 100644
--- a/Tests/Compositor/TestVideoSurfaceImage.mm
+++ b/Tests/Compositor/TestVideoSurfaceImage.mm
@@ -298,3 +298,80 @@ TEST_CASE(refuses_to_read_back_a_surface_that_holds_no_video_planes)
auto surface = Core::IOSurfaceHandle::create(16, 16);
EXPECT(Gfx::bitmap_from_video_surface(surface, {}).is_error());
}
+
+namespace {
+
+// A '420v' surface described plane by plane, so a test can give it the geometry another process could.
+Optional<Core::IOSurfaceHandle> create_surface_with_chroma_height(int width, int height, int chroma_height)
+{
+ auto set_number = [](CFMutableDictionaryRef dictionary, CFStringRef key, long value) {
+ auto* number = CFNumberCreate(kCFAllocatorDefault, kCFNumberLongType, &value);
+ CFDictionarySetValue(dictionary, key, number);
+ CFRelease(number);
+ };
+ auto create_dictionary = [] {
+ return CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+ };
+ auto create_plane = [&](long plane_width, long plane_height, long bytes_per_element, long offset) {
+ auto* plane = create_dictionary();
+ set_number(plane, kIOSurfacePlaneWidth, plane_width);
+ set_number(plane, kIOSurfacePlaneHeight, plane_height);
+ set_number(plane, kIOSurfacePlaneBytesPerElement, bytes_per_element);
+ set_number(plane, kIOSurfacePlaneBytesPerRow, plane_width * bytes_per_element);
+ set_number(plane, kIOSurfacePlaneOffset, offset);
+ set_number(plane, kIOSurfacePlaneSize, plane_width * bytes_per_element * plane_height);
+ return plane;
+ };
+
+ long luma_size = static_cast<long>(width) * height;
+ CFTypeRef planes[] = { create_plane(width, height, 1, 0), create_plane(width / 2, chroma_height, 2, luma_size) };
+ auto* plane_array = CFArrayCreate(kCFAllocatorDefault, planes, 2, &kCFTypeArrayCallBacks);
+ CFRelease(planes[0]);
+ CFRelease(planes[1]);
+
+ auto* properties = create_dictionary();
+ set_number(properties, kIOSurfaceWidth, width);
+ set_number(properties, kIOSurfaceHeight, height);
+ set_number(properties, kIOSurfacePixelFormat, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
+ set_number(properties, kIOSurfaceAllocSize, luma_size + (static_cast<long>(width) * chroma_height));
+ CFDictionarySetValue(properties, kIOSurfacePlaneInfo, plane_array);
+ CFRelease(plane_array);
+
+ auto* io_surface = IOSurfaceCreate(properties);
+ CFRelease(properties);
+ if (io_surface == nullptr)
+ return {};
+ auto handle = Core::IOSurfaceHandle::from_ref(io_surface);
+ CFRelease(io_surface);
+ return handle;
+}
+
+}
+
+TEST_CASE(accepts_surfaces_laid_out_like_a_decoders)
+{
+ EXPECT(!Gfx::validate_biplanar_video_surface(create_decoder_shaped_surface(63, 47, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange).release_value()).is_error());
+ EXPECT(!Gfx::validate_biplanar_video_surface(create_decoder_shaped_surface(64, 48, kCVPixelFormatType_420YpCbCr10BiPlanarFullRange).release_value()).is_error());
+ EXPECT(!Gfx::validate_biplanar_video_surface(create_surface_with_chroma_height(64, 48, 24).release_value()).is_error());
+}
+
+// Surfaces arrive from the decoder's process, so their planes must hold what their format says before anything reads
+// them, or the readers walk past the chroma plane.
+TEST_CASE(refuses_surfaces_whose_planes_do_not_hold_their_format)
+{
+ EXPECT(Gfx::validate_biplanar_video_surface(Core::IOSurfaceHandle::create(16, 16)).is_error());
+
+ auto short_chroma = create_surface_with_chroma_height(64, 48, 1);
+ VERIFY(short_chroma.has_value());
+ EXPECT(Gfx::validate_biplanar_video_surface(*short_chroma).is_error());
+}
+
+TEST_CASE(biplanar_conversion_refuses_planes_shorter_than_the_size)
+{
+ auto luma = MUST(ByteBuffer::create_zeroed(64 * 48));
+ auto chroma = MUST(ByteBuffer::create_zeroed(64 * 24));
+ Gfx::BiplanarYUVPlane luma_plane { .data = luma.bytes(), .stride = 64 };
+ EXPECT(!Gfx::biplanar_yuv_to_bitmap({ 64, 48 }, 8, {}, luma_plane, { .data = chroma.bytes(), .stride = 64 }).is_error());
+ EXPECT(Gfx::biplanar_yuv_to_bitmap({ 64, 48 }, 8, {}, luma_plane, { .data = chroma.bytes().slice(0, 64), .stride = 64 }).is_error());
+ EXPECT(Gfx::biplanar_yuv_to_bitmap({ 64, 48 }, 8, {}, luma_plane, { .data = chroma.bytes(), .stride = 16 }).is_error());
+}