Tcl Source Code

View Ticket
Login
2026-10-20
18:11 • Ticket [d6c1d2f78e] Socket channel names contain a heap address (visible to safe... artifact: ebad0e5da1 user: serhiy.storchaka
2026-10-17
12:39 • Ticket [d6c1d2f78e]: 3 changes artifact: f712c13fa9 user: serhiy.storchaka
12:25
Fix [d6c1d2f78e]: socket channel names (and on Windows all channel names) contained a heap address, ... check-in: cbb6334c93 user: serhiy.storchaka tags: channel-names-no-address
10:53 • Ticket [1577730fff] interp transfer leaks information when transfering channels<... artifact: 2f842c82b7 user: serhiy.storchaka
10:51 • New ticket [d6c1d2f78e] Socket channel names contain a heap address (visible to ... artifact: 15e9fe54dd user: serhiy.storchaka

Ticket UUID: d6c1d2f78ef003dcb8d11a08ab83b51b576a6427
Title: Socket channel names contain a heap address (visible to safe interps)
Type: Bug Created on: 2026-10-17 10:51:11
Submitter: serhiy.storchaka Assigned to:
Subsystem: 27. Channel Types Severity: Minor
Priority: 5 Medium Last modified: 2026-10-20 18:11:29
Status: Open Closed by:
Resolution: None Closed on:
Version: 8.6.18, 9.0.5, 9.1.0
Description:

Socket channel names are sock followed by the address of the socket's TcpState structure in hex, so every socket handle reveals a heap address. A safe interpreter that is given a socket ([interp transfer], [interp share], or an alias returning the name) learns it from the handle alone, which helps defeat ASLR if some other memory bug is reachable from scripts. Safe interpreters otherwise have no way to see addresses ([tcl::unsupported::representation] is hidden there).

set srv [socket -server {apply {{c a p} {close $c}}} -myaddr 127.0.0.1 0]
set c [socket 127.0.0.1 [lindex [chan configure $srv -sockname] 2]]
set s [interp create -safe]
interp transfer {} $c $s
puts [interp eval $s {chan names}]

Actual (9.1, 9.0, 8.6.18 on Linux): sock5644b332a410. Expected: a name that does not depend on memory layout, e.g. sock plus a counter, as reflected channels already do (rc%lu in generic/tclIORChan.c).

Where the names are formed:

  • Unix: SOCK_TEMPLATE in unix/tclUnixSock.c, used with PTR2INT(statePtr) at all four Tcl_CreateChannel calls. Since [8eefe5a06f] (2011, the IPv6 socket merge, first in 8.6.0); up to 8.5 the name was sock + fd. The fd could not be kept because a listening socket may have several fds.
  • Windows: SOCK_TEMPLATE "sock%p" in win/tclWinSock.c since [fdd8917252] (2014, first in 8.6.2). In addition TclWinGenerateChannelName (win/tclWinChan.c) names file, pipe, serial and console channels file + pointer to their info structure; that has been so at least since 8.4 ("file%lx"). Not tested on Windows, read from the sources.
  • Unix file, pipe and serial channels use the fd (file%d, serial%d), zipfs channels an archive offset and a counter, reflected channels a counter; stacked channels (zlib, transforms) keep the name of the base channel. Those do not leak addresses.

Because the address is reused once the structure is freed, the same name also comes back quickly after a close (three connect/close cycles above gave sock...a490 sock...a510 sock...a490), the same reuse problem as with fd-based names ([2826430fff]).

A per-process counter, as for reflected channels, would fix both. This is a narrower part of [1577730fff] (channel names given to a safe interpreter show the channel type); unlike that ticket it needs no new API.

User Comments:
serhiy.storchaka added on 2026-10-17 12:39:35:

Proposed fix in branch channel-names-no-address: the number in socket names on Unix, and in all channel names on Windows (TclWinGenerateChannelName()), is now a per-process counter instead of an address. Names are no longer reused after a close either. Test socket-bug-d6c1d2f78e, passes on Linux and Windows.