Skip to contentSkip to conversation

glx: Refcnt the GLXDrawable to avoid use after free with multiple FreeResource

PATCH In review Chris Wilson 4 participants 4 messages Dec 10, 2010 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 glx: Refcnt the GLXDrawable to avoid use after free with multiple FreeResource

Although there may be more than one resource handles pointing to the Drawable, we only want to destroy it once and only reference the resource which may have just been deleted on the first instance.

Cc Kristian Høgsberg <[email protected]>
Cc Michel Dänzer <[email protected]>
---
 glx/glxcmds.c     |    6 ++++++
 glx/glxdrawable.h |    3 +++
 glx/glxext.c      |   15 ++++++++++-----
 3 files changed, 19 insertions(+), 5 deletions(-)
glx/glxcmds.c +6 -0
index de9c3f0..fa153d5 100644
@@ -529,6 +529,7 @@ __glXGetDrawable(__GLXcontext *glxc, GLXDrawable drawId, ClientPtr client,
 	*error = BadAlloc;
 	return NULL;
     }
+    pGlxDraw->refcnt++;
 
     return pGlxDraw;
 }
@@ -1099,8 +1100,10 @@ __glXDrawableInit(__GLXdrawable *drawable,
     drawable->pDraw = pDraw;
     drawable->type = type;
     drawable->drawId = drawId;
+    drawable->otherId = 0;
     drawable->config = config;
     drawable->eventMask = 0;
+    drawable->refcnt = 0;
 
     return GL_TRUE;
 }
@@ -1130,6 +1133,7 @@ DoCreateGLXDrawable(ClientPtr client, __GLXscreen *pGlxScreen,
 	pGlxDraw->destroy (pGlxDraw);
 	return BadAlloc;
     }
+    pGlxDraw->refcnt++;
 
     /* Add the glx drawable under the XID of the underlying X drawable
      * too.  That way we'll get a callback in DrawableGone and can
@@ -1139,6 +1143,8 @@ DoCreateGLXDrawable(ClientPtr client, __GLXscreen *pGlxScreen,
 	pGlxDraw->destroy (pGlxDraw);
 	return BadAlloc;
     }
+    pGlxDraw->refcnt++;
+    pGlxDraw->otherId = pDraw->id;
 
     return Success;
 }
glx/glxdrawable.h +3 -0
index 2a365c5..80c3234 100644
@@ -51,8 +51,11 @@ struct __GLXdrawable {
     void      (*waitX)(__GLXdrawable *);
     void      (*waitGL)(__GLXdrawable *);
 
+    int refcnt; /* number of resources handles referencing this */
+
     DrawablePtr pDraw;
     XID drawId;
+    XID otherId; /* for glx1.3 we need to track the original Drawable as well */
 
     /*
     ** Either GLX_DRAWABLE_PIXMAP, GLX_DRAWABLE_WINDOW or
glx/glxext.c +10 -5
index 4bd5d6b..c473aa9 100644
@@ -128,13 +128,18 @@ static Bool DrawableGone(__GLXdrawable *glxPriv, XID xid)
      * constructors, we added it as a glx drawable resource under both
      * its glx drawable ID and it X drawable ID.  Remove the other
      * resource now so we don't a callback for freed memory. */
