Tk Source Code

Artifact [ffe73d4b]
Login

Artifact ffe73d4baae9976f8c45b39b3a777017c3ff5a7d97dc301f89ccff1b22fb476e:

Attachment "tk-macos-ttk-uaf-9.0.3.patch" to ticket [b964b98d] added by anonymous 2026-07-05 15:45:26. (unpublished)
diff --git a/generic/tkGeometry.c b/generic/tkGeometry.c
index bf35916..b0f2b39 100644
--- a/generic/tkGeometry.c
+++ b/generic/tkGeometry.c
@@ -432,8 +432,16 @@ Tk_MaintainGeometry(
 	 * setting up the extra infrastructure for management, just make a
 	 * call to Tk_MoveResizeWindow; the parent/child relationship will
 	 * take care of the rest.
+	 *
+	 * Tk_MoveResizeWindow delivers <Configure> synchronously
+	 * (TkDoConfigureNotify), so a binding may destroy either window
+	 * before it returns (ticket [b964b98d1b]).  Preserve both windows so
+	 * the flag checks below never touch freed memory, and don't map a
+	 * window that is being destroyed.
 	 */
 
+	Tcl_Preserve(window);
+	Tcl_Preserve(container);
 	Tk_MoveResizeWindow(window, x, y, width, height);
 
 	/*
@@ -442,9 +450,13 @@ Tk_MaintainGeometry(
 	 * is taken care of elsewhere).
 	 */
 
-	if (Tk_IsMapped(container)) {
+	if (!(((TkWindow *)window)->flags & TK_ALREADY_DEAD)
+		&& !(((TkWindow *)container)->flags & TK_ALREADY_DEAD)
+		&& Tk_IsMapped(container)) {
 	    Tk_MapWindow(window);
 	}
+	Tcl_Release(container);
+	Tcl_Release(window);
 	return;
     }
 
diff --git a/generic/ttk/ttkManager.c b/generic/ttk/ttkManager.c
index e660538..1433936 100644
--- a/generic/ttk/ttkManager.c
+++ b/generic/ttk/ttkManager.c
@@ -39,6 +39,23 @@
  * in this case Tk does _not_ call the lostContentProc (documented behavior).
  * Tk doesn't handle case (3) either; to account for that we
  * register an event handler on the content window to track <Destroy> events.
+ *
+ * +++ Re-entrancy <<NOTE-REENTRANCY>>.
+ *
+ * Calls into Tk made while placing or (un)mapping content windows may
+ * service events (on Aqua, window operations pump the Cocoa run loop, which
+ * can service the Tcl event queue and idle calls mid-operation).  Such a
+ * re-entrant event can destroy a content window, running RemoveContent()
+ * in the middle of a placement: the Ttk_Content record (and possibly the
+ * manager's data record for it) is freed and content[] is compacted while
+ * a caller still holds pointers into it.  Dereferencing them afterwards is
+ * a use-after-free (see ticket [b964b98d1b]).  To guard against this,
+ * mgr->epoch is bumped on every mutation of content[], and routines that
+ * call into Tk while holding a Ttk_Content pointer re-check the epoch
+ * after each such call, bailing out if the content list changed.  Bailing
+ * out early is safe: every mutation path (InsertContent, RemoveContent,
+ * Ttk_ReorderContent) schedules a fresh relayout, so a consistent layout
+ * pass always follows.
  */
 
 /* ++ Data structures.
@@ -63,6 +80,13 @@ struct TtkManager_
     unsigned		flags;
     Tcl_Size		nContent;
     Ttk_Content		**content;
+    size_t		epoch;	/* Bumped on every mutation of content[].
+				 * Calls into Tk (e.g. Tk_MaintainGeometry,
+				 * Tk_MapWindow) may service events and
+				 * re-enter RemoveContent/InsertContent,
+				 * freeing Ttk_Content records out from under
+				 * an iteration; comparing epochs detects
+				 * this.  See <<NOTE-REENTRANCY>>. */
 };
 
 /* manager->flags bits:
@@ -118,6 +142,10 @@ static void ManagerIdleProc(void *clientData)
     Ttk_Manager *mgr = (Ttk_Manager *)clientData;
     mgr->flags &= ~MGR_UPDATE_PENDING;
 
+    if (((TkWindow *)mgr->window)->flags & TK_ALREADY_DEAD) {
+	/* The container is being torn down; don't touch the layout. */
+	return;
+    }
     if (mgr->flags & MGR_RESIZE_REQUIRED) {
 	RecomputeSize(mgr);
     }
@@ -152,15 +180,27 @@ static void ManagerEventHandler(void *clientData, XEvent *eventPtr)
 	case MapNotify:
 	    for (i = 0; i < mgr->nContent; ++i) {
 		Ttk_Content *content = mgr->content[i];
-		if (content->flags & CONTENT_MAPPED) {
+		if ((content->flags & CONTENT_MAPPED)
+			&& !(((TkWindow *)content->window)->flags
+			    & TK_ALREADY_DEAD)) {
+		    size_t epoch = mgr->epoch;
 		    Tk_MapWindow(content->window);
+		    if (mgr->epoch != epoch) {
+			/* <<NOTE-REENTRANCY>> */
+			break;
+		    }
 		}
 	    }
 	    break;
 	case UnmapNotify:
 	    for (i = 0; i < mgr->nContent; ++i) {
 		Ttk_Content *content = mgr->content[i];
+		size_t epoch = mgr->epoch;
 		Tk_UnmapWindow(content->window);
+		if (mgr->epoch != epoch) {
+		    /* <<NOTE-REENTRANCY>> */
+		    break;
+		}
 	    }
 	    break;
     }
@@ -216,6 +256,7 @@ Ttk_Manager *Ttk_CreateManager(
     mgr->nContent	= 0;
     mgr->content	= NULL;
     mgr->flags	= 0;
+    mgr->epoch	= 0;
 
     Tk_CreateEventHandler(
 	mgr->window, ManagerEventMask, ManagerEventHandler, mgr);
@@ -250,6 +291,7 @@ void Ttk_DeleteManager(Ttk_Manager *mgr)
 static void InsertContent(Ttk_Manager *mgr, Ttk_Content *content, Tcl_Size index)
 {
     Tcl_Size endIndex = mgr->nContent++;
+    ++mgr->epoch;
     mgr->content = (Ttk_Content **)ckrealloc(mgr->content, mgr->nContent * sizeof(Ttk_Content *));
 
     while (endIndex > index) {
@@ -281,6 +323,8 @@ static void RemoveContent(Ttk_Manager *mgr, Tcl_Size index)
     Ttk_Content *content = mgr->content[index];
     Tcl_Size i;
 
+    ++mgr->epoch;
+
     /* Notify manager:
      */
     mgr->managerSpec->ContentRemoved(mgr->managerData, index);
@@ -303,7 +347,13 @@ static void RemoveContent(Ttk_Manager *mgr, Tcl_Size index)
 
     DeleteContent(content);
 
-    ScheduleUpdate(mgr, MGR_RESIZE_REQUIRED);
+    if (!(((TkWindow *)mgr->window)->flags & TK_ALREADY_DEAD)) {
+	/* Don't re-arm the relayout idle while the container itself is
+	 * being torn down; it could fire mid-teardown.  Ttk_DeleteManager
+	 * cancels any pending call anyway.
+	 */
+	ScheduleUpdate(mgr, MGR_RESIZE_REQUIRED);
+    }
 }
 
 /*------------------------------------------------------------------------
@@ -368,10 +418,27 @@ void Ttk_ForgetContent(Ttk_Manager *mgr, Tcl_Size index)
 void Ttk_PlaceContent(
     Ttk_Manager *mgr, Tcl_Size index, int x, int y, int width, int height)
 {
-    Ttk_Content *content = mgr->content[index];
+    Ttk_Content *content;
+    size_t epoch = mgr->epoch;
+
+    if (index >= mgr->nContent) {
+	return;
+    }
+    content = mgr->content[index];
+    if (((TkWindow *)content->window)->flags & TK_ALREADY_DEAD) {
+	/* The content window is being torn down; its DestroyNotify will
+	 * remove it from the manager.
+	 */
+	return;
+    }
     Tk_MaintainGeometry(content->window,mgr->window,x,y,width,height);
+    if (mgr->epoch != epoch) {
+	/* <<NOTE-REENTRANCY>>: 'content' may have been freed. */
+	return;
+    }
     content->flags |= CONTENT_MAPPED;
-    if (Tk_IsMapped(mgr->window)) {
+    if (Tk_IsMapped(mgr->window)
+	    && !(((TkWindow *)content->window)->flags & TK_ALREADY_DEAD)) {
 	Tk_MapWindow(content->window);
     }
 }
@@ -381,8 +448,18 @@ void Ttk_PlaceContent(
  */
 void Ttk_UnmapContent(Ttk_Manager *mgr, Tcl_Size index)
 {
-    Ttk_Content *content = mgr->content[index];
+    Ttk_Content *content;
+    size_t epoch = mgr->epoch;
+
+    if (index >= mgr->nContent) {
+	return;
+    }
+    content = mgr->content[index];
     Tk_UnmaintainGeometry(content->window, mgr->window);
+    if (mgr->epoch != epoch) {
+	/* <<NOTE-REENTRANCY>>: 'content' may have been freed. */
+	return;
+    }
     content->flags &= ~CONTENT_MAPPED;
     /* Contrary to documentation, Tk_UnmaintainGeometry doesn't always
      * unmap the content window:
@@ -502,6 +579,8 @@ void Ttk_ReorderContent(Ttk_Manager *mgr, Tcl_Size fromIndex, Tcl_Size toIndex)
 {
     Ttk_Content *moved = mgr->content[fromIndex];
 
+    ++mgr->epoch;
+
     /* Shuffle down: */
     while (fromIndex > toIndex) {
 	mgr->content[fromIndex] = mgr->content[fromIndex - 1];
diff --git a/generic/ttk/ttkPanedwindow.c b/generic/ttk/ttkPanedwindow.c
index 43271e5..52cba65 100644
--- a/generic/ttk/ttkPanedwindow.c
+++ b/generic/ttk/ttkPanedwindow.c
@@ -385,6 +385,11 @@ static void PlacePanes(Paned *pw)
     for (index = 0; index < Ttk_NumberContent(pw->paned.mgr); ++index) {
 	Pane *pane = (Pane *)Ttk_ContentData(pw->paned.mgr, index);
 	int size = pane->sashPos - pos;
+	/* Read sashPos before placing: Ttk_PlaceContent/Ttk_UnmapContent may
+	 * service events that destroy the pane, freeing 'pane'
+	 * (see <<NOTE-REENTRANCY>> in ttkManager.c).
+	 */
+	int nextPos = pane->sashPos + sashThickness;
 
 	if (size > 0) {
 	    if (horizontal) {
@@ -396,7 +401,7 @@ static void PlacePanes(Paned *pw)
 	    Ttk_UnmapContent(pw->paned.mgr, index);
 	}
 
-	pos = pane->sashPos + sashThickness;
+	pos = nextPos;
     }
 }