Memory corruption in Type 2 charstring roll operator during font subsetting
Description
The Type 2 (CFF/OpenType) charstring interpreter's roll operator does not validate its operands against the operand stack. It pops two operands after checking for only one, puts back itemsCount items even when fewer were taken off the stack, reads back()/front() of an empty list, and rotates the group shiftAmount times without bound. A crafted font corrupts memory (or hangs 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:1296 if(mOperandStack.size() < 1) -- then pops two operands (1302, 1304)
packages/native-with-source/src/deps/PDFWriter/CharStringType2Interpreter.cpp:1312 takes at most mOperandStack.size() items, but itemsCount is not bounded
packages/native-with-source/src/deps/PDFWriter/CharStringType2Interpreter.cpp:1321 shift loop runs shiftAmount (or -shiftAmount) times, unbounded; back()/front() on an empty list
packages/native-with-source/src/deps/PDFWriter/CharStringType2Interpreter.cpp:1337 put-back loop runs itemsCount times and calls front()/pop_front() on an exhausted list(Line numbers refer to the 7.2.3 source.)
Impact
Memory corruption and denial of service for applications that embed untrusted fonts (user-uploaded fonts, fonts passed through to writeText, Recipe text with a custom font). The faulty code runs at writer.end() during subsetting.
- Native:
1 rolland0 5 1 rollcorrupt the heap ("double free or corruption (out)", abort; SIGSEGV depending on build), the Node.js process terminates. - Wasm: AddressSanitizer reports "attempting free on address which was not malloc()-ed" on the interpreter's stack frame (
CFFFileInput.cpp:1168). The release build does not detect it: with1 rollor0 5 1 roll,end()returns a PDF normally after silently corrupting Wasm linear memory. - Both: a huge shift count keeps the CPU busy. Rolling one item about 2^30 times takes tens of seconds;
1 1 32767 32767 mul 32767 mul rollwas still running on native and Wasm after the timeout.
Proof of Concept
An OTF/CFF font whose charstring for a used glyph contains 1 roll, 0 5 1 roll or 1 1 32767 32767 mul 32767 mul roll. 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.3, Wasm1.2.3
Patches
Fixed in @muhammara/native-with-source and @muhammara/native 7.2.4 and @muhammara/wasm 1.2.4.
CharStringType2Interpreter::InterpretRoll now fails the charstring when fewer than two operands are on the stack, or when the item count is negative, larger than the remaining stack, or a real operand that does not fit a long (for example infinity or NaN). A real shift amount that does not fit a long rolls by zero; any other shift is reduced to the remainder of the shift amount divided by the item count. Real operands are range-checked before they are converted, so no undefined conversion remains. end() then throws (Unable to end PDF on native, Unable to finish PDF on Wasm) instead of corrupting memory, and a large shift completes immediately.
Workarounds
Only embed trusted fonts.
References
- Related: GHSA-32r6-f4hm-ccc4 (division by zero in the same interpreter's
divoperator). - 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.