Tk Source Code

View Ticket
Login
Ticket UUID: ab95811e074e9507a8e97480ca3774d94af0876e
Title: Aqua: prevent use-after-free crashes
Type: Patch Version: core-8-6-branch
Submitter: chrstphrchvz Created on: 2022-08-30 18:59:03
Subsystem: 69. Events Assigned To: marc_culler
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2023-05-23 17:26:17
Resolution: Accepted Closed By: fvogel
    Closed on: 2023-05-23 17:26:17
Description:

The approach I proposed in [6da885404a] gives me an idea of how to avoid crashes due to issues like [5692042764] and [6d5354bb36]: use Tcl_Preserve(), and then eventually Tcl_Release(), for any TkWindow * used to set TKApplication properties tkPointerWindow, tkEventTarget, and tkDragTarget; see attached patch.

I do not intend for this to be a substitute for properly cleaning up stale references to windows; I suggest it mainly because crashes often seem difficult to reproduce and debug. It could be that avoiding these use-after-free crashes instead reveals other issues. However, because Tk uses Tcl_EventuallyFree() when it is done with a TkWindow, my impression is that this means it is okay to access its contents after Tcl_EventuallyFree() but before it is actually freed, even though probably nothing useful can be done with them.

User Comments: fvogel added on 2023-05-23 17:26:17:
Merged in core-8-6-branch and trunk.

fvogel added on 2023-05-21 17:54:33:

Committed in [f5fb3497]. Thanks!


chrstphrchvz added on 2023-05-19 00:37:35:

Please consider the attached patch ab95811e07-pbr.diff. To my knowledge, the relevant Tcl_Release() calls are for a data structure which was called with Tcl_EventuallyFree(…, TCL_DYNAMIC), and so should not have side effects that cause winPtr to become stale before Tcl_Preserve() is called. But it would be better to call Tcl_Preserve() first and eliminate that possibility.


fvogel added on 2022-09-20 17:19:50:

This was merged as [ecca072b].


fvogel added on 2022-09-18 18:36:07:
The CI runned is happy with the patch. I guess one can merge this?

nab added on 2022-09-14 16:33:31:
many thanks

++

marc_culler (claiming to be Marc Culler) added on 2022-09-14 16:30:54:
I don't see how this could hurt anything and we already have evidence that
it helps prevent crashes.  So I applied the patch in the patch-ab95811e07
branch.  I will merge in a few days if no problems are reported.

nab added on 2022-09-01 09:46:29:
Hi Christopher,
I've applied your patch on top of mac_styles_87 branch (the branch I use) and send myApp to the guy where it was initially crashing and he told me that he couldn't make it crash :)

thank you for investigating on this,
++

Attachments: