Repository navigation
Conversation
The table directory was used to size a buffer before any table data was read: a small file could declare a multi gigabyte table and make the loader allocate it. Compressed WOFF tables used both the compressed section and the uncompressed length unchecked. Keep the resource size in the Loader and reject tables whose declared range is outside it. For WOFF tables, decode into a buffer growing with the actual data, unless the caller provides a buffer large enough to hold the announced size.
|
I don't understand the declared fix need - the declared size is used without checking so a malicious font can allocate lots of memory - this indeed sounds unfortunate. However you then say that an intentional deflate bomb can still cause the same outcome - so is the fix incomplete? |
|
The two cases differ in kind, so the fix is not a partial version of the bomb case. Before the change the buffer size came from the declared length alone. A 28-byte OTF naming a 256 MB table allocated 256 MB, and a 2 KB WOFF declaring a 2 GiB table allocated ~2 GB — memory chosen by the font for free, with no data behind it. After the change:
A deliberate deflate bomb therefore still exists, but it is the narrower case: the compressed bytes have to be present and really expand, and the output is capped by deflate's expansion ratio (~1000:1 at most, so up to hundreds of MB per MB of input) rather than by a number in the header. Removing that last factor needs an explicit limit; since legitimate tables can be large (sbix), such a cap would have to be generous. That could be a follow-up if a hard ceiling is wanted. |
But isn't the font declaring it and so that is the data behind the sizing (not arbitrary) - so both are malicious fonts as a source of memory over-allocation? |
What was wrong
Loader.findTableBuffersized a buffer from the table directory before reading any table data:s.lengthwas passed tomake([]byte, ...)andReadAtwas called afterwards;s.zLength(the announced uncompressed length) was passed tomake([]byte, ...)and only then decoded into.Neither length was compared with the size of the resource, so a font file of a few dozen bytes could declare a multi-gigabyte table and make the loader allocate it before the read failed with
io.EOF/io.ErrUnexpectedEOF. Fonts are routinely loaded from untrusted sources (fontscan over user directories, web fonts, documents with embedded fonts).Fix
Loadernow keeps the resource size, recorded inparseOneFont.findTableBufferrejects a table whose declared range is outside the resource, with the existingunsupported table offset or lengtherror, before any allocation. Tables of null length keep their previous behaviour.dstwith enough capacity, the table is decoded into a buffer that grows with the data actually produced (io.ReadAlloverio.LimitReader(r, zLength)); a short result is still reported asio.ErrUnexpectedEOF. A lyingzLengththerefore no longer sizes an allocation.A zlib stream can still legitimately expand (up to the deflate maximum), so a deliberate compression bomb remains possible; bounding that would need an explicit implementation limit, which this change does not add.
Tests
TestRawTableOutOfBoundsbuilds three minimal files: an OTF table whose range ends past the end of the file, an OTF table whose offset is past the end, and a WOFF table holding a valid zlib stream oftinywhile announcing a 256MB uncompressed length. For each one,RawTablemust return an error andruntime.MemStatsmust show less than 1MB allocated.Without the fix, all three subtests fail on the allocation assertion (256MB allocated each); with the fix they pass:
Two existing tests build
Loaderliterals directly and now set the newsizefield (TestSlicedResourceEmptyTableAtEOF,TestResourceSlicerErrors).Full suite, vet and build on windows/amd64, Go 1.27.3:
Fixes #302
AI was used to write this code.