1 Fix CVE-2016-7947 and CVE-2016-7948.
3 https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-7947
4 https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-7948
6 Patch copied from upstream source repository:
8 https://cgit.freedesktop.org/xorg/lib/libXrandr/commit/?id=a0df3e1c7728205e5c7650b2e6dce684139254a6
10 From a0df3e1c7728205e5c7650b2e6dce684139254a6 Mon Sep 17 00:00:00 2001
11 From: Tobias Stoeckmann <tobias@stoeckmann.org>
12 Date: Sun, 25 Sep 2016 22:21:40 +0200
13 Subject: [PATCH] Avoid out of boundary accesses on illegal responses
15 The responses of the connected X server have to be properly checked
16 to avoid out of boundary accesses that could otherwise be triggered
17 by a malicious server.
19 Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
20 Reviewed-by: Matthieu Herrb <matthieu@herrb.eu>
22 src/XrrConfig.c | 32 +++++++++++++--------
23 src/XrrCrtc.c | 83 ++++++++++++++++++++++++++++++++++++++++++-------------
24 src/XrrMonitor.c | 18 ++++++++++++
25 src/XrrOutput.c | 11 ++++++++
26 src/XrrProvider.c | 28 ++++++++++++++++---
27 src/XrrScreen.c | 52 ++++++++++++++++++++++------------
28 6 files changed, 172 insertions(+), 52 deletions(-)
30 diff --git a/src/XrrConfig.c b/src/XrrConfig.c
31 index 2f0282b..e68c45a 100644
41 /* we need to be able to manipulate the Display structure on events */
42 @@ -272,23 +273,30 @@ static XRRScreenConfiguration *_XRRGetScreenInfo (Display *dpy,
46 + if (rep.length < INT_MAX >> 2) {
47 + nbytes = (long) rep.length << 2;
49 - nbytes = (long) rep.length << 2;
50 + nbytesRead = (long) (rep.nSizes * SIZEOF (xScreenSizes) +
51 + ((rep.nrateEnts + 1)& ~1) * 2 /* SIZEOF(CARD16) */);
53 - nbytesRead = (long) (rep.nSizes * SIZEOF (xScreenSizes) +
54 - ((rep.nrateEnts + 1)& ~1) * 2 /* SIZEOF (CARD16) */);
56 + * first we must compute how much space to allocate for
57 + * randr library's use; we'll allocate the structures in a single
58 + * allocation, on cleanlyness grounds.
62 - * first we must compute how much space to allocate for
63 - * randr library's use; we'll allocate the structures in a single
64 - * allocation, on cleanlyness grounds.
66 + rbytes = sizeof (XRRScreenConfiguration) +
67 + (rep.nSizes * sizeof (XRRScreenSize) +
68 + rep.nrateEnts * sizeof (int));
70 - rbytes = sizeof (XRRScreenConfiguration) +
71 - (rep.nSizes * sizeof (XRRScreenSize) +
72 - rep.nrateEnts * sizeof (int));
73 + scp = (struct _XRRScreenConfiguration *) Xmalloc(rbytes);
81 - scp = (struct _XRRScreenConfiguration *) Xmalloc(rbytes);
83 _XEatData (dpy, (unsigned long) nbytes);
85 diff --git a/src/XrrCrtc.c b/src/XrrCrtc.c
86 index 5ae35c5..6665092 100644
96 /* we need to be able to manipulate the Display structure on events */
97 @@ -57,22 +58,33 @@ XRRGetCrtcInfo (Display *dpy, XRRScreenResources *resources, RRCrtc crtc)
101 - nbytes = (long) rep.length << 2;
102 + if (rep.length < INT_MAX >> 2)
104 + nbytes = (long) rep.length << 2;
106 - nbytesRead = (long) (rep.nOutput * 4 +
107 - rep.nPossibleOutput * 4);
108 + nbytesRead = (long) (rep.nOutput * 4 +
109 + rep.nPossibleOutput * 4);
112 - * first we must compute how much space to allocate for
113 - * randr library's use; we'll allocate the structures in a single
114 - * allocation, on cleanlyness grounds.
117 + * first we must compute how much space to allocate for
118 + * randr library's use; we'll allocate the structures in a single
119 + * allocation, on cleanlyness grounds.
122 - rbytes = (sizeof (XRRCrtcInfo) +
123 - rep.nOutput * sizeof (RROutput) +
124 - rep.nPossibleOutput * sizeof (RROutput));
125 + rbytes = (sizeof (XRRCrtcInfo) +
126 + rep.nOutput * sizeof (RROutput) +
127 + rep.nPossibleOutput * sizeof (RROutput));
129 + xci = (XRRCrtcInfo *) Xmalloc(rbytes);
139 - xci = (XRRCrtcInfo *) Xmalloc(rbytes);
141 _XEatDataWords (dpy, rep.length);
143 @@ -194,12 +206,21 @@ XRRGetCrtcGamma (Display *dpy, RRCrtc crtc)
144 if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
147 - nbytes = (long) rep.length << 2;
148 + if (rep.length < INT_MAX >> 2)
150 + nbytes = (long) rep.length << 2;
152 - /* three channels of CARD16 data */
153 - nbytesRead = (rep.size * 2 * 3);
154 + /* three channels of CARD16 data */
155 + nbytesRead = (rep.size * 2 * 3);
157 - crtc_gamma = XRRAllocGamma (rep.size);
158 + crtc_gamma = XRRAllocGamma (rep.size);
169 @@ -357,7 +378,7 @@ XRRGetCrtcTransform (Display *dpy,
170 xRRGetCrtcTransformReq *req;
171 int major_version, minor_version;
172 XRRCrtcTransformAttributes *attr;
173 - char *extra = NULL, *e;
174 + char *extra = NULL, *end = NULL, *e;
178 @@ -395,9 +416,17 @@ XRRGetCrtcTransform (Display *dpy,
181 int extraBytes = rep.length * 4 - CrtcTransformExtra;
182 - extra = Xmalloc (extraBytes);
183 + if (rep.length < INT_MAX / 4 &&
184 + rep.length * 4 >= CrtcTransformExtra) {
185 + extra = Xmalloc (extraBytes);
186 + end = extra + extraBytes;
190 - _XEatDataWords (dpy, rep.length - (CrtcTransformExtra >> 2));
191 + if (rep.length > (CrtcTransformExtra >> 2))
192 + _XEatDataWords (dpy, rep.length - (CrtcTransformExtra >> 2));
194 + _XEatDataWords (dpy, rep.length);
198 @@ -429,22 +458,38 @@ XRRGetCrtcTransform (Display *dpy,
202 + if (e + rep.pendingNbytesFilter > end) {
206 memcpy (attr->pendingFilter, e, rep.pendingNbytesFilter);
207 attr->pendingFilter[rep.pendingNbytesFilter] = '\0';
208 e += (rep.pendingNbytesFilter + 3) & ~3;
209 for (p = 0; p < rep.pendingNparamsFilter; p++) {
217 attr->pendingParams[p] = (XFixed) f;
219 attr->pendingNparams = rep.pendingNparamsFilter;
221 + if (e + rep.currentNbytesFilter > end) {
225 memcpy (attr->currentFilter, e, rep.currentNbytesFilter);
226 attr->currentFilter[rep.currentNbytesFilter] = '\0';
227 e += (rep.currentNbytesFilter + 3) & ~3;
228 for (p = 0; p < rep.currentNparamsFilter; p++) {
236 attr->currentParams[p] = (XFixed) f;
237 diff --git a/src/XrrMonitor.c b/src/XrrMonitor.c
238 index a9eaa7b..adc5330 100644
239 --- a/src/XrrMonitor.c
240 +++ b/src/XrrMonitor.c
247 #include <X11/Xlib.h>
248 /* we need to be able to manipulate the Display structure on events */
249 @@ -65,6 +66,15 @@ XRRGetMonitors(Display *dpy, Window window, Bool get_active, int *nmonitors)
253 + if (rep.length > INT_MAX >> 2 ||
254 + rep.nmonitors > INT_MAX / SIZEOF(xRRMonitorInfo) ||
255 + rep.noutputs > INT_MAX / 4 ||
256 + rep.nmonitors * SIZEOF(xRRMonitorInfo) > INT_MAX - rep.noutputs * 4) {
257 + _XEatData (dpy, rep.length);
258 + UnlockDisplay (dpy);
262 nbytes = (long) rep.length << 2;
263 nmon = rep.nmonitors;
264 noutput = rep.noutputs;
265 @@ -111,6 +121,14 @@ XRRGetMonitors(Display *dpy, Window window, Bool get_active, int *nmonitors)
266 mon[m].outputs = output;
267 buf += SIZEOF (xRRMonitorInfo);
268 xoutput = (CARD32 *) buf;
269 + if (xmon->noutput > rep.noutputs) {
272 + UnlockDisplay (dpy);
276 + rep.noutputs -= xmon->noutput;
277 for (o = 0; o < xmon->noutput; o++)
278 output[o] = xoutput[o];
279 output += xmon->noutput;
280 diff --git a/src/XrrOutput.c b/src/XrrOutput.c
281 index 85f0b6e..30f3d40 100644
282 --- a/src/XrrOutput.c
283 +++ b/src/XrrOutput.c
290 #include <X11/Xlib.h>
291 /* we need to be able to manipulate the Display structure on events */
292 @@ -60,6 +61,16 @@ XRRGetOutputInfo (Display *dpy, XRRScreenResources *resources, RROutput output)
296 + if (rep.length > INT_MAX >> 2 || rep.length < (OutputInfoExtra >> 2))
298 + if (rep.length > (OutputInfoExtra >> 2))
299 + _XEatDataWords (dpy, rep.length - (OutputInfoExtra >> 2));
301 + _XEatDataWords (dpy, rep.length);
302 + UnlockDisplay (dpy);
306 nbytes = ((long) (rep.length) << 2) - OutputInfoExtra;
308 nbytesRead = (long) (rep.nCrtcs * 4 +
309 diff --git a/src/XrrProvider.c b/src/XrrProvider.c
310 index 9e620c7..d796cd0 100644
311 --- a/src/XrrProvider.c
312 +++ b/src/XrrProvider.c
319 #include <X11/Xlib.h>
320 /* we need to be able to manipulate the Display structure on events */
321 @@ -59,12 +60,20 @@ XRRGetProviderResources(Display *dpy, Window window)
325 - nbytes = (long) rep.length << 2;
326 + if (rep.length < INT_MAX >> 2) {
327 + nbytes = (long) rep.length << 2;
329 - nbytesRead = (long) (rep.nProviders * 4);
330 + nbytesRead = (long) (rep.nProviders * 4);
332 - rbytes = (sizeof(XRRProviderResources) + rep.nProviders * sizeof(RRProvider));
333 - xrpr = (XRRProviderResources *) Xmalloc(rbytes);
334 + rbytes = (sizeof(XRRProviderResources) + rep.nProviders *
335 + sizeof(RRProvider));
336 + xrpr = (XRRProviderResources *) Xmalloc(rbytes);
345 _XEatDataWords (dpy, rep.length);
346 @@ -121,6 +130,17 @@ XRRGetProviderInfo(Display *dpy, XRRScreenResources *resources, RRProvider provi
350 + if (rep.length > INT_MAX >> 2 || rep.length < ProviderInfoExtra >> 2)
352 + if (rep.length < ProviderInfoExtra >> 2)
353 + _XEatDataWords (dpy, rep.length);
355 + _XEatDataWords (dpy, rep.length - (ProviderInfoExtra >> 2));
356 + UnlockDisplay (dpy);
361 nbytes = ((long) rep.length << 2) - ProviderInfoExtra;
363 nbytesRead = (long)(rep.nCrtcs * 4 +
364 diff --git a/src/XrrScreen.c b/src/XrrScreen.c
365 index b8ce7e5..1f7ffe6 100644
366 --- a/src/XrrScreen.c
367 +++ b/src/XrrScreen.c
374 #include <X11/Xlib.h>
375 /* we need to be able to manipulate the Display structure on events */
376 @@ -105,27 +106,36 @@ doGetScreenResources (Display *dpy, Window window, int poll)
377 xrri->has_rates = _XRRHasRates (xrri->minor_version, xrri->major_version);
380 - nbytes = (long) rep.length << 2;
381 + if (rep.length < INT_MAX >> 2) {
382 + nbytes = (long) rep.length << 2;
384 - nbytesRead = (long) (rep.nCrtcs * 4 +
386 - rep.nModes * SIZEOF (xRRModeInfo) +
387 - ((rep.nbytesNames + 3) & ~3));
388 + nbytesRead = (long) (rep.nCrtcs * 4 +
390 + rep.nModes * SIZEOF (xRRModeInfo) +
391 + ((rep.nbytesNames + 3) & ~3));
394 - * first we must compute how much space to allocate for
395 - * randr library's use; we'll allocate the structures in a single
396 - * allocation, on cleanlyness grounds.
399 + * first we must compute how much space to allocate for
400 + * randr library's use; we'll allocate the structures in a single
401 + * allocation, on cleanlyness grounds.
404 + rbytes = (sizeof (XRRScreenResources) +
405 + rep.nCrtcs * sizeof (RRCrtc) +
406 + rep.nOutputs * sizeof (RROutput) +
407 + rep.nModes * sizeof (XRRModeInfo) +
408 + rep.nbytesNames + rep.nModes); /* '\0' terminate names */
410 - rbytes = (sizeof (XRRScreenResources) +
411 - rep.nCrtcs * sizeof (RRCrtc) +
412 - rep.nOutputs * sizeof (RROutput) +
413 - rep.nModes * sizeof (XRRModeInfo) +
414 - rep.nbytesNames + rep.nModes); /* '\0' terminate names */
415 + xrsr = (XRRScreenResources *) Xmalloc(rbytes);
416 + wire_names = (char *) Xmalloc (rep.nbytesNames);
425 - xrsr = (XRRScreenResources *) Xmalloc(rbytes);
426 - wire_names = (char *) Xmalloc (rep.nbytesNames);
427 if (xrsr == NULL || wire_names == NULL) {
430 @@ -174,6 +184,14 @@ doGetScreenResources (Display *dpy, Window window, int poll)
431 wire_name = wire_names;
432 for (i = 0; i < rep.nModes; i++) {
433 xrsr->modes[i].name = names;
434 + if (xrsr->modes[i].nameLength > rep.nbytesNames) {
436 + Xfree (wire_names);
437 + UnlockDisplay (dpy);
441 + rep.nbytesNames -= xrsr->modes[i].nameLength;
442 memcpy (names, wire_name, xrsr->modes[i].nameLength);
443 names[xrsr->modes[i].nameLength] = '\0';
444 names += xrsr->modes[i].nameLength + 1;