[GPOS] Charge propagate_attachment_offsets() base-to-mark walk to max_ops

propagate_attachment_offsets() turns a mark's stored attachment into an
offset by summing the advances of every glyph between the mark and its
base.  That distance can be up to 32767 glyphs (the range of
attach_chain), and position_finish_offsets() runs the pass once per
attached glyph, so the total work is the sum of all mark-to-base
distances and is not bounded by the font; it was never charged to
buffer->max_ops.  Attacker-chosen text over a cooperating font (or the
kerx path) could thus make hb_shape() spend several times the budgeted
CPU.

Thread the buffer into propagate_attachment_offsets() and charge the
base-to-mark distance to buffer->max_ops before the summation loops,
failing the shape when the budget runs out (the same budget the
unsafe_to_break walk over this range already uses).  Normal text, whose
marks sit next to their base, pays a negligible amount.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-11-10 19:23:24 -06:00
1 parent efe5821d7a
commit d6d66b9422
1 file changed
+19 -7
+19 -7
View File
@@ -13,7 +13,8 @@ using Layout::GPOS_impl::PosLookup;
namespace Layout {
static void
propagate_attachment_offsets (hb_glyph_position_t *pos,
propagate_attachment_offsets (hb_buffer_t *buffer,
hb_glyph_position_t *pos,
unsigned int len,
unsigned int i,
hb_direction_t direction,
@@ -69,7 +70,8 @@ struct GPOS : GSUBGPOS
static void
propagate_attachment_offsets (hb_glyph_position_t *pos,
propagate_attachment_offsets (hb_buffer_t *buffer,
hb_glyph_position_t *pos,
unsigned int len,
unsigned int i,
hb_direction_t direction,
@@ -91,7 +93,7 @@ propagate_attachment_offsets (hb_glyph_position_t *pos,
return;
if (pos[j].attach_chain())
propagate_attachment_offsets (pos, len, j, direction, nesting_level - 1);
propagate_attachment_offsets (buffer, pos, len, j, direction, nesting_level - 1);
assert (!!(type & GPOS_impl::ATTACH_TYPE_MARK) ^ !!(type & GPOS_impl::ATTACH_TYPE_CURSIVE));
@@ -110,6 +112,16 @@ propagate_attachment_offsets (hb_glyph_position_t *pos,
pos[i].y_offset = hb_saturate_add (pos[i].y_offset, pos[j].y_offset);
// i is the position of the mark; j is the base.
/* The base can be up to 32767 glyphs away (the range of attach_chain),
* and this runs once per mark, so the total work is unbounded by the
* font. Charge the walk to the buffer's work budget, the same one the
* unsafe_to_break walk over this range uses. */
buffer->max_ops -= (int) (j < i ? i - j : j - i);
if (unlikely (buffer->max_ops < 0))
{
buffer->successful = false;
return;
}
if (j < i)
{
/* This is the common case: mark follows base.
@@ -173,13 +185,13 @@ GPOS::position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
// https://github.com/harfbuzz/harfbuzz/issues/5514
if (HB_DIRECTION_IS_FORWARD (direction))
{
for (unsigned i = 0; i < len; i++)
for (unsigned i = 0; i < len && buffer->successful; i++)
if (pos[i].attach_chain())
propagate_attachment_offsets (pos, len, i, direction);
propagate_attachment_offsets (buffer, pos, len, i, direction);
} else {
for (unsigned i = len; i-- > 0; )
for (unsigned i = len; i-- > 0 && buffer->successful; )
if (pos[i].attach_chain())
propagate_attachment_offsets (pos, len, i, direction);
propagate_attachment_offsets (buffer, pos, len, i, direction);
}
}