Tcl Source Code

View Ticket
Login
Ticket UUID: 1157137
Title: Cannot open file if parent directory unreadable on Solaris
Type: Bug Created on: 2005-03-05 05:47:53
Submitter: nobody Assigned to: vincentdarley
Subsystem: 37. File System Severity:
Priority: 6 Last modified: 2005-10-05 12:28:27
Status: Open Closed by:
Resolution: None Closed on:
Version: obsolete: 8.4.9
Description:
Tcl version: 8.4.6 & 8.4.9
OS: Solaris 2.6 & 8 (possibly other Unixes, but *not* 
Linux)

To replicate the problem, do the following under a shell:

mkdir -p /tmp/dir1/dir2
chmod 111 /tmp/dir1
cd /tmp/dir1/dir2
echo JUNK > file
tclsh
% open file
error getting working directory name: permission denied

Yet this file is very readable! It's just that a parent 
directory is not readable (dir1). For some reason Tcl 8.4 
appears to want to do a getcwd() before opening the file, 
which fails on Solaris (and possibly other Unices).

This is causing us great grief, and I cannot even find a 
workaround other than to change the directory 
permissions, which we do not want to do for security 
reasons.

Is this problem known or patched somewhere? We need 
a fix real soon!

Thank you,
Mike Selig
User Comments:
vincentdarley added on 2005-03-15 01:31:32:
Logged In: YES 
user_id=32170

You'll want to try debugging TclpObjNormalizePath in
tcl/unix/tclUnixFCmd.c, to see what goes wrong, I would say.

It does seem odd that 'cd' works, put 'getcwd' fails?  Can
you confirm that [pwd] in dir2 also throws an error?

nobody added on 2005-04-13 07:22:27:
Logged In: NO 

Hi Vince,

Sorry for the slow response - I have been away for a few 
weeks.

I have done quite a bit of debugging, and the problem occurs 
because TCL appears to be making the assumption that 
getcwd() will ALWAYS succeed if the current directory 
exists. This seems to be true on Linux, but it is NOT true on 
other Unices. Strictly, on Unix you can CD to a directory as 
long as you have EXECUTE access to it - you do NOT need 
read access. However to read the directory (eg: do an "ls", or 
call readdir()), you need READ access to it.

Under some Unices, getcwd() works by recursively reading 
the directory tree from the current directory back up to root, 
finding the name of the directory at each level. In order to do 
this, you need READ access to every directory in the chain.

When TCL opens a file in Tcl_FSOpenFileChannel(), it first 
calls Tcl_FSGetNormalizedPath() (in generic/tclIOUtil.c). If 
the file name provided is TCL_PATH_RELATIVE this calls 
Tcl_FSGetCwd() (a few lines below the comment "We have to 
be a little bit careful here to avoid infinite loops"). On Unices 
where getcwd() fails because it cannot read one of the parent 
directories, Tcl_FSGetCwd() returns NULL, causing 
Tcl_FSGetNormalizedPath() to also return NULL, in turn 
causing Tcl_FSOpenFileChannel() to return NULL.

Thus the file open fails even though the file is actually 
readable, simply becuase TCL seems to always want the 
filename as a full pathname. This appears to be a design flaw 
in TCL. It *should* be able to open a readable file even if 
getcwd() fails - just like any other programming language.

Best regards,
Mike Selig ([email protected])

vincentdarley added on 2005-04-13 18:54:35:
Logged In: YES 
user_id=32170

I'm not really sure how to resolve this.  The first point to
make is that Tcl must check cwd() because if the getcwd()
has changed (possible in C programs using the Tcl API) then
the file reference is different. Also this is required to
deal with the whole virtual-filesystem stuff. Now there are
some changes that could perhaps be made to help with this,
so all is hopefully not lost.

However, Tcl's test suite actually tests for the case where:

cd foo
exec chmod foo .... (remove permissions)
pwd
->throws error that there are no permissions.

So, Tcl wants to throw an error in this case. 

The only solution I can think of is that, if Tcl_FSGetCwd()
returns NULL _and_ the error is a lack of permissions _and_
we're just accessing Tcl_FSGetCwd() in order to normalize a
path _then_ we use the cached value that was passed to 'cd'
instead.

This might allow this situation to work ok on Solaris but
not break anything else.

any thoughts?

hobbs added on 2005-10-05 12:28:27:
Logged In: YES 
user_id=72656

Is there a patch to try for this?