typeface: tables may not overlap or sum past the file
A directory that points several tables at the same bytes passed the per-table bounds check, so an app copying each table (Slides renaming a face for its PDF) copied those bytes once per table: a 10 MiB file became gigabytes (final review, C1). Inspect now refuses an sfnt or WOFF whose tables overlap one another or the directory, or whose lengths sum past the file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+169
−0
typeface/bounds_test.go+111 −0typeface/typeface.go+45 −0typeface/woff.go+13 −0
diff --git a/typeface/bounds_test.go b/typeface/bounds_test.go| new file mode 100644 |
| index 0000000..f304e86 |
| --- /dev/null |
| +++ b/typeface/bounds_test.go |
| @@ -0,0 +1,111 @@ |
| +package typeface |
| + |
| +import ( |
| + "bytes" |
| + "encoding/binary" |
| + "errors" |
| + "fmt" |
| + "testing" |
| +) |
| + |
| +// An sfnt whose directory points several tables at the same bytes passes |
| +// a per-table bounds check, yet a reader that copies each table (an |
| +// app renaming the font for its PDF, say) copies those bytes once per |
| +// table: a 10 MiB file became 4.8 GB to copy (final review, C1). Tables |
| +// may not overlap, nor sum past the file. |
| + |
| +// pointedAt is data with the directory entries of the given tags set to |
| +// the offset and length of the table at. |
| +func pointedAt(t *testing.T, data []byte, at string, tags ...string) []byte { |
| + t.Helper() |
| + out := bytes.Clone(data) |
| + n := int(binary.BigEndian.Uint16(out[4:])) |
| + var off, length uint32 |
| + found := false |
| + for i := 0; i < n; i++ { |
| + rec := out[12+16*i:] |
| + if string(rec[:4]) == at { |
| + off, length, found = binary.BigEndian.Uint32(rec[8:]), binary.BigEndian.Uint32(rec[12:]), true |
| + } |
| + } |
| + if !found { |
| + t.Fatalf("no %q table", at) |
| + } |
| + for i := 0; i < n; i++ { |
| + rec := out[12+16*i:] |
| + for _, tag := range tags { |
| + if string(rec[:4]) == tag { |
| + binary.BigEndian.PutUint32(rec[8:], off) |
| + binary.BigEndian.PutUint32(rec[12:], length) |
| + } |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// withFillers is Liberation plus n small tables named z000, z001, ... |
| +func withFillers(t *testing.T, n int) ([]byte, []string) { |
| + t.Helper() |
| + v, ts := tablesOf(t, liberation(t)) |
| + var tags []string |
| + for i := 0; i < n; i++ { |
| + tag := fmt.Sprintf("z%03d", i) |
| + tags = append(tags, tag) |
| + ts = append(ts, table{tag, []byte{0, 0, 0, 0}}) |
| + } |
| + return buildSFNT(v, ts), tags |
| +} |
| + |
| +func TestInspectRefusesTablesThatShareBytes(t *testing.T) { |
| + // One table over another: the sum still fits the file. |
| + two := pointedAt(t, liberation(t), "hhea", "gasp") |
| + |
| + // A hundred tables over glyf, the amplification itself. |
| + many, tags := withFillers(t, 100) |
| + many = pointedAt(t, many, "glyf", tags...) |
| + |
| + // A table inside the table directory. |
| + inDir := bytes.Clone(liberation(t)) |
| + binary.BigEndian.PutUint32(inDir[12+8:], 0) // the first table at offset 0 |
| + |
| + // WOFF: two identical tables whose compressed bytes are one range. |
| + v, ts := tablesOf(t, liberation(t)) |
| + same := bytes.Repeat([]byte("same"), 64) |
| + woff := buildWOFF(t, buildSFNT(v, append(ts, table{"zzz1", same}, table{"zzz2", same}))) |
| + { |
| + n := int(binary.BigEndian.Uint16(woff[12:])) |
| + var off []byte |
| + for i := 0; i < n; i++ { |
| + rec := woff[44+20*i:] |
| + switch string(rec[:4]) { |
| + case "zzz1": |
| + off = bytes.Clone(rec[4:8]) |
| + case "zzz2": |
| + copy(rec[4:8], off) |
| + } |
| + } |
| + } |
| + |
| + for _, c := range []struct { |
| + name string |
| + data []byte |
| + }{ |
| + {"two tables over one range", two}, |
| + {"a hundred tables over glyf", many}, |
| + {"a table over the directory", inDir}, |
| + {"a WOFF with two tables over one range", woff}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + f, err := Inspect(c.data) |
| + if !errors.Is(err, ErrMalformed) { |
| + t.Fatalf("Inspect = %+v, %v; want ErrMalformed", f, err) |
| + } |
| + t.Log(err) |
| + }) |
| + } |
| + |
| + // Tables that only touch, and empty ones, are fine. |
| + touching, _ := withFillers(t, 3) |
| + mustInspect(t, touching) |
| + mustInspect(t, buildSFNT(v, append(ts, table{"zero", nil}))) |
| +} |
diff --git a/typeface/typeface.go b/typeface/typeface.go| index 49537c8..34df26c 100644 |
| --- a/typeface/typeface.go |
| +++ b/typeface/typeface.go |
| @@ -11,12 +11,14 @@ |
| package typeface |
| import ( |
| + "cmp" |
| "crypto/sha256" |
| "encoding/binary" |
| "encoding/hex" |
| "errors" |
| "fmt" |
| "net/http" |
| + "slices" |
| "strings" |
| "unicode" |
| "unicode/utf16" |
| @@ -184,6 +186,7 @@ func readSFNT(data []byte) (fontTables, error) { |
| return fontTables{}, malformed("table directory cut short: %d tables", n) |
| } |
| ft := fontTables{version: binary.BigEndian.Uint32(data), has: map[string]bool{}, data: map[string][]byte{}} |
| + spans := make([]span, 0, n) |
| for i := 0; i < n; i++ { |
| rec := data[12+16*i:] |
| tag := string(rec[:4]) |
| @@ -196,13 +199,55 @@ func readSFNT(data []byte) (fontTables, error) { |
| return fontTables{}, malformed("table %q appears twice", tag) |
| } |
| ft.has[tag] = true |
| + spans = append(spans, span{tag, off, length}) |
| if wanted(tag) { |
| ft.data[tag] = data[off : off+length] |
| } |
| } |
| + if err := disjoint(spans, uint64(12+16*n), uint64(len(data))); err != nil { |
| + return fontTables{}, err |
| + } |
| return ft, nil |
| } |
| +// span is where one table's bytes lie in a file. |
| +type span struct { |
| + tag string |
| + off, length uint64 |
| +} |
| + |
| +// disjoint refuses tables whose bytes overlap one another or the header |
| +// and directory before them (the first `header` bytes), and tables whose |
| +// lengths sum past the file's size. Each table is in bounds already; this |
| +// is what stops a reader that copies every table from copying the same |
| +// bytes once per table that names them. |
| +func disjoint(spans []span, header, size uint64) error { |
| + var sum uint64 |
| + for _, s := range spans { |
| + if sum += s.length; sum > size { |
| + return malformed("tables sum to more than the file's %d bytes", size) |
| + } |
| + } |
| + sorted := slices.Clone(spans) |
| + slices.SortFunc(sorted, func(a, b span) int { |
| + return cmp.Or(cmp.Compare(a.off, b.off), cmp.Compare(a.length, b.length)) |
| + }) |
| + end, last := header, "" |
| + for _, s := range sorted { |
| + if s.length == 0 { |
| + continue |
| + } |
| + if s.off < end { |
| + if last == "" { |
| + return malformed("table %q overlaps the table directory", s.tag) |
| + } |
| + return malformed("tables %q and %q overlap", last, s.tag) |
| + } |
| + end, last = s.off+s.length, s.tag |
| + } |
| + return nil |
| +} |
| + |
| // describe turns the tables into a Face (all but Ref and Format). |
| func describe(ft fontTables) (Face, error) { |
| var f Face |
diff --git a/typeface/woff.go b/typeface/woff.go| index 1c6c4fa..8e5c37f 100644 |
| --- a/typeface/woff.go |
| +++ b/typeface/woff.go |
| @@ -38,6 +38,19 @@ func readWOFF(data []byte) (fontTables, error) { |
| return fontTables{}, malformed("WOFF table directory cut short: %d tables", n) |
| } |
| ft := fontTables{version: flavor, has: map[string]bool{}, data: map[string][]byte{}} |
| + spans := make([]span, 0, n) |
| + for i := 0; i < n; i++ { |
| + e := data[headerLen+entryLen*i:] |
| + spans = append(spans, span{string(e[:4]), uint64(binary.BigEndian.Uint32(e[4:])), uint64(binary.BigEndian.Uint32(e[8:]))}) |
| + } |
| + for _, s := range spans { |
| + if s.off+s.length > uint64(len(data)) { |
| + return fontTables{}, malformed("table %q runs past the end of the file", s.tag) |
| + } |
| + } |
| + if err := disjoint(spans, uint64(headerLen+entryLen*n), uint64(len(data))); err != nil { |
| + return fontTables{}, err |
| + } |
| var unpacked uint64 |
| for i := 0; i < n; i++ { |
| e := data[headerLen+entryLen*i:] |