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

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