Attachments for ticket b964b98d
Attachment 57ca8327
b964b98d-9.1.0.patch [download]
Added by anonymous on 2026-10-04 13:25:57.Attachment 7db7f4bc
b964b98d-repro.tcl [download]
Added by anonymous on 2026-10-04 13:25:57.Attachment e6ec51e0
report.tar.gz [download]
# Tk on macOS/Aqua: use-after-free family in the geometry-relayout path during window teardown and `wm manage`/`wm forget` reparent Relates to ticket **`b964b98d1b`** ("ttk manager relayout use-after-free during teardown (Aqua)"). This report supersedes the original with (a) a **deterministic, standalone `wish` reproducer** and a **fix** for the ticket as filed, and (b) **AddressSanitizer free-sites** for a deeper, related family of UAFs that the same workload exposes on Aqua, plus a fix for one more of them. Platform: macOS 26.x / arm64, Aqua backend. Tk 9.0.3 and 9.0.4 (the relevant code is identical between them and on trunk). Not reproducible on X11 — see "Why Aqua only" below. All symbol offsets below are from unstripped `-g` builds; the standalone report has `file:line`. --- ## Part 1 — Ticket `b964b98d1b` as filed: reproducer + fix (actionable) ### Mechanism `Tk_MoveResizeWindow()` / `Tk_MapWindow()` deliver `<Configure>`/`<Map>` **synchronously** on Aqua (`TkDoConfigureNotify` → `Tk_HandleEvent`, and the Aqua stubs pump the run loop). A ttk geometry manager's deferred relayout (`ManagerIdleProc → PlaceContent → Ttk_PlaceContent → Tk_MaintainGeometry → Tk_MoveResizeWindow`) therefore runs a Tcl binding / nested relayout **in the middle of placing a content window**. If that re-entrant work destroys a content window (or forgets a pane), `RemoveContent()`/`Tk_UnmaintainGeometry()` frees the record the outer placement still holds, and the placement then dereferences freed memory. On X11 the `<Configure>` is queued, so the relayout unwinds before anything is freed and the bug never manifests. ### Deterministic standalone reproducer — crashes stock `wish` `repro/repro-b964b98d-standalone-wish.tcl`. It builds a `ttk::panedwindow`, binds `<Configure>` on the pane that is about to be placed so the binding **destroys that pane while it is being placed**, then resizes in a loop. Unpatched `wish` 9.0.4: - **macOS/Aqua: `SIGBUS` every run** (write to `winPtr->flags |= TK_MAPPED` through a freed content window; the `x0` arg to `Tk_MapWindow` is a wild pointer into `__TEXT`). - **Linux/X11: `SIGSEGV` every run** — i.e. this reduced form is **not** actually Aqua-only; the synchronous `<Configure>` from `Tk_MoveResizeWindow → TkDoConfigureNotify` is generic. (The *field* reports were Aqua-only because only Aqua's spontaneous run-loop pumping triggers it without such a binding.) ### AddressSanitizer — free-site `asan/01-original-reentrancy--Tk_MapWindow--stock-wish.txt` (standalone `wish`, `-DPURIFY` so Tcl's allocator hands frees to ASan): ``` heap-use-after-free READ, in Tk_MapWindow tkWindow.c:1782 #1 Tk_MaintainGeometry tkGeometry.c:446 (direct-child fast path) #2 Ttk_PlaceContent ttkManager.c:372 #3 PlacePanes / PanedPlaceContent #4 RecomputeLayout ← ManagerIdleProc (the deferred relayout idle) freed by: #2 Tcl_EventuallyFree tclPreserve.c:294 #3 Tk_DestroyWindow tkWindow.c:1755 #4 Tk_DestroyObjCmd (the "destroy" run by …) #10 Tk_BindEvent tkBind.c:2582 (… a <Configure> binding …) #13 TkDoConfigureNotify tkWindow.c:2365 (… delivered synchronously by …) #14 Tk_MoveResizeWindow tkWindow.c:2069 (… the very placement move) ``` ### Fix — `patches/01-tk-b964b98d-reentrancy-uaf.patch` (3 files, generic + ttk) Applies from the Tk source root (`patch -p1`); against 9.0.3/9.0.4, ports to trunk with trivial fuzz. 1. `generic/ttk/ttkManager.c` — an `epoch` counter on the manager, bumped on every `content[]` mutation (`InsertContent`, `RemoveContent`, `Ttk_ReorderContent`). `Ttk_PlaceContent` / `Ttk_UnmapContent` bounds-check the index, skip content whose window is `TK_ALREADY_DEAD`, and **re-check the epoch after `Tk_MaintainGeometry`** — bailing without touching the (possibly-freed) record if the list changed. Bailing is safe: every mutation path schedules a fresh relayout. The `ManagerEventHandler` Map/Unmap loops and `ManagerIdleProc` get matching guards. 2. `generic/ttk/ttkPanedwindow.c` — `PlacePanes` reads `pane->sashPos` **before** the placement call (the `Pane` can be freed by it). 3. `generic/tkGeometry.c` — `Tk_MaintainGeometry`'s direct-child fast path wraps the `Tk_MoveResizeWindow` + `Tk_MapWindow` pair in `Tcl_Preserve`/`Tcl_Release` and skips the map when either window died in the synchronous `<Configure>`. Validated: the standalone reproducer goes from crash-every-run to 0 crashes on both Aqua and X11; ASan build reports nothing; `tests/ttk` 200/200 before and after. --- ## Part 2 — The deeper family the same workload exposes (ASan free-sites attached) Once Part 1's fix is in, a *heavier* version of the workload — float/dock churn (`wm manage` / `wm forget`) of a **deep** widget subtree, e.g. a scroll viewport + nested panes + a placed overlay — still crashes on Aqua. It is the same shape one layer down: **drawable/record references that outlive a destroy or reparent, dereferenced by a later relayout.** We captured these with an AddressSanitizer build (recipe in `repro/build-asan-player.sh`); each is deterministic under ASan. Note on the reproducer: this heavier variant did **not** reduce to a standalone `wish` script (five attempts in `repro/standalone-attempts/` all survive) — it needs a host that drives float/dock churn from inside the event loop. The attached `repro/churn-heavy.js` reproduces it ~90%/run in our host (embeds Tk via CGo). We recognize that is less convenient than a `wish` script and are happy to help a maintainer reproduce; the ASan stacks below are unambiguous regardless. ### Free-site A — refcounted `toplevel` `MacDrawable` dangles → **fixed**, `patches/02-…` `asan/02-heavy-toplevel-MacDrawable--MoveResizeWindow.txt`: ``` heap-use-after-free READ, in MoveResizeWindow (macosx/tkMacOSXSubwindows.c) ← XMoveResizeWindow ← Tk_MoveResizeWindow ← Tk_MaintainGeometry ← Ttk_PlaceContent ← relayout (reads macWin->toplevel->winPtr via TkMacOSXGetNSWindowForDrawable, on a LIVE window) freed by: XDestroyWindow (referenceCount==0) ← Tk_DestroyWindow ← [destroy] allocated: Tk_MakeWindow ← Tk_MakeWindowExist (a refcounted toplevel MacDrawable) ``` Root cause is in **`RemapWindows()`** (`macosx/tkMacOSXWm.c`, the `wm manage`/`wm forget` subtree reparent walk): it recurses into children passing `winPtr->window` as their `parentWin`, but when an intermediate window is not yet realized (`window == None`) it passes `None`. Each child then does `macWin->toplevel = ((MacDrawable *)None)->toplevel` — reading wild memory and, worse, **decrementing its real toplevel's `referenceCount` while re-pointing elsewhere**, so a live `toplevel` MacDrawable is freed early and every window still referencing it dangles. Deep subtrees (many unrealized frames during churn) hit it. Fix (`patches/02-tk-remapwindows-toplevel-refcount.patch`, one hunk): pass the nearest *realized* ancestor's drawable down instead of `None`. Validated under ASan — this free-site is gone. ### Free-site B — pack `MaintainContent`/`MaintainContainer` re-entrancy `asan/03-heavy-pack-MaintainContent--Tk_MaintainGeometry.txt` (with fix A applied): ``` heap-use-after-free READ, in Tk_MaintainGeometry (generic/tkGeometry.c) ← ArrangePacking ← TclServiceIdle (a pack relayout idle) freed by: Tk_UnmaintainGeometry ← Tk_PackObjCmd ("pack forget" during the churn) allocated: Tk_MaintainGeometry (the MaintainContainer/Content record) ``` A re-entrant `pack forget` frees the `Maintain*` record that an in-flight `Tk_MaintainGeometry` (driven by a pack relayout idle) still holds — the generic-code analogue of Part 1, in `tkGeometry.c`'s slow-path maintain machinery. Not yet fixed; a `Tcl_Preserve`/re-validate guard around the pump-capable calls is the likely shape, mirroring Part 1's fast-path fix. ### It is a family, not a single bug Each targeted fix shifts the crash to the next freed structure: ttk `Ttk_Content` (Part 1) → toplevel `MacDrawable` (A) → pack `MaintainContent` (B) → a *relative* `MacDrawable` read by the move stubs → … We also tried two *general* fixes and report them as dead ends so they are not re-attempted: - **Suppress idle servicing during teardown/reparent** (the approach suggested on the original ticket). Does not work: there are many idle-drain sites on Aqua (`MapFrame`, `TkInstallFrameMenu`, `setState`, `updateLayer`, the CFRunLoop notifier), and the dangling *use* happens in a **later** event after the teardown returns, so a teardown-scoped guard cannot cover it. - **A `winTable`-validity guard on the Aqua move stubs** (`XMoveResizeWindow`/`XMoveWindow`/ `XResizeWindow`, using `Tk_IdToWindow()` — a non-dereferencing hash check). Does not fully work: the freed object is often a *relative* (`macWin->toplevel`, the parent's `privatePtr`) while the moved window itself is still registered; and on Aqua `Tk_DestroyWindow` **frees the `MacDrawable` (`XDestroyWindow`, ~`tkWindow.c:1578`) before removing it from `winTable` (~`:1592`)**, so a freed-but-still-registered drawable is admitted. (That ordering — delete-from-winTable before free — looks worth fixing on its own.) The underlying condition is that Aqua's synchronous run-loop pumping lets geometry relayouts run against drawables/records that are freed but still referenced from several places, with no single choke point. A complete fix is a systematic pass over the `MacDrawable`/`toplevel`-refcount lifecycle and the move/relayout stubs (plus the winTable delete-before-free reorder), gated by the full Tk regression suite. We're deferring that behind an app-side workaround and raising it here with the evidence so the lifecycle work can start from ASan stacks rather than guesswork. --- ## Why Aqua only The generic re-entrancy in Part 1 also bites X11 with the explicit binding, but not spontaneously, because `Tk_MoveResizeWindow`/`Tk_MapWindow` do not pump the event loop on X11. Part 2's family is Aqua-specific: the platform move/map stubs (`macosx/tkMacOSXSubwindows.c`) deliver `<Configure>` synchronously and pump the Cocoa run loop, so relayouts run mid-teardown/mid-reparent where X11 would have deferred them. ## Contents of the attached archive — `report.tar.gz` Extracts to `report/`: ``` report.md This report. patches/ 01-tk-b964b98d-reentrancy-uaf.patch Part 1 fix (the ticket as filed). 3 files: generic/ttk/ttkManager.c, generic/ttk/ttkPanedwindow.c, generic/tkGeometry.c. `patch -p1` from the Tk source root; against 9.0.3/9.0.4, ports to trunk with trivial fuzz. 02-tk-remapwindows-toplevel-refcount.patch Part 2 free-site A fix. 1 hunk in macosx/tkMacOSXWm.c. `patch -p1` from the source root. asan/ AddressSanitizer reports (heap-use-after-free; full alloc + free + use stacks). 01-original-reentrancy--Tk_MapWindow--stock-wish.txt Part 1, standalone wish, file:line symbols. 02-heavy-toplevel-MacDrawable--MoveResizeWindow.txt Part 2 free-site A (before patch 02). 03-heavy-pack-MaintainContent--Tk_MaintainGeometry.txt Part 2 free-site B (after patch 02). repro/ repro-b964b98d-standalone-wish.tcl Part 1 reproducer. `wish repro-b964b98d-standalone-wish.tcl` — crashes stock/unpatched Tk 9.0.4 every run (SIGBUS on Aqua, SIGSEGV on X11), 0 with patch 01. churn-heavy.js Part 2 reproducer (host-driven float/dock churn of a deep subtree; needs a Tk-embedding host). ~90%/run on Aqua. build-asan-player.sh ASan build recipe (-fsanitize=address -g -DPURIFY on Tcl+Tk; -DPURIFY essential — without it Tcl's ckalloc recycler hides frees from ASan). Adapt the final link step to your host/wish. standalone-attempts/ Five wish scripts that try and fail to reduce Part 2 to a standalone script (they survive on a patched build) — kept so the reduction isn't re-attempted blindly. background/ TK_TTK_MANAGER_MACOS_UAF.md The original upstream write-up for b964b98d1b. TK_MACOS_HEAVY_SUBTREE_RACE_FINDINGS.md Full multi-layer findings: every free-site, the three fix approaches tried (one landed, two dead ends and why), and the "no bounded fix / lifecycle pass needed" conclusion. ``` Full disclosure: The above report and the attached artifacts were both produced by an AI after multiple debugging sessions.
Added by anonymous on 2026-07-06 11:36:53.Attachment 79654cae
tk-macos-ttk-uaf-9.0.3.patch [download]
Fix attached: the ttk manager relayout UAF is re-entrancy during content placement. Patch against Tk 9.0.3, patch -p1 from the source root; 3 files. Root cause (refined): Ttk_PlaceContent() -> Tk_MaintainGeometry() -> Tk_MoveResizeWindow() delivers <Configure> synchronously; on Aqua, window ops also pump the Cocoa run loop. A binding or a content window <Destroy> serviced there runs RemoveContent() mid-placement, freeing the Ttk_Content the manager still holds - the next Tk_MapWindow(content->window) on freed memory is the EXC_BAD_ACCESS. (Hits X11 too via a synchronous <Configure> binding; just pumped less reliably there.) Fix: (1) ttkManager.c - an epoch counter bumped on every content[] mutation; placement/map/idle routines bail if it changed after a call into Tk (+ TK_ALREADY_DEAD / bounds / no-re-arm guards). (2) tkGeometry.c - Tcl_Preserve both windows across Tk_MoveResizeWindow and a TK_ALREADY_DEAD guard before the direct-child map (the crash site). (3) ttkPanedwindow.c - hoist the pane->sashPos read before placement can free pane. Validation: deterministic repro 100%->0 on Aqua and X11; app churn stress 0 crashes on macOS/arm64 (was ~42% / 100%); tests/ttk 200/200 before and after. No build flag; size_t epoch; gcc + clang clean. ---- Full disclosure: The patch and the description above were both AI generated after a debug session.
Added by anonymous on 2026-07-05 15:45:26.
