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

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