Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 8fd47123d554c1e3f3000f88f08ca1c194767af4dcc883bcd973dfeb71601a82
Ticket: e713c36677dda525f53876e152024daac73fae24
Stack and heap buffer overflow in `clock format %z`
User & Date: bef on 2026-11-05 21:24:13
Changes

  1. icomment:
    A second overflow in the same result buffer, with the same reach (safe child through the `clock` alias, or a trusted `clock format` with a time zone from outside data; the default format contains `%Z`, so no `-format` is needed). Trunk, 9.1.0 and 9.0.4 are affected; 8.6 is not.
    
    `FrmResultAllocate(DateFormat *, int len)` (`tclClockFmt.c:2714`) is called with `Tcl_Size` lengths, and computes `needed` and `newsize` as `int`. For a `%Z` zone name or a literal format word of 2 GiB or more, `needed` comes out negative, nothing is reserved, and the following `memcpy` (`:2958` for `%Z`, `:3517` for a word) copies the full length into the 80-byte `resMem` on the stack. Above 4 GiB the length wraps to a small positive value and the copy overruns a small heap block instead. The fixed-offset pattern in `clock.tcl` is not anchored at the end, so `+00` followed by anything is accepted, and the whole string becomes the zone name.
    
        interp eval [interp create -safe] {
            clock format 0 -format %Z -timezone [string repeat +00 715827916]
        }
    
        WRITE of size 2147483749 ...
        #1 ClockFmtToken_TimeZone_Proc tclClockFmt.c:2958
        SUMMARY: AddressSanitizer: stack-buffer-overflow tclClockFmt.c:2958 in ClockFmtToken_TimeZone_Proc
    
    `clock format 0 -gmt 1 -format [string repeat x 2147483748]` gives the same at `:3517`. Without sanitisers the process dies with SIGSEGV or SIGBUS. The `%Z` case needs about 10 GiB of memory, because of the UCS-4 copy of the zone made by the regexp in `SetupTimeZone`; the word case needs about 4 GiB.
    
    Suggested fix, on top of the patch attached to this ticket (it touches different lines):
    
    ```diff
     static inline int
     FrmResultAllocate(
         DateFormat *dateFmt,
    -    int len)
    +    Tcl_Size len)
     {
    -    int needed = dateFmt->output + len - dateFmt->resEnd;
    +    Tcl_Size needed = dateFmt->output + len - dateFmt->resEnd;
         if (needed >= 0) {		/* >= 0 - regards NTS zero */
    -	int newsize = dateFmt->resEnd - dateFmt->resMem
    +	Tcl_Size newsize = dateFmt->resEnd - dateFmt->resMem
     		+ needed + MIN_FMT_RESULT_BLOCK_ALLOC * 2;
    ```
    
    When the allocation fails, the function returns `TCL_ERROR` without setting a message, so `clock format` fails with an empty error. Separately, anchoring the fixed-offset pattern in `clock.tcl` with `$` would stop arbitrary text from becoming a zone name. That changes accepted input, though, and 8.6 shares it.
    
  2. login: "bef"
  3. mimetype: "text/x-markdown"