Blame SOURCES/CVE-2021-31535.patch

798623
From 2714e4478c1262c94de6295cce605c14572968d3 Mon Sep 17 00:00:00 2001
798623
From: Matthieu Herrb <matthieu@herrb.eu>
798623
Date: Fri, 19 Feb 2021 15:30:39 +0100
798623
Subject: [PATCH libX11] Reject string longer than USHRT_MAX before sending
798623
 them on the wire
798623
798623
The X protocol uses CARD16 values to represent the length so
798623
this would overflow.
798623
798623
CVE-2021-31535
798623
798623
Signed-off-by: Matthieu Herrb <matthieu@herrb.eu>
798623
798623
[mustard: backported 10 1.6.8 by merging the warning fixes from
798623
upstream commimt 84427130 first - ajax]
798623
---
798623
 src/Font.c      | 10 ++++++----
798623
 src/FontInfo.c  |  5 ++++-
798623
 src/FontNames.c |  5 ++++-
798623
 src/GetColor.c  |  6 +++++-
798623
 src/LoadFont.c  |  6 +++++-
798623
 src/LookupCol.c |  6 ++++--
798623
 src/ParseCol.c  |  7 +++++--
798623
 src/QuExt.c     |  7 ++++++-
798623
 src/SetFPath.c  | 12 +++++++++---
798623
 src/SetHints.c  |  9 ++++++++-
798623
 src/StNColor.c  |  5 ++++-
798623
 src/StName.c    | 11 ++++++++---
798623
 12 files changed, 68 insertions(+), 21 deletions(-)
