Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 15e9fe54dd171be041a2d1c1560fd6a6ad892bae40cf442d81028325c1bdbfae
Ticket: d6c1d2f78ef003dcb8d11a08ab83b51b576a6427
Socket channel names contain a heap address (visible to safe interps)
User & Date: serhiy.storchaka on 2026-10-17 10:51:11
Changes

  1. cmimetype changed to: "text/x-fossil-wiki"
  2. comment changed to:
    Socket channel names are <tt>sock</tt> followed by the address of the socket's
    <tt>TcpState</tt> structure in hex, so every socket handle reveals a heap address. A safe
    interpreter that is given a socket (<nowiki>[interp transfer]</nowiki>,
    <nowiki>[interp share]</nowiki>, 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 (<nowiki>[tcl::unsupported::representation]</nowiki>
    is hidden there).
    
    <verbatim>
    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}]
    </verbatim>
    
    Actual (9.1, 9.0, 8.6.18 on Linux): <tt>sock5644b332a410</tt>. Expected: a name that does
    not depend on memory layout, e.g. <tt>sock</tt> plus a counter, as reflected channels already
    do (<tt>rc%lu</tt> in generic/tclIORChan.c).
    
    Where the names are formed:
      *  Unix: <tt>SOCK_TEMPLATE</tt> in unix/tclUnixSock.c, used with <tt>PTR2INT(statePtr)</tt>
         at all four <tt>Tcl_CreateChannel</tt> calls. Since [8eefe5a06f] (2011, the IPv6 socket
         merge, first in 8.6.0); up to 8.5 the name was <tt>sock</tt> + fd. The fd could not be kept
         because a listening socket may have several fds.
      *  Windows: <tt>SOCK_TEMPLATE "sock%p"</tt> in win/tclWinSock.c since [fdd8917252] (2014,
         first in 8.6.2). In addition <tt>TclWinGenerateChannelName</tt> (win/tclWinChan.c) names
         file, pipe, serial and console channels <tt>file</tt> + pointer to their info structure;
         that has been so at least since 8.4 (<tt>"file%lx"</tt>). Not tested on Windows, read from
         the sources.
      *  Unix file, pipe and serial channels use the fd (<tt>file%d</tt>, <tt>serial%d</tt>), 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
    <tt>sock...a490 sock...a510 sock...a490</tt>), 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.
    
  3. foundin changed to: "8.6.18, 9.0.5, 9.1.0"
  4. login: "serhiy.storchaka"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. severity changed to: "Minor"
  8. status changed to: "Open"
  9. subsystem changed to: "27. Channel Types"
  10. title changed to:
    Socket channel names contain a heap address (visible to safe interps)
    
  11. type changed to: "Bug"