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

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