Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Heap buffer overflow in `clock format` from a data race on the shared `fmtMinAlloc`

Openbdd1b346f89df079ce13dfb3be5c80a6608cb282Created 26 days agoUpdated 24 days ago
Descriptionmarkdown2026-11-06 16:00:06

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):

    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):

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.

Referenced by1
  • 03a7ecb1b8Fix [bdd1b346f8]: TclClockFormat reads the shared fmtMinAlloc only once check-in2026-11-06 16:19:23
befmarkdown2026-11-06 16:25:46
dkfmarkdown2026-11-08 17:32:30

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.

dkfmarkdown2026-11-08 17:41:29

Summary here.

befmarkdown2026-11-09 08:30:25

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.

Log in (anonymous is fine) to add a comment.

Attachments: