From c4d252e090da4bace159fb7ceb15345e98eef47c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Poo-Caama=C3=B1o?= Date: Fri, 13 Nov 2026 01:25:21 -0300 Subject: [PATCH 1/5] backends: Fix buffer overflow reading the TFM font family 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 --- backend/dvi/mdvi-lib/tfmfile.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/dvi/mdvi-lib/tfmfile.c b/backend/dvi/mdvi-lib/tfmfile.c index c23d9fe0..c3c9e94d 100644 --- a/backend/dvi/mdvi-lib/tfmfile.c +++ b/backend/dvi/mdvi-lib/tfmfile.c @@ -242,7 +242,7 @@ int tfm_load_file(const char *filename, TFMInfo *info) if(lh > 12) { n = msget1(ptr); if(n > 0) { - i = Max(n, 63); + i = Min(n, 63); memcpy(info->family, ptr, i); info->family[i] = 0; } else @@ -344,7 +344,7 @@ static int ofm1_load_file(FILE *in, TFMInfo *info) if(lh > 12) { n = fsget1(in); if(n > 0) { - i = Max(n, 63); + i = Min(n, 63); fread(info->family, i, 1, in); info->family[i] = 0; } else @@ -521,7 +521,7 @@ static int ofm_load_file(const char *filename, TFMInfo *info) if(lh > 12) { n = fsget1(in); if(n > 0) { - i = Max(n, 63); + i = Min(n, 63); fread(info->family, i, 1, in); info->family[i] = 0; } else From 24b2493f5c953c10e139538cf1a77e03456ad21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Poo-Caama=C3=B1o?= Date: Fri, 13 Nov 2026 01:32:48 -0300 Subject: [PATCH 2/5] backends: Fix out-of-bounds write in mdvi read_string() 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 --- backend/dvi/mdvi-lib/common.c | 5 ++++- backend/dvi/mdvi-lib/gf.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/dvi/mdvi-lib/common.c b/backend/dvi/mdvi-lib/common.c index 97b34b56..1cefdbb2 100644 --- a/backend/dvi/mdvi-lib/common.c +++ b/backend/dvi/mdvi-lib/common.c @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -66,10 +67,12 @@ Ulong mugetn(const Uchar *p, size_t n) char *read_string(FILE *in, int s, char *buffer, size_t len) { - int n; + Ulong n; char *str; n = fugetn(in, s ? s : 1); + if(n >= INT_MAX) + return NULL; if((str = buffer) == NULL || n + 1 > len) str = mdvi_malloc(n + 1); if(fread(str, 1, n, in) != n) { diff --git a/backend/dvi/mdvi-lib/gf.c b/backend/dvi/mdvi-lib/gf.c index 00607ff6..71a5c700 100644 --- a/backend/dvi/mdvi-lib/gf.c +++ b/backend/dvi/mdvi-lib/gf.c @@ -190,6 +190,8 @@ static int gf_read_bitmap(FILE *p, DviFontChar *ch) char *s; s = read_string(p, op - GF_XXX1 + 1, NULL, 0); + if(s == NULL) + goto error; DEBUG((DBG_SPECIAL, "(gf) Character %d: Special \"%s\"\n", ch->code, s)); mdvi_free(s); From 5820bc1a1279a9bd676fd0715d1bcf1759ac0685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Poo-Caama=C3=B1o?= Date: Fri, 13 Nov 2026 01:33:18 -0300 Subject: [PATCH 3/5] backends: Reject invalid VF packet lengths 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 --- backend/dvi/mdvi-lib/vf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/backend/dvi/mdvi-lib/vf.c b/backend/dvi/mdvi-lib/vf.c index a5ae3bbe..3cd70bc7 100644 --- a/backend/dvi/mdvi-lib/vf.c +++ b/backend/dvi/mdvi-lib/vf.c @@ -18,6 +18,7 @@ */ #include +#include #include #include "mdvi.h" @@ -165,6 +166,8 @@ static int vf_load_font(DviParams *params, DviFont *font) cc = fuget1(p); tfm = fuget3(p); } + if(pl < 0 || pl > INT_MAX - 256 - mlen) + goto badvf; if (cc < 0 || cc > 65536) { /* TeX engines do not support char codes bigger than 65535 */ mdvi_error(_("(vf) %s: unexpected character %d\n"), @@ -199,7 +202,7 @@ static int vf_load_font(DviParams *params, DviFont *font) msize = mlen + pl + 256; macros = xresize(macros, Uchar, msize); } - if(pl && fread(macros + mlen, 1, pl, p) != pl) + if(pl && fread(macros + mlen, 1, pl, p) != (size_t)pl) break; macros[mlen+pl] = DVI_EOP; mlen += pl + 1; From facdce0742419f3bbc8415c9550924a6000ec399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Poo-Caama=C3=B1o?= Date: Fri, 13 Nov 2026 09:09:49 -0300 Subject: [PATCH 4/5] backends: Fix stack overflow on a truncated PK preamble 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 --- backend/dvi/mdvi-lib/pk.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/backend/dvi/mdvi-lib/pk.c b/backend/dvi/mdvi-lib/pk.c index 1b42a319..fd50ff4b 100644 --- a/backend/dvi/mdvi-lib/pk.c +++ b/backend/dvi/mdvi-lib/pk.c @@ -348,6 +348,8 @@ static int pk_load_font(DviParams *unused, DviFont *font) if(loc != PK_PRE || hic != PK_ID) goto badpk; i = fuget1(p); + if(feof(p)) + goto badpk; #ifndef NODEBUG for(loc = 0; loc < i; loc++) s[loc] = fuget1(p); @@ -387,24 +389,22 @@ static int pk_load_font(DviParams *unused, DviFont *font) case PK_X3: case PK_X4: { #ifndef NODEBUG - char *t; int n; + /* keep at most the first 255 bytes, for debugging */ i = fugetn(p, flag_byte - PK_X1 + 1); - if(i < 256) - t = &s[0]; - else - t = mdvi_malloc(i + 1); - for(n = 0; n < i; n++) - t[n] = fuget1(p); - t[n] = 0; + for(n = 0; n < i && !feof(p); n++) { + int c = fuget1(p); + + if(n < (int)sizeof(s) - 1) + s[n] = c; + } + s[Min(n, (int)sizeof(s) - 1)] = 0; DEBUG((DBG_SPECIAL, "(pk) %s: Special \"%s\"\n", - font->fontname, t)); - if(t != &s[0]) - mdvi_free(t); + font->fontname, s)); #else i = fugetn(p, flag_byte - PK_X1 + 1); - while(i-- > 0) + while(i-- > 0 && !feof(p)) fuget1(p); #endif break; From b31208d4dc12c7e50b3a7c0d34c26b368de2645a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Poo-Caama=C3=B1o?= Date: Sat, 14 Nov 2026 02:04:00 -0300 Subject: [PATCH 5/5] backends: Validate packed glyphs of PK fonts in the DVI backend 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 --- backend/dvi/mdvi-lib/pk.c | 40 +++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/backend/dvi/mdvi-lib/pk.c b/backend/dvi/mdvi-lib/pk.c index fd50ff4b..3de3f0e2 100644 --- a/backend/dvi/mdvi-lib/pk.c +++ b/backend/dvi/mdvi-lib/pk.c @@ -49,6 +49,9 @@ #define PK_NOOP 246 #define PK_PRE 247 +/* more leading zeros in a packed number would overflow an int */ +#define PK_MAX_ZEROS 6 + #define PK_DYN_F(x) (((x) >> 4) & 0xf) #define PK_PACKED(x) (PK_DYN_F(x) != 14) @@ -167,11 +170,15 @@ static int pk_packed_num(FILE *p, pkread *pkr, int *repeat) int dyn_f = pkr->dyn_f; i = pk_get_nyb(p, pkr); + if(feof(p)) + return -1; if(i == 0) { do { j = pk_get_nyb(p, pkr); i++; - } while(j == 0); + } while(j == 0 && i < PK_MAX_ZEROS); + if(j == 0) + return -1; while(i-- > 0) j = (j << 4) + pk_get_nyb(p, pkr); return (j - 15 + ((13 - dyn_f) << 4) + @@ -182,9 +189,15 @@ static int pk_packed_num(FILE *p, pkread *pkr, int *repeat) return ((i - dyn_f - 1) << 4) + pk_get_nyb(p, pkr) + dyn_f + 1; else { + /* a repeat count must be followed by a run count */ + if(*repeat) + return -1; *repeat = 1; - if(i == 14) + if(i == 14) { *repeat = pk_packed_num(p, pkr, repeat); + if(*repeat < 0) + return -1; + } return pk_packed_num(p, pkr, repeat); } } @@ -255,6 +268,8 @@ static BITMAP *get_packed(FILE *p, int w, int h, int flags) int i = 0; count = pk_packed_num(p, &pkr, &i); + if(count < 0 || i < 0) + goto bad; if(i > 0) { if(repeat_count) fprintf(stderr, "second repeat count for this row (had %d and got %d)\n", @@ -270,6 +285,8 @@ static BITMAP *get_packed(FILE *p, int w, int h, int flags) if(paint) bitmap_set_row(bm, row, w - inrow, inrow, paint); /* now copy it as many times as required */ + if(repeat_count >= h - row) + goto bad; r = (Uchar *)bm->data + row * bm->stride; while(repeat_count-- > 0) { t = r + bm->stride; @@ -292,6 +309,8 @@ static BITMAP *get_packed(FILE *p, int w, int h, int flags) a = (BmUnit *)(r + bm->stride); /* deal with entirely with/black rows */ while(count >= w) { + if(row >= h) + goto bad; /* count number of atoms in a row */ i = ROUND(w, BITMAP_BITS); while(i-- > 0) @@ -301,17 +320,22 @@ static BITMAP *get_packed(FILE *p, int w, int h, int flags) } inrow = w; } - if(count > 0) + if(count > 0) { + if(row >= h) + goto bad; bitmap_set_row(bm, row, w - inrow, count, paint); + } inrow -= count; paint = !paint; } - if(row != h || inrow != w) { - mdvi_error(_("Bad PK file: More bits than required\n")); - bitmap_destroy(bm); - return NULL; - } + if(row != h || inrow != w) + goto bad; return bm; + +bad: + mdvi_error(_("Bad PK file: More bits than required\n")); + bitmap_destroy(bm); + return NULL; } static BITMAP *get_char(FILE *p, int w, int h, int flags)