Time-of-check to time-of-use: Difference between revisions

Content deleted Content added
BenzolBot (talk | contribs)
m access Example: I updated the example to show how TOCTTOU races can be used to escalate privilege and the interleaving. Also, I tried to make the implication of the example more precise.
Line 12:
 
fd = open(file, O_RDONLY);
write(fd, buffer, sizeof(buffer));
// do something with fd...
</source>
Here, ''access'' is intended to check whether the real user who executed the setuid program would normally be allowed to read the file (i.e., ''access'' checks the [[real userid]] rather than [[effective userid]]).
Line 18:
This race condition is vulnerable to an attack:
 
{| border="1" cellpadding="2"
# Create a file the user can read
|Victim
# Start the program
|Attacker
# Change the file to a [[symlink]] pointing to a file that the user shouldn't be able to read
|-
|<source lang="c">
if (access(file, R_OK) != 0) {
exit(1);
}
 
fd = open("file", O_RDWR);
// Actually writing over /etc/passwd
write(fd, buffer, sizeof(buffer));
</source>
||
<source lang="c">
 
// After the access check
//
symlink("file", "/etc/passwd");
 
// Before the open, "file" points to the password database
//
</source>
|}
 
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 theapplications ''access''cannot systemassume call,state asmanaged itby currentlythe existsoperating system (in Unix,this shouldcase neverthe befilesystem usednamespace) exceptwill asnot thechange firstbetween step of a [[Test andsystem Test-and-set]]calls.
 
== References ==