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

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