Blame SOURCES/0003-xkb-add-request-length-validation-for-XkbSetGeometry.patch

ff98c0
From bd134231e282d9eb126b6fdaa40bb383180fa72b Mon Sep 17 00:00:00 2001
ff98c0
From: Peter Hutterer <peter.hutterer@who-t.net>
ff98c0
Date: Tue, 5 Jul 2022 11:11:06 +1000
ff98c0
Subject: [PATCH xserver 3/3] xkb: add request length validation for
ff98c0
 XkbSetGeometry
ff98c0
ff98c0
No validation of the various fields on that report were done, so a
ff98c0
malicious client could send a short request that claims it had N
ff98c0
sections, or rows, or keys, and the server would process the request for
ff98c0
N sections, running out of bounds of the actual request data.
ff98c0
ff98c0
Fix this by adding size checks to ensure our data is valid.
ff98c0
ff98c0
ZDI-CAN 16062, CVE-2022-2319.
ff98c0
ff98c0
This vulnerability was discovered by:
ff98c0
Jan-Niklas Sohn working with Trend Micro Zero Day Initiative
ff98c0
ff98c0
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
ff98c0
(cherry picked from commit 6907b6ea2b4ce949cb07271f5b678d5966d9df42)
ff98c0
---
ff98c0
 xkb/xkb.c | 43 ++++++++++++++++++++++++++++++++++++++-----
ff98c0
 1 file changed, 38 insertions(+), 5 deletions(-)
ff98c0
ff98c0
diff --git a/xkb/xkb.c b/xkb/xkb.c
ff98c0
index 36464a770..27d19793e 100644
ff98c0
--- a/xkb/xkb.c
ff98c0
+++ b/xkb/xkb.c
ff98c0
@@ -5160,7 +5160,7 @@ _GetCountedString(char **wire_inout, ClientPtr client, char **str)
ff98c0
 }
ff98c0
 
ff98c0
 static Status
