Blame SOURCES/0001-Correct-bounds-checking-in-XkbSetNames.patch

23ac27
From 1d3a1092c30af660b1366fcd344af745590aa29f Mon Sep 17 00:00:00 2001
23ac27
From: Matthieu Herrb <matthieu@herrb.eu>
23ac27
Date: Tue, 18 Aug 2020 14:46:32 +0200
23ac27
Subject: [PATCH xserver] Correct bounds checking in XkbSetNames()
23ac27
MIME-Version: 1.0
23ac27
Content-Type: text/plain; charset=UTF-8
23ac27
Content-Transfer-Encoding: 8bit
23ac27
23ac27
CVE-2020-14345 / ZDI 11428
23ac27
23ac27
This vulnerability was discovered by:
23ac27
Jan-Niklas Sohn working with Trend Micro Zero Day Initiative
23ac27
23ac27
Signed-off-by: Matthieu Herrb <matthieu@herrb.eu>
23ac27
(cherry picked from commit 11f22a3bf694d7061d552c99898d843bcdaf0cf1)
23ac27
Signed-off-by: Michel Dänzer <mdaenzer@redhat.com>
23ac27
---
23ac27
 xkb/xkb.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
23ac27
 1 file changed, 48 insertions(+)
23ac27
23ac27
diff --git a/xkb/xkb.c b/xkb/xkb.c
23ac27
index 3162574a4..2139da7ee 100644
23ac27
--- a/xkb/xkb.c
23ac27
+++ b/xkb/xkb.c
23ac27
@@ -152,6 +152,19 @@ static RESTYPE RT_XKBCLIENT;
23ac27
 #define	CHK_REQ_KEY_RANGE(err,first,num,r)  \
23ac27
 	CHK_REQ_KEY_RANGE2(err,first,num,r,client->errorValue,BadValue)
23ac27
 
23ac27
+static Bool
23ac27
+_XkbCheckRequestBounds(ClientPtr client, void *stuff, void *from, void *to) {
23ac27
+    char *cstuff = (char *)stuff;
23ac27
+    char *cfrom = (char *)from;
23ac27
+    char *cto = (char *)to;
23ac27
+
23ac27
+    return cfrom < cto &&
23ac27
+           cfrom >= cstuff &&
23ac27
+           cfrom < cstuff + ((size_t)client->req_len << 2) &&
23ac27
+           cto >= cstuff &&
23ac27
+           cto <= cstuff + ((size_t)client->req_len << 2);
23ac27
+}
23ac27
+
23ac27
 /***====================================================================***/
23ac27
 
23ac27
 int
23ac27
@@ -4045,6 +4058,8 @@ _XkbSetNamesCheck(ClientPtr client, DeviceIntPtr dev,
23ac27
             client->errorValue = _XkbErrCode2(0x04, stuff->firstType);
23ac27
             return BadAccess;
23ac27
         }
23ac27
+        if (!_XkbCheckRequestBounds(client, stuff, tmp, tmp + stuff->nTypes))
23ac27
+            return BadLength;
23ac27
         old = tmp;
23ac27
         tmp = _XkbCheckAtoms(tmp, stuff->nTypes, client->swapped, &bad;;
23ac27
         if (!tmp) {
23ac27
@@ -4074,6 +4089,8 @@ _XkbSetNamesCheck(ClientPtr client, DeviceIntPtr dev,
23ac27
         }
23ac27
         width = (CARD8 *) tmp;
23ac27
         tmp = (CARD32 *) (((char *) tmp) + XkbPaddedSize(stuff->nKTLevels));
23ac27
+        if (!_XkbCheckRequestBounds(client, stuff, width, tmp))
23ac27
+            return BadLength;
23ac27
         type = &xkb->map->types[stuff->firstKTLevel];
23ac27
         for (i = 0; i < stuff->nKTLevels; i++, type++) {
23ac27
             if (width[i] == 0)
23ac27
@@ -4083,6 +4100,8 @@ _XkbSetNamesCheck(ClientPtr client, DeviceIntPtr dev,
23ac27
                                                   type->num_levels, width[i]);
23ac27
                 return BadMatch;
23ac27
             }
23ac27
+            if (!_XkbCheckRequestBounds(client, stuff, tmp, tmp + width[i]))
23ac27
+                return BadLength;
23ac27
             tmp = _XkbCheckAtoms(tmp, width[i], client->swapped, &bad;;
23ac27
             if (!tmp) {
23ac27
                 client->errorValue = bad;
23ac27
@@ -4095,6 +4114,9 @@ _XkbSetNamesCheck(ClientPtr client, DeviceIntPtr dev,
23ac27
             client->errorValue = 0x08;
23ac27
             return BadMatch;
23ac27
         }
23ac27
+        if (!_XkbCheckRequestBounds(client, stuff, tmp,
23ac27
+                                    tmp + Ones(stuff->indicators)))
23ac27
+            return BadLength;
23ac27
         tmp = _XkbCheckMaskedAtoms(tmp, XkbNumIndicators, stuff->indicators,
23ac27
                                    client->swapped, &bad;;
23ac27
         if (!tmp) {
23ac27
@@ -4107,6 +4129,9 @@ _XkbSetNamesCheck(ClientPtr client, DeviceIntPtr dev,
23ac27
             client->errorValue = 0x09;
23ac27
             return BadMatch;
23ac27
         }
23ac27
+        if (!_XkbCheckRequestBounds(client, stuff, tmp,
23ac27
+                                    tmp + Ones(stuff->virtualMods)))
23ac27
+            return BadLength;
23ac27
         tmp = _XkbCheckMaskedAtoms(tmp, XkbNumVirtualMods,
23ac27
                                    (CARD32) stuff->virtualMods,
23ac27
                                    client->swapped, &bad;;
23ac27
@@ -4120,6 +4145,9 @@ _XkbSetNamesCheck(ClientPtr client, DeviceIntPtr dev,
23ac27
             client->errorValue = 0x0a;
23ac27
             return BadMatch;
23ac27
         }
23ac27
+        if (!_XkbCheckRequestBounds(client, stuff, tmp,
23ac27
+                                    tmp + Ones(stuff->groupNames)))
23ac27
+            return BadLength;
23ac27
         tmp = _XkbCheckMaskedAtoms(tmp, XkbNumKbdGroups,
23ac27
                                    (CARD32) stuff->groupNames,
23ac27
                                    client->swapped, &bad;;
23ac27
@@ -4141,9 +4169,14 @@ _XkbSetNamesCheck(ClientPtr client, DeviceIntPtr dev,
23ac27
                              stuff->nKeys);
23ac27
             return BadValue;
23ac27
         }
23ac27
+        if (!_XkbCheckRequestBounds(client, stuff, tmp, tmp + stuff->nKeys))
23ac27
+            return BadLength;
23ac27
         tmp += stuff->nKeys;
23ac27
     }
23ac27
     if ((stuff->which & XkbKeyAliasesMask) && (stuff->nKeyAliases > 0)) {
23ac27
+        if (!_XkbCheckRequestBounds(client, stuff, tmp,
23ac27
+                                    tmp + (stuff->nKeyAliases * 2)))
23ac27
+            return BadLength;
23ac27
         tmp += stuff->nKeyAliases * 2;
23ac27
     }
23ac27
     if (stuff->which & XkbRGNamesMask) {
23ac27
@@ -4151,6 +4184,9 @@ _XkbSetNamesCheck(ClientPtr client, DeviceIntPtr dev,
23ac27
             client->errorValue = _XkbErrCode2(0x0d, stuff->nRadioGroups);
23ac27
             return BadValue;
23ac27
         }
23ac27
+        if (!_XkbCheckRequestBounds(client, stuff, tmp,
23ac27
+                                    tmp + stuff->nRadioGroups))
23ac27
+            return BadLength;
23ac27
         tmp = _XkbCheckAtoms(tmp, stuff->nRadioGroups, client->swapped, &bad;;
23ac27
         if (!tmp) {
23ac27
             client->errorValue = bad;
23ac27
@@ -4344,6 +4380,8 @@ ProcXkbSetNames(ClientPtr client)
23ac27
     /* check device-independent stuff */
23ac27
     tmp = (CARD32 *) &stuff[1];
23ac27
 
23ac27
+    if (!_XkbCheckRequestBounds(client, stuff, tmp, tmp + 1))
23ac27
+        return BadLength;
23ac27
     if (stuff->which & XkbKeycodesNameMask) {
23ac27
         tmp = _XkbCheckAtoms(tmp, 1, client->swapped, &bad;;
23ac27
         if (!tmp) {
23ac27
@@ -4351,6 +4389,8 @@ ProcXkbSetNames(ClientPtr client)
23ac27
             return BadAtom;
23ac27
         }
23ac27
     }
23ac27
+    if (!_XkbCheckRequestBounds(client, stuff, tmp, tmp + 1))
23ac27
+        return BadLength;
23ac27
     if (stuff->which & XkbGeometryNameMask) {
23ac27
         tmp = _XkbCheckAtoms(tmp, 1, client->swapped, &bad;;
23ac27
         if (!tmp) {
23ac27
@@ -4358,6 +4398,8 @@ ProcXkbSetNames(ClientPtr client)
23ac27
             return BadAtom;
23ac27
         }
23ac27
     }
23ac27
+    if (!_XkbCheckRequestBounds(client, stuff, tmp, tmp + 1))
23ac27
+        return BadLength;
23ac27
     if (stuff->which & XkbSymbolsNameMask) {
23ac27
         tmp = _XkbCheckAtoms(tmp, 1, client->swapped, &bad;;
23ac27
         if (!tmp) {
23ac27
@@ -4365,6 +4407,8 @@ ProcXkbSetNames(ClientPtr client)
23ac27
             return BadAtom;
23ac27
         }
23ac27
     }
23ac27
+    if (!_XkbCheckRequestBounds(client, stuff, tmp, tmp + 1))
23ac27
+        return BadLength;
23ac27
     if (stuff->which & XkbPhysSymbolsNameMask) {
23ac27
         tmp = _XkbCheckAtoms(tmp, 1, client->swapped, &bad;;
23ac27
         if (!tmp) {
23ac27
@@ -4372,6 +4416,8 @@ ProcXkbSetNames(ClientPtr client)
23ac27
             return BadAtom;
23ac27
         }
23ac27
     }
23ac27
+    if (!_XkbCheckRequestBounds(client, stuff, tmp, tmp + 1))
23ac27
+        return BadLength;
23ac27
     if (stuff->which & XkbTypesNameMask) {
23ac27
         tmp = _XkbCheckAtoms(tmp, 1, client->swapped, &bad;;
23ac27
         if (!tmp) {
23ac27
@@ -4379,6 +4425,8 @@ ProcXkbSetNames(ClientPtr client)
23ac27
             return BadAtom;
23ac27
         }
23ac27
     }
23ac27
+    if (!_XkbCheckRequestBounds(client, stuff, tmp, tmp + 1))
23ac27
+        return BadLength;
23ac27
     if (stuff->which & XkbCompatNameMask) {
23ac27
         tmp = _XkbCheckAtoms(tmp, 1, client->swapped, &bad;;
23ac27
         if (!tmp) {
23ac27
-- 
23ac27
2.28.0
23ac27