Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 611d675c4adbed093de3d79ea7ed8ab9d6c55606a30f80a2ebb52f85c7505b54
Ticket: bdd1b346f89df079ce13dfb3be5c80a6608cb282
Heap buffer overflow in `clock format` from a data race on the shared `fmtMinAlloc`
User & Date: bef on 2026-11-06 16:00:06
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-markdown"
  4. comment changed to:
    Security issue: a heap buffer overflow that outside data (a client-supplied time zone) can trigger in threaded applications.
    
    The parsed format storage (`ClockFmtScnStorage`) is shared by all threads that use the same format string, but `TclClockFormat` reads and updates its size hint `fmtMinAlloc` without a lock. The hint is read once for the allocation and again for the buffer end (`tclClockFmt.c:3418-3423`):
    
    ```c
        if (fss->fmtMinAlloc > sizeof(resMem)) {
    	dateFmt->resMem = (char *)Tcl_AttemptAlloc(fss->fmtMinAlloc);
    	...
    	dateFmt->resEnd = dateFmt->resMem + fss->fmtMinAlloc;
    ```
    
    If another thread raises the hint (`:3561`) in between, `resEnd` lies past the end of the allocated block. Later output is written up to `resEnd` and overflows the heap block by the difference. The compiler does reload the field after the allocator call at `-O1` and `-O2`.
    
    This can happen in any threaded application that formats dates with the same format string in several threads. If output lengths vary and the attacker controls them, for example with a user-supplied `-timezone` and `%Z`, the overflow can be triggered repeatedly. A single safe interpreter cannot reach it, since it cannot create threads; a host that runs untrusted scripts in several threads can.
    
    PoC, run with `tcltest` (for `testthread`):
    
    ```tcl
    set worker {
        for {set j 0} {$j < 100} {incr j} {
            lappend tzs "[string repeat Z [expr {100 + 8*$j + IDX}]]-1"
        }
        foreach tz $tzs { clock format 0 -timezone $tz -format warm }
        for {set r 0} {$r < 5000} {incr r} {
            foreach tz $tzs { clock format 0 -timezone $tz -format "%Z $r" }
        }
    }
    for {set i 0} {$i < 8} {incr i} {
        lappend ids [testthread create -joinable [string map [list IDX $i] $worker]]
    }
    foreach id $ids { testthread join $id }
    ```
    
    With ASan this fails within a second, at `:2962` (`%Z` copy), `:3521` (literal word) or `:3563` (final NUL):
    
    ```
    WRITE of size 455 at 0x615000120445 thread T3
        #1 ClockFmtToken_TimeZone_Proc tclClockFmt.c:2962
    SUMMARY: AddressSanitizer: heap-buffer-overflow tclClockFmt.c:2962 in ClockFmtToken_TimeZone_Proc
    ```
    
    With one thread, or with a separate format string per thread, it runs clean. ThreadSanitizer reports the race as a read at `:3418` against a write at `:3561`. Trunk, 9.1.0 and 9.0.4 are affected; 8.6 is not.
    
    Suggested fix: read `fss->fmtMinAlloc` once into a local and use that for both the allocation and `resEnd`. The remaining unsynchronised access to the hint is still a data race in C terms, so ideally the hint is read and updated under `ClockFmtMutex`.
    
  5. foundin changed to: "9.0.4, 9.1.0, 9.1.1 trunk"
  6. is_private changed to: "0"
  7. login: "bef"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "37918056d323dc69a2f76cecc85476189094f3b8"
  10. resolution changed to: "None"
  11. severity changed to: "Severe"
  12. status changed to: "Open"
  13. submitter changed to: "bef"
  14. subsystem changed to: "16. Commands A-H"
  15. title changed to:
    Heap buffer overflow in `clock format` from a data race on the shared `fmtMinAlloc`
    
  16. type changed to: "Bug"