...
A program that performs a file operation on a filename or path twice creates a race window between the two file operations. This race window comes from the assumption that the filename refers to the same file 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 (write)
If an existing file is opened for writing, the file's previous contents are destroyed. This noncompliant code example tries to prevent an existing file from being overwritten by first trying to open 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.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> void open_some_file(const char *file) { FILE *f; if ((f = fopen(file, "r")) != NULL) { /* File exists, handle error */ } else { if (fclose(f) != 0) { /* handle error */ } if ((f = fopen(file, "w")) == NULL) { /* Handle error */ } /* write to file */ if (fclose(f) != 0) { /* handle error */ } } } |
Compliant Solution (C11 write)
This compliant solution uses only one instance of fopen()
, 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 done without creating a race window. Note that the x
mode provides exclusive access to the file only if the operating platform provides this support.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> void open_some_file(const char *file) { FILE *f; if ((f = fopen(file, "wx")) != NULL) { /* handle error */ } /* write to file */ if (fclose(f) != 0) { /* handle error */ } } |
Compliant Solution (POSIX write)
This compliant solution uses the O_CREAT
and O_EXCL
flags of POSIX's open()
system call. These flags cause open()
to fail if the file exists.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> #include <unistd.h> #include <fcntl.h> void open_some_file(const char *file) { int fd = open(file, O_CREAT | O_EXCL | O_RDONLY); if (fd != -1) { FILE *f = fdopen(fd, "rw+"); if (f != NULL) { printf("access granted.\n"); /* write to file */ if (fclose(f) != 0) { /* handle error */ } } if (close(fd) != 0) { /* handle error */ } } } |
Noncompliant Code Example (POSIX read)
This noncompliant code example attempts to ensure that an open will succeed by first calling the POSIX access()
function, which returns zero if the file exists and can be accessed using the specified permissions. An attacker can exploit the race window between the access and the open to cause fopen()
to fail in spite of the check.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> #include <unistd.h> void open_some_file(const char *file) { FILE *f; if (access(file, R_OK) == 0) { printf("access granted.\n"); f = fopen(file, "rb"); if (f == NULL) { /* Handle error */ } /* read file */ if (fclose(f) != 0) { /* handle error */ } } } |
Compliant Solution (read)
This compliant solution dispenses with an access call and merely relies on fopen()
to verify the file.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> void open_some_file(const char *file) { FILE *f = fopen(file, "rb"); if (f == NULL) { /* Handle error */ } printf("access granted.\n"); /* read file */ if (fclose(f) != 0) { /* handle error */ } } |
Exceptions
FIO45-EX1: Accessing a path multiple times is permitted if it is requested specifically by a user. A program that accepts commands from a user to read or write a specific filename does not violate this standard. Example programs would include file managers or text editors.
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <sys/stat.h> #include <fcntl.h> int open_regular_file(char *filename, int flags) { struct stat lstat_info; struct stat fstat_info; int f; if (lstat(filename, &lstat_info) == -1) { /* file does not exist, handle error */ } if (!S_ISREG(lstat_info.st_mode)) { /* file is a symlink, handle error */ } if ((f = open(filename, flags)) == -1) { /* file has disappeared, handle error */ } if (fstat(f, &fstat_info) == -1) { /* handle error */ } if (lstat_info.st_ino != fstat_info.st_ino || lstat_info.st_dev != fstat_info.st_dev) { /* open file is not non-symlink file, handle error */ } /* f is true open file, and file was not symlink */ return f; } |
Risk Assessment
TOCTOU race conditions can result in unexpected behavior, including privilege escalation.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
FIO45-C | High | Probable | High | P6 | L2 |
Bibliography
[Seacord 2013] | Chapter 7, "Files" |
...