From babb1fc823ab3be192c48fe115feeb0d57f74d05 Mon Sep 17 00:00:00 2001
From: Alan Coopersmith <alan.coopersmith@oracle.com>
Date: Fri, 26 Apr 2013 23:59:25 -0700
Subject: [PATCH] integer overflow in XpGetAttributes & XpGetOneAttribute
 [CVE-2013-2062 1/3]

stringLen & valueLen are CARD32s and need to be bounds checked before adding
one to them to come up with the total size to allocate, to avoid integer
overflow leading to underallocation and writing data from the network past
the end of the allocated buffer.

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
---
 src/XpAttr.c | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

Index: libXp-X11R7.0-1.0.0/src/XpAttr.c
===================================================================
--- libXp-X11R7.0-1.0.0.orig/src/XpAttr.c
+++ libXp-X11R7.0-1.0.0/src/XpAttr.c
@@ -50,6 +50,8 @@
 
 #include <stdio.h>
 #include <sys/stat.h>
+#include <limits.h>
+#include "eat.h"
 
 char *
 XpGetAttributes (
@@ -85,17 +87,18 @@ XpGetAttributes (
     /*
      * Read pool and return to caller.
      */
-    buf = Xmalloc( (unsigned) rep.stringLen + 1 );
+    if (rep.stringLen < INT_MAX)
+        buf = Xmalloc(rep.stringLen + 1);
+    else
+        buf = NULL;
 
     if (!buf) {
-        UnlockDisplay(dpy);
-        SyncHandle();
-        return( (char *) NULL ); /* malloc error */
+        _XEatDataWords(dpy, rep.length);
+    }
+    else {
+        _XReadPad (dpy, (char *) buf, rep.stringLen );
+        buf[rep.stringLen] = 0;
     }
-
-    _XReadPad (dpy, (char *) buf, (long) rep.stringLen );
-
-    buf[rep.stringLen] = 0;
 
     UnlockDisplay(dpy);
     SyncHandle();
@@ -146,18 +149,18 @@ XpGetOneAttribute (
     /*
      * Read variable answer.
      */
-    buf = Xmalloc( (unsigned) rep.valueLen + 1 );
+    if (rep.valueLen < INT_MAX)
+        buf = Xmalloc(rep.valueLen + 1);
+    else
+        buf = NULL;
 
     if (!buf) {
-        UnlockDisplay(dpy);
-        SyncHandle();
-        return( (char *) NULL ); /* malloc error */
+        _XEatDataWords(dpy, rep.length);
+    }
+    else {
+        _XReadPad (dpy, (char *) buf, rep.valueLen);
+        buf[rep.valueLen] = 0;
     }
-
-    buf[rep.valueLen] = 0;
-
-    _XReadPad (dpy, (char *) buf, (long) rep.valueLen );
-    buf[rep.valueLen] = 0;
 
     UnlockDisplay(dpy);
     SyncHandle();
Index: libXp-X11R7.0-1.0.0/src/eat.h
===================================================================
--- /dev/null
+++ libXp-X11R7.0-1.0.0/src/eat.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifndef HAVE__XEATDATAWORDS
+#include <X11/Xmd.h>  /* for LONG64 on 64-bit platforms */
+#include <limits.h>
+
+static inline void _XEatDataWords(Display *dpy, unsigned long n)
+{
+# ifndef LONG64
+    if (n >= (ULONG_MAX >> 2))
+        _XIOError(dpy);
+# endif
+    _XEatData (dpy, n << 2);
+}
+#endif
