Blame SOURCES/0004-render-fix-refcounting-of-glyphs-during-ProcRenderAd.patch

191e41
From bdca6c3d1f5057eeb31609b1280fc93237b00c77 Mon Sep 17 00:00:00 2001
191e41
From: Peter Hutterer <peter.hutterer@who-t.net>
191e41
Date: Tue, 30 Jan 2024 13:13:35 +1000
191e41
Subject: [PATCH 4/4] render: fix refcounting of glyphs during
191e41
 ProcRenderAddGlyphs
191e41
191e41
Previously, AllocateGlyph would return a new glyph with refcount=0 and a
191e41
re-used glyph would end up not changing the refcount at all. The
191e41
resulting glyph_new array would thus have multiple entries pointing to
191e41
the same non-refcounted glyphs.
191e41
191e41
AddGlyph may free a glyph, resulting in a UAF when the same glyph
191e41
pointer is then later used.
191e41
191e41
Fix this by returning a refcount of 1 for a new glyph and always
191e41
incrementing the refcount for a re-used glyph, followed by dropping that
191e41
refcount back down again when we're done with it.
191e41
191e41
CVE-2024-31083, ZDI-CAN-22880
191e41
191e41
This vulnerability was discovered by:
191e41
Jan-Niklas Sohn working with Trend Micro Zero Day Initiative
191e41
191e41
Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/1463>
191e41
---
191e41
 render/glyph.c         |  5 +++--
191e41
 render/glyphstr_priv.h |  1 +
191e41
 render/render.c        | 15 +++++++++++----
191e41
 3 files changed, 15 insertions(+), 6 deletions(-)
191e41
191e41
diff --git a/render/glyph.c b/render/glyph.c
191e41
index 850ea8440..13991f8a1 100644
191e41
--- a/render/glyph.c
191e41
+++ b/render/glyph.c
191e41
@@ -245,10 +245,11 @@ FreeGlyphPicture(GlyphPtr glyph)
191e41
     }
191e41
 }
191e41
 
191e41
-static void
191e41
+void
191e41
 FreeGlyph(GlyphPtr glyph, int format)
191e41
 {
191e41
     CheckDuplicates(&globalGlyphs[format], "FreeGlyph");
191e41
+    BUG_RETURN(glyph->refcnt == 0);
191e41
     if (--glyph->refcnt == 0) {
191e41
         GlyphRefPtr gr;
191e41
         int i;
191e41
@@ -354,7 +355,7 @@ AllocateGlyph(xGlyphInfo * gi, int fdepth)
191e41
     glyph = (GlyphPtr) malloc(size);
191e41
     if (!glyph)
191e41
         return 0;
191e41
-    glyph->refcnt = 0;
191e41
+    glyph->refcnt = 1;
191e41
     glyph->size = size + sizeof(xGlyphInfo);
191e41
     glyph->info = *gi;
191e41
     dixInitPrivates(glyph, (char *) glyph + head_size, PRIVATE_GLYPH);
191e41
diff --git a/render/glyphstr.h b/render/glyphstr.h
191e41
index 2f51bd244..3b1d806d1 100644
191e41
--- a/render/glyphstr.h
191e41
+++ b/render/glyphstr.h
191e41
@@ -108,6 +108,7 @@ extern Bool
191e41
 extern GlyphPtr FindGlyph(GlyphSetPtr glyphSet, Glyph id);
191e41
 
191e41
 extern GlyphPtr AllocateGlyph(xGlyphInfo * gi, int format);
191e41
+extern void FreeGlyph(GlyphPtr glyph, int format);
191e41
 
191e41
 extern Bool
191e41
  ResizeGlyphSet(GlyphSetPtr glyphSet, CARD32 change);
191e41
diff --git a/render/render.c b/render/render.c
191e41
index 29c5055c6..fe5e37dd9 100644
191e41
--- a/render/render.c
191e41
+++ b/render/render.c
191e41
@@ -1076,6 +1076,7 @@ ProcRenderAddGlyphs(ClientPtr client)
191e41
 
191e41
         if (glyph_new->glyph && glyph_new->glyph != DeletedGlyph) {
191e41
             glyph_new->found = TRUE;
191e41
+            ++glyph_new->glyph->refcnt;
191e41
         }
191e41
         else {
191e41
             GlyphPtr glyph;
191e41
@@ -1168,8 +1169,10 @@ ProcRenderAddGlyphs(ClientPtr client)
191e41
         err = BadAlloc;
191e41
         goto bail;
191e41
     }
191e41
-    for (i = 0; i < nglyphs; i++)
191e41
+    for (i = 0; i < nglyphs; i++) {
191e41
         AddGlyph(glyphSet, glyphs[i].glyph, glyphs[i].id);
191e41
+        FreeGlyph(glyphs[i].glyph, glyphSet->fdepth);
191e41
+    }
191e41
 
191e41
     if (glyphsBase != glyphsLocal)
191e41
         free(glyphsBase);
191e41
@@ -1179,9 +1182,13 @@ ProcRenderAddGlyphs(ClientPtr client)
191e41
         FreePicture((void *) pSrc, 0);
191e41
     if (pSrcPix)
191e41
         FreeScratchPixmapHeader(pSrcPix);
191e41
-    for (i = 0; i < nglyphs; i++)
191e41
-        if (glyphs[i].glyph && !glyphs[i].found)
191e41
-            free(glyphs[i].glyph);
191e41
+    for (i = 0; i < nglyphs; i++) {
191e41
+        if (glyphs[i].glyph) {
191e41
+            --glyphs[i].glyph->refcnt;
191e41
+            if (!glyphs[i].found)
191e41
+                free(glyphs[i].glyph);
191e41
+        }
191e41
+    }
191e41
     if (glyphsBase != glyphsLocal)
191e41
         free(glyphsBase);
191e41
     return err;
191e41
-- 
191e41
2.44.0
191e41