Tk Source Code

View Ticket
Login
Ticket UUID: 88cfdcb3046cd26c111389c1db76dfcce1de3301
Title: Aqua: use-after-free when file selection dialog dismissed
Type: Patch Version: 8.6.12
Submitter: chrstphrchvz Created on: 2022-02-09 00:25:24
Subsystem: 37. [tk_get*File] Assigned To: kevin_walzer
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2022-05-11 06:45:58
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2022-05-11 06:45:58
Description:

As mentioned in [fc5073a451], AddressSanitizer (compile Tcl and Tk with -fsanitize=address -DPURIFY) reports a use-after-free when I dismiss a file selection dialog (I have yet to find an instance where it doesn’t; “File” > “Source…” is enough to trigger it):

=================================================================
==7704==ERROR: AddressSanitizer: heap-use-after-free on address 0x60300037d3e8 at pc 0x0001020edb72 bp 0x7ff7bfefa690 sp 0x7ff7bfefa688
READ of size 8 at 0x60300037d3e8 thread T0
    #0 0x1020edb71 in showOpenSavePanel tkMacOSXDialog.c:390
    #1 0x1020ea774 in Tk_GetOpenFileObjCmd tkMacOSXDialog.c:852
    #2 0x1043aed21 in Dispatch tclBasic.c:4468
    #3 0x10438f85e in TclNRRunCallbacks tclBasic.c:4504
    #4 0x10438dbdb in Tcl_EvalObjv tclBasic.c:4226
    #5 0x10439b1d3 in TclEvalEx tclBasic.c:5373
    #6 0x104394699 in Tcl_EvalEx tclBasic.c:5038
    #7 0x10162fdc3 in Tk_BindEvent tkBind.c:2602
    #8 0x10169066b in TkBindEventProc tkCmds.c:319
    #9 0x101700334 in Tk_HandleEvent tkEvent.c:1292
    #10 0x101707cc0 in WindowEventProc tkEvent.c:1723
    #11 0x104ff8fe8 in Tcl_ServiceEvent tclNotify.c:670
    #12 0x104ffbb21 in Tcl_DoOneEvent tclNotify.c:967
    #13 0x10170b8af in Tk_MainLoop tkEvent.c:2109
    #14 0x1017da5e4 in Tk_MainEx tkMain.c:377
    #15 0x100006c5e in main tkAppInit.c:93
    #16 0x1000194fd in start+0x1cd (dyld:x86_64+0x54fd)

0x60300037d3e8 is located 8 bytes inside of 24-byte region [0x60300037d3e0,0x60300037d3f8)
freed by thread T0 here:
    #0 0x10051f829 in wrap_free+0xa9 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x47829)
    #1 0x104325cc4 in TclpFree tclAlloc.c:722
    #2 0x104405324 in Tcl_Free tclCkalloc.c:1226
    #3 0x1020e297e in -[TKApplication(TKDialog) tkFilePanelDidEnd:returnCode:contextInfo:] tkMacOSXDialog.c:261
    #4 0x1020ed97b in showOpenSavePanel tkMacOSXDialog.c:379
    #5 0x1020ea774 in Tk_GetOpenFileObjCmd tkMacOSXDialog.c:852
    #6 0x1043aed21 in Dispatch tclBasic.c:4468
    #7 0x10438f85e in TclNRRunCallbacks tclBasic.c:4504
    #8 0x10438dbdb in Tcl_EvalObjv tclBasic.c:4226
    #9 0x10439b1d3 in TclEvalEx tclBasic.c:5373
    #10 0x104394699 in Tcl_EvalEx tclBasic.c:5038
    #11 0x10162fdc3 in Tk_BindEvent tkBind.c:2602
    #12 0x10169066b in TkBindEventProc tkCmds.c:319
    #13 0x101700334 in Tk_HandleEvent tkEvent.c:1292
    #14 0x101707cc0 in WindowEventProc tkEvent.c:1723
    #15 0x104ff8fe8 in Tcl_ServiceEvent tclNotify.c:670
    #16 0x104ffbb21 in Tcl_DoOneEvent tclNotify.c:967
    #17 0x10170b8af in Tk_MainLoop tkEvent.c:2109
    #18 0x1017da5e4 in Tk_MainEx tkMain.c:377
    #19 0x100006c5e in main tkAppInit.c:93
    #20 0x1000194fd in start+0x1cd (dyld:x86_64+0x54fd)

previously allocated by thread T0 here:
    #0 0x10051f6e0 in wrap_malloc+0xa0 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x476e0)
    #1 0x104325ca4 in TclpAlloc tclAlloc.c:699
    #2 0x104404fd2 in Tcl_Alloc tclCkalloc.c:1059
    #3 0x1020ea27a in Tk_GetOpenFileObjCmd tkMacOSXDialog.c:836
    #4 0x1043aed21 in Dispatch tclBasic.c:4468
    #5 0x10438f85e in TclNRRunCallbacks tclBasic.c:4504
    #6 0x10438dbdb in Tcl_EvalObjv tclBasic.c:4226
    #7 0x10439b1d3 in TclEvalEx tclBasic.c:5373
    #8 0x104394699 in Tcl_EvalEx tclBasic.c:5038
    #9 0x10162fdc3 in Tk_BindEvent tkBind.c:2602
    #10 0x10169066b in TkBindEventProc tkCmds.c:319
    #11 0x101700334 in Tk_HandleEvent tkEvent.c:1292
    #12 0x101707cc0 in WindowEventProc tkEvent.c:1723
    #13 0x104ff8fe8 in Tcl_ServiceEvent tclNotify.c:670
    #14 0x104ffbb21 in Tcl_DoOneEvent tclNotify.c:967
    #15 0x10170b8af in Tk_MainLoop tkEvent.c:2109
    #16 0x1017da5e4 in Tk_MainEx tkMain.c:377
    #17 0x100006c5e in main tkAppInit.c:93
    #18 0x1000194fd in start+0x1cd (dyld:x86_64+0x54fd)

SUMMARY: AddressSanitizer: heap-use-after-free tkMacOSXDialog.c:390 in showOpenSavePanel Shadow bytes around the buggy address: 0x1c060006fa20: fa fa fd fd fd fa fa fa fd fd fd fa fa fa fd fd 0x1c060006fa30: fd fa fa fa fd fd fd fa fa fa fd fd fd fa fa fa 0x1c060006fa40: fd fd fd fa fa fa fd fd fd fa fa fa fd fd fd fa 0x1c060006fa50: fa fa fd fd fd fa fa fa fd fd fd fd fa fa fd fd 0x1c060006fa60: fd fa fa fa fd fd fd fa fa fa fd fd fd fd fa fa =>0x1c060006fa70: 00 00 00 00 fa fa 00 00 00 00 fa fa fd[fd]fd fa 0x1c060006fa80: fa fa fd fd fd fd fa fa 00 00 00 00 fa fa 00 00 0x1c060006fa90: 00 00 fa fa fd fd fd fd fa fa 00 00 00 00 fa fa 0x1c060006faa0: 00 00 00 00 fa fa 00 00 00 00 fa fa 00 00 00 00 0x1c060006fab0: fa fa fd fd fd fd fa fa fd fd fd fd fa fa fd fd 0x1c060006fac0: fd fd fa fa fd fd fd fa fa fa fd fd fd fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 … Heap left redzone: fa Freed heap region: fd … ==7704==ABORTING

User Comments: jan.nijtmans added on 2022-05-11 06:45:58:

Merged to 8.6 and up.

Closing. Thanks!


kevin_walzer added on 2022-05-11 01:37:37:
The tests suggested by Christopher work as advertised and the test suite runs without issue. Jan, can you merge to core-8-6 branch and main? Thank you!

kevin_walzer added on 2022-05-10 19:05:03:
Ah, I see Jan has created a branch with the patch applied. Thanks, Jan. I will review and provide feedback.

kevin_walzer added on 2022-05-10 18:17:50:
So I just need to review this patch, and merge? Must have neglected to come back to this.

jan.nijtmans added on 2022-05-10 16:48:35:

@kevin_walzer, would you be able to review this?


jan.nijtmans added on 2022-05-10 16:47:29:

> Can someone please check again? I believe this patch still has yet to be applied.

Indeed, there were multiple "chaves_patches", branch-naming could have been more helpful, known which ones were handled .....

Created branch "bug-88cfdcb304" now.


chrstphrchvz added on 2022-05-10 15:54:55:

Can someone please check again? I believe this patch still has yet to be applied.


jan.nijtmans added on 2022-05-10 06:35:59:

Merged to 8.6 [390768179e915f97|here]


kevin_walzer added on 2022-02-12 16:52:27:
I will have to review this patch next week. Thank you for clarifying.

chrstphrchvz added on 2022-02-12 16:38:00:

Sorry for the confusion, but this patch has yet to be merged into core-8-6-branch. It was submitted after Kevin’s check-in [5a6bb861ffd], and Jan’s rebasing in [b0684b5ecc] did not handle any additional patches.


kevin_walzer added on 2022-02-12 16:19:16:
Patch accepted and committed to core-8-6-branch and trunk. Thank you.

chrstphrchvz added on 2022-02-10 02:55:07:

I am attaching a proposed fix based on core-8-6-branch. In Tk_GetOpenFileObjCmd()/Tk_GetSaveFileObjCmd()/Tk_ChooseDirectoryObjCmd(), FilePanelCallbackInfo *callbackInfo will point to the existing automatic variable callbackInfoStruct so that ownership is not given to showOpenSavePanel() or tkFilePanelDidEnd:returnCode:contextInfo:. For consistency, in Tk_MessageBoxObjCmd(), AlertCallbackInfo *callbackInfo will likewise point to the existing automatic variable callbackInfoStruct and ownership will not be given to tkAlertDidEnd:returnCode:contextInfo:; and functions which perform Tcl_IncrRefCount(cmdObj) will also be responsible for doing Tcl_DecrRefCount(cmdObj).

Tested using these commands:

% proc myputs {s} {puts "<$s>"}
% tk_messageBox -command myputs
<ok>
% tk_chooseDirectory -command myputs
</Users/user>
% tk_getOpenFile -command myputs
</Users/user/file.txt>
% tk_getSaveFile -command myputs
</Users/user/file.txt>


Attachments: