crash in exception handling
% proc foo args {puts $args}
% while 1 {foo 1 2 3 [continue] 5 6}
Segmentation fault
Also crashes 8.4.19, 8.3.5, 8.2.3, 8.1.0
Hangs 8.0.5
Something deep in the design of
ExceptionRanges etc. just doesn't
handle this right.
Joe Mistachkin kindly reminds me that hanging it proper functioning for this example.
From a debug build:
% while 1 {foo [continue]}
Bad stack top 2 at pc 2 in TclExecuteByteCode (min 0, max 1)
executing continue
TclExecuteByteCode execution failure: bad stack top
Program received signal SIGABRT, Aborted.
0x40000402 in __kernel_vsyscall ()
This cannot be fixed easily without deep changes in the exception range mechanism, or possibly some costly extra functionality in INST_START_CMD. The problem is that the exec stack depth is not recorded on entering an exception range: neither at compile time, which would be iffy, nor at run time (catch does it with INST_BEGIN_CATCH). Given this, there is no way to restore the stack when jumping to one of the special targets (continue or break). The whole mechanism just ASSUMES that the stack is at its proper depth when a jump to one of the targets occurs. That is: a solution to this problem is possible, but it will definitely break in a difficult-to-fix way [tbcloading] old precompiled programs. Some day we might decide to do that.
This issue was encountered yesterday on the Tcler's chat when kbk was trying to explain to dogeen what an exception range is. One possible fix would be to create an extra instruction which clears the stack down to a given mark. Old code wouldn't use it, but that's also probably not having a problem anyway.
allow_comments - 1
Fixed on trunk. Will not backport as the changes were quite extensive internally (and dealing with jumping past {*}expansion required a new opcode).
Dealt with the other ways in which exceptions could be generated. Evilness abounded!
Analysis
! This was in fact one of the most serious bugs in Tcl's implementation that existed; it allowed a whole range of attacks against Tcl deployments that were impossible to guard against without totally crippling the language. I'm only willing to admit just how serious this was now that I've got code in place that makes the problem impossible to exploit, though it should be noted that a backport to 8.5 is definitely impossible.Execution of untrusted code in a critical environment will be strongly recommended against until Tcl 8.6.2. This applies even in a safe interpreter. It is not believed possible to exploit this in any way other than forcing a process to hang or crash, as it is not possible for the attacker to control the locations of any values in Tcl or to force them to be placed at any known location.
Mitigation Strategies
A mitigation is to use a build of Tcl that has been configured with --enable-symbols=compile, as that will trap Tcl execution stack imbalances and convert them into definite crashes (i.e., a call to Tcl_Panic()); the disadvantage of that mechanism is that there is a substantial performance penalty. Code must also be written to cope with the process crashing (if desired), perhaps via a watchdog parent process; at the point where the problem is detected, insufficient information remains in the process to determine what the cause was.Troubles on the trunk (--enable-symbols=all testing):
---- assemble-16.5 start
Test file error: bad stack adjustment when compiling tcl::unsupported::assemble (was -1 instead of 1)
---- for-7.14 start
Test file error:
Bad stack top 13 at pc 182 in TclNRExecuteByteCode (min 0, max 12)
executing for {set x 0} {[incr x]<50} {} {
puts [puts {*}$l {*}[puts a b c {*}$l {*}[apply {{} {
re...
TclNRExecuteByteCode execution failure: bad stack top
Other panicking tests: assemble-16.6 assemble-30.[123] for-7.22
for-7.* are the tests I've committed specifically to detect issues relating to this bug; they try very hard to break things.
Problems in for.test were due to a total think-o. Still investigating assemble.test…
Only simple fix for assemble.test problems is to roll back the changes in tclAssemble.c.
The deep problem is that invokeStk is issued within a basic block, yet can throw exceptions. In particular, it can throw TCL_BREAK and TCL_CONTINUE with a trapping loop context outside the assembled code. Attempting to do the stack rebalance with the same machinery as used in the rest of the compiler won't work, as the information required to do so (the current and target stack- and expansion-depths, and so the size of correction to apply) is not present in the same format due to the basic block management mechanism. I don't know how to do that. (It all becomes much more pressing/complex once/if loop exception ranges become issuable in the assembler.)Because of these deeper problems, I'm now assigning this to the assembler maintainer for consideration and fix/reject-fixing. (Right now, the assembler is the only way to generate the sort of trouble described in this bug report.)
Anything left to do here?
No. The issues that remain with the assembler are deep and nasty, but need addressing in the assembler through different mechanisms. Since there's no explicit break, continue or return... instructions, the problems are as averted as I can manage. (The real fix would involve using something like beginCatch/endCatch for all loops. Which is heavyweight.)