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