Time-of-check to time-of-use

This is an old revision of this page, as edited by 78.52.96.219 (talk) at 03:51, 7 October 2011 (Examples: Fix argument order of call to symlink (see symlink man page)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In software development, time-of-check-to-time-of-use (TOCTTOU, pronounced "TOCK too") is a class of software bug caused by changes in a system between the checking of a condition (such as a security credential) and the use of the results of that check. It is a kind of race condition.

A simple example is as follows: Consider a Web application that allows a user to edit pages, and also allows administrators to lock pages to prevent editing. A user requests to edit a page, getting a form by which he can alter its content. Before the user submits the form, an administrator locks the page, which should prevent editing. However, since the user has already begun editing, when he submits the form, his edits are accepted. When the user began editing, his authorization was checked, and he was indeed allowed to edit. However, the authorization was used later, after he should no longer have been allowed.

TOCTTOU race conditions are most common in Unix between operations on the file system, but can occur in other contexts, including local sockets and improper use of database transactions. Early versions of OpenSSH had an exploitable race condition for Unix ___domain sockets.[1]

Examples

In Unix, the following C code, when used in a setuid program, is a TOCTTOU bug:

if (access("file", W_OK) != 0) {
   exit(1);
}

fd = open("file", O_WRONLY);
write(fd, buffer, sizeof(buffer));

Here, access is intended to check whether the real user who executed the setuid program would normally be allowed to write the file (i.e., access checks the real userid rather than effective userid).

This race condition is vulnerable to an attack:

Victim Attacker
if (access("file", W_OK) != 0) {
   exit(1);
}

fd = open("file", O_WRONLY);
// Actually writing over /etc/passwd
write(fd, buffer, sizeof(buffer));
// After the access check
//
symlink("/etc/passwd", "file");

// Before the open, "file" points to the password database
//

In this example, an attacker can exploit the race condition between the access and open to trick the setuid victim into overwriting an entry in the system password database. TOCTTOU races can be used for privilege escalation, to get administrative access to a machine.

Although this sequence of events requires precise timing, it is possible for an attacker to arrange such conditions without too much difficulty.

The implication is that applications cannot assume the state managed by the operating system (in this case the file system namespace) will not change between system calls.

Reliably timing TOCTTOU

Exploiting a TOCTTOU race condition requires precise timing to ensure that the attacker's operations interleave properly with the victim's. In the example above, the attacker must execute the symlink system calls precisely between the access and open. For the most general attack, the attacker must be scheduled for execution after each operation by the victim, also known as "single-stepping" the victim.

Techniques for single-stepping a victim program include file system mazes[2] and algorithmic complexity attacks.[3] In both cases, the attacker manipulates the OS state to control scheduling of the victim.

File system mazes force the victim to read a directory entry that is not in the OS cache, and the OS puts the victim to sleep while it is reading the directory from disk. Algorithmic complexity attacks force the victim to spend its entire scheduling quantum inside a single system call traversing the kernel's hash table of cached file names. The attacker creates a very large number of files with names that hash to the same value as the file the victim will look up.

Preventing TOCTTOU

Despite conceptual simplicity, TOCTTOU race conditions are difficult to avoid and eliminate. One general technique is to use exception handling instead of checking, under the philosophy of EAFP "It is easier to ask for forgiveness than permission" rather than LBYL "look before you leap" – in this case there is no check, and failure of assumptions to hold are detected at use time, by an exception.[original research?]

In the context of file system TOCTTOU race conditions, the fundamental challenge is ensuring that the file system cannot be changed between two system calls. In 2004, an impossibility result was published, showing that there was no portable, deterministic technique for avoiding TOCTTOU race conditions.[4]

Since this impossibility result, libraries for tracking file descriptors and ensuring correctness have been proposed by researchers.[5]

An alternative solution proposed in the research community is for UNIX systems to adopt transactions in the file system or the OS kernel. Transactions provide a concurrency control abstraction for the OS, and can be used to prevent TOCTTOU races. While no production UNIX kernel has yet adopted transactions, proof-of-concept research prototypes have been developed for Linux, including the Valor file system[6] and the TxOS kernel.[7] Microsoft Windows has added transactions to its NTFS file system.[8]

File locking is a common technique for preventing race conditions for a single file, but it does not extend to the file system namespace and other metadata, and cannot prevent TOCTTOU race conditions.

References

  1. ^ Acheson, Steve; SSH FAQ
  2. ^ Borisov, Nikita; Johnson, Rob; Sastry, Naveen; and Wagner, David; 2005; Fixing Races for Fun and Profit: How to abuse atime; Proceedings of the 14th Conference on USENIX Security Symposium (Security'05), Baltimore (MD), July 31–August 5, 2005, Vol. 14, pp. 303–314
  3. ^ Cai, Xiang; Gui, Yuwei; and Johnson, Rob; 2009; Exploiting UNIX file-system races via algorithmic complexity attacks; Proceedings of the IEEE Symposium on Security and Privacy, Berkeley (CA), May 17–20, 2009
  4. ^ Dean, Drew; and Hu, Alan J.; 2004; Fixing races for fun and profit: How to use access(2); Proceedings of the 13th USENIX Security Symposium, San Diego (CA), August 9–13, 2004, pp. 195–206
  5. ^ Tsafrir, Dan; Hertz, Tomer; Wagner, David; and Da Silva, Dilma; 2008; "Portably preventing file race attacks with user-mode path resolution"; Technical Report RC24572, IBM T. J. Watson Research Center; June 2008, Yorktown Heights (NY)
  6. ^ Spillane, Richard; Gaikwad, Sachin; Chinni, Manjunath; Zadok, Erez; and Wright, Charles P.; 2009; "Enabling transactional file access via lightweight kernel extensions"; Seventh USENIX Conference on File and Storage Technologies (FAST 2009), San Francisco (CA), February 24–27, 2009
  7. ^ Porter, Donald E.; Hofmann, Owen S.; Rossbach, Christopher J.; Benn, Alexander; and Witchel, Emmett; 2009; "Operating System Transactions"; In Proceedings of the 22nd ACM Symposium on Operating Systems Principles (SOSP '09), Big Sky (MT), October 11–14, 2009]
  8. ^ Russinovich, Mark; and Solomon, David A.; 2009; Windows Internals; Microsoft Press

Further reading