Heap buffer overflow (OOB write) in HarfBuzz apply_stch() via crafted web font
An integer overflow in HarfBuzz's apply_stch() function causes a heap out-of-bounds write when processing a crafted OpenType font with many Arabic kashida stretching groups. An attacker could crash Firefox's content process by serving a malicious webpage with an embedded font and Arabic text.
Originally disclosed on Mozilla
- Fixed in 4 days
- Paid in 24 days
- Disclosed after 4 days
Created attachment 9550995 poc.zip
An integer overflow in HarfBuzz's apply_stch() function causes a heap out-of-bounds write that crashes Firefox's content process. A malicious webpage can trigger this by serving a crafted OpenType font (which passes OTS) and rendering Arabic text via canvas or DOM. No user interaction is required beyond navigating to the page.
This bug is in upstream HarfBuzz and will be reported simultaneously to the HarfBuzz maintainers.
Affected Product
- Firefox: 148.0 (Manjaro Linux, dynamically linked against system HarfBuzz 12.3.2)
- HarfBuzz: All versions up to and including 13.0.0 (current upstream)
- OS: Linux x86_64 (confirmed), all platforms affected (platform-independent code)
Steps to Reproduce
- Open the attached
poc_firefox.htmlin Firefox (self-contained, crafted font embedded as base64 data URI) - The content process crashes with SIGSEGV
The page loads a crafted font via @font-face (CSS data URI), builds a string of 131,082 Arabic characters (3 groups of [1 baa U+0628 + 43,693 alefs U+0627]), and uses canvas.measureText() or DOM rendering to trigger HarfBuzz text shaping.
Expected Behavior
The page renders without crashing.
Actual Behavior
The Firefox content process crashes with SIGSEGV (signal 11) when HarfBuzz's apply_stch() function writes glyph data past the end of the heap-allocated buffer.
Firefox Crash Report
ProcessType: content
CrashType: SIGSEGV / SEGV_MAPERR
ContentSandboxLevel: 6
DOMFissionEnabled: 1
Version: 148.0
ASAN Stack Trace
Built HarfBuzz 13.0.0 from upstream source with -Db_sanitize=address, reproduced with a standalone C test program (./test_multi_group StchFx.ttf 1000 43693 3):
==417213==ERROR: AddressSanitizer: SEGV on unknown address 0x7b9fcd0477ec
==417213==The signal is caused by a WRITE memory access.
#0 apply_stch ../src/hb-ot-shaper-arabic.cc:616
#1 postprocess_glyphs_arabic ../src/hb-ot-shaper-arabic.cc:652
#2 hb_ot_substitute_post ../src/hb-ot-shape.cc:963
#3 hb_ot_shape_internal ../src/hb-ot-shape.cc:1195
#4 _hb_ot_shape ../src/hb-ot-shape.cc:1213
#5 _hb_shape_plan_execute_internal ../src/hb-shaper-list.hh:47
#6 hb_shape_plan_execute ../src/hb-shape-plan.cc:452
#7 hb_shape_full ../src/hb-shape.cc:150
#8 hb_shape ../src/hb-shape.cc:194
#9 main test_multi_group.c
SUMMARY: AddressSanitizer: SEGV ../src/hb-ot-shaper-arabic.cc:616 in apply_stch
Line 616 is info[j] = info[k - 1]; — writing glyph info through a wrapped unsigned index j into unmapped memory.
Root Cause
File: src/hb-ot-shaper-arabic.cc, function apply_stch()
The function implements Arabic kashida stretching (STCH feature) in two passes: MEASURE and CUT.
During MEASURE, it accumulates extra glyphs needed across all STCH groups:
unsigned int extra_glyphs_needed = 0; // 32-bit unsigned
// ... for each STCH group:
extra_glyphs_needed += n_copies * n_repeating; // line 587
The buffer is then allocated to hold count + extra_glyphs_needed glyphs. The CUT pass writes n_copies repetitions of each repeating tile into the buffer using a backwards-decrementing index j:
--j;
info[j] = info[k - 1]; // line 616 — the crashing write
pos[j] = pos[k - 1]; // line 617
The bug: extra_glyphs_needed accumulates across all STCH groups in the buffer. A crafted font that produces M groups, each contributing a large product P = n_copies * n_repeating, can make the sum M × P overflow uint32, wrapping to a small value. The buffer is then under-allocated, and the CUT pass writes past the end of the buffer.
Important detail: buffer ordering
The apply_stch() function runs during hb_ot_substitute_post(), which is called AFTER hb_ot_position(). For RTL text (Arabic), hb_ot_position() reverses the buffer into visual order (line 1102-1103 in hb-ot-shape.cc). The apply_stch() function does NOT reverse the buffer back for RTL (line 488: if (!rtl) buffer->reverse()). As a result, apply_stch processes the buffer in visual (reversed) order, where each STCH tile group has its context alefs BEFORE it. This means all M groups contribute to extra_glyphs_needed, not just M-1.
Firefox-Specific Details
Firefox's HarfBuzz integration sets hb_font_set_scale(font, upem, upem) where upem=1000, making the HarfBuzz scale independent of CSS font-size. This means:
alef_adv = 32767(raw design units, confirmed via canvas.measureText: 524.27px at 16px = 32767 × 16/1000)tile_adv = 1(raw design units)- The overflow parameters are constant regardless of font-size
Overflow Arithmetic
The crafted font maps baa (U+0628) to 5000 tile glyphs via MultipleSubst under the stch feature. Tiles alternate STCH_FIXED and STCH_REPEATING (2500 each), all with advance=1 at upem=1000.
The stch feature is applied first in the Arabic shaper (before init/medi/fina/isol), so baa is expanded to tiles before joining features run. The joining features then map alef (U+0627) to alef.fina/alef.isol, all with advance=32767.
With Firefox's scale=1000 (upem), N=43,693 alefs per group, M=3 groups:
For each STCH group:
w_total= 43,693 × 32,767 = 1,431,688,531w_fixed= 2,500 × 1 = 2,500w_repeating= 2,500 × 1 = 2,500w_remaining= 1,431,688,531 − 2,500 = 1,431,686,031n_copies= 1,431,686,031 / 2,500 − 1 = 572,673- Shortfall = 1,431,686,031 − 2,500 × 572,674 = 1,031 > 0 →
++n_copies= 572,674 - Per-group product: P = 572,674 × 2,500 = 1,431,685,000
Accumulation across 3 groups (extra_glyphs_needed is unsigned int, 32-bit):
- After group 1: 1,431,685,000
- After group 2: 2,863,370,000
- After group 3: 4,295,055,000 → wraps uint32 to 87,704 (4,295,055,000 − 4,294,967,296 = 87,704)
After GSUB, buffer count = 131,082 + 3 × 4,999 = 146,079:
- Buffer allocated for: 146,079 + 87,704 = 233,783 glyphs (~9.4 MB)
- CUT pass attempts to write: 146,079 + 3 × 1,431,685,000 = 4,295,201,079 entries → SIGSEGV
Font Requirements for Firefox
Firefox requires Arabic shaping features (init/medi/fina/isol) to use a web font for Arabic text. Without them, Firefox falls back to a system Arabic font, preventing the stch feature from triggering. The crafted font includes these features (SingleSubst identity substitutions for alef and baa variants) to satisfy Firefox's requirements.
What Was Demonstrated
- Content process crash (SIGSEGV) triggered by web content — a crafted font served via
@font-facewith a data URI - The crafted font passes OTS (OpenType Sanitizer) — confirmed by
ots-sanitizeand successful font loading in Firefox - The crash is a WRITE to an unmapped address (confirmed by ASAN "caused by a WRITE memory access")
- Reproduced on system HarfBuzz 12.3.2 (Arch/Manjaro Firefox 148.0) and upstream HarfBuzz 13.0.0 (ASAN build)
- Firefox Fission/Content Sandbox is enabled (level 6), so this is a sandbox escape prerequisite — the content process crash is within the sandbox
Attachments
- poc_firefox.html — Self-contained PoC (crafted font embedded as base64 data URI). Open in Firefox to crash the content process.
- StchFx.ttf — The crafted OpenType font with Arabic shaping features (190KB).
- gen_firefox_font.py — Font generator script (requires fonttools).
- test_multi_group.c — Standalone C reproduction program for ASAN testing. Usage:
./test_multi_group StchFx.ttf 1000 43693 3(scale=1000 matches Firefox's upem-based scaling).
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the variable `extra_glyphs_needed` is a 32-bit unsigned integer that accumulates the product of copies and repeating tiles across all STCH groups, and with enough groups the sum overflows and wraps to a small value, causing under-allocation of the glyph buffer.
Impact
A malicious webpage could crash the Firefox content process, and because the crash is a write to an unmapped address, it may be exploitable for code execution within the sandbox, though the report only demonstrates a crash.
What the attacker needed
Victim visits a malicious webpage; crafted font passes OTS; Arabic text rendering enabled
How it was found
- Open the attached HTML file in Firefox.
- The page loads a crafted font via @font-face and renders Arabic text.
- The content process crashes with SIGSEGV.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A font file can contain instructions for stretching Arabic letters with decorative marks. The software that reads these instructions (HarfBuzz) miscounts how much memory it needs when there are many such marks, so it writes past the end of a buffer. This can crash the browser tab, and in theory could be used for more serious attacks.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.