Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2014-05-15
14:59
Minor bug-fix in [3118489] implementation: NUL in filenames. Filenames starting with <drive>: but wi... check-in: 46ad3d9162 user: jan.nijtmans tags: trunk
14:56 • Ticket [3118489fff] NUL in filenames status still Closed with 4 other change... artifact: e7b93d07b0 user: jan.nijtmans
14:54
Fix [3118489]: NUL in filenames. (On Windows, protect against invalid use of ':' in filenames as wel... check-in: 874ce8daf2 user: jan.nijtmans tags: core-8-5-branch
2014-05-13
12:25 • Ticket [3118489fff] NUL in filenames status still Closed with 4 other change... artifact: 6092cda88b user: jan.nijtmans
2014-04-11
09:55
Fix [3118489]: NUL in filenames, now fixed for both Windows and UNIX. For consistancy, any NUL char... check-in: b5dd510e85 user: jan.nijtmans tags: trunk
09:54 • Closed ticket [3118489fff]: NUL in filenames plus 5 other changes artifact: 90407ee788 user: jan.nijtmans
09:52 • Ticket [3118489fff]: 3 changes artifact: 225f0ad2b1 user: jan.nijtmans
08:23
Fix [3118489] for Windows only: NUL in filenames. This allows various characters to be used in win32... check-in: 0f4597a73c user: jan.nijtmans tags: trunk
08:20 • Ticket [3118489fff] NUL in filenames status still Open with 6 other changes artifact: 10782d8a98 user: jan.nijtmans
2014-02-21
19:07 • Ticket [3118489fff]: 1 change artifact: ecf4868fda user: jan.nijtmans
2012-11-21
18:09 • Ticket [3118489fff]: 4 changes artifact: 61ba5314a7 user: dkf
14:29 • Ticket [3118489fff]: 5 changes artifact: 049d277f61 user: nijtmans
10:38 • Ticket [3118489fff]: 5 changes artifact: 84c1bf8f24 user: andygoth
2010-11-26
01:19 • Ticket [3118489fff]: 4 changes artifact: c6098be455 user: andygoth
00:18 • Ticket [3118489fff]: 1 change artifact: 75200bf3f0 user: hobbs
00:18 • Ticket [3118489fff]: 4 changes artifact: 85ed674981 user: hobbs
2010-11-25
14:51 • New ticket [3118489fff]. artifact: 22ad1c705b user: andygoth

NUL in filenames

Closed / Fixed3118489fffffffffffffffffffffffffffffffffCreated 16 years agoUpdated 13 years ago
Descriptionplain2010-11-25 14:51:20
Some file commands treat NUL (\0) as a string terminator.  Others treat it as a normal character.  This inconsistency makes it possible to spoof commands that filter based on filename, particularly extension.  Example:

proc access {name} {
    if {[file extension $name] eq ".secret"} {
        error denied
    } else {
        puts granted
        open $name
    }
}
% access file.secret
denied
% access file.secret\0
granted
file5

Generally, it's the commands that talk to the operating system that treat NUL as the string terminator.  (More likely it's the operating system itself that does this!)  For example, [open] and [cd] do this:

% cd .
% cd .\0
% cd \0.
couldn't change working directory to "NUL.": no such file or directory
# (in the previous line, NUL is a real NUL character; to see it, use tkcon instead of xterm)
% file exists .
1
% file exists .\0
1
% file exists \0.
0
hobbsplain2010-11-26 00:18:47
I have a hard time understanding why this would be important.  Adding more "cleanliness" on top of the direct OS layers would be ill advised imo.  I think these are indeed valid on unix.

Why are you tossing nulls in?  If you are taking user input, why not strip nuls?
hobbs Ticket [3118489fff]: 1 change [History diff]2010-11-26 00:18:48
andygothplain2010-11-26 01:19:45
The problem is that "internal" commands are not consistent with "operating system" commands.  Either make the internal commands, like [file split], strip NULs and subsequent characters; or make the operating system commands treat filenames with NULs as nonexistent.  I prefer the latter approach, because Tcl can't always distinguish between filenames and other strings: filenames are manipulated not only by [file] but also [string] and variable interpolation and other methods.  Also it might be acceptable to have embedded NULs in the names of files on a VFS, just not for the native filesystem.

Yes, NULs come from user input.  I found this while testing Wibble: http://wiki.tcl.tk/27378#pagetocdd31402f .  The user can easily insert a NUL by typing %00 in the URL; this convinces the [string match] that the requested file doesn't match any of the forbidden patterns, but then [open] drops the NUL and [chan copy] happily sends the forbidden file to the user.

I can't easily strip NUL from filenames in Wibble because the core can't tell what parts of the URI or POST will ultimately correspond to an on-disk file, what parts are names of objects that aren't disk files and can legitimately have NULs, and what parts are arbitrary binary data.

What can I do?  Here's a script to strip NUL and subsequent:

proc stripnul {str} {
    if {[set index [string first \0 $str]] == -1} {
        return $str
    } else {
        string range $str 0 [expr {$index - 1}]
    }
}

Then I must use this every time I make any decisions on the basis of a filename that's assembled from user input.  That's a pain and there's a lot of room for error.  Or, fix the problem everywhere for everybody by making the native filesystem code treat files whose names have embedded NULs as nonexistent.  That makes sense to me, because there *aren't* any files with NULs in their names!

Very old versions of Tcl used to strip NUL everywhere, whether you liked it or not. ;^)
andygothplain2012-11-21 10:38:38
I just came across this article describing the problem in the more general case:

http://blog.portswigger.net/2008/05/null-byte-attacks-are-alive-and-well.html

It turns out Tcl isn't the only system affected.
nijtmansplain2012-11-21 14:29:10
The most easy solution is the one used by cygwin: Map all invalid
characters to a character in the Unicode private area range U+F000 to U+F0FF

See:
<http://cygwin.com/cygwin-ug-net/using-specialnames.html#pathnames-specialchars>

On UNIX it could mean mapping \0 to \uF000  (or EF 80 80 in UTF-8))
in internal/external filename translation.

After 8.6.0 is released, I'll have a further look whether this is practical
or not. So 8.6.1 is the earliest chance to get it in, if all works out well.
No high prio.
dkfplain2012-11-21 18:09:27
Quite apart from the fact that protecting files by extension is stupid (yeah, its just an example ;-)) it's arguably the case that NUL should always be treated as the end of the filename on all platforms. I've never encountered an OS that allowed it, though there's the prospect of its denormalized form turning up on systems that use UTF-8 (oh, what a tangled web we weave!).

Anyway, my point is that we should fix [file]'s subcommands to do the right thing; if someone is abusing them with non-filenames, that's their fault.
jan.nijtmans Ticket [3118489fff]: 1 change [History diff]2014-02-21 19:07:21
jan.nijtmanswiki2014-04-11 08:20:54

Fixed in trunk for Windows only [0f4597a73c] by translating some characters which are invalid for win32 filenames to other characters as described here: http://cygwin.com/cygwin-ug-net/using-specialnames.html#pathnames-specialchars. (The characters ':' and '?' are not handled, because ':' is valid as drive specifier and '?' is valid in the extended path prefix.)

Code inspired by fossil: http://www.fossil-scm.org/index.html/artifact/76559aefff?ln=245-252

jan.nijtmanswiki2014-04-11 09:52:23

Now fixed for UNIX too, in [b5dd510e85].

jan.nijtmans Closed ticket [3118489fff]. [History diff]2014-04-11 09:54:21
jan.nijtmansplain2014-05-13 12:25:22
On Windows, it's even worse: The ':' character can be used to gain access to "file.secret". Try (using the given "access" proc):

% access file.secret:abc.txt
granted
file2f6b9b0
%

Ough! (It doesn't matter what's after the ':', but it must contain at least a '.')
jan.nijtmanswiki2014-05-15 14:56:29

Backported to core-8-5-branch: [874ce8daf2]

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