Commit Graph
5 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 4.8 b76754924e [vector] Make the document-size cap per document, not per buffer
HB_VECTOR_MAX_DOCUMENT_SIZE was enforced in hb_vector_buf_t against the
length of the one buffer being appended to, but both backends spread one
document across many buffers, so the cap bounded each buffer and not the
document.  A few-KB COLRv1 font could exceed it by a large multiple:

- SVG starts a fresh buffer per open paint group, so a chain of nested
  PaintComposite records keeps dozens of near-cap backdrop buffers alive
  at once (about 1 GB from a 1.3 KB font).  An open group now inherits
  the bytes already held by the buffers it will be appended to, via a
  base member folded into would_overflow(), so base + length of every
  open group stays within the cap.

- PDF keeps every object (functions, shadings, ExtGStates, images) in a
  buffer of its own with no running total, so one large gradient painted
  thousands of times produces millions of objects (gigabytes) that are
  only summed at render.  add_object() now charges a running total
  against the same cap with a sticky overflow flag; the image and
  gradient callbacks stop when res->in_error(), and render returns NULL
  in that state because the content stream would refer to dropped
  objects.

hb_vector_buf_t and hb_pdf_resources_t are internal, so there is no API
or ABI change; documents that fit under the cap are unchanged.
Regression tests are added to test/vector/test-vector-budget.c.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-C20MD9VR.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:57:14 -06:00
Behdad EsfahbodandClaude Opus 4.8 14e1f86467 [vector] Decline color_glyph in the SVG paint backend
The SVG backend's color_glyph callback re-entered hb_font_paint_glyph()
for every COLRv1 PaintColrGlyph, and each re-entry built a fresh COLR
paint context whose HB_MAX_GRAPH_EDGE_COUNT and HB_MAX_NESTING_LEVEL
counters started over.  The limits therefore applied per nested glyph
instead of per top-level glyph, so a DAG in which each level references
the next one f times was walked f^levels times, letting a sub-2 KB font
spin indefinitely; the same per-re-entry depth reset let a crafted font
overflow a small thread stack.

Return false, as hb-raster and the PDF backend do (and as the COLR and
FreeType walkers expect), so the walker paints the nested glyph inline
in the same context, where the existing decycler, nesting limit and edge
limit bound the whole graph.  The color_glyph_depth counter and
active_color_glyphs set existed only to guard the re-entry, so they are
dropped from hb_vector_paint_t and from create, destroy and clear.

A regression test building a 30-level, 3-reference COLR chain in memory
is added to test/vector/test-vector-budget.c for both SVG and PDF.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-YR4RMFWV.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:55:57 -06:00
Behdad EsfahbodandClaude Opus 4.8 a14c095e3f [vector] Refuse PDF indexed-PNG soft masks larger than the document cap
hb_pdf_build_indexed_smask() allocated and filled (width+1)*height and
width*height bytes straight from the attacker-controlled PNG IHDR, while
HB_VECTOR_MAX_DOCUMENT_SIZE was only consulted when the finished mask was
appended to the PDF object.  A ~1 MB sbix/CBDT font could thus make the
PDF backend inflate and un-filter up to a 4 GiB mask per painted glyph,
only to drop it afterwards.

The mask is emitted uncompressed into a document-capped hb_vector_buf_t,
so a mask larger than the cap can never be appended: reject it alongside
the existing 32-bit overflow guards in the builder (the same pattern as
the raster PNG decoder's HB_RASTER_MAX_BUFFER_SIZE check), which bounds
both allocations (raw_len = mask_len + height) before any work is done.

add_xobject_png_image() additionally skips registering and referencing
the SMask object when its buffer tripped the cap, so the output no longer
contains a dangling empty-stream object for an edge-case mask that fits
the builder but not the object with its header.  Masks that fit today are
unaffected.

A regression test in test/vector/test-vector-budget.c hand-rolls a zlib
stream for a 4097x4096 indexed PNG with tRNS, paints it to PDF, and
asserts the render succeeds without an /SMask.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-6AWUPU2N.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:54:23 -06:00
Behdad Esfahbod 7eec305e71 [vector] Cap serialized document size; un-conflate output from outline
The vector work budget charged both outline traversal and serialized
output bytes, and left embedded bitmaps entirely uncharged -- so a COLR
walk with many sweep-gradient or image layers could emit output that the
outline budget never sees, bounded only by the structural per-node caps
times the node count.

Bound the output with a flat document-size cap instead: hb_vector_buf_t
refuses to grow past HB_VECTOR_MAX_DOCUMENT_SIZE (16 MB) and folds that
into in_error(), so every emission site that already checks it -- path
commands, gradient meshes -- stops cleanly, and image embedding is bounded
for the first time.  clear() resets the sticky flag so a context reused
across renders (the documented clear -> paint -> render loop) recovers
after one over-cap glyph.  vector is one glyph per context, so a flat cap
suffices; SVG's whole document and PDF's (assembled through the same
hb_vector_buf_t) share it.

With output bounded structurally, the work budget goes back to pure
outline traversal: drop the per-command byte charge and the sweep-patch
charge so it no longer conflates two different resources.  It is still
needed on its own -- a CFF charstring can do a lot of work while emitting
little output.

Once the cap trips, remaining appends are dropped, so a truncated document
is malformed; render returns it (or null) rather than looping to build
gigabytes, which is the point.

Add a regression test that paints an oversized image and confirms the
document stays bounded and the context recovers on reuse; it fails if the
cap is removed.

Tests: meson test -C build (275/275 passed); the vector COLR regression
seed completes in 0.85s.

Assisted-by: Claude <noreply@anthropic.com>
2026-08-27 17:38:30 -06:00
Behdad Esfahbod b0606f9d8a [vector] Share rendering work budgets
Store budget policy and live state on vector draw and paint contexts.  Path
sinks borrow the owning context's live counter, making glyph traversal,
serialized path commands, and sweep-gradient patches consume one aggregate
budget throughout a clear-to-render session.

Recharge the configured policy on clear, restore the finite default on
reset, and expose the generic draw and paint budget callbacks for SVG and
PDF contexts.  Add lifecycle and traversal-depletion coverage for both
formats.

Tests: meson test -C build (273/273 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:17:20 -06:00