diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 49073d3a8..97d01128a 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -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 diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc index ece9e068d..763a85b85 100644 --- a/src/hb-raster-draw.cc +++ b/src/hb-raster-draw.cc @@ -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 */ } } diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index edee5fd3a..360ae29a8 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -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; diff --git a/src/hb-subset-plan-var.cc b/src/hb-subset-plan-var.cc index ea28fcc0d..c8068f72b 100644 --- a/src/hb-subset-plan-var.cc +++ b/src/hb-subset-plan-var.cc @@ -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; diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-5513551012495360 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-5513551012495360 new file mode 100644 index 000000000..62f6138ee Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-5513551012495360 differ diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6344015149858816 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6344015149858816 new file mode 100644 index 000000000..85daa6cff Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6344015149858816 differ diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-subset-fuzzer-4846730722738176 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-subset-fuzzer-4846730722738176 new file mode 100644 index 000000000..b5d0a14da Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-subset-fuzzer-4846730722738176 differ