798623
798623
diff --git a/src/Font.c b/src/Font.c
798623
index 09d2ae91..1cd89cca 100644
798623
--- a/src/Font.c
798623
+++ b/src/Font.c
798623
@@ -102,12 +102,14 @@ XFontStruct *XLoadQueryFont(
798623
     XF86BigfontCodes *extcodes = _XF86BigfontCodes(dpy);
798623
 #endif
798623
 
798623
+    if (strlen(name) >= USHRT_MAX)
798623
+        return NULL;
798623
     if (_XF86LoadQueryLocaleFont(dpy, name, &font_result, (Font *)0))
798623
       return font_result;
798623
     LockDisplay(dpy);
798623
     GetReq(OpenFont, req);
798623
     seq = dpy->request; /* Can't use extended sequence number here */
798623
-    nbytes = req->nbytes  = name ? strlen(name) : 0;
798623
+    nbytes = req->nbytes = (CARD16) (name ? strlen(name) : 0);
798623
     req->fid = fid = XAllocID(dpy);
798623
     req->length += (nbytes+3)>>2;
798623
     Data (dpy, name, nbytes);
798623
@@ -662,8 +664,8 @@ int _XF86LoadQueryLocaleFont(
798623
 
798623
     if (!name)
798623
 	return 0;
798623
-    l = strlen(name);
798623
-    if (l < 2 || name[l - 1] != '*' || name[l - 2] != '-')
798623
+    l = (int) strlen(name);
798623
+    if (l < 2 || name[l - 1] != '*' || name[l - 2] != '-' || l >= USHRT_MAX)
798623
 	return 0;
798623
     charset = NULL;
798623
     /* next three lines stolen from _XkbGetCharset() */
798623
@@ -679,7 +681,7 @@ int _XF86LoadQueryLocaleFont(
798623
 	return 0;
798623
     if (_XlcNCompareISOLatin1(name + l - 2 - (p - charset), charset, p - charset))
798623
 	return 0;
798623
-    if (strlen(p + 1) + l - 1 >= sizeof(buf) - 1)
798623
+    if (strlen(p + 1) + (size_t) l - 1 >= sizeof(buf) - 1)
798623
 	return 0;
798623
     strcpy(buf, name);
798623
     strcpy(buf + l - 1, p + 1);
798623
diff --git a/src/FontInfo.c b/src/FontInfo.c
798623
index f870e431..6644b3fa 100644
798623
--- a/src/FontInfo.c
798623
+++ b/src/FontInfo.c
798623
@@ -58,10 +58,13 @@ XFontStruct **info)	/* RETURN */
798623
     register xListFontsReq *req;
798623
     int j;
798623
 
798623
+    if (strlen(pattern) >= USHRT_MAX)
798623
+        return NULL;
798623
+
798623
     LockDisplay(dpy);
798623
     GetReq(ListFontsWithInfo, req);
798623
     req->maxNames = maxNames;
798623
-    nbytes = req->nbytes = pattern ? strlen (pattern) : 0;
798623
+    nbytes = req->nbytes = pattern ? (CARD16) strlen (pattern) : 0;
798623
     req->length += (nbytes + 3) >> 2;
798623
     _XSend (dpy, pattern, nbytes);
798623
     /* use _XSend instead of Data, since subsequent _XReply will flush buffer */
798623
diff --git a/src/FontNames.c b/src/FontNames.c
798623
index b78792d6..458d80c9 100644
798623
--- a/src/FontNames.c
798623
+++ b/src/FontNames.c
798623
@@ -51,10 +51,13 @@ int *actualCount)	/* RETURN */
798623
     register xListFontsReq *req;
798623
     unsigned long rlen = 0;
798623
 
798623
+    if (strlen(pattern) >= USHRT_MAX)
798623
+        return NULL;
798623
+
798623
     LockDisplay(dpy);
798623
     GetReq(ListFonts, req);
798623
     req->maxNames = maxNames;
798623
-    nbytes = req->nbytes = pattern ? strlen (pattern) : 0;
798623
+    nbytes = req->nbytes = pattern ? (CARD16) strlen (pattern) : 0;
798623
     req->length += (nbytes + 3) >> 2;
798623
     _XSend (dpy, pattern, nbytes);
798623
     /* use _XSend instead of Data, since following _XReply will flush buffer */
798623
diff --git a/src/GetColor.c b/src/GetColor.c
798623
index cd0eb9f6..c8178067 100644
798623
--- a/src/GetColor.c
798623
+++ b/src/GetColor.c
798623
@@ -27,6 +27,7 @@ in this Software without prior written authorization from The Open Group.
798623
 #ifdef HAVE_CONFIG_H
798623
 #include <config.h>
798623
 #endif
798623
+#include <limits.h>
798623
 #include <stdio.h>
798623
 #include "Xlibint.h"
798623
 #include "Xcmsint.h"
798623
@@ -48,6 +49,9 @@ XColor *exact_def) /* RETURN */
798623
     XcmsColor cmsColor_exact;
798623
     Status ret;
798623
 
798623
+    if (strlen(colorname) >= USHRT_MAX)
798623
+        return (0);
798623
+
798623
 #ifdef XCMS
798623
     /*
798623
      * Let's Attempt to use Xcms and i18n approach to Parse Color
798623
@@ -83,7 +87,7 @@ XColor *exact_def) /* RETURN */
798623
     GetReq(AllocNamedColor, req);
798623
 
798623
     req->cmap = cmap;
798623
-    nbytes = req->nbytes = strlen(colorname);
798623
+    nbytes = req->nbytes = (CARD16) strlen(colorname);
798623
     req->length += (nbytes + 3) >> 2; /* round up to mult of 4 */
798623
 
798623
     _XSend(dpy, colorname, nbytes);
798623
diff --git a/src/LoadFont.c b/src/LoadFont.c
798623
index f547976b..3996436f 100644
798623
--- a/src/LoadFont.c
798623
+++ b/src/LoadFont.c
798623
@@ -27,6 +27,7 @@ in this Software without prior written authorization from The Open Group.
798623
 #ifdef HAVE_CONFIG_H
798623
 #include <config.h>
798623
 #endif
798623
+#include <limits.h>
798623
 #include "Xlibint.h"
798623
 
798623
 Font
