Skip to content

[font/opentype] do not size table buffers from untrusted lengths - #312

Open
lyn1dev wants to merge 1 commit into
go-text:mainfrom
lyn1dev:fix-table-buffer-bounds
Open

lyn1dev wants to merge 1 commit into
go-text:mainfrom
lyn1dev:fix-table-buffer-bounds

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 23, 2026

Copy link
Copy Markdown

What was wrong

Loader.findTableBuffer sized a buffer from the table directory before reading any table data:

  • for an uncompressed OTF/WOFF table, s.length was passed to make([]byte, ...) and ReadAt was called afterwards;
  • for a compressed WOFF table, s.zLength (the announced uncompressed length) was passed to make([]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

  • Loader now keeps the resource size, recorded in parseOneFont.
  • findTableBuffer rejects a table whose declared range is outside the resource, with the existing unsupported table offset or length error, before any allocation. Tables of null length keep their previous behaviour.
  • For a compressed WOFF table, unless the caller passes a dst with enough capacity, the table is decoded into a buffer that grows with the data actually produced (io.ReadAll over io.LimitReader(r, zLength)); a short result is still reported as io.ErrUnexpectedEOF. A lying zLength therefore 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

TestRawTableOutOfBounds builds 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 of tiny while announcing a 256MB uncompressed length. For each one, RawTable must return an error and runtime.MemStats must show less than 1MB allocated.

Without the fix, all three subtests fail on the allocation assertion (256MB allocated each); with the fix they pass:

$ go test ./font/opentype/ -run TestRawTableOutOfBounds -v
--- PASS: TestRawTableOutOfBounds (0.00s)
    --- PASS: TestRawTableOutOfBounds/uncompressed_table_outside_the_resource (0.00s)
    --- PASS: TestRawTableOutOfBounds/uncompressed_table_after_the_resource (0.00s)
    --- PASS: TestRawTableOutOfBounds/compressed_table_with_a_bogus_uncompressed_length (0.00s)

Two existing tests build Loader literals directly and now set the new size field (TestSlicedResourceEmptyTableAtEOF, TestResourceSlicerErrors).

Full suite, vet and build on windows/amd64, Go 1.27.3:

$ go vet ./...        # no output
$ go build ./...      # ok
$ go test ./...       # all packages ok

Fixes #302

AI was used to write this code.

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.
@andydotxyz

Copy link
Copy Markdown
Contributor

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?

@lyn1dev

lyn1dev commented Nov 23, 2026

Copy link
Copy Markdown
Author

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:

  • An uncompressed table's declared range must fit inside the resource, so its buffer is at most the file size; the same 28-byte file now fails with unsupported table offset or length and allocates nothing.
  • A compressed table is decoded into a buffer that grows with the bytes actually produced, so a lying OrigLength no longer sizes anything. The same 2 KB WOFF now allocates ~2 MB, which is the real ~1 MiB of decompressed output it contains.

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.

@andydotxyz

Copy link
Copy Markdown
Contributor

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.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[font/opentype] table length from the font header sets the buffer size before any read

2 participants