Skip to content

DVI backend: fix a heap overflow in the PK font decoder and four other font parser bugs - #737

Open
gpoo wants to merge 5 commits into
linuxmint:masterfrom
gpoo:wip/gpoo/dvi-font-fixes
Open

gpoo wants to merge 5 commits into
linuxmint:masterfrom
gpoo:wip/gpoo/dvi-font-fixes

Conversation

@gpoo

@gpoo gpoo commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

These are fixes from Evince 48.5 (2026-11-16) for the DVI backend
(backend/dvi/mdvi-lib), which your code shares. Each commit is the
Evince commit as is, with a reference to it.

Two hunks needed only whitespace changes of context to apply.

The main one is a heap out-of-bounds write in the PK font decoder,
reported to Evince by David Chen:
https://gitlab.gnome.org/GNOME/evince/-/issues/2169

A long run count in a packed glyph overflows to a negative value and
the decoder writes before the start of the glyph bitmap; run and repeat
counts larger than the rows left write past its end. It is triggered by
opening a DVI document while a crafted PK font is in the font search
path, which includes the working directory. The reporter says it leads
to code execution; we reproduced the out-of-bounds write under
AddressSanitizer.

The other four are also out-of-bounds writes with lengths read from a
font file: the TFM/OFM font family, GF specials, VF packets, and a
truncated PK preamble.

We are requesting a CVE for #2169 from Red Hat's CNA, and will tell
them that this project is affected too. Please tell us if you have
requested one already, to avoid duplicates. We will request a CVE for
the other four as well.

Tested: Evince's test for the PK decoder (test-mdvi-pk), built against
this mdvi-lib under AddressSanitizer, overflows the heap on master and
passes with this branch; so does its truncated preamble case. The DVI
backend builds without warnings. The tests are not included, since
this project has no test setup for the backend.

gpoo added 5 commits December 4, 2026 10:03
The TFM and OFM loaders used Max (n, 63) instead of Min (n, 63) to
clamp the length of the font family. Since n comes from the font file,
a length between 64 and 127 wrote past the 64-byte family array and
corrupted the rest of TFMInfo. For short names, it also appended
garbage from the header to the family name.

Use Min(), so at most 63 bytes are copied.

Taken from Evince commit 2af8b504efa6898639ce4c78c7515970aae7e126
The length of a GF xxx4 special is a 32-bit value read into an int.
A crafted GF font could make it negative, which allocated a tiny buffer
and then made fread() write past it.

Hence, keep the length unsigned and reject lengths of INT_MAX or more.
Additionally, handle a NULL result in gf_read_bitmap(), which passed
it to mdvi_free() and aborted.

Taken from Evince commit b2501fe8cdddaa90eb220058994355218e66e575
The length of a long_char packet is read into an int, so a crafted VF
file could make it negative, or large enough to overflow the size of
the buffer. In both cases fread() wrote past the end of the buffer.

Treat those lengths as a corrupted file.

Taken from Evince commit 33108259a7819c091dbaa56b013234aa63426db2
When a PK file ended after its first two bytes, the length of the
comment was EOF, which became a huge unsigned length. The loop then
copied bytes into a buffer on the stack until it crashed.

Fail when the length cannot be read. Also, keep only the first 255
bytes of the specials (they are only used in a debug message), and stop
reading them at EOF.

Taken from Evince commit bc3bb1746d08d0506d85e262f193868bb8164d06
The decoder of packed PK glyphs trusted the counts read from the font:

* A long run count could overflow to a negative value, and the next
  run painted bits before the start of the bitmap. This is the heap
  overflow of the issue.
* A repeat count, or a run, larger than the rows left wrote past the
  end of the bitmap.
* A truncated glyph never ended.

Reject those counts, and stop at the end of the file.

In Evince, test-mdvi-pk decodes crafted glyphs: without this change
they overflow the heap (under AddressSanitizer) or hang. The 50363
glyphs of the 311 PK fonts generated by TeX Live on my machine still
decode. The test is not included here.

Reported by David Chen:
https://gitlab.gnome.org/GNOME/evince/-/issues/2169

Taken from Evince commit d19fae674cbf0b85f4fe825f6d8e2dd3d3b3c598
@gpoo gpoo changed the title Wip/gpoo/dvi font fixes DVI backend: fix a heap overflow in the PK font decoder and four other font parser bugs Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant