Tcl Source Code

View Ticket
Login
Ticket UUID: 3118489
Title: NUL in filenames
Type: Bug Created on: 2010-11-25 14:51:20
Submitter: andygoth Assigned to: jan.nijtmans
Subsystem: 37. File System Severity: Minor
Priority: 3 Low Last modified: 2014-05-15 14:56:29
Status: Closed Closed by: jan.nijtmans
Resolution: Fixed Closed on: 2014-05-15 14:56:29
Version: current: 8.6.0
Description:
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
User Comments:
hobbs added on 2010-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?

andygoth added on 2010-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. ;^)

andygoth added on 2012-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.

nijtmans added on 2012-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.

dkf added on 2012-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 added on 2014-04-11 08:20:54:

Fixed in trunk for Windows only [0f4597a73cae916f979f4bd22eeb9f8dc5cd467f|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.nijtmans added on 2014-04-11 09:52:23:

Now fixed for UNIX too, in [b5dd510e85bb0711c9f00a94620d602ffd2a9350|b5dd510e85].


jan.nijtmans added on 2014-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.nijtmans added on 2014-05-15 14:56:29:

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