JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++
Binaries


IO.RACE : File System Race Condition

Summary

A File System Race Condition vulnerability occurs when a program calls a function that checks a named file and then later calls a function that uses the same named file. The source code assumes the file is the same at both times, when in fact another process may have changed the file between the 'check' and 'use'. For example, an attacker could replace the original file with a link to a file containing confidential data.

Properties

Class Name File System Race Condition
Significance security
Mnemonic IO.RACE
Categories
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:367 Time-of-check Time-of-use (TOCTOU) Race Condition
CERT-C CERT-C:FIO01-C Be careful using functions that use file names for identification
  CERT-C:FIO24-C Do not open a file that is already open
  CERT-C:FIO45-C Avoid TOCTOU race conditions while accessing files
DISA-6r1 DISA-6r1:V-222567 The application must not be vulnerable to race conditions.
  DISA-6r1:V-222612 The application must not be vulnerable to overflow attacks.
DISA-5r3 DISA-5r3:V-70185 The application must not be vulnerable to race conditions.
  DISA-5r3:V-70277 The application must not be vulnerable to overflow attacks.
DISA-4r3 DISA-4r3:V-70185 The application must not be vulnerable to race conditions.
  DISA-4r3:V-70277 The application must not be vulnerable to overflow attacks.
DISA-3r10 DISA-3r10:V-16804 The designer will ensure the application does not rely solely on a resource name to control access to a resource.
  DISA-3r10:V-16815 The designer will ensure the application is not vulnerable to race conditions.
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="File System Race Condition"

Triggering Functions

CodeSonar ships with library models that allow it to recognize a large number of functions that take a file name or directory name argument. Some examples are shown in the table below. If a file/directory name is passed to one of these functions (the check) and later the same name is passed to another of these functions (the use), a warning will be issued.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering File System Race Condition warnings.

Functions that can trigger warnings include...
gcc Builtins __builtin_execl(), __builtin_execv(),
libc chmod(), open(), stat()
Win32 CanShareFolderW(), PathMakeUniqueName(), freopen_s()

Example

TOCTTOU vulnerabilities in source code provide a window of opportunity for attackers to manipulate the file system in order to gain unauthorized access. For example, the following use of unlink before open is vulnerable to attack:

#include <fcntl.h>
#include <unistd.h>

void tocttou(const void *secret, size_t secret_size){
    int fdesc;
    /* choose a filename for recording sensitive data */
    char *fname = "myfile.txt";
    /* unlink to ensure that fname isn't already in use by someone else */
    unlink(fname);
    /* DANGER */

    /* open the file and write the data to it */
    fdesc = open(fname, O_CREAT|O_RDWR);           /* 'File System Race Condition' warning issued here */
    if (fdesc < 0){return;}
    (void) write(fdesc, secret, secret_size);
    (void) close(fdesc);
}

In the interval marked /* DANGER */ an adversary could create a file named myfile.txt, thus obtaining ownership of the file and access to its contents. To cause further problems, they could symlink myfile.txt to /etc/passwd, causing the original program to corrupt the system password file by writing secret on top of it.

Many TOCTTOU vulnerabilities arise because attackers can take advantage of the software's use of named files. This can often be avoided by referring to file descriptors or file streams rather than to file names.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.