Tcl Source Code

View Ticket
Login
2026-10-23
07:07 • New ticket [72d1c8c271] Windows 8.6: under ephemeral port exhaustion, sockets ge... artifact: c1d7527219 user: serhiy.storchaka
2025-06-25
16:41
Merge 9.0 - Fix [40b1814b93] and [7c2716733a] Window handle use-after-frees. check-in: 54eb90319d user: apnadkarni tags: trunk, main
15:49 • Closed ticket [40b1814b93]: App verifier shows user-after-free on ThreadSpecific... artifact: 83459a1b23 user: apnadkarni
15:48
Fix [40b1814b93] - use after free on TSD in Winsock check-in: a36f76eb6b user: apnadkarni tags: core-9-0-branch
2025-06-14
05:37 • Ticket [40b1814b93] App verifier shows user-after-free on ThreadSpecificData for... artifact: 867ce3ac5d user: apnadkarni
05:31
Bug [40b1814b93] - Proposed fix for Winsock use-after-frees check-in: bbb565eca7 user: apnadkarni tags: bug-40b1814b93
03:32 • Ticket [40b1814b93] App verifier shows user-after-free on ThreadSpecificData for... artifact: b4f9ae787c user: apnadkarni
2025-06-13
17:43 • New ticket [40b1814b93]. artifact: 354da95bc2 user: apnadkarni

Ticket UUID: 40b1814b93e50b4a9627e6651184880233d63616
Title: App verifier shows user-after-free on ThreadSpecificData for Windows sockets
Type: Bug Created on: 2025-06-13 17:43:50
Submitter: apnadkarni Assigned to: nobody
Subsystem: - New Builtin Commands Severity: Important
Priority: 5 Medium Last modified: 2025-06-25 15:49:01
Status: Closed Closed by: apnadkarni
Resolution: Fixed Closed on: 2025-06-25 15:49:01
Version: trunk
Description:

The offending tsdPtr access is here and happens on exit.

Preliminary thoughts:

Why does this show up only under AppVerifier? Because it scrubs memory and protects freed blocks (above requires compile with -DPURIFY so Tcl_Alloc translates directly to malloc). Under normal operation, the freed tsdPtr page remains accessible though the memory block has been freed. The tsdPtr->readyEvent actually is garbage data and so the SetEvent call which checks for valid handles returns an error which Tcl ignores.

Note the tsdPtr referenced above is thread specific data in a different thread which is really asking for trouble but it appears to have been that way forever, likely even before 8.6.

User Comments:
apnadkarni added on 2025-06-14 03:32:00:

Notes for reproduction:

Build as

nmake /f makefile.vc /nologo INSTALLDIR=C:\Tcl\wip\%VSCMD_ARG_TGT_ARCH%-debug OPTS=pdbs cdebug="-Zi -Od -DPURIFY" tcltest

Add tcltest90.exe to AppVerifier. Run tcltest90 within a VS debugger (that's where AppVerifier messages will show up).

close [socket www.example.com 443]
exit <- where the use after free is triggered

apnadkarni added on 2025-06-14 05:37:49:

Also, use-after-free of socket handle here on socket_inet-2.9.

The tsdPtr bug appears to be caused by use of the same event handle to communicate socket notifications and worker thread completion to the main thread. The main thread waits for completion of worker to ensure its TSD is still available but mistakenly confuses a pending socket notification for thread completion as they use the same event and frees the TSD immediately while the worker thread is still active.

Proposed fix for both is in commit bug-40b1814b93. The tsdPtr bug is fixed by waiting on the worker thread handle rather than the notification event handle. The socket handle bug is a simple failure to mark socket closed in a loop error and then attempting to close it again.


apnadkarni added on 2025-06-25 15:49:01:
Fixed in [a36f76eb6b].