Commit 77edd88e authored by Alan Coopersmith's avatar Alan Coopersmith Committed by Ulrich Sibiller

Avoid overflows in XGetFontPath() [CVE-2013-1997 14/15]

Ensure that when breaking the returned list into individual strings, we don't walk past the end of allocated memory to write the '\0' bytes Signed-off-by: 's avatarAlan Coopersmith <alan.coopersmith@oracle.com> Reviewed-by: 's avatarMatthieu Herrb <matthieu.herrb@laas.fr> Signed-off-by: 's avatarJulien Cristau <jcristau@debian.org> Backported-to-NX-by: 's avatarUlrich Sibiller <uli42@gmx.de>
parent f6c5069a
...@@ -28,15 +28,18 @@ in this Software without prior written authorization from The Open Group. ...@@ -28,15 +28,18 @@ in this Software without prior written authorization from The Open Group.
#include <config.h> #include <config.h>
#endif #endif
#include "Xlibint.h" #include "Xlibint.h"
#include <limits.h>
char **XGetFontPath( char **XGetFontPath(
register Display *dpy, register Display *dpy,
int *npaths) /* RETURN */ int *npaths) /* RETURN */
{ {
xGetFontPathReply rep; xGetFontPathReply rep;
register long nbytes; unsigned long nbytes;
char **flist; char **flist = NULL;
char *ch; char *ch = NULL;
char *chend;
int count = 0;
register unsigned i; register unsigned i;
register int length; register int length;
register xReq *req; register xReq *req;
...@@ -46,16 +49,17 @@ char **XGetFontPath( ...@@ -46,16 +49,17 @@ char **XGetFontPath(
(void) _XReply (dpy, (xReply *) &rep, 0, xFalse); (void) _XReply (dpy, (xReply *) &rep, 0, xFalse);
if (rep.nPaths) { if (rep.nPaths) {
flist = (char **) flist = Xmalloc(rep.nPaths * sizeof (char *));
Xmalloc((unsigned) rep.nPaths * sizeof (char *)); if (rep.length < (LONG_MAX >> 2)) {
nbytes = (long)rep.length << 2; nbytes = (unsigned long) rep.length << 2;
ch = (char *) Xmalloc ((unsigned) (nbytes + 1)); ch = Xmalloc (nbytes + 1);
/* +1 to leave room for last null-terminator */ /* +1 to leave room for last null-terminator */
}
if ((! flist) || (! ch)) { if ((! flist) || (! ch)) {
if (flist) Xfree((char *) flist); if (flist) Xfree((char *) flist);
if (ch) Xfree(ch); if (ch) Xfree(ch);
_XEatData(dpy, (unsigned long) nbytes); _XEatDataWords(dpy, rep.length);
UnlockDisplay(dpy); UnlockDisplay(dpy);
SyncHandle(); SyncHandle();
return (char **) NULL; return (char **) NULL;
...@@ -65,16 +69,20 @@ char **XGetFontPath( ...@@ -65,16 +69,20 @@ char **XGetFontPath(
/* /*
* unpack into null terminated strings. * unpack into null terminated strings.
*/ */
chend = ch + (nbytes + 1);
length = *ch; length = *ch;
for (i = 0; i < rep.nPaths; i++) { for (i = 0; i < rep.nPaths; i++) {
if (ch + length < chend) {
flist[i] = ch+1; /* skip over length */ flist[i] = ch+1; /* skip over length */
ch += length + 1; /* find next length ... */ ch += length + 1; /* find next length ... */
length = *ch; length = *ch;
*ch = '\0'; /* and replace with null-termination */ *ch = '\0'; /* and replace with null-termination */
count++;
} else
flist[i] = NULL;
} }
} }
else flist = NULL; *npaths = count;
*npaths = rep.nPaths;
UnlockDisplay(dpy); UnlockDisplay(dpy);
SyncHandle(); SyncHandle();
return (flist); return (flist);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment