Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2014-10-11
16:03 • Closed ticket [2835313fff]: crash in exception handling plus 6 other changes artifact: 16791ea95b user: dkf
2014-10-09
15:12 • Ticket [2835313fff]: 4 changes artifact: f5717d5591 user: dgp
2013-10-22
18:28 • Ticket [2835313fff]: 7 changes artifact: f78e189b26 user: dkf
14:31 • Ticket [2835313fff]: 4 changes artifact: a5257e9063 user: dkf
2013-10-21
18:28 • Ticket [2835313fff]: 4 changes artifact: ca781e66d1 user: dkf
16:24 • Ticket [2835313fff]: 3 changes artifact: b3c564844e user: dgp
14:51 • Open ticket [2835313fff]. artifact: 1d2c927c99 user: dgp
2013-10-20
18:41 • Ticket [2835313fff]: 8 changes artifact: 54fbc9b468 user: dkf
18:15
[2835313] Ensure correct stack balance when break and continue exceptions are about: the hard cases ... check-in: 582877d8ab user: dkf tags: trunk
2013-06-05
15:15 • Closed ticket [2835313fff]: crash in exception handling plus 8 other changes artifact: 1339fafa0c user: dkf
2010-09-01
17:56 • Ticket [2835313fff]: 4 changes artifact: a7272c3573 user: dkf
2010-05-17
19:49 • Ticket [2835313fff]: 1 change artifact: b58dcf3e91 user: msofer
2009-08-22
04:42 • Ticket [2835313fff]: 4 changes artifact: 2f31c69002 user: msofer
2009-08-11
11:27 • Ticket [2835313fff]: 4 changes artifact: 151dc16adc user: dgp
10:38 • Ticket [2835313fff]: 4 changes artifact: 740ed536be user: dgp
03:09 • New ticket [2835313fff]. artifact: 685194c379 user: dgp

crash in exception handling

Closed / Fixed2835313fffffffffffffffffffffffffffffffffCreated 17 years agoUpdated 12 years ago
Descriptionplain2009-08-11 03:09:56
% 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.
dgpplain2009-08-11 10:38:45
Joe Mistachkin kindly reminds me that
hanging it proper functioning for this example.
dgpplain2009-08-11 11:27:50
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 ()
msoferplain2009-08-22 04:42:06
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.
msofer Ticket [2835313fff]: 1 change [History diff]2010-05-17 19:49:31
dkfplain2010-09-01 17:56:20
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.
dkfplain2013-06-05 15:15:17
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).
dkfhtml2013-10-20 18:41:35

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.

dgpplain2013-10-21 14:51:13
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
dgpplain2013-10-21 16:24:09
Other panicking tests:

assemble-16.6
assemble-30.[123]

for-7.22
dkfhtml2013-10-21 18:28:45

for-7.* are the tests I've committed specifically to detect issues relating to this bug; they try very hard to break things.

dkfhtml2013-10-22 14:31:21

Problems in for.test were due to a total think-o. Still investigating assemble.test…

dkfhtml2013-10-22 18:28:45

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

dgpplain2014-10-09 15:12:33
Anything left to do here?
dkfhtml2014-10-11 16:03:42

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

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