ff98c0
-_CheckSetDoodad(char **wire_inout,
ff98c0
+_CheckSetDoodad(char **wire_inout, xkbSetGeometryReq *req,
ff98c0
                 XkbGeometryPtr geom, XkbSectionPtr section, ClientPtr client)
ff98c0
 {
ff98c0
     char *wire;
ff98c0
@@ -5171,6 +5171,9 @@ _CheckSetDoodad(char **wire_inout,
ff98c0
     Status status;
ff98c0
 
ff98c0
     dWire = (xkbDoodadWireDesc *) (*wire_inout);
ff98c0
+    if (!_XkbCheckRequestBounds(client, req, dWire, dWire + 1))
ff98c0
+        return BadLength;
ff98c0
+
ff98c0
     any = dWire->any;
ff98c0
     wire = (char *) &dWire[1];
ff98c0
     if (client->swapped) {
ff98c0
@@ -5273,7 +5276,7 @@ _CheckSetDoodad(char **wire_inout,
ff98c0
 }
ff98c0
 
ff98c0
 static Status
ff98c0
-_CheckSetOverlay(char **wire_inout,
ff98c0
+_CheckSetOverlay(char **wire_inout, xkbSetGeometryReq *req,
ff98c0
                  XkbGeometryPtr geom, XkbSectionPtr section, ClientPtr client)
ff98c0
 {
ff98c0
     register int r;
ff98c0
@@ -5284,6 +5287,9 @@ _CheckSetOverlay(char **wire_inout,
ff98c0
 
ff98c0
     wire = *wire_inout;
ff98c0
     olWire = (xkbOverlayWireDesc *) wire;
ff98c0
+    if (!_XkbCheckRequestBounds(client, req, olWire, olWire + 1))
ff98c0
+        return BadLength;
ff98c0
+
ff98c0
     if (client->swapped) {
ff98c0
         swapl(&olWire->name);
ff98c0
     }
ff98c0
@@ -5295,6 +5301,9 @@ _CheckSetOverlay(char **wire_inout,
ff98c0
         xkbOverlayKeyWireDesc *kWire;
ff98c0
         XkbOverlayRowPtr row;
ff98c0
 
ff98c0
+        if (!_XkbCheckRequestBounds(client, req, rWire, rWire + 1))
ff98c0
+            return BadLength;
ff98c0
+
ff98c0
         if (rWire->rowUnder > section->num_rows) {
ff98c0
             client->errorValue = _XkbErrCode4(0x20, r, section->num_rows,
ff98c0
                                               rWire->rowUnder);
ff98c0
@@ -5303,6 +5312,9 @@ _CheckSetOverlay(char **wire_inout,
ff98c0
         row = XkbAddGeomOverlayRow(ol, rWire->rowUnder, rWire->nKeys);
ff98c0
         kWire = (xkbOverlayKeyWireDesc *) &rWire[1];
ff98c0
         for (k = 0; k < rWire->nKeys; k++, kWire++) {
ff98c0
+            if (!_XkbCheckRequestBounds(client, req, kWire, kWire + 1))
ff98c0
+                return BadLength;
ff98c0
+
ff98c0
             if (XkbAddGeomOverlayKey(ol, row,
ff98c0
                                      (char *) kWire->over,
ff98c0
                                      (char *) kWire->under) == NULL) {
ff98c0
@@ -5336,6 +5348,9 @@ _CheckSetSections(XkbGeometryPtr geom,
ff98c0
         register int r;
ff98c0
         xkbRowWireDesc *rWire;
ff98c0
 
ff98c0
+        if (!_XkbCheckRequestBounds(client, req, sWire, sWire + 1))
ff98c0
+            return BadLength;
ff98c0
+
ff98c0
         if (client->swapped) {
ff98c0
             swapl(&sWire->name);
ff98c0
             swaps(&sWire->top);
ff98c0
@@ -5361,6 +5376,9 @@ _CheckSetSections(XkbGeometryPtr geom,
ff98c0
             XkbRowPtr row;
ff98c0
             xkbKeyWireDesc *kWire;
ff98c0
 
ff98c0
+            if (!_XkbCheckRequestBounds(client, req, rWire, rWire + 1))
ff98c0
+                return BadLength;
ff98c0
+
ff98c0
             if (client->swapped) {
ff98c0
                 swaps(&rWire->top);
ff98c0
                 swaps(&rWire->left);
ff98c0
@@ -5375,6 +5393,9 @@ _CheckSetSections(XkbGeometryPtr geom,
ff98c0
             for (k = 0; k < rWire->nKeys; k++, kWire++) {
ff98c0
                 XkbKeyPtr key;
ff98c0
 
ff98c0
+                if (!_XkbCheckRequestBounds(client, req, kWire, kWire + 1))
ff98c0
+                    return BadLength;
ff98c0
+
ff98c0
                 key = XkbAddGeomKey(row);
ff98c0
                 if (!key)
ff98c0
                     return BadAlloc;
ff98c0
@@ -5400,7 +5421,7 @@ _CheckSetSections(XkbGeometryPtr geom,
ff98c0
             register int d;
ff98c0
 
ff98c0
             for (d = 0; d < sWire->nDoodads; d++) {
ff98c0
-                status = _CheckSetDoodad(&wire, geom, section, client);
ff98c0
+                status = _CheckSetDoodad(&wire, req, geom, section, client);
ff98c0
                 if (status != Success)
ff98c0
                     return status;
ff98c0
             }
ff98c0
@@ -5409,7 +5430,7 @@ _CheckSetSections(XkbGeometryPtr geom,
ff98c0
             register int o;
ff98c0
 
ff98c0
             for (o = 0; o < sWire->nOverlays; o++) {
ff98c0
-                status = _CheckSetOverlay(&wire, geom, section, client);
ff98c0
+                status = _CheckSetOverlay(&wire, req, geom, section, client);
ff98c0
                 if (status != Success)
ff98c0
                     return status;
ff98c0
             }
ff98c0
@@ -5443,6 +5464,9 @@ _CheckSetShapes(XkbGeometryPtr geom,
ff98c0
             xkbOutlineWireDesc *olWire;
ff98c0
             XkbOutlinePtr ol;
ff98c0
 
ff98c0
+            if (!_XkbCheckRequestBounds(client, req, shapeWire, shapeWire + 1))
ff98c0
+                return BadLength;
ff98c0
+
ff98c0
             shape =
ff98c0
                 XkbAddGeomShape(geom, shapeWire->name, shapeWire->nOutlines);
ff98c0
             if (!shape)
ff98c0
@@ -5453,12 +5477,18 @@ _CheckSetShapes(XkbGeometryPtr geom,
ff98c0
                 XkbPointPtr pt;
ff98c0
                 xkbPointWireDesc *ptWire;
ff98c0
 
ff98c0
+                if (!_XkbCheckRequestBounds(client, req, olWire, olWire + 1))
ff98c0
+                    return BadLength;
ff98c0
+
ff98c0
                 ol = XkbAddGeomOutline(shape, olWire->nPoints);
ff98c0
                 if (!ol)
ff98c0
                     return BadAlloc;
ff98c0
                 ol->corner_radius = olWire->cornerRadius;
ff98c0
                 ptWire = (xkbPointWireDesc *) &olWire[1];
ff98c0
                 for (p = 0, pt = ol->points; p < olWire->nPoints; p++, pt++, ptWire++) {
ff98c0
+                    if (!_XkbCheckRequestBounds(client, req, ptWire, ptWire + 1))
ff98c0
+                        return BadLength;
ff98c0
+
ff98c0
                     pt->x = ptWire->x;
ff98c0
                     pt->y = ptWire->y;
ff98c0
                     if (client->swapped) {
ff98c0
@@ -5564,12 +5594,15 @@ _CheckSetGeom(XkbGeometryPtr geom, xkbSetGeometryReq * req, ClientPtr client)
ff98c0
         return status;
ff98c0
 
ff98c0
     for (i = 0; i < req->nDoodads; i++) {
ff98c0
-        status = _CheckSetDoodad(&wire, geom, NULL, client);
ff98c0
+        status = _CheckSetDoodad(&wire, req, geom, NULL, client);
ff98c0
         if (status != Success)
ff98c0
             return status;
ff98c0
     }
ff98c0
 
ff98c0
     for (i = 0; i < req->nKeyAliases; i++) {
ff98c0
+        if (!_XkbCheckRequestBounds(client, req, wire, wire + XkbKeyNameLength))
ff98c0
+                return BadLength;
ff98c0
+
ff98c0
         if (XkbAddGeomKeyAlias(geom, &wire[XkbKeyNameLength], wire) == NULL)
ff98c0
             return BadAlloc;
ff98c0
         wire += 2 * XkbKeyNameLength;
ff98c0
-- 
ff98c0
2.36.1
ff98c0