<i>Heap buffer overflow in `clock format` from a data race on the shared `fmtMinAlloc`</i>.
by
.
D 2026-11-06T16:00:06.905
J assignee nobody
J closer nobody
J cmimetype text/x-markdown
J comment Security\sissue:\sa\sheap\sbuffer\soverflow\sthat\soutside\sdata\s(a\sclient-supplied\stime\szone)\scan\strigger\sin\sthreaded\sapplications.\r\n\r\nThe\sparsed\sformat\sstorage\s(`ClockFmtScnStorage`)\sis\sshared\sby\sall\sthreads\sthat\suse\sthe\ssame\sformat\sstring,\sbut\s`TclClockFormat`\sreads\sand\supdates\sits\ssize\shint\s`fmtMinAlloc`\swithout\sa\slock.\sThe\shint\sis\sread\sonce\sfor\sthe\sallocation\sand\sagain\sfor\sthe\sbuffer\send\s(`tclClockFmt.c:3418-3423`):\r\n\r\n```c\r\n\s\s\s\sif\s(fss->fmtMinAlloc\s>\ssizeof(resMem))\s{\r\n\tdateFmt->resMem\s=\s(char\s*)Tcl_AttemptAlloc(fss->fmtMinAlloc);\r\n\t...\r\n\tdateFmt->resEnd\s=\sdateFmt->resMem\s+\sfss->fmtMinAlloc;\r\n```\r\n\r\nIf\sanother\sthread\sraises\sthe\shint\s(`:3561`)\sin\sbetween,\s`resEnd`\slies\spast\sthe\send\sof\sthe\sallocated\sblock.\sLater\soutput\sis\swritten\sup\sto\s`resEnd`\sand\soverflows\sthe\sheap\sblock\sby\sthe\sdifference.\sThe\scompiler\sdoes\sreload\sthe\sfield\safter\sthe\sallocator\scall\sat\s`-O1`\sand\s`-O2`.\r\n\r\nThis\scan\shappen\sin\sany\sthreaded\sapplication\sthat\sformats\sdates\swith\sthe\ssame\sformat\sstring\sin\sseveral\sthreads.\sIf\soutput\slengths\svary\sand\sthe\sattacker\scontrols\sthem,\sfor\sexample\swith\sa\suser-supplied\s`-timezone`\sand\s`%Z`,\sthe\soverflow\scan\sbe\striggered\srepeatedly.\sA\ssingle\ssafe\sinterpreter\scannot\sreach\sit,\ssince\sit\scannot\screate\sthreads;\sa\shost\sthat\sruns\suntrusted\sscripts\sin\sseveral\sthreads\scan.\r\n\r\nPoC,\srun\swith\s`tcltest`\s(for\s`testthread`):\r\n\r\n```tcl\r\nset\sworker\s{\r\n\s\s\s\sfor\s{set\sj\s0}\s{$j\s<\s100}\s{incr\sj}\s{\r\n\s\s\s\s\s\s\s\slappend\stzs\s"[string\srepeat\sZ\s[expr\s{100\s+\s8*$j\s+\sIDX}]]-1"\r\n\s\s\s\s}\r\n\s\s\s\sforeach\stz\s$tzs\s{\sclock\sformat\s0\s-timezone\s$tz\s-format\swarm\s}\r\n\s\s\s\sfor\s{set\sr\s0}\s{$r\s<\s5000}\s{incr\sr}\s{\r\n\s\s\s\s\s\s\s\sforeach\stz\s$tzs\s{\sclock\sformat\s0\s-timezone\s$tz\s-format\s"%Z\s$r"\s}\r\n\s\s\s\s}\r\n}\r\nfor\s{set\si\s0}\s{$i\s<\s8}\s{incr\si}\s{\r\n\s\s\s\slappend\sids\s[testthread\screate\s-joinable\s[string\smap\s[list\sIDX\s$i]\s$worker]]\r\n}\r\nforeach\sid\s$ids\s{\stestthread\sjoin\s$id\s}\r\n```\r\n\r\nWith\sASan\sthis\sfails\swithin\sa\ssecond,\sat\s`:2962`\s(`%Z`\scopy),\s`:3521`\s(literal\sword)\sor\s`:3563`\s(final\sNUL):\r\n\r\n```\r\nWRITE\sof\ssize\s455\sat\s0x615000120445\sthread\sT3\r\n\s\s\s\s#1\sClockFmtToken_TimeZone_Proc\stclClockFmt.c:2962\r\nSUMMARY:\sAddressSanitizer:\sheap-buffer-overflow\stclClockFmt.c:2962\sin\sClockFmtToken_TimeZone_Proc\r\n```\r\n\r\nWith\sone\sthread,\sor\swith\sa\sseparate\sformat\sstring\sper\sthread,\sit\sruns\sclean.\sThreadSanitizer\sreports\sthe\srace\sas\sa\sread\sat\s`:3418`\sagainst\sa\swrite\sat\s`:3561`.\sTrunk,\s9.1.0\sand\s9.0.4\sare\saffected;\s8.6\sis\snot.\r\n\r\nSuggested\sfix:\sread\s`fss->fmtMinAlloc`\sonce\sinto\sa\slocal\sand\suse\sthat\sfor\sboth\sthe\sallocation\sand\s`resEnd`.\sThe\sremaining\sunsynchronised\saccess\sto\sthe\shint\sis\sstill\sa\sdata\srace\sin\sC\sterms,\sso\sideally\sthe\shint\sis\sread\sand\supdated\sunder\s`ClockFmtMutex`.
J foundin 9.0.4,\s9.1.0,\s9.1.1\strunk
J is_private 0
J login bef
J priority 5\sMedium
J private_contact 37918056d323dc69a2f76cecc85476189094f3b8
J resolution None
J severity Severe
J status Open
J submitter bef
J subsystem 16.\sCommands\sA-H
J title Heap\sbuffer\soverflow\sin\s`clock\sformat`\sfrom\sa\sdata\srace\son\sthe\sshared\s`fmtMinAlloc`
J type Bug
K bdd1b346f89df079ce13dfb3be5c80a6608cb282
U bef
Z c73d536580843c24418602f7deb86a97