Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 9c97fffe7f3a2f9eb51036666e2e8744fedb19743cde97133ebb8dd79831e3c1
Ticket: 8911644dc28094b08b47b20df766aeafc64ace7a
clock: use-after-free when a callback clears the clock caches, and memory errors on malformed locale data
User & Date: bef on 2026-11-13 20:05:37
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-markdown"
  4. comment changed to:
    Memory errors in the C clock engine: a use-after-free when a callback clears the clock caches, an out-of-bounds read and a double free in `TclStrIdxTree`, and a NULL dereference on malformed locale data. They need a script that installs its own callbacks or locale data; outside data does not reach them.
    
    Affected: 9.0.4, 9.1.0 and trunk (as of 2026-11-13, [12c60e9c42](https://core.tcl-lang.org/tcl/info/12c60e9c42)). 8.6 is not affected, because its clock formatting and scanning are Tcl code. All PoCs below are for trunk. Fixes follow in a comment.
    
    **1. Use-after-free when a callback clears the clock caches during `clock format` or `clock scan`**
    
    `ClockLocalizeFormat`, `TclClockMCDict` and the scan and format token loops hold the locale catalogue (`opts->mcDictObj`), the normalised locale, the normalised time zone (`opts->timezoneObj`), the format storage and the client data by plain pointer while they run Tcl callbacks (`LocalizeFormat`, `mcget`, `SetupTimeZone`, `TZData` reads). A callback that runs `::tcl::unsupported::clock::configure -clear` or `::tcl::clock::ClearCaches`, or that deletes the clock commands, frees them:
    
        clock format 0 -gmt 1
        trace add variable ::tcl::clock::LocFmtMap read {apply {args {
            ::tcl::unsupported::clock::configure -clear
        }}}
        clock format 0 -format %x -locale fr -gmt 1
    
        SUMMARY: AddressSanitizer: heap-use-after-free tclClockFmt.c:951 in ClockLocalizeFormat
    
    The same pattern gives a NULL dereference in `Tcl_DictObjPut` from `TclClockMCDict` (`tclClock.c:779`) with a msgcat `loadcmd` for `::tcl::clock`. With a `TZData` read trace during `clock scan ... %Z`, it gives a use-after-free of the token array (`tclClockFmt.c:2421`) or of the client data (`tclClock.c:1324`). The time zone object is freed the same way when only the caches own it, which is the case once an equal zone name has been used before; `%Z` in `clock format` and the conversion at the end of `clock scan` then read it in `ClockGetTZData`. Two smaller problems are close by: `TclClockMCDict` uses the `mcget` result after `Tcl_ResetResult`, and `ClockLocalizeFormat` leaks a smart reference.
    
    **2. `TclStrIdxTree`: out-of-bounds read with U+023A or U+023E in locale names, double free on allocation failure**
    
    `TclStrIdxTreeBuildFromList` lowers keys with `Tcl_UtfToLower`, which keeps U+023A and U+023E because their lowercase forms need more bytes. Keys that match case-insensitively then have prefixes of different byte lengths, and `TclStrIdxTreeSearch` applies an offset measured in one key to its siblings:
    
        clock format 0 -gmt 1
        namespace eval ::tcl::clock {
            ::msgcat::mcset xx MONTHS_FULL [list ⱥⱥy ȺȺz March April May June July \
                August September October November December]
        }
        catch {clock scan "ⱥⱥq 2000" -format "%B %Y" -locale xx -gmt 1}
    
        SUMMARY: AddressSanitizer: heap-buffer-overflow tclStrIdxTree.h:130 in TclUtfFindEqualNCInLwr
    
    No shipped `msgs/*.msg` file contains these characters. Matching is wrong as well: input `ⱥⱥz` does not match the key `ȺȺz`.
    
    On an allocation failure, `TclStrIdxTreeBuildFromList` frees the tree items but leaves `firstPtr` set. Its callers (`ClockMCGetListIdxTree`, `ClockMCGetMultiListIdxTree`) then free the object, and the internal-rep free proc frees the same items again.
    
    **3. NULL dereference on malformed `LOCALE_ERAS` or `LOCALE_NUMERALS`, and format errors that are dropped or have no message**
    
    `ClockFmtToken_LocaleERAYear_Proc` does not check the NULL that `Tcl_ListObjIndex` returns for a short era row or a `LOCALE_NUMERALS` list under 100 entries:
    
        clock format 0 -gmt 1
        namespace eval ::tcl::clock {
            ::msgcat::mcset xx LOCALE_ERAS {{-1000000000000}}
        }
        clock format 0 -format %EC -locale xx -gmt 1
    
        tclClockFmt.c:3074:6: runtime error: member access within null pointer of type 'Tcl_Obj'
    
    Related: `TclClockFormat` jumps to `done` instead of `error` when a `CTOKT_INT` format procedure fails, so `clock format` returns the output so far with `TCL_OK`. With `LOCALE_ERAS {{-1000000000000 E notanumber}}`, `clock format 0 -format "<%Ey>"` returns `<` (8.6 reports the error).
    
    A list shorter than the format needs in the other locale lookups of `TclClockFormat` (`MONTHS_*` and `DAYS_OF_WEEK_*`, and `LOCALE_NUMERALS` for the `%O` conversions) makes `clock format` fail with an empty error message and error code `NONE`. With `MONTHS_ABBREV {a b}`, `clock format 1700000000 -format %b -gmt 1` fails that way; 8.6 formats an empty name.
    
  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: "Important"
  12. status changed to: "Open"
  13. submitter changed to: "bef"
  14. subsystem changed to: "16. Commands A-H"
  15. title changed to:
    clock: use-after-free when a callback clears the clock caches, and memory errors on malformed locale data
    
  16. type changed to: "Bug"