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

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