Division by zero in Type 2 charstring div operator during font subsetting
Description
The Type 2 (CFF/OpenType) charstring interpreter performs integer division in the div operator without checking for a zero divisor or for LONG_MIN / -1. A crafted font crashes the process when text using it is written and the font is subset at the end of the document.
Root Cause
packages/native-with-source/src/deps/PDFWriter/CharStringType2Interpreter.cpp:961 newOperand.IntegerValue = valueA.IntegerValue / valueB.IntegerValue;
packages/native-with-source/src/deps/PDFWriter/CharStringType1Interpreter.cpp:518 Type 1 interpreter already checks valueB == 0 || (LONG_MIN, -1)(Line numbers refer to the 7.2.3 source.)
Impact
Denial of service for applications that embed untrusted fonts (user-uploaded fonts, fonts passed through to writeText, Recipe text with a custom font). The crash happens at writer.end() during subsetting.
- Native: SIGFPE (zero divisor, and
LONG_MIN / -1on 64-bit), the Node.js process terminates. - Wasm: the instance traps with a
RuntimeError("divide by zero", or "divide result unrepresentable" for the 32-bitINT_MIN / -1case).
Proof of Concept
An OTF/CFF font whose charstring for a used glyph contains 1 0 div (or -2147483648 -1 div / the 64-bit equivalent as integer operands). Write any text with the font and call end().
Affected Versions
- Ecosystem:
npm - Packages:
@muhammara/native-with-sourceand@muhammara/native<= 7.2.3;@muhammara/wasm<= 1.2.3 - Patched versions:
@muhammara/native-with-sourceand@muhammara/native7.2.4;@muhammara/wasm1.2.4 - Confirmed on: native
7.2.3and7.2.2, Wasm1.2.3and1.2.2
Patches
Fixed in @muhammara/native-with-source and @muhammara/native 7.2.4 and @muhammara/wasm 1.2.4.
CharStringType2Interpreter::InterpretDiv now fails the charstring on a zero divisor or on LONG_MIN / -1, as the Type 1 interpreter already did. end() then throws (Unable to end PDF on native, Unable to finish PDF on Wasm) instead of crashing; text whose glyphs run no such division embeds as before.
Workarounds
Only embed trusted fonts.
References
- Related: GHSA-fm69-f3cr-v22g (
rolloperator in the same interpreter). - The same code is in upstream galkahana/PDF-Writer.
---
Found by the project's scheduled WebAssembly fuzzing and the follow-up review of its results.