logo

etc_portage

Unnamed repository; edit this file 'description' to name the repository. git clone https://hacktivis.me/git/etc_portage.git

0002-fix-use-after-free-in-font-caching-algorithm.patch (2052B)


  1. From a8cb8e94547d7e31441d2444e8a196415e3e4c1f Mon Sep 17 00:00:00 2001
  2. From: magras <dr.magras@gmail.com>
  3. Date: Thu, 28 Feb 2019 04:56:01 +0300
  4. Subject: [PATCH 2/5] fix use after free in font caching algorithm
  5. Current font caching algorithm contains a use after free error. A font
  6. removed from `frc` might be still listed in `wx.specbuf`. It will lead
  7. to a crash inside `XftDrawGlyphFontSpec()`.
  8. Steps to reproduce:
  9. $ st -f 'Misc Tamsyn:scalable=false'
  10. $ curl https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt
  11. Of course, result depends on fonts installed on a system and fontconfig.
  12. In my case, I'm getting consistent segfaults with different fonts.
  13. I replaced a fixed array with a simple unbounded buffer with a constant
  14. growth rate. Cache starts with a capacity of 0, gets increments by 16,
  15. and never shrinks. On my machine after `cat UTF-8-demo.txt` buffer
  16. reaches a capacity of 192. During casual use capacity stays at 0.
  17. ---
  18. x.c | 15 +++++++++------
  19. 1 file changed, 9 insertions(+), 6 deletions(-)
  20. diff --git a/x.c b/x.c
  21. index 865dacc..2cd76d0 100644
  22. --- a/x.c
  23. +++ b/x.c
  24. @@ -226,8 +226,9 @@ typedef struct {
  25. } Fontcache;
  26. /* Fontcache is an array now. A new font will be appended to the array. */
  27. -static Fontcache frc[16];
  28. +static Fontcache *frc = NULL;
  29. static int frclen = 0;
  30. +static int frccap = 0;
  31. static char *usedfont = NULL;
  32. static double usedfontsize = 0;
  33. static double defaultfontsize = 0;
  34. @@ -1244,12 +1245,14 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x
  35. fcpattern, &fcres);
  36. /*
  37. - * Overwrite or create the new cache entry.
  38. + * Allocate memory for the new cache entry.
  39. */
  40. - if (frclen >= LEN(frc)) {
  41. - frclen = LEN(frc) - 1;
  42. - XftFontClose(xw.dpy, frc[frclen].font);
  43. - frc[frclen].unicodep = 0;
  44. + if (frclen >= frccap) {
  45. + frccap += 16;
  46. + if (!frc)
  47. + frc = xmalloc(frccap * sizeof(Fontcache));
  48. + else
  49. + frc = xrealloc(frc, frccap * sizeof(Fontcache));
  50. }
  51. frc[frclen].font = XftFontOpenPattern(xw.dpy,
  52. --
  53. 2.21.0