Skip to content

Truly independent web browser

LibMedia+MediaServer: Cap decodeAudioData output at 512 MiB

Zaggy10246cffda19bd94
diff --git a/Libraries/LibMedia/DecodeAudioStream.cpp b/Libraries/LibMedia/DecodeAudioStream.cpp
index dc6b338fc8a..31db87c1f6a 100644
--- a/Libraries/LibMedia/DecodeAudioStream.cpp
+++ b/Libraries/LibMedia/DecodeAudioStream.cpp
@@ -20,7 +20,7 @@ namespace Media {
// Materializing an entire stream can require a large amount of memory for long inputs, so all sample storage is
// allocated fallibly and any failure is reported as a decoder error instead of crashing the process.
-static DecoderErrorOr<void> append_block_samples(AudioBlock const& block, DecodedAudioData& data)
+static DecoderErrorOr<void> append_block_samples(AudioBlock const& block, DecodedAudioData& data, size_t maximum_decoded_byte_count)
{
if (block.frame_count() == 0)
return {};
@@ -34,7 +34,10 @@ static DecoderErrorOr<void> append_block_samples(AudioBlock const& block, Decode
Checked<size_t> new_frame_count { data.channels[0].size() };
new_frame_count += block.frame_count();
- if (new_frame_count.has_overflow())
+ auto new_byte_count = new_frame_count;
+ new_byte_count *= data.channels.size();
+ new_byte_count *= sizeof(float);
+ if (new_byte_count.has_overflow() || new_byte_count.value() > maximum_decoded_byte_count)
return DecoderError::with_description(DecoderErrorCategory::Memory, "Decoded audio stream is too large"sv);
for (size_t channel = 0; channel < data.channels.size(); channel++) {
@@ -49,7 +52,7 @@ static DecoderErrorOr<void> append_block_samples(AudioBlock const& block, Decode
// to a single sample specification: the requested output sample rate (or the stream's native rate) combined with the
// stream's native channel map. Routing all blocks through the converter also makes streams whose sample specification
// changes mid-stream come out uniform.
-DecoderErrorOr<DecodedAudioData> decode_entire_audio_stream(NonnullRefPtr<MediaStream> const& stream, Optional<u32> output_sample_rate)
+DecoderErrorOr<DecodedAudioData> decode_entire_audio_stream(NonnullRefPtr<MediaStream> const& stream, Optional<u32> output_sample_rate, size_t maximum_decoded_byte_count)
{
auto demuxer = TRY(create_demuxer(stream));
auto track = TRY(demuxer->get_preferred_track_for_type(TrackType::Audio));
@@ -146,7 +149,7 @@ DecoderErrorOr<DecodedAudioData> decode_entire_audio_stream(NonnullRefPtr<MediaS
return block_result.release_error();
}
- TRY(append_block_samples(block, data));
+ TRY(append_block_samples(block, data, maximum_decoded_byte_count));
}
}
diff --git a/Libraries/LibMedia/DecodeAudioStream.h b/Libraries/LibMedia/DecodeAudioStream.h
index 289bd1ef879..b6436f6b691 100644
--- a/Libraries/LibMedia/DecodeAudioStream.h
+++ b/Libraries/LibMedia/DecodeAudioStream.h
@@ -13,6 +13,7 @@
#include <LibMedia/Export.h>
#include <LibMedia/Forward.h>
#include <LibMediaCodecs/DecoderError.h>
+#include <LibMediaCodecs/Limits.h>
namespace Media {
@@ -22,6 +23,6 @@ struct DecodedAudioData {
Vector<Vector<float>> channels;
};
-MEDIA_API DecoderErrorOr<DecodedAudioData> decode_entire_audio_stream(NonnullRefPtr<MediaStream> const&, Optional<u32> output_sample_rate = {});
+MEDIA_API DecoderErrorOr<DecodedAudioData> decode_entire_audio_stream(NonnullRefPtr<MediaStream> const&, Optional<u32> output_sample_rate = {}, size_t maximum_decoded_byte_count = MAX_DECODED_AUDIO_BYTE_COUNT);
}
diff --git a/Libraries/LibMediaCodecs/Limits.h b/Libraries/LibMediaCodecs/Limits.h
index 84f64c08e51..58ae1f22a7d 100644
--- a/Libraries/LibMediaCodecs/Limits.h
+++ b/Libraries/LibMediaCodecs/Limits.h
@@ -26,4 +26,6 @@ constexpr bool audio_sample_rate_is_outside_limits(u64 sample_rate)
return sample_rate < MIN_AUDIO_SAMPLE_RATE || sample_rate > MAX_AUDIO_SAMPLE_RATE;
}
+static constexpr size_t MAX_DECODED_AUDIO_BYTE_COUNT = 512 * MiB;
+
}
diff --git a/Services/MediaServer/ConnectionFromClient.cpp b/Services/MediaServer/ConnectionFromClient.cpp
index ffc93df05d3..5f4e2e84ac4 100644
--- a/Services/MediaServer/ConnectionFromClient.cpp
+++ b/Services/MediaServer/ConnectionFromClient.cpp
@@ -230,10 +230,11 @@ static Media::DecoderErrorOr<DecodedAudioSamples> decode_audio_data_to_shared_bu
auto planar_samples = planar_samples_or_error.release_value();
auto* destination = planar_samples.data<float>();
- for (auto const& channel : decoded.channels) {
+ for (auto& channel : decoded.channels) {
VERIFY(channel.size() == frame_count);
memcpy(destination, channel.data(), frame_count * sizeof(float));
destination += frame_count;
+ channel.clear();
}
return DecodedAudioSamples { decoded.sample_specification.sample_rate(), static_cast<u32>(decoded.channels.size()), frame_count, move(planar_samples) };
}
diff --git a/Tests/LibMedia/TestWav.cpp b/Tests/LibMedia/TestWav.cpp
index c01a80fd430..9b86d599bbf 100644
--- a/Tests/LibMedia/TestWav.cpp
+++ b/Tests/LibMedia/TestWav.cpp
@@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibMedia/DecodeAudioStream.h>
#include <LibMedia/DecoderRegistrations.h>
#include <Tests/LibMedia/TestMediaCommon.h>
@@ -75,3 +76,20 @@ TEST_CASE(underspecified_5_1_44khz)
{
decode_audio("WAV/tone_44100_5_1_underspecified.wav"sv, 44100, 6, 44100, Audio::ChannelMap::surround_5_1());
}
+
+TEST_CASE(decoding_an_entire_stream_refuses_audio_past_the_size_limit)
+{
+ auto file = MUST(Core::File::open("WAV/tone_8000_mono.wav"sv, Core::File::OpenMode::Read));
+ auto data = MUST(file->read_until_eof());
+ auto decode_with_limit = [&](size_t maximum_decoded_byte_count) {
+ return Media::decode_entire_audio_stream(Media::IncrementallyPopulatedStream::create_from_buffer(data), {}, maximum_decoded_byte_count);
+ };
+
+ static constexpr size_t decoded_byte_count = 40000 * sizeof(float);
+ auto decoded = decode_with_limit(decoded_byte_count);
+ EXPECT(!decoded.is_error());
+ EXPECT_EQ(decoded.value().channels.first().size(), 40000u);
+
+ auto refused = decode_with_limit(decoded_byte_count - sizeof(float));
+ EXPECT_EQ(refused.error().category(), Media::DecoderErrorCategory::Memory);
+}