Time-of-check to time-of-use

In software development, time-of-check to time-of-use (TOCTOU, TOCTTOU or TOC/TOU) is a class of software bugs caused by a race condition involving the checking of the state of a part of a system (such as a security credential) and the use of the results of that check.

TOCTOU race conditions are common in Unix between operations on the file system,[1] but can occur in other contexts, including local sockets and improper use of database transactions. In the early 1990s, the mail utility of BSD 4.3 UNIX had an exploitable race condition for temporary files because it used the mktemp() function.[2] Early versions of OpenSSH had an exploitable race condition for Unix domain sockets.[3] They remain a problem in modern systems; as of 2019, a TOCTOU race condition in Docker allows root access to the filesystem of the host platform.[4]

Examples

In Unix, the following C code, when used in a setuid program, has a TOCTOU 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. TOCTOU 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 TOCTOU

Exploiting a TOCTOU 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 call 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.

In the case of BSD 4.3 mail utility and mktemp(),[5] the attacker can simply keep launching mail utility in one process, and keep guessing the temporary file names and keep making symlinks in another process. The attack can usually succeed in less than one minute.

Techniques for single-stepping a victim program include file system mazes[6] and algorithmic complexity attacks.[7] 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 TOCTOU

Despite conceptual simplicity, TOCTOU 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.[8]

In the context of file system TOCTOU 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 TOCTOU race conditions.[9]

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

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 TOCTOU 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[11] and the TxOS kernel.[12] Microsoft Windows has added transactions to its NTFS file system,[13] but Microsoft discourages their use, and has indicated that they may be removed in a future version of Windows.[14]

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, nor does locking work well with networked filesystems, and cannot prevent TOCTOU race conditions.

For setuid binaries a possible solution is to use the seteuid() system call to change the effective user and then perform the open(). Differences in setuid() between operating systems can be problematic.[15]

See also

References

  1. Wei, Jinpeng; Pu, Calton. "TOCTTOU Vulnerabilities in UNIX-Style File Systems: An Anatomical Study". www.usenix.org. Retrieved 2019-01-14.
  2. Shangde Zhou(周尚德) (1991-10-01). "A Security Loophole in Unix". Archived from the original on 2013-01-16.
  3. Acheson, Steve (1999-11-04). "The Secure Shell (SSH) Frequently Asked Questions". Archived from the original on 2017-02-13.
  4. "Docker Bug Allows Root Access to Host File System". Decipher. Retrieved 2019-05-29.
  5. "mktemp(3) - Linux man page".
  6. Borisov, Nikita; Johnson, Rob; Sastry, Naveen; Wagner, David (2005). "Fixing races for fun and profit: how to abuse atime". Proceedings of the 14th Conference on USENIX Security Symposium, Baltimore (MD), July 31 – August 5, 2005. 14: 303–314. CiteSeerX 10.1.1.117.7757.
  7. Xiang Cai; Yuwei Gui; Johnson, Rob (2009-03-06). "Exploiting Unix File-System Races via Algorithmic Complexity Attacks" (PDF). Proceedings of the IEEE Symposium on Security and Privacy, Berkeley (CA), May 17–20, 2009.
  8. Martelli, Alex (2006). "Chapter 6: Exceptions". Python in a Nutshell (2nd ed.). O'Reilly Media. p. 134. ISBN 978-0-596-10046-9.
  9. Dean, Drew; 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: 195–206. CiteSeerX 10.1.1.83.8647.
  10. Tsafrir, Dan; Hertz, Tomer; Wagner, David; Da Silva, Dilma (June 2008). "Portably Preventing File Race Attacks with User-Mode Path Resolution". Technical Report RC24572, IBM T. J. Watson Research Center, Yorktown Heights (NY).
  11. Spillane, Richard P.; Gaikwad, Sachin; Chinni, Manjunath; Zadok, Erez (2009). "Enabling Transactional File Access via Lightweight Kernel Extensions" (PDF). Seventh USENIX Conference on File and Storage Technologies (FAST 2009), San Francisco (CA), February 24–27, 2009.
  12. Porter, Donald E.; Hofmann, Owen S.; Rossbach, Christopher J.; Benn, Alexander; Witchel, Emmett (2009). "Operating System Transactions" (PDF). Proceedings of the 22nd ACM Symposium on Operating Systems Principles (SOSP '09), Big Sky (MT), October 11–14, 2009.
  13. Russinovich, Mark; Solomon, David A. (2009). Windows Internals. Microsoft Press. ISBN 978-0735648739.
  14. "Alternatives to using Transactional NTFS". Microsoft Developer Network. Retrieved 10 December 2015.
  15. Hao Chen; Wagner, David; Dean, Drew (2002-05-12). "Setuid Demystified" (PDF).

Further reading

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.