798623
@@ -38,12 +39,15 @@ XLoadFont (
798623
     Font fid;
798623
     register xOpenFontReq *req;
798623
 
798623
+    if (strlen(name) >= USHRT_MAX)
798623
+        return (0);
798623
+
798623
     if (_XF86LoadQueryLocaleFont(dpy, name, (XFontStruct **)0, &fid))
798623
       return fid;
798623
 
798623
     LockDisplay(dpy);
798623
     GetReq(OpenFont, req);
798623
-    nbytes = req->nbytes = name ? strlen(name) : 0;
798623
+    nbytes = req->nbytes = name ? (CARD16) strlen(name) : 0;
798623
     req->fid = fid = XAllocID(dpy);
798623
     req->length += (nbytes+3)>>2;
798623
     Data (dpy, name, nbytes);
798623
diff --git a/src/LookupCol.c b/src/LookupCol.c
798623
index f7f969f5..cd9b1368 100644
798623
--- a/src/LookupCol.c
798623
+++ b/src/LookupCol.c
798623
@@ -27,6 +27,7 @@ in this Software without prior written authorization from The Open Group.
798623
 #ifdef HAVE_CONFIG_H
798623
 #include <config.h>
798623
 #endif
798623
+#include <limits.h>
798623
 #include <stdio.h>
798623
 #include "Xlibint.h"
798623
 #include "Xcmsint.h"
798623
@@ -46,6 +47,9 @@ XLookupColor (
798623
 	XcmsCCC ccc;
798623
 	XcmsColor cmsColor_exact;
798623
 
798623
+	n = (int) strlen (spec);
798623
+	if (n >= USHRT_MAX)
798623
+            return 0;
798623
 #ifdef XCMS
798623
 	/*
798623
 	 * Let's Attempt to use Xcms and i18n approach to Parse Color
798623
@@ -77,8 +81,6 @@ XLookupColor (
798623
 	 * Xcms and i18n methods failed, so lets pass it to the server
798623
 	 * for parsing.
798623
 	 */
798623
-
798623
-	n = strlen (spec);
798623
 	LockDisplay(dpy);
798623
 	GetReq (LookupColor, req);
798623
 	req->cmap = cmap;
798623
diff --git a/src/ParseCol.c b/src/ParseCol.c
798623
index e997b1b8..7a84a17b 100644
798623
--- a/src/ParseCol.c
798623
+++ b/src/ParseCol.c
798623
@@ -27,6 +27,7 @@ in this Software without prior written authorization from The Open Group.
798623
 #ifdef HAVE_CONFIG_H
798623
 #include <config.h>
798623
 #endif
798623
+#include <limits.h>
798623
 #include <stdio.h>
798623
 #include "Xlibint.h"
798623
 #include "Xcmsint.h"
798623
@@ -46,7 +47,9 @@ XParseColor (
798623
 	XcmsColor cmsColor;
798623
 
798623
         if (!spec) return(0);
798623
-	n = strlen (spec);
798623
+	n = (int) strlen (spec);
798623
+	if (n >= USHRT_MAX)
798623
+            return(0);
798623
 	if (*spec == '#') {
798623
 	    /*
798623
 	     * RGB
798623
@@ -119,7 +122,7 @@ XParseColor (
798623
 	    LockDisplay(dpy);
798623
 	    GetReq (LookupColor, req);
798623
 	    req->cmap = cmap;
798623
-	    req->nbytes = n = strlen(spec);
798623
+	    req->nbytes = (CARD16) (n = (int) strlen(spec));
798623
 	    req->length += (n + 3) >> 2;
798623
 	    Data (dpy, spec, (long)n);
798623
 	    if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) {
798623
diff --git a/src/QuExt.c b/src/QuExt.c
798623
index 4e230e77..4cb99fcf 100644
798623
--- a/src/QuExt.c
798623
+++ b/src/QuExt.c
798623
@@ -27,6 +27,8 @@ in this Software without prior written authorization from The Open Group.
798623
 #ifdef HAVE_CONFIG_H
798623
 #include <config.h>
798623
 #endif
798623
+#include <limits.h>
798623
+#include <stdbool.h>
798623
 #include "Xlibint.h"
798623
 
798623
 Bool
798623
@@ -40,9 +42,12 @@ XQueryExtension(
798623
     xQueryExtensionReply rep;
798623
     register xQueryExtensionReq *req;
798623
 
798623
+    if (strlen(name) >= USHRT_MAX)
798623
+        return false;
798623
+
798623
     LockDisplay(dpy);
798623
     GetReq(QueryExtension, req);
798623
-    req->nbytes = name ? strlen(name) : 0;
798623
+    req->nbytes = name ? (CARD16) strlen(name) : 0;
798623
     req->length += (req->nbytes+(unsigned)3)>>2;
798623
     _XSend(dpy, name, (long)req->nbytes);
798623
     (void) _XReply (dpy, (xReply *)&rep, 0, xTrue);
798623
diff --git a/src/SetFPath.c b/src/SetFPath.c
798623
index 60aaef01..13fce49e 100644
798623
--- a/src/SetFPath.c
798623
+++ b/src/SetFPath.c
798623
@@ -26,6 +26,7 @@ in this Software without prior written authorization from The Open Group.
798623
 
798623
 #ifdef HAVE_CONFIG_H
798623
 #include <config.h>
798623
+#include <limits.h>
798623
 #endif
798623
 #include "Xlibint.h"
798623
 
798623
@@ -48,7 +49,12 @@ XSetFontPath (
798623
 	GetReq (SetFontPath, req);
798623
 	req->nFonts = ndirs;
798623
 	for (i = 0; i < ndirs; i++) {
798623
-		n += safestrlen (directories[i]) + 1;
798623
+		n = (int) ((size_t) n + (safestrlen (directories[i]) + 1));
798623
+		if (n >= USHRT_MAX) {
798623
+			UnlockDisplay(dpy);
798623
+			SyncHandle();
798623
+			return 0;
798623
+		}
798623
 	}
798623
 	nbytes = (n + 3) & ~3;
798623
 	req->length += nbytes >> 2;
798623
@@ -59,9 +65,9 @@ XSetFontPath (
798623
 		char	*tmp = p;
798623
 
798623
 		for (i = 0; i < ndirs; i++) {
798623
-			register int length = safestrlen (directories[i]);
798623
+			register int length = (int) safestrlen (directories[i]);
798623
 			*p = length;
798623
-			memcpy (p + 1, directories[i], length);
798623
+			memcpy (p + 1, directories[i], (size_t)length);
798623
 			p += length + 1;
798623
 		}
798623
 		Data (dpy, tmp, nbytes);
798623
diff --git a/src/SetHints.c b/src/SetHints.c
798623
index bc46498a..61cb0684 100644
798623
--- a/src/SetHints.c
798623
+++ b/src/SetHints.c
798623
@@ -49,6 +49,7 @@ SOFTWARE.
798623
 #ifdef HAVE_CONFIG_H
798623
 #include <config.h>
798623
 #endif
798623
+#include <limits.h>
798623
 #include <X11/Xlibint.h>
798623
 #include <X11/Xutil.h>
798623
 #include "Xatomtype.h"
798623
@@ -214,6 +215,8 @@ XSetCommand (
798623
 	register char *buf, *bp;
798623
 	for (i = 0, nbytes = 0; i < argc; i++) {
798623
 		nbytes += safestrlen(argv[i]) + 1;
798623
+		if (nbytes >= USHRT_MAX)
798623
+                    return 1;
798623
 	}
798623
 	if ((bp = buf = Xmalloc(nbytes))) {
798623
 	    /* copy arguments into single buffer */
798623
@@ -256,11 +259,13 @@ XSetStandardProperties (
798623
 
798623
 	if (name != NULL) XStoreName (dpy, w, name);
798623
 
798623
+        if (safestrlen(icon_string) >= USHRT_MAX)
798623
+            return 1;
798623
 	if (icon_string != NULL) {
798623
 	    XChangeProperty (dpy, w, XA_WM_ICON_NAME, XA_STRING, 8,
798623
                              PropModeReplace,
798623
                              (_Xconst unsigned char *)icon_string,
798623
-                             safestrlen(icon_string));
798623
+                             (int)safestrlen(icon_string));
798623
 		}
798623
 
798623
 	if (icon_pixmap != None) {
798623
@@ -298,6 +303,8 @@ XSetClassHint(
798623
 
798623
 	len_nm = safestrlen(classhint->res_name);
798623
 	len_cl = safestrlen(classhint->res_class);
798623
+        if (len_nm + len_cl >= USHRT_MAX)
798623
+            return 1;
798623
 	if ((class_string = s = Xmalloc(len_nm + len_cl + 2))) {
798623
 	    if (len_nm) {
798623
 		strcpy(s, classhint->res_name);
798623
diff --git a/src/StNColor.c b/src/StNColor.c
798623
index 8b821c3e..16dc9cbc 100644
798623
--- a/src/StNColor.c
798623
+++ b/src/StNColor.c
798623
@@ -27,6 +27,7 @@ in this Software without prior written authorization from The Open Group.
798623
 #ifdef HAVE_CONFIG_H
798623
 #include <config.h>
798623
 #endif
798623
+#include <limits.h>
798623
 #include <stdio.h>
798623
 #include "Xlibint.h"
798623
 #include "Xcmsint.h"
798623
@@ -46,6 +47,8 @@ int flags)  /* DoRed, DoGreen, DoBlue */
798623
     XcmsColor cmsColor_exact;
798623
     XColor scr_def;
798623
 
798623
+    if (strlen(name) >= USHRT_MAX)
798623
+        return 0;
798623
 #ifdef XCMS
798623
     /*
798623
      * Let's Attempt to use Xcms approach to Parse Color
798623
@@ -76,7 +79,7 @@ int flags)  /* DoRed, DoGreen, DoBlue */
798623
     req->cmap = cmap;
798623
     req->flags = flags;
798623
     req->pixel = pixel;
798623
-    req->nbytes = nbytes = strlen(name);
798623
+    req->nbytes = (CARD16) (nbytes = (unsigned) strlen(name));
798623
     req->length += (nbytes + 3) >> 2; /* round up to multiple of 4 */
798623
     Data(dpy, name, (long)nbytes);
798623
     UnlockDisplay(dpy);
798623
diff --git a/src/StName.c b/src/StName.c
798623
index b4048bff..04bb3aa6 100644
798623
--- a/src/StName.c
798623
+++ b/src/StName.c
798623
@@ -27,6 +27,7 @@ in this Software without prior written authorization from The Open Group.
798623
 #ifdef HAVE_CONFIG_H
798623
 #include <config.h>
798623
 #endif
798623
+#include <limits.h>
798623
 #include <X11/Xlibint.h>
798623
 #include <X11/Xatom.h>
798623
 
798623
@@ -36,9 +37,11 @@ XStoreName (
798623
     Window w,
798623
     _Xconst char *name)
798623
 {
798623
-    return XChangeProperty(dpy, w, XA_WM_NAME, XA_STRING,
798623
+    if (strlen(name) >= USHRT_MAX)
798623
+        return 0;
798623
+    return XChangeProperty(dpy, w, XA_WM_NAME, XA_STRING, /*  */
798623
 			   8, PropModeReplace, (_Xconst unsigned char *)name,
798623
-			   name ? strlen(name) : 0);
798623
+			   name ? (int) strlen(name) : 0);
798623
 }
798623
 
798623
 int
798623
@@ -47,7 +50,9 @@ XSetIconName (
798623
     Window w,
798623
     _Xconst char *icon_name)
798623
 {
798623
+    if (strlen(icon_name) >= USHRT_MAX)
798623
+        return 0;
798623
     return XChangeProperty(dpy, w, XA_WM_ICON_NAME, XA_STRING, 8,
798623
                            PropModeReplace, (_Xconst unsigned char *)icon_name,
798623
-			   icon_name ? strlen(icon_name) : 0);
798623
+			   icon_name ? (int) strlen(icon_name) : 0);
798623
 }
798623
-- 
798623
2.30.1
798623