Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2015-08-25
12:06 • Closed ticket [0df7a1ec26]: unix/tclUnixFile.c: return value of call to TclpGetC... artifact: 11b70fba1b user: jan.nijtmans
11:35
Fix [0df7a1ec2674a76a]: unix/tclUnixFile.c: return value of call to TclpGetCwd in TclpFindExecutable... check-in: 69fd7c4740 user: jan.nijtmans tags: trunk
11:29
Fix [0df7a1ec2674a76a]: unix/tclUnixFile.c: return value of call to TclpGetCwd in TclpFindExecutable... check-in: 35abd326e1 user: jan.nijtmans tags: core-8-5-branch
2015-08-21
18:19 • New ticket [0df7a1ec26] unix/tclUnixFile.c: return value of call to TclpGetCwd i... artifact: caea86e1a4 user: scmc

unix/tclUnixFile.c: return value of call to TclpGetCwd in TclpFindExecutable is not checked, variable cwd is used uninitialized

Closed / Fixed0df7a1ec2674a76a04f8ce3f405a1c1f8273ec20Created 11 years agoUpdated 11 years ago
Descriptionplain2015-08-21 18:19:48
Hello,

I'm embedding Tcl in a process running in FreeBSD capability mode (a.k.a. Capsicum) on an amd64 platform. When doing so, use of system calls referencing global namespaces (e.g., the file system) is restricted.

In the function TclpFindExecutable, the call to TclpGetCwd fails because calling getcwd will fail with errno 94, ECAPMODE - "Not permitted in capability mode". This happens if argv consists of a relative path.

If that call fails, the variable cwd is uninitialized. Use of uninitialized variable causes application crash.

GDB output with stack trace:

Program terminated with signal 10, Bus error.
#0  0x00000000004e1a9b in UtfToUtfProc (clientData=0x0, src=0x800000000000 <Address 0x800000000000 out of bounds>, srcLen=5984000, flags=2, 
    statePtr=0x7fffffffe038, dst=0x8010b5db5 "", dstLen=6402, srcReadPtr=0x7fffffffe01c, dstWrotePtr=0x7fffffffe018, dstCharsPtr=0x7fffffffe014, 
    pureNullMode=1) at /home/scmc/src/tcl8.6.4/generic/tclEncoding.c:2329
2329            if (UCHAR(*src) < 0x80 && !(UCHAR(*src) == 0 && pureNullMode == 0)) {
(gdb) bt
#0  0x00000000004e1a9b in UtfToUtfProc (clientData=0x0, src=0x800000000000 <Address 0x800000000000 out of bounds>, srcLen=5984000, flags=2, 
    statePtr=0x7fffffffe038, dst=0x8010b5db5 "", dstLen=6402, srcReadPtr=0x7fffffffe01c, dstWrotePtr=0x7fffffffe018, dstCharsPtr=0x7fffffffe014, pureNullMode=1) at /home/scmc/src/tcl8.6.4/generic/tclEncoding.c:2329
#1  0x00000000004dfca0 in UtfIntToUtfExtProc (clientData=0x0, src=0x7ffffffffb47 "", srcLen=5984000, flags=2, statePtr=0x7fffffffe038, dst=0x8010b58fc "", dstLen=6402, srcReadPtr=0x7fffffffe01c, dstWrotePtr=0x7fffffffe018, dstCharsPtr=0x7fffffffe014) 
    at home/scmc/src/tcl8.6.4/generic/tclEncoding.c:2194
#2  0x00000000004e1649 in Tcl_UtfToExternalDString (encoding=0x80100b150, src=0x7ffffffffb47 "", srcLen=5984000, dstPtr=0x7fffffffe370)
    at /home/scmc/src/tcl8.6.4/generic/tclEncoding.c:1327
#3  0x00000000005b02e4 in TclpFindExecutable (argv0=0x7fffffffe998 "./tcl") at tclUnixFile.c:177
#4  0x0000000000549fba in Tcl_MainEx (argc=1, argv=0x7fffffffe738, appInitProc=0x400410 <Tcl_AppInit>, interp=0x801044400)
    at /home/scmc/src/tcl8.6.4/generic/tclMain.c:317

The following change fixes this problem for me, however I am not sure it's the way to go (e.g., it causes [info nameofexecutable] to return an empty string).

--- a/unix/tclUnixFile.c
+++ b/unix/tclUnixFile.c
@@ -164,12 +164,13 @@ TclpFindExecutable(
     if ((name[0] == '.') && (name[1] == '/')) {
        name += 2;
     }
+
+    if (TclpGetCwd(NULL, &cwd) == NULL) {
+       goto done;
+    }
 
     Tcl_DStringInit(&nameString);
     Tcl_DStringAppend(&nameString, name, -1);
-
-    TclpGetCwd(NULL, &cwd);
-
     Tcl_DStringFree(&buffer);
     Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&cwd),
            Tcl_DStringLength(&cwd), &buffer);




//Sebastian Cato
jan.nijtmansplain2015-08-25 12:06:36
Fixed in core-8-5-branch and trunk, will be in next release. Thanks for the report!
Log in (anonymous is fine) to add a comment.