A TOCTOU (time-of-check, time-of-use) race condition is possible when two or more concurrent processes are operating on a shared file system [Seacord 2013b]. Typically, the first access is a check to verify some attribute of the file, followed by a call to use the file. An attacker can alter the file between the two accesses, or replace the file with a symbolic or hard link to a different file. These TOCTOU conditions can be exploited when a program performs two or more file operations on the same file name or path name.
A program that performs two or more file operations on a single file name or path name creates a race window between the two file operations. This race window comes from the assumption that the file name or path name refers to the same resource both times. If an attacker can modify the file, remove it, or replace it with a different file, then this assumption will not hold.
Noncompliant Code Example
If an existing file is opened for writing with the
w mode argument, the file's previous contents (if any) are destroyed. This noncompliant code example tries to prevent an existing file from being overwritten by first opening it for reading before opening it for writing. An attacker can exploit the race window between the two calls to
fopen() to overwrite an existing file.
This compliant solution invokes
fopen() at a single location and uses the
x mode of
fopen(), which was added in C11. This mode causes
fopen() to fail if the file exists. This check and subsequent open is performed without creating a race window. The
x mode provides exclusive access to the file only if the host environment provides this support.
Compliant Solution (POSIX)
This compliant solution uses the
O_EXCL flags of POSIX's
open() function. These flags cause
open() to fail if the file exists.
FIO45-C-EX2: Accessing a file name or path name multiple times is permitted if the file referenced resides in a secure directory. (For more information, see FIO15-C. Ensure that file operations are performed in a secure directory.)
FIO45-C-EX3: Accessing a file name or path name multiple times is permitted if the program can verify that every operation operates on the same file.
This POSIX code example verifies that each subsequent file access operates on the same file. In POSIX, every file can be uniquely identified by using its device and i-node attributes. This code example checks that a file name refers to a regular file (and not a directory, symbolic link, or other special file) by invoking
lstat(). This call also retrieves its device and i-node. The file is subsequently opened. Finally, the program verifies that the file that was opened is the same one (matching device and i-nodes) as the file that was confirmed as a regular file.
An attacker can still exploit this code if they have the ability to delete the benign file and create the malicious file within the race window between lstat() and open(). It is possible that the OS kernel will reuse the same device and i-node for both files. This can be mitigated by making sure that the attacker lacks the permissions to delete the benign file.
|IO.RACE||File system race condition|
|LDRA tool suite|
|75 D||Partially implemented|
Avoid race conditions while accessing files
|Polyspace Bug Finder|
|CERT C: Rule FIO45-C|
Checks for file access between time of check and use (rule fully covered)
|[Seacord 2013b]||Chapter 7, "Files"|