Truly independent web browser
LibGfx: Bound WOFF table decompression by the table's orig_length
Andreas Kling487117c20ef8
diff --git a/Libraries/LibGfx/Font/WOFF/Loader.cpp b/Libraries/LibGfx/Font/WOFF/Loader.cppindex e2543c2f691..7098f78b704 100644--- a/Libraries/LibGfx/Font/WOFF/Loader.cpp+++ b/Libraries/LibGfx/Font/WOFF/Loader.cpp@@ -163,10 +163,18 @@ ErrorOr<NonnullRefPtr<Gfx::Typeface>> try_load_from_bytes(ReadonlyBytes buffer,if (entry.comp_length < entry.orig_length) {auto compressed_data_stream = make<FixedMemoryStream>(buffer.slice(entry.offset, entry.comp_length));auto decompressor = TRY(Compress::ZlibDecompressor::create(move(compressed_data_stream)));- auto decompressed = TRY(decompressor->read_until_eof());- if (entry.orig_length != decompressed.size())+ // NB: Decompress straight into the table's slot, so the output never grows past orig_length.+ Bytes table_data { font_buffer_data + font_buffer_offset, entry.orig_length };+ size_t decompressed_size = 0;+ while (decompressed_size < table_data.size() && !decompressor->is_eof())+ decompressed_size += TRY(decompressor->read_some(table_data.slice(decompressed_size))).size();+ if (decompressed_size != entry.orig_length)return Error::from_string_literal("Invalid decompressed WOFF table length");- memcpy(font_buffer_data + font_buffer_offset, decompressed.data(), entry.orig_length);+ while (!decompressor->is_eof()) {+ u8 extra_byte;+ if (!TRY(decompressor->read_some({ &extra_byte, 1 })).is_empty())+ return Error::from_string_literal("Invalid decompressed WOFF table length");+ }} else {if (entry.comp_length != entry.orig_length)return Error::from_string_literal("Invalid uncompressed WOFF table length");diff --git a/Tests/LibGfx/TestWOFF.cpp b/Tests/LibGfx/TestWOFF.cppindex be2943e0747..00902b84551 100644--- a/Tests/LibGfx/TestWOFF.cpp+++ b/Tests/LibGfx/TestWOFF.cpp@@ -9,10 +9,19 @@#define TEST_INPUT(x) ("test-inputs/" x)+TEST_CASE(load_compressed_woff)+{+ auto file = MUST(Core::MappedFile::map(TEST_INPUT("woff/valid.woff"sv)));+ auto font = TRY_OR_FAIL(WOFF::try_load_from_bytes(file->bytes()));+ EXPECT_EQ(font->family(), "Lato"_string);+ EXPECT_EQ(font->glyph_count(), 7u);+}+TEST_CASE(malformed_woff){Array test_inputs = {- TEST_INPUT("woff/invalid_sfnt_size.woff"sv)+ TEST_INPUT("woff/invalid_sfnt_size.woff"sv),+ TEST_INPUT("woff/decompressed_table_too_long.woff"sv)};for (auto test_input : test_inputs) {diff --git a/Tests/LibGfx/test-inputs/woff/decompressed_table_too_long.woff b/Tests/LibGfx/test-inputs/woff/decompressed_table_too_long.woffnew file mode 100644index 00000000000..1e7922d8343Binary files /dev/null and b/Tests/LibGfx/test-inputs/woff/decompressed_table_too_long.woff differdiff --git a/Tests/LibGfx/test-inputs/woff/valid.woff b/Tests/LibGfx/test-inputs/woff/valid.woffnew file mode 100644index 00000000000..18c5edefb35Binary files /dev/null and b/Tests/LibGfx/test-inputs/woff/valid.woff differ