Skip to content

Truly independent web browser

LibMedia: Cap metadata strings and codec configurations by size

Zaggy102452271d6b4666
diff --git a/Libraries/LibMedia/Containers/ISOBMFF/Reader.cpp b/Libraries/LibMedia/Containers/ISOBMFF/Reader.cpp
index c4c08248e7a..be30953fc5b 100644
--- a/Libraries/LibMedia/Containers/ISOBMFF/Reader.cpp
+++ b/Libraries/LibMedia/Containers/ISOBMFF/Reader.cpp
@@ -17,6 +17,7 @@
#include <LibMediaCodecs/Codecs/H265.h>
#include <LibMediaCodecs/Codecs/MPEG4Audio.h>
#include <LibMediaCodecs/Codecs/VP9.h>
+#include <LibMediaCodecs/Limits.h>
#include "Reader.h"
#include "Streamer.h"
@@ -650,8 +651,10 @@ DecoderErrorOr<SampleEntry> Reader::parse_sample_entry(Streamer& streamer, BoxHe
Optional<CodingIndependentCodePoints> color_from_colour_information_box;
TRY(parse_child_boxes(streamer, header, IsTopLevel::No, [&](BoxHeader const& child) -> DecoderErrorOr<IterationDecision> {
- auto read_configuration = [&] -> DecoderErrorOr<FixedArray<u8>> {
- return streamer.read_bytes(child.content_size.value());
+ auto read_configuration = [&](size_t size) -> DecoderErrorOr<FixedArray<u8>> {
+ if (size > MAX_CODEC_CONFIGURATION_SIZE)
+ return DecoderError::corrupted("Codec configuration box is too large"sv);
+ return streamer.read_bytes(size);
};
auto require_sample_entry_codec = [&](auto... codec_ids) -> DecoderErrorOr<void> {
@@ -666,15 +669,15 @@ DecoderErrorOr<SampleEntry> Reader::parse_sample_entry(Streamer& streamer, BoxHe
if (child.type == AVC_CONFIGURATION_BOX) {
TRY(require_sample_entry_codec(CodecID::H264));
- entry.codec_initialization_data = TRY(read_configuration());
+ entry.codec_initialization_data = TRY(read_configuration(child.content_size.value()));
adopt_parameters(Codecs::H264::parse_configuration_record(entry.codec_initialization_data.span()));
} else if (child.type == HEVC_CONFIGURATION_BOX) {
TRY(require_sample_entry_codec(CodecID::H265));
- entry.codec_initialization_data = TRY(read_configuration());
+ entry.codec_initialization_data = TRY(read_configuration(child.content_size.value()));
adopt_parameters(Codecs::H265::parse_configuration_record(entry.codec_initialization_data.span()));
} else if (child.type == AV1_CONFIGURATION_BOX) {
TRY(require_sample_entry_codec(CodecID::AV1));
- entry.codec_initialization_data = TRY(read_configuration());
+ entry.codec_initialization_data = TRY(read_configuration(child.content_size.value()));
adopt_parameters(Codecs::AV1::parse_configuration_record(entry.codec_initialization_data.span()));
} else if (child.type == VP_CONFIGURATION_BOX) {
TRY(require_sample_entry_codec(CodecID::VP8, CodecID::VP9));
@@ -683,14 +686,14 @@ DecoderErrorOr<SampleEntry> Reader::parse_sample_entry(Streamer& streamer, BoxHe
return DecoderError::corrupted("VP codec configuration box is too small"sv);
auto full_box = TRY(read_full_box_header(streamer));
if (entry.codec_id == CodecID::VP9 && full_box.version == 1) {
- auto record = TRY(streamer.read_bytes(child.content_size.value() - FULL_BOX_HEADER_SIZE));
+ auto record = TRY(read_configuration(child.content_size.value() - FULL_BOX_HEADER_SIZE));
adopt_parameters(Codecs::VP9::parse_configuration_record(record.span()));
}
} else if (child.type == OPUS_CONFIGURATION_BOX) {
- auto configuration = TRY(read_configuration());
+ auto configuration = TRY(read_configuration(child.content_size.value()));
entry.codec_initialization_data = TRY(Codecs::Opus::codec_initialization_data_from_isobmff_configuration(configuration.span()));
} else if (child.type == FLAC_CONFIGURATION_BOX) {
- auto configuration = TRY(read_configuration());
+ auto configuration = TRY(read_configuration(child.content_size.value()));
entry.codec_initialization_data = TRY(Codecs::FLAC::codec_initialization_data_from_isobmff_configuration(configuration.span()));
} else if (child.type == ELEMENTARY_STREAM_DESCRIPTOR_BOX) {
u8 object_type_indication = 0;
@@ -772,6 +775,8 @@ DecoderErrorOr<FixedArray<u8>> Reader::parse_elementary_stream_descriptor_box(St
auto size = TRY(read_descriptor_size(streamer));
if (streamer.position() > end || size > end - streamer.position())
return DecoderError::corrupted("DecoderSpecificInfo extends past the end of its box"sv);
+ if (size > MAX_CODEC_CONFIGURATION_SIZE)
+ return DecoderError::corrupted("DecoderSpecificInfo is too large"sv);
return streamer.read_bytes(size);
}
diff --git a/Libraries/LibMedia/Containers/ISOBMFF/Streamer.cpp b/Libraries/LibMedia/Containers/ISOBMFF/Streamer.cpp
index 5f15ec6174e..6410482c792 100644
--- a/Libraries/LibMedia/Containers/ISOBMFF/Streamer.cpp
+++ b/Libraries/LibMedia/Containers/ISOBMFF/Streamer.cpp
@@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibMediaCodecs/Limits.h>
+
#include "Streamer.h"
namespace Media::ISOBMFF {
@@ -27,6 +29,8 @@ DecoderErrorOr<double> Streamer::read_fixed_point_16_16()
DecoderErrorOr<String> Streamer::read_null_terminated_string(size_t maximum_size)
{
+ if (maximum_size > MAX_METADATA_STRING_SIZE)
+ return DecoderError::corrupted("String is too large"sv);
auto bytes = TRY(read_bytes(maximum_size));
auto length = maximum_size;
for (size_t i = 0; i < maximum_size; i++) {
diff --git a/Libraries/LibMedia/Containers/Matroska/Reader.cpp b/Libraries/LibMedia/Containers/Matroska/Reader.cpp
index 67ea115ade7..82b15c15caf 100644
--- a/Libraries/LibMedia/Containers/Matroska/Reader.cpp
+++ b/Libraries/LibMedia/Containers/Matroska/Reader.cpp
@@ -23,6 +23,7 @@
#include <LibMediaCodecs/Codecs/AV1.h>
#include <LibMediaCodecs/Codecs/H264.h>
#include <LibMediaCodecs/Codecs/H265.h>
+#include <LibMediaCodecs/Limits.h>
#include "Reader.h"
@@ -546,7 +547,10 @@ DecoderErrorOr<NonnullRefPtr<TrackEntry>> Reader::parse_track_entry(Streamer& st
dbgln_if(MATROSKA_TRACE_DEBUG, "Read Track's CodecID attribute: {}", track_entry->codec_id());
break;
case TRACK_CODEC_PRIVATE_ID: {
- auto codec_private_data = TRY(streamer.read_raw_octets(TRY(streamer.read_variable_size_integer())));
+ auto codec_private_size = TRY(streamer.read_variable_size_integer());
+ if (codec_private_size > MAX_CODEC_CONFIGURATION_SIZE)
+ return DecoderError::corrupted("Track's CodecPrivate element is too large"sv);
+ auto codec_private_data = TRY(streamer.read_raw_octets(codec_private_size));
track_entry->set_codec_private_data(move(codec_private_data));
dbgln_if(MATROSKA_TRACE_DEBUG, "Read Track's CodecPrivateData element");
break;
diff --git a/Libraries/LibMedia/Containers/Matroska/Streamer.cpp b/Libraries/LibMedia/Containers/Matroska/Streamer.cpp
index 196d227b36d..621bbb3ab88 100644
--- a/Libraries/LibMedia/Containers/Matroska/Streamer.cpp
+++ b/Libraries/LibMedia/Containers/Matroska/Streamer.cpp
@@ -10,6 +10,7 @@
#include <AK/String.h>
#include <AK/Utf8View.h>
#include <LibMedia/MediaStream.h>
+#include <LibMediaCodecs/Limits.h>
#include "Streamer.h"
@@ -25,6 +26,8 @@ Streamer::~Streamer() = default;
DecoderErrorOr<String> Streamer::read_string()
{
auto string_length = TRY(read_variable_size_integer());
+ if (string_length > MAX_METADATA_STRING_SIZE)
+ return DecoderError::corrupted("String element is too large"sv);
auto string_data = TRY(read_raw_octets(string_length));
if (string_data.is_empty())
return String {};
diff --git a/Libraries/LibMediaCodecs/Limits.h b/Libraries/LibMediaCodecs/Limits.h
index 58ae1f22a7d..00237e1d63e 100644
--- a/Libraries/LibMediaCodecs/Limits.h
+++ b/Libraries/LibMediaCodecs/Limits.h
@@ -28,4 +28,7 @@ constexpr bool audio_sample_rate_is_outside_limits(u64 sample_rate)
static constexpr size_t MAX_DECODED_AUDIO_BYTE_COUNT = 512 * MiB;
+static constexpr size_t MAX_METADATA_STRING_SIZE = 16 * KiB;
+static constexpr size_t MAX_CODEC_CONFIGURATION_SIZE = 16 * MiB;
+
}
diff --git a/Tests/LibMedia/TestISOBMFF.cpp b/Tests/LibMedia/TestISOBMFF.cpp
index 9279edcf904..dd1df576d48 100644
--- a/Tests/LibMedia/TestISOBMFF.cpp
+++ b/Tests/LibMedia/TestISOBMFF.cpp
@@ -15,6 +15,7 @@
#include <LibMedia/Containers/ISOBMFF/Streamer.h>
#include <LibMedia/IncrementallyPopulatedStream.h>
#include <LibMedia/ReadonlyBytesCursor.h>
+#include <LibMediaCodecs/Limits.h>
#include <LibTest/TestCase.h>
#include <LibThreading/Thread.h>
@@ -1435,6 +1436,36 @@ TEST_CASE(accepts_a_vp_configuration_box_under_a_vp8_sample_entry)
EXPECT(!parse_movie(bytes).is_error());
}
+TEST_CASE(rejects_a_configuration_box_past_the_codec_configuration_limit)
+{
+ ByteBuffer bytes;
+ auto append_oversized_configuration_box = [](ByteBuffer& bytes) {
+ auto configuration = begin_box(bytes, "av1C");
+ MUST(bytes.try_resize(bytes.size() + Media::MAX_CODEC_CONFIGURATION_SIZE + 1));
+ finish_box(bytes, configuration);
+ };
+ append_video_movie(bytes, "av01", append_oversized_configuration_box, ColourBoxOrder::None);
+ auto result = parse_movie(bytes);
+ EXPECT(result.is_error());
+ EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::Corrupted);
+}
+
+TEST_CASE(rejects_null_terminated_strings_past_the_metadata_string_limit)
+{
+ ByteBuffer bytes;
+ MUST(bytes.try_resize(Media::MAX_METADATA_STRING_SIZE + 1));
+ bytes.bytes().fill('a');
+
+ auto streamer = streamer_for(bytes);
+ auto string = MUST(streamer.read_null_terminated_string(Media::MAX_METADATA_STRING_SIZE));
+ EXPECT_EQ(string.bytes().size(), Media::MAX_METADATA_STRING_SIZE);
+
+ auto oversized_streamer = streamer_for(bytes);
+ auto result = oversized_streamer.read_null_terminated_string(Media::MAX_METADATA_STRING_SIZE + 1);
+ EXPECT(result.is_error());
+ EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::Corrupted);
+}
+
TEST_CASE(uses_first_edit_list_entry_for_presentation_offset)
{
for (u8 version : { 0, 1 }) {
diff --git a/Tests/LibMedia/TestParseMatroska.cpp b/Tests/LibMedia/TestParseMatroska.cpp
index 73a1115d439..09b03db7515 100644
--- a/Tests/LibMedia/TestParseMatroska.cpp
+++ b/Tests/LibMedia/TestParseMatroska.cpp
@@ -13,6 +13,7 @@
#include <LibMedia/Containers/Matroska/MatroskaDemuxer.h>
#include <LibMedia/IncrementallyPopulatedStream.h>
#include <LibMedia/ReadonlyBytesCursor.h>
+#include <LibMediaCodecs/Limits.h>
#include <LibTest/TestCase.h>
#include <LibMedia/Containers/Matroska/Reader.h>
@@ -326,6 +327,27 @@ ByteBuffer make_matroska_without_cues(SegmentSize segment_size, ClusterSize clus
return data;
}
+ByteBuffer make_track_entry_with_element_of_size(u32 id, size_t payload_size)
+{
+ ByteBuffer data;
+ auto track_entry = begin_ebml_master(data, Media::Matroska::TRACK_ENTRY_ID);
+ append_ebml_uint(data, Media::Matroska::TRACK_NUMBER_ID, 1);
+ append_ebml_id(data, id);
+ append_ebml_size(data, payload_size, 4);
+ auto payload_offset = data.size();
+ MUST(data.try_resize(payload_offset + payload_size));
+ data.bytes().slice(payload_offset).fill('a');
+ finish_ebml_master(data, track_entry);
+ return data;
+}
+
+Media::DecoderErrorOr<NonnullRefPtr<Media::Matroska::TrackEntry>> parse_track_entry(ByteBuffer const& data)
+{
+ // Parsing starts after the element ID.
+ auto streamer = streamer_from_bytes(data.bytes().slice(1));
+ return Media::Matroska::Reader::parse_track_entry(streamer);
+}
+
}
TEST_CASE(reader_reads_segment_with_no_declared_size_and_no_cues)
@@ -1200,3 +1222,21 @@ TEST_CASE(unknown_track_type_values_are_invalid)
EXPECT_EQ(Media::Matroska::track_type_from_matroska_track_type(Media::Matroska::TrackEntry::TrackType::Metadata), Media::TrackType::Subtitles);
EXPECT_EQ(Media::Matroska::track_type_from_matroska_track_type(Media::Matroska::TrackEntry::TrackType::Control), Media::TrackType::Unknown);
}
+
+TEST_CASE(strings_past_the_metadata_string_limit_are_corrupted)
+{
+ auto track_entry = MUST(parse_track_entry(make_track_entry_with_element_of_size(Media::Matroska::TRACK_NAME_ID, Media::MAX_METADATA_STRING_SIZE)));
+ auto name = track_entry->name();
+ EXPECT_EQ(name.bytes().size(), Media::MAX_METADATA_STRING_SIZE);
+
+ auto result = parse_track_entry(make_track_entry_with_element_of_size(Media::Matroska::TRACK_NAME_ID, Media::MAX_METADATA_STRING_SIZE + 1));
+ EXPECT(result.is_error());
+ EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::Corrupted);
+}
+
+TEST_CASE(codec_private_data_past_the_codec_configuration_limit_is_corrupted)
+{
+ auto result = parse_track_entry(make_track_entry_with_element_of_size(Media::Matroska::TRACK_CODEC_PRIVATE_ID, Media::MAX_CODEC_CONFIGURATION_SIZE + 1));
+ EXPECT(result.is_error());
+ EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::Corrupted);
+}