Artifacts Associated With Ticket bdd1b346f8
Ticket change [611d675c4a] (rid 261260) by bef on 2026-11-06 16:00:06:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-markdown"
- comment initialized 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`. - foundin initialized to: "9.0.4, 9.1.0, 9.1.1 trunk"
- is_private initialized to: "0"
- login: "bef"
- priority initialized to: "5 Medium"
- private_contact initialized to: "37918056d323dc69a2f76cecc85476189094f3b8"
- resolution initialized to: "None"
- severity initialized to: "Severe"
- status initialized to: "Open"
- submitter initialized to: "bef"
- subsystem initialized to: "16. Commands A-H"
- title initialized to:
Heap buffer overflow in `clock format` from a data race on the shared `fmtMinAlloc`
- type initialized to: "Bug"
Add attachment "fix.patch" [735edf953d] (rid 261262) by bef on 2026-11-06 16:01:55.
Ticket change [2e9cf38bb1] (rid 261266) by bef on 2026-11-06 16:25:46:
- icomment:
Fix on branch [bug-bdd1b3-clock-format-minalloc-race](/info/bug-bdd1b3-clock-format-minalloc-race)
- login: "bef"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [97fe1c3438] (rid 261306) by dkf on 2026-11-08 17:32:30:
- icomment:
Seems reasonable, but why didn't you add in the locking too? (Also, there's a read of the field later in the function that looks unsafe, and which can use the new local you made.) I've added a commit to the branch with these fixes.
- login: "dkf"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [f2781bcdf3] (rid 261307) by dkf on 2026-11-08 17:41:29:
- icomment:
Summary [here](https://core.tcl-lang.org/tcl/timeline?r=bug-bdd1b3-clock-format-minalloc-race).
- login: "dkf"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [c30181b508] (rid 261324) by bef on 2026-11-09 08:30:25:
- icomment:
Thanks for the two commits, both look right. The original patch only removed the double read that leads to the overflow, since the remaining accesses only affect the size hint. Taking `ClockFmtMutex` makes them race-free as well. A related pattern, not covered by this ticket: `ClockGetOrParseFmtFormat` tests `fss->fmtTok` without the mutex (`tclClockFmt.c:3253` on the branch) and returns the storage as soon as it is non-NULL, while the token array is filled under the mutex and published with a plain store at `:3377`. Nothing orders the stores into the array before the store of the pointer, so on a weakly ordered CPU (arm64, for example) a thread that formats with the same format string while another thread is still tokenising it can see the new pointer before the tokens behind it. `TclClockFormat` then walks the array until `tok->map == NULL` and calls through `map`. The scan side has the same shape (`ClockGetOrParseScanFormat`, `scnTok` at `:2164` and `:2346`). This has not been reproduced; it follows from the code, and the source comment at `:3376` ("note the threaded race condition") suggests it is known. The simplest fix is probably to drop the unlocked fast path and test `fmtTok` and `scnTok` only under the mutex, at the cost of one more uncontended lock per `clock format` and `clock scan`. - login: "bef"
- mimetype: "text/x-markdown"
- icomment: