Three fuzzer fixes: subset advance-clamp timeout, raster y_bot overflow, raster surface-size cap (#6146)

* [subset] Clamp variable advances to UFWORD range in plan

When instancing, the hmtx/vmtx advance is computed by adding the
HVAR/VVAR delta to the base advance.  The delta is attacker-
controlled and unbounded, so the sum can go negative or exceed
65535.  The negative case, cast to unsigned, produced advances
near 4 billion in hmtx_map; CFF::optimize_widths () then looped
over the full [min_w, max_w] width range, taking minutes (the
oversized cumsum resize fails cleanly, so this was a pure hang,
no memory unsafety).

Clamp the computed advance to [0, 0xFFFF], matching the UFWORD
range that hmtx/vmtx serialization stores and the hb_max (0.f, …)
clamp already used by the runtime metrics path.

Fixes a ClusterFuzz timeout in hb-subset-fuzzer (testcase
4846730722738176, minimized from timeout-fd8f4bd2a650).  The
testcase went from >60s to 5ms; added as a fuzz seed.  Full
meson test suite passes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* [raster] Fix signed overflow computing row bottom at coordinate limit

edge_sweep_row () computed y_bot = y_top + ONE_PIXEL in int32.  The
scanline loop clamps y_top itself to INT32_MAX, but when the surface
sits at the extreme of the 24.8 fixed-point coordinate space y_top
can be within ONE_PIXEL of INT32_MAX (0x7FFFFF00), and the addition
overflows: UBSan "2147483392 + 256 cannot be represented in type
'int'".

Saturate the addition at INT32_MAX using the same 64-bit-then-clamp
idiom already used for y_top and the x interpolation.  Edges are
int32-clamped too, so no coverage exists beyond INT32_MAX and the
clipped bottom band loses nothing real.

Fixes ClusterFuzz issue 541858526 (testcase 5513551012495360).
Reproduced with a -Db_sanitize=undefined build: one UBSan report
before, clean after.  Testcase added as a fuzz seed.  Full meson
test suite passes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* [raster] Cap font-derived surface dimensions at 4096 pixels per side

When no extents are set explicitly, the paint and draw contexts
derive the surface size from font data: glyph extents (which the
auto path in hb_raster_paint_glyph_impl () feeds through
set_glyph_extents ()) or the accumulated outline bounds in
hb_raster_draw_render ().  A malicious font with a tiny upem and
huge glyph coordinates turns that into arbitrarily large
allocations: a 49 KB fuzz font produced several ~885 MB BGRA
surfaces live at once (root surface plus one per PaintComposite
push_group), nearly 4 GB total.

Add HB_RASTER_MAX_AUTO_DIMENSION (4096) to hb-limits.hh and clamp
width/height at the three font-derived sizing points: both
set_glyph_extents () implementations and draw render's
auto-size-from-edges path.  Since push_group and clip surfaces
inherit fixed_extents, the cap bounds those too.  Extents set
explicitly via hb_raster_{draw,paint}_set_extents () are not
limited.

Fixes an hb-raster-fuzzer out-of-memory finding (testcase
6344015149858816): peak RSS 3.47 GB before, 350 MB after.
Testcase added as a fuzz seed.  Full meson test suite passes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Sonnet 5 authored and GitHub committed 2026-08-10 13:34:39 -06:00
commit 0fef675a5e
7 files changed
+27 -7

No files matched your search

+8
View File
@@ -128,6 +128,14 @@
#define HB_RASTER_MAX_BUFFER_SIZE ((size_t) 1 << 30)
#endif
/* Maximum surface dimension (pixels per side) when extents are derived
* from font data (glyph extents or accumulated outline bounds). Bounds
* attacker-controlled allocations; extents set explicitly through
* hb_raster_{draw,paint}_set_extents() are not limited. */
#ifndef HB_RASTER_MAX_AUTO_DIMENSION
#define HB_RASTER_MAX_AUTO_DIMENSION 4096
#endif
#ifndef HB_REPACKER_MAX_ITERATIONS
#define HB_REPACKER_MAX_ITERATIONS 500
+10 -5
View File
@@ -346,6 +346,8 @@ hb_raster_draw_get_extents (const hb_raster_draw_t *draw,
* This is equivalent to computing a transformed bounding box in pixel
* space and calling hb_raster_draw_set_extents().
*
* The resulting dimensions are capped at 4096 pixels per side.
*
* Return value: `true` if transformed extents are non-empty and set;
* `false` otherwise.
*
@@ -396,8 +398,8 @@ hb_raster_draw_set_glyph_extents (hb_raster_draw_t *draw,
draw->fixed_extents = {
ex0, ey0,
(unsigned) ((int64_t) ex1 - ex0),
(unsigned) ((int64_t) ey1 - ey0),
(unsigned) hb_min ((int64_t) ex1 - ex0, (int64_t) HB_RASTER_MAX_AUTO_DIMENSION),
(unsigned) hb_min ((int64_t) ey1 - ey0, (int64_t) HB_RASTER_MAX_AUTO_DIMENSION),
0
};
draw->has_extents = true;
@@ -1057,7 +1059,10 @@ edge_sweep_row (int32_t *area,
unsigned &x_min,
unsigned &x_max)
{
int32_t y_bot = y_top + HB_RASTER_ONE_PIXEL;
/* Saturate: y_top can be within ONE_PIXEL of INT32_MAX when the surface
* sits at the extreme of the fixed-point coordinate space. Edges are
* int32-clamped too, so no coverage exists beyond INT32_MAX anyway. */
int32_t y_bot = (int32_t) hb_min ((int64_t) y_top + HB_RASTER_ONE_PIXEL, (int64_t) INT32_MAX);
int32_t ey0 = hb_max (edge.yL, y_top);
int32_t ey1 = hb_min (edge.yH, y_bot);
@@ -1299,8 +1304,8 @@ hb_raster_draw_render (hb_raster_draw_t *draw)
ext.x_origin = x0;
ext.y_origin = y0;
ext.width = (unsigned) hb_max (0, x1 - x0);
ext.height = (unsigned) hb_max (0, y1 - y0);
ext.width = (unsigned) hb_min (hb_max (0, x1 - x0), HB_RASTER_MAX_AUTO_DIMENSION);
ext.height = (unsigned) hb_min (hb_max (0, y1 - y0), HB_RASTER_MAX_AUTO_DIMENSION);
ext.stride = 0; /* filled below */
}
}
+4 -2
View File
@@ -2045,6 +2045,8 @@ hb_raster_paint_get_extents (const hb_raster_paint_t *paint,
* This is equivalent to computing a transformed bounding box in pixel
* space and calling hb_raster_paint_set_extents().
*
* The resulting dimensions are capped at 4096 pixels per side.
*
* Return value: `true` if transformed extents are non-empty and set;
* `false` otherwise.
*
@@ -2099,8 +2101,8 @@ hb_raster_paint_set_glyph_extents (hb_raster_paint_t *paint,
paint->fixed_extents = {
ex0, ey0,
(unsigned) ((int64_t) ex1 - ex0),
(unsigned) ((int64_t) ey1 - ey0),
(unsigned) hb_min ((int64_t) ex1 - ex0, (int64_t) HB_RASTER_MAX_AUTO_DIMENSION),
(unsigned) hb_min ((int64_t) ey1 - ey0, (int64_t) HB_RASTER_MAX_AUTO_DIMENSION),
0
};
paint->has_extents = true;
+5
View File
@@ -587,6 +587,10 @@ update_instance_metrics_map_from_cff2 (hb_subset_plan_t *plan)
if (_hmtx.var_table.get_length ())
hori_aw += (int) roundf (_hmtx.var_table->get_advance_delta_unscaled (old_gid, font->coords, font->num_coords,
hvar_store_cache));
/* Malicious deltas can take the advance out of the UFWORD range that
* hmtx serialization stores. Clamp, to keep downstream consumers
* (e.g. CFF width optimizer) bounded and consistent with hmtx. */
hori_aw = hb_clamp (hori_aw, 0, 0xFFFF);
int lsb = extents.x_bearing;
if (!has_bounds_info)
{
@@ -602,6 +606,7 @@ update_instance_metrics_map_from_cff2 (hb_subset_plan_t *plan)
if (_vmtx.var_table.get_length ())
vert_aw += (int) roundf (_vmtx.var_table->get_advance_delta_unscaled (old_gid, font->coords, font->num_coords,
vvar_store_cache));
vert_aw = hb_clamp (vert_aw, 0, 0xFFFF);
hb_position_t vorg_x = 0;
hb_position_t vorg_y = 0;
int tsb = 0;