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

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