- What Is a Race Condition?
- Time-of-Check, Time-of-Use
- Secure File Access
- Temporary Files
- File Locking
- Other Race Conditions
- Conclusion
Time-of-Check, Time-of-Use
Not every race condition occurs in threaded programs. Any time that there are multiple threads of execution at once, race conditions are possible, regardless of whether they are really simultaneous as in a distributed system, such as on a single-processor multitasking machine. Therefore, multiple processes on a single machine can have race conditions between them when they operate on data that may be shared. What kinds of data may be shared? Although some systems allow you to share memory between processes, all systems allow processes to share files. File-based race conditions are the most notorious in terms of security-critical race conditions.
Note that this kind of race condition is primarily a problem on UNIX machines, mostly because local access is usually required. Much of the time, if an attacker can remotely break into a Windows machine, the attacker already has all the access necessary for whatever nefarious ends the attacker has in mind. Also, many Windows machines are not really multiuser machines. Nonetheless, this does not make security-critical, file-based race conditions impossible on a Windows machine, and you should still watch out for them. The Windows API for opening files makes these kinds of race conditions much more difficult, but they are still possible. 1 Most file-based race conditions that are security hazards follow a common formula. There is a check on some property of the file that precedes the use of that file. The check needs to be valid at the time of use for proper behavior, but may not be. (Recall the elevator problem.) Such flaws are called time-of-check, time-of-use flaws, often abbreviated TOCTOU. In the canonical example, a program running setuid root is asked to write a file owned by the user running the program. The root user can write to any file it wants, so the program must take care not to write to anything unless the actual user has permission to do so. The preferred way to solve this problem is to set the EUID to the UID running the program. However, programmers commonly use the call access in an attempt to get the same results:
/* access returns 0 on success */ if(!access(file, W_OK)) { f = fopen(file, "wb+"); write_to_file(f); } else { fprintf(stderr, "Permission denied when trying to open %s.\n", file); }
The access call checks whether the real UID has permissions for a particular check, and returns 0 if it does. A text editor that needs to run as root for some reason may do this. In this case, the attacker can create a file that is malicious, such as a bogus /etc/passwd. It's then just a matter of exploiting the race condition to install it.
The window of vulnerability here is the time it takes to call fopen and have it open a file, after having called access(). If an attacker can replace a valid file to which the attacker has permissions to write with a file owned by root, all within that time window, then the root file will be overwritten. The easiest way to do this is by using a symbolic link, which creates a file that acts very much like any other file, except that it "points to" some other file. The attacker creates a dummy file with his permissions, and then creates a symbolic link to it:
$ touch dummy $ ln s dummy pointer $
Now, the attacker tells the program to open the file pointer. The attacker's goal is to perform a command such as the following within the window of vulnerability:
$ rm pointer; ln s /etc/passwd pointer
If successful, the program will overwrite the system password file. The attacker will have a better chance of success if using a C program that makes system calls directly rather than using the shell. To make the job easy, the attacker would write a program that fires up the editor, performs these commands, checks to see if the real password file was overwritten, and repeats the attack until successful. Problems like this are unfortunately common. A well-known, similar problem in old versions of xterm provides a classic example.
When it comes to exploitable file system race conditions, there are a few things that should be true. Usually, the attacker must have access to the local machine, legitimate or not. Also, the program with the race condition needs to be running with an EUID of root. The program must have this EUID for the period of time over which the race condition exists. Otherwise, the attacker will not be able to obtain root privileges, only the privileges he already has. There is no sense in running a race for your own privilege!
Broken passwd
Let's look at a historic case of a TOCTOU problem (introduced in [Bishop, 1996]): a broken version of the passwd command on SunOS and HP/UX machines. The UNIX utility program passwd allows someone to change a password entry, usually their own. In this particular version of the program, passwd took the name of a password file to manipulate as one of its parameters. The broken version of passwd works as follows when the user inputs a passwd file to use:
passwd step 1. Open the password file and read it in, retrieving the entry for the user running the program.
passwd step 2. Create and open a temporary file called ptmp in the same directory as the password file.
passwd step 3. Open the password file again, copying the unchanged contents into ptmp, while updating modified information.
passwd step 4. Close both the password file and ptmp, then rename ptmp to be the password file.
Let's pretend we're the attacker, and that we can "step" the activities of passwd at will (causing it to wait for us in between steps while we modify the file system). Of course, in practice, we need to automate our attack, and run it multiple times in parallel with the passwd process until we hit just the right interleaving.
In this attack, we are going to overwrite some other user's .rhosts file so that we can log in as that user. We could just as easily write over the system password file. We'll also use symbolic linking on a directory level, instead of a file level.
Figure 91A shows the state of the file system just before our attack begins. Note that we're running our attack in our own directory, attack-dir, within which we'll create a subdirectory pwd and the file .rhosts. We also need to fill the .rhosts file with valid information (a simulated password file entry with a blank password). And finally, we run passwd itself.
Here's how to do this in a generic UNIX shell:
$ mkdir pwd $ touch pwd/.rhosts $ echo "localhost attacker :::::" >> pwd/.rhosts $ ln s pwd link $ passwd link/.rhosts
Figure 91A The state of the system after step 1 of the passwd race condition.
Figure 91B The state of the system after step 2 of the passwd race condition.
And then . . .
passwd step 1. Open and read the password file (link/.rhosts) to retrieve the entry for the user running the program. Just after step 1, passwd will have opened and read the file we created (link/.rhosts). The system is now in a situation similar to what is shown in Figure 91A. Before step 2 runs, we need to change link quickly to point to target-dir. This is the part that must happen with exactly the right timing. (In our pretend version of the attack, remember that we have control over what happens when.) Change the link as follows: rm link; ln s target-dir link
(target-dir actually has to be specified relative to the root directory, but we'll ignore that detail in our simplified example).
passwd step 2. Create and open a temporary file called ptmp in the same directory as the password file (link/.rhosts). Note that passwd is now using a different location to write out the file ptmp. It ends up creating a file in target-dir called ptmp, because link now points to target-dir. Now quickly, before step 3 happens, we need to change the link to point back to our directory as follows: rm link; ln s pwd link. We need to do this because the passwd file is going to be looking for an entry in it with our UID. If we're attacking the system password file, we wouldn't have to go through this step (see Figure 91B).
passwd step 3. Open the password file (link/.rhosts in attack-dir) again and copy the contents of ptmp while updating the changed information. Note that the file ptmp has not yet been closed, so it is still the file that lives in target-dir. This means that the .rhosts file is being read from our pwd directory and is being copied to the temporary file in target-dir. See Figure 92A.
Finally, once again, we need to change link quickly to point to the target-dir: rm link; ln s target-dir link. We do this so that when passwd performs step 4, its version of link points to target-dir again.
passwd step 4. Close both the password file and ptmp, then rename ptmp to be the password file. After all this work, our attack will have written a new .rhosts file into target-dir. This allows us to log in to the target's account with no password (remember all those ::::'s?) and become the user who owns target-dir. Note that we can do this for target-dir equivalent to root; meaning, that if we choose the right target, we can win complete control over the machine. See Figure 92B.
Figure 92A The state of the system after step 3 of the passwd race condition.
Figure 92B The state of the system after step 4 of the passwd race condition.
Clearly, timing is everything in this attack. The directory pointed to through link must be pointed at precisely the right place in every stage of the attack. Getting this to happen is not always possible. In a real version of this attack we create a program to do all of the renaming and hope that our script interleaves in exactly the way described earlier with the passwd process. We do have one huge advantage in getting this to happen, though. We can run passwd as many times as we want and attack it over and over again in an automated fashion until the attack is successful.
Avoiding TOCTOU Problems
One thing you should do to help avoid TOCTOU problems is to avoid any file system call that takes a filename for an input, instead of a file handle or a file descriptor. By dealing with file descriptors or file pointers, we ensure that the file on which we are operating does not change behind our back after we first start dealing with it. Instead of doing a stat() on a file before opening it, open the file and then do an fstat() on the resulting file descriptor. Sometimes there is no reasonable alternative to a call, whether it is a check or use (and some calls can be both). In such cases, you can still use the call, but you must do so carefully. We detail a technique for doing so in the next section.
Additionally, you should avoid doing your own access checking on files. Leave that to the underlying file system. This means that you should never use the access() call. Instead, set the EUID and the EGID to the appropriate user, drop any extra group privileges by calling setgroups(0,0);.
Also, when opening arbitrary files, we recommend that you start by using open() and then using fdopen to create a FILE object once you're sure you have the proper file. To be sure you have the proper file, we recommend the following approach:
lstat() the file before opening it, saving the stat structure. Note that this may be susceptible to a race condition if we perform no other checks.
Perform an open().
fstat() the file descriptor returned by the open() call, saving the stat structure.
Compare three fields in the two stat structures to be sure they are equivalent: st_mode, st_ino and st_dev. If these comparisons are successful, then we know the lstat() call happened on the file we ultimately opened. Moreover, we know that we did not follow a symbolic link (which is why we used lstat() instead of stat()).
If the file does not exist, lstat() says so, but an attacker may be able to place a file there where there isn't one, before you perform the open(). To thwart this problem, pass the O_CREAT and O_EXCL flags to open(), which causes the open to fail if the file cannot be created.
As an example of these techniques, here is some code that shows how to open a file safely in a way that simulates fopen's w+ mode. That is, the file should be created if it doesn't exist. If it does exist, then it will be truncated:
include <sys/stat.h> include <sys/types.h> include <sys/stat.h> include <fcntl.h> include <unistd.h> include <stdio.h> include <errno.h> FILE *safe_open_wplus(char *fname) { struct stat lstat_info, fstat_info; FILE *fp; char *mode = "rb+"; /*We perform our own truncation.*/ int fd; if(lstat(fname, &lstat_info) == 1) { /* If the lstat() failed for reasons other than the file not existing, return 0, specifying error. */ if( errno != ENOENT ) { return 0; } if((fd = open(fname, O_CREAT|O_EXCL| O_RDWR, 0600)) == 1) { return 0; } mode = "wb"; } else { /* Open an existing file */ if((fd = open(fname, O_RDWR)) == 1) { return 0; } if(fstat(fd, &fstat_info) == 1 || lstat_info.st_mode != fstat_info.st_mode || lstat_info.st_ino != fstat_info.st_ino || lstat_info.st_dev != fstat_info.st_dev ) { close(fd); return 0; } /* Turn the file into an empty file, to mimic w+ semantics. */ ftruncate(fd, 0); } /* Open an stdio file over the low-level one */ fp = fdopen(fd, mode); if(!fp) { close(fd); unlink(fname); return 0; } return fp; }
Note in the previous code that we truncate the file ourselves, and only after ensuring that it is safe to do so.