-    if (glxPriv->drawId != glxPriv->pDraw->id) {
-	if (xid == glxPriv->drawId)
-	    FreeResourceByType(glxPriv->pDraw->id, __glXDrawableRes, TRUE);
-	else
-	    FreeResourceByType(glxPriv->drawId, __glXDrawableRes, TRUE);
+    if (glxPriv->otherId) {
+	    XID other = glxPriv->otherId;
+	    glxPriv->otherId = 0;
+	    if (xid == other)
+		    FreeResourceByType(glxPriv->drawId, __glXDrawableRes, TRUE);
+	    else
+		    FreeResourceByType(other, __glXDrawableRes, TRUE);
     }
 
+    if (--glxPriv->refcnt)
+	    return TRUE;
+
     for (c = glxAllContexts; c; c = next) {
 	next = c->next;
 	if (c->isCurrent && (c->drawPriv == glxPriv || c->readPriv == glxPriv)) {
Signature · 6 lines
1.7.2.3

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): 1291985012-12090-1-git-send-email-chris-Y6uKTt2uX1cEflXRtASbqLVCufUGDwFn@public.gmane.org

#2 lore raw
Quoted 18 lines · Chris Wilson

Although there may be more than one resource handles pointing to the Drawable, we only want to destroy it once and only reference the resource which may have just been deleted on the first instance.

Cc Kristian Høgsberg <[email protected]>
Cc Michel Dänzer <[email protected]>
---
 glx/glxcmds.c     |    6 ++++++
 glx/glxdrawable.h |    3 +++
 glx/glxext.c      |   15 ++++++++++-----
 3 files changed, 19 insertions(+), 5 deletions(-)
glx/glxcmds.c +0 -0
index de9c3f0..fa153d5 100644

glx/glxcmds.c DoCreateGLXDrawable(ClientPtr client, __GLXscreen *pGlxScreen, · line 1147

Quoted 5 lines
glx/glxcmds.c +2 -0
@@ -1139,6 +1143,8 @@ DoCreateGLXDrawable(ClientPtr client, __GLXscreen *pGlxScreen,
 	pGlxDraw->destroy (pGlxDraw);
 	return BadAlloc;
     }
+    pGlxDraw->refcnt++;
+    pGlxDraw->otherId = pDraw->id;

I think this should use drawableId instead of pDraw->id, and refcnt should only be incremented if it's different from glxDrawableId. Combined with the patch I attached to
https://bugs.freedesktop.org/show_bug.cgi?id=28181 this block could look something like

    if (drawableId != glxDrawableId) {
	/* Add the glx drawable under the XID of the underlying X drawable
	 * too.  That way we'll get a callback in DrawableGone and can
	 * clean up properly when the drawable is destroyed. */
	if (!AddResource(drawableId, __glXDrawableRes, pGlxDraw)) {
	    pGlxDraw->destroy (pGlxDraw);
	    return BadAlloc;
	}

	pGlxDraw->refcnt++;
	pGlxDraw->otherId = pDraw->id;
    }

Then this patch will hopefully address that bug and maybe others with references to DrawableGone. Fingers crossed. :)

Quoted 14 lines
glx/glxext.c +1 -5
index 4bd5d6b..c473aa9 100644
@@ -128,13 +128,18 @@ static Bool DrawableGone(__GLXdrawable *glxPriv, XID xid)
      * constructors, we added it as a glx drawable resource under both
      * its glx drawable ID and it X drawable ID.  Remove the other
      * resource now so we don't a callback for freed memory. */
-    if (glxPriv->drawId != glxPriv->pDraw->id) {
-	if (xid == glxPriv->drawId)
-	    FreeResourceByType(glxPriv->pDraw->id, __glXDrawableRes, TRUE);
-	else
-	    FreeResourceByType(glxPriv->drawId, __glXDrawableRes, TRUE);
+    if (glxPriv->otherId) {

glx/glxext.c static Bool DrawableGone(__GLXdrawable *glxPriv, XID xid) · line 141

Quoted 10 lines
glx/glxext.c +8 -0
+	    XID other = glxPriv->otherId;
+	    glxPriv->otherId = 0;
+	    if (xid == other)
+		    FreeResourceByType(glxPriv->drawId, __glXDrawableRes, TRUE);
+	    else
+		    FreeResourceByType(other, __glXDrawableRes, TRUE);
     }
 
+    if (--glxPriv->refcnt)
+	    return TRUE;

The return at the end of the function uses True instead of TRUE.

Signature · 6 lines
Earthling Michel Dänzer           |                http://www.vmware.com
Libre software enthusiast         |          Debian, X and DRI developer
_______________________________________________
[email protected]: X.Org development
Archives: http://lists.x.org/archives/xorg-devel
Info: http://lists.x.org/mailman/listinfo/xorg-devel

A message that is not in the archive (sent off-list or not yet received): 1291986983.11622.125.camel-2h6evNeVVYGs1BDpvl8NfQ@public.gmane.org

#4 lore raw

PATCH 1/1 glx: Refcnt the GLXDrawable to avoid use after free with multiple FreeResource

Although there may be more than one resource handles pointing to the Drawable, we only want to destroy it once and only reference the resource which may have just been deleted on the first instance.

v2: Apply fixes and combine with another bug fix from Michel Dänzer,

    https://bugs.freedesktop.org/show_bug.cgi?id=28181
Cc Kristian Høgsberg <[email protected]>
Cc Michel Dänzer <[email protected]>
---
 glx/glxcmds.c     |   23 +++++++++++++++--------
 glx/glxdrawable.h |    3 +++
 glx/glxext.c      |   15 ++++++++++-----
 3 files changed, 28 insertions(+), 13 deletions(-)
glx/glxcmds.c +14 -7
index de9c3f0..b3ea784 100644
@@ -529,6 +529,7 @@ __glXGetDrawable(__GLXcontext *glxc, GLXDrawable drawId, ClientPtr client,
 	*error = BadAlloc;
 	return NULL;
     }
+    pGlxDraw->refcnt++;
 
     return pGlxDraw;
 }
@@ -1099,8 +1100,10 @@ __glXDrawableInit(__GLXdrawable *drawable,
     drawable->pDraw = pDraw;
     drawable->type = type;
     drawable->drawId = drawId;
+    drawable->otherId = 0;
     drawable->config = config;
     drawable->eventMask = 0;
+    drawable->refcnt = 0;
 
     return GL_TRUE;
 }
@@ -1130,14 +1133,18 @@ DoCreateGLXDrawable(ClientPtr client, __GLXscreen *pGlxScreen,
 	pGlxDraw->destroy (pGlxDraw);
 	return BadAlloc;
     }
+    pGlxDraw->refcnt++;
 
-    /* Add the glx drawable under the XID of the underlying X drawable
-     * too.  That way we'll get a callback in DrawableGone and can
-     * clean up properly when the drawable is destroyed. */
-    if (drawableId != glxDrawableId &&
-	!AddResource(pDraw->id, __glXDrawableRes, pGlxDraw)) {
-	pGlxDraw->destroy (pGlxDraw);
-	return BadAlloc;
+    if (drawableId != glxDrawableId) {
+	/* Add the glx drawable under the XID of the underlying X drawable
+	 * too.  That way we'll get a callback in DrawableGone and can
+	 * clean up properly when the drawable is destroyed. */
+	if (!AddResource(drawableId, __glXDrawableRes, pGlxDraw)) {
+	    pGlxDraw->destroy (pGlxDraw);
+	    return BadAlloc;
+	}
+	pGlxDraw->refcnt++;
+	pGlxDraw->otherId = drawableId;
     }
 
     return Success;
glx/glxdrawable.h +3 -0
index 2a365c5..80c3234 100644
@@ -51,8 +51,11 @@ struct __GLXdrawable {
     void      (*waitX)(__GLXdrawable *);
     void      (*waitGL)(__GLXdrawable *);
 
+    int refcnt; /* number of resources handles referencing this */
+
     DrawablePtr pDraw;
     XID drawId;
+    XID otherId; /* for glx1.3 we need to track the original Drawable as well */
 
     /*
     ** Either GLX_DRAWABLE_PIXMAP, GLX_DRAWABLE_WINDOW or
glx/glxext.c +10 -5
index 4bd5d6b..77db8b0 100644
@@ -128,13 +128,18 @@ static Bool DrawableGone(__GLXdrawable *glxPriv, XID xid)
      * constructors, we added it as a glx drawable resource under both
      * its glx drawable ID and it X drawable ID.  Remove the other
      * resource now so we don't a callback for freed memory. */
-    if (glxPriv->drawId != glxPriv->pDraw->id) {
-	if (xid == glxPriv->drawId)
-	    FreeResourceByType(glxPriv->pDraw->id, __glXDrawableRes, TRUE);
-	else
-	    FreeResourceByType(glxPriv->drawId, __glXDrawableRes, TRUE);
+    if (glxPriv->otherId) {
+	    XID other = glxPriv->otherId;
+	    glxPriv->otherId = 0;
+	    if (xid == other)
+		    FreeResourceByType(glxPriv->drawId, __glXDrawableRes, TRUE);
+	    else
+		    FreeResourceByType(other, __glXDrawableRes, TRUE);
     }
 
+    if (--glxPriv->refcnt)
+	    return True;
+
     for (c = glxAllContexts; c; c = next) {
 	next = c->next;
 	if (c->isCurrent && (c->drawPriv == glxPriv || c->readPriv == glxPriv)) {
Signature · 6 lines
1.7.2.3

_______________________________________________
[email protected]: X.Org development
Archives: http://lists.x.org/archives/xorg-devel
Info: http://lists.x.org/mailman/listinfo/xorg-devel
Reviewed-by on v1 1/1
#5 lore raw
Quoted 10 lines · Chris Wilson

Although there may be more than one resource handles pointing to the Drawable, we only want to destroy it once and only reference the resource which may have just been deleted on the first instance.

v2: Apply fixes and combine with another bug fix from Michel Dänzer,

    https://bugs.freedesktop.org/show_bug.cgi?id=28181
Cc Kristian Høgsberg <[email protected]>
Cc Michel Dänzer <[email protected]>

I'd wait for Kristian's review as well though.

Signature · 6 lines
Earthling Michel Dänzer           |                http://www.vmware.com
Libre software enthusiast         |          Debian, X and DRI developer
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel