{"@ID": "367", "@Name": "Time-of-check Time-of-use (TOCTOU) Race Condition", "@Abstraction": "Base", "@Structure": "Simple", "@Status": "Incomplete", "@Diagram": "/data/images/CWE-367-Diagram.png", "Description": "The product checks the state of a resource before using that resource, but the resource's state can change between the check and the use in a way that invalidates the results of the check.", "Related_Weaknesses": {"Related_Weakness": [{"@Nature": "ChildOf", "@CWE_ID": "362", "@View_ID": "1000", "@Ordinal": "Primary"}, {"@Nature": "ChildOf", "@CWE_ID": "362", "@View_ID": "1003", "@Ordinal": "Primary"}]}, "Weakness_Ordinalities": {"Weakness_Ordinality": {"Ordinality": "Primary"}}, "Applicable_Platforms": {"Language": {"@Class": "Not Language-Specific", "@Prevalence": "Undetermined"}}, "Alternate_Terms": {"Alternate_Term": [{"Term": "TOCTTOU", "Description": "The TOCTTOU acronym expands to \"Time Of Check To Time Of Use\"."}, {"Term": "TOCCTOU", "Description": "The TOCCTOU acronym is most likely a typo of TOCTTOU, but it has been used in some influential documents, so the typo is repeated fairly frequently."}]}, "Modes_Of_Introduction": {"Introduction": {"Phase": "Implementation", "Note": "This weakness can be security-relevant when an attacker can influence the state of the resource between check and use. This can happen with shared resources such as files, memory, or even variables in multithreaded programs."}}, "Likelihood_Of_Exploit": "Medium", "Common_Consequences": {"Consequence": [{"Scope": ["Integrity", "Other"], "Impact": ["Alter Execution Logic", "Unexpected State"], "Note": "The attacker can gain access to otherwise unauthorized resources."}, {"Scope": ["Integrity", "Other"], "Impact": ["Modify Application Data", "Modify Files or Directories", "Modify Memory", "Other"], "Note": "Race conditions such as this kind may be employed to gain read or write access to resources which are not normally readable or writable by the user in question."}, {"Scope": ["Integrity", "Other"], "Impact": "Other", "Note": "The resource in question, or other resources (through the corrupted one), may be changed in undesirable ways by a malicious user."}, {"Scope": "Non-Repudiation", "Impact": "Hide Activities", "Note": "If a file or other resource is written in this method, as opposed to in a valid way, logging of the activity may not occur."}, {"Scope": ["Non-Repudiation", "Other"], "Impact": "Other", "Note": "In some cases it may be possible to delete files a malicious user might not otherwise have access to, such as log files."}, {"Scope": "Other", "Impact": "Unexpected State", "Note": "The product may perform invalid actions when the resource is in an unexpected state."}]}, "Detection_Methods": {"Detection_Method": {"@Detection_Method_ID": "DM-14", "Method": "Automated Static Analysis", "Description": "Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect \"sources\" (origins of input) with \"sinks\" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)", "Effectiveness": "High"}}, "Potential_Mitigations": {"Mitigation": [{"Phase": "Implementation", "Description": "The most basic advice for TOCTOU vulnerabilities is to not perform a check before the use. This does not resolve the underlying issue of the execution of a function on a resource whose state and identity cannot be assured, but it does help to limit the false sense of security given by the check."}, {"Phase": "Implementation", "Description": "When the file being altered is owned by the current user and group, set the effective gid and uid to that of the current user and group when executing this statement."}, {"Phase": "Architecture and Design", "Description": "Limit the interleaving of operations on files from multiple processes."}, {"Phase": ["Implementation", "Architecture and Design"], "Description": "If you cannot perform operations atomically and you must share access to the resource between multiple processes or threads, then try to limit the amount of time (CPU cycles) between the check and use of the resource. This will not fix the problem, but it could make it more difficult for an attack to succeed."}, {"Phase": "Implementation", "Description": "Recheck the resource after the use call to verify that the action was taken appropriately."}, {"Phase": "Architecture and Design", "Description": "Ensure that some environmental locking mechanism can be used to protect resources effectively."}, {"Phase": "Implementation", "Description": "Ensure that locking occurs before the check, as opposed to afterwards, such that the resource, as checked, is the same as it is when in use."}]}, "Demonstrative_Examples": {"Demonstrative_Example": [{"Intro_Text": "The following code checks a file, then updates its contents.", "Example_Code": {"@Nature": "Bad", "@Language": "C", "xhtml:div": {"xhtml:br": [null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "print(\"Now updating things\\n\");updateThings();"}, "#text": "struct stat *sb;...lstat(\"...\",sb); // it has not been updated since the last time it was readprintf(\"stated file\\n\");if (sb->st_mtimespec==...){}"}}, "Body_Text": "Potentially the file could have been updated between the time of the check and the lstat, especially since the printf has latency."}, {"Intro_Text": "The following code is from a program installed setuid root. The program performs certain file operations on behalf of non-privileged users, and uses access checks to ensure that it does not use its root privileges to perform operations that should otherwise be unavailable the current user. The program uses the access() system call to check if the person running the program has permission to access the specified file before it opens the file and performs the necessary operations.", "Example_Code": {"@Nature": "Bad", "@Language": "C", "xhtml:div": {"xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": [null, null], "#text": "f = fopen(file,\"w+\");operate(f);..."}, {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:br": null, "#text": "fprintf(stderr,\"Unable to open file %s.\\n\",file);"}}], "xhtml:br": null, "#text": "if(!access(file,W_OK)) {}else {}"}}, "Body_Text": "The call to access() behaves as expected, and returns 0 if the user running the program has the necessary permissions to write to the file, and -1 otherwise. However, because both access() and fopen() operate on filenames rather than on file handles, there is no guarantee that the file variable still refers to the same file on disk when it is passed to fopen() that it did when it was passed to access(). If an attacker replaces file after the call to access() with a symbolic link to a different file, the program will use its root privileges to operate on the file even if it is a file that the attacker would otherwise be unable to modify. By tricking the program into performing an operation that would otherwise be impermissible, the attacker has gained elevated privileges. This type of vulnerability is not limited to programs with root privileges. If the application is capable of performing any operation that the attacker would not otherwise be allowed perform, then it is a possible target."}, {"@Demonstrative_Example_ID": "DX-49", "Intro_Text": "This code prints the contents of a file if a user has permission.", "Example_Code": {"@Nature": "Bad", "@Language": "PHP", "xhtml:div": {"xhtml:div": {"@style": "margin-left:1em;", "xhtml:div": {"xhtml:br": [null, null, null, null, null, null], "xhtml:i": "//resolve file if its a symbolic link", "xhtml:div": [{"@style": "margin-left:1em;", "#text": "$filename = readlink($filename);"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "echo file_get_contents($realFile);return;"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "echo 'Access denied';return false;"}], "#text": "$user = getCurrentUser();\n                           \n                           \n                           if(is_link($filename)){}\n                           if(fileowner($filename) == $user){}else{}"}}, "#text": "function readFile($filename){}"}}, "Body_Text": "This code attempts to resolve symbolic links before checking the file and printing its contents. However, an attacker may be able to change the file from a real file to a symbolic link between the calls to is_link() and file_get_contents(), allowing the reading of arbitrary files. Note that this code fails to log the attempted access (CWE-778)."}, {"Intro_Text": "This example is adapted from [REF-18]. Assume that this code block is invoked from multiple threads. The switch statement will execute different code depending on the time when MYFILE.txt was last changed.", "Example_Code": {"@Nature": "Bad", "@Language": "C", "xhtml:div": {"xhtml:br": [null, null, null, null, null, null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": [null, null, null], "#text": "case 0: printf(\"Option 1\\n\"); break;\n\t\t        case 1: printf(\"Option 2\\n\"); break;\n\t\t        default: printf(\"this should be unreachable?\\n\"); break;"}, "#text": "#include <sys/types.h>\n\t\t    #include <sys/stat.h>\n\t\t    \n\t\t    ...\n\t\t    \n\t\t      struct stat sb;\n\t\t      stat(\"MYFILE.txt\",&sb);\n\t\t      printf(\"file change time: %d\\n\",sb->st_ctime);\n\t\t      switch(sb->st_ctime % 2){}"}}, "Body_Text": "If this code block were executed within multiple threads, and MYFILE.txt changed between the operation of one thread and another, then the switch could produce different, possibly unexpected results."}]}, "Observed_Examples": {"Observed_Example": [{"Reference": "CVE-2015-1743", "Description": "TOCTOU in sandbox process allows installation of untrusted browser add-ons by replacing a file after it has been verified, but before it is executed", "Link": "https://www.cve.org/CVERecord?id=CVE-2015-1743"}, {"Reference": "CVE-2003-0813", "Description": "Chain: A multi-threaded race condition (CWE-367) allows attackers to cause two threads to process the same RPC request, which causes a use-after-free (CWE-416) in one thread", "Link": "https://www.cve.org/CVERecord?id=CVE-2003-0813"}, {"Reference": "CVE-2004-0594", "Description": "PHP flaw allows remote attackers to execute arbitrary code by aborting execution before the initialization of key data structures is complete.", "Link": "https://www.cve.org/CVERecord?id=CVE-2004-0594"}, {"Reference": "CVE-2008-2958", "Description": "chain: time-of-check time-of-use (TOCTOU) race condition in program allows bypass of protection mechanism that was designed to prevent symlink attacks.", "Link": "https://www.cve.org/CVERecord?id=CVE-2008-2958"}, {"Reference": "CVE-2008-1570", "Description": "chain: time-of-check time-of-use (TOCTOU) race condition in program allows bypass of protection mechanism that was designed to prevent symlink attacks.", "Link": "https://www.cve.org/CVERecord?id=CVE-2008-1570"}]}, "Affected_Resources": {"Affected_Resource": "File or Directory"}, "Taxonomy_Mappings": {"Taxonomy_Mapping": [{"@Taxonomy_Name": "PLOVER", "Entry_Name": "Time-of-check Time-of-use race condition"}, {"@Taxonomy_Name": "7 Pernicious Kingdoms", "Entry_Name": "File Access Race Conditions: TOCTOU"}, {"@Taxonomy_Name": "CLASP", "Entry_Name": "Time of check, time of use race condition"}, {"@Taxonomy_Name": "CLASP", "Entry_Name": "Race condition in switch"}, {"@Taxonomy_Name": "CERT C Secure Coding", "Entry_ID": "FIO01-C", "Entry_Name": "Be careful using functions that use file names for identification"}, {"@Taxonomy_Name": "Software Fault Patterns", "Entry_ID": "SFP20", "Entry_Name": "Race Condition Window"}]}, "Related_Attack_Patterns": {"Related_Attack_Pattern": [{"@CAPEC_ID": "27"}, {"@CAPEC_ID": "29"}]}, "References": {"Reference": [{"@External_Reference_ID": "REF-18", "@Section": "5.4.6 Race condition in switch"}, {"@External_Reference_ID": "REF-367"}, {"@External_Reference_ID": "REF-44", "@Section": "\"Sin 13: Race Conditions.\" Page 205"}, {"@External_Reference_ID": "REF-62", "@Section": "Chapter 9, \"TOCTOU\", Page 527"}]}, "Mapping_Notes": {"Usage": "Allowed", "Rationale": "This CWE entry is at the Base level of abstraction, which is a preferred level of abstraction for mapping to the root causes of vulnerabilities.", "Comments": "Carefully read both the name and description to ensure that this mapping is an appropriate fit. Do not try to 'force' a mapping to a lower-level Base/Variant simply to comply with this preferred level of abstraction.", "Reasons": {"Reason": {"@Type": "Acceptable-Use"}}}, "Notes": {"Note": [{"@Type": "Relationship", "#text": "TOCTOU issues do not always involve symlinks, and not every symlink issue is a TOCTOU problem."}, {"@Type": "Research Gap", "#text": "Non-symlink TOCTOU issues are not reported frequently, but they are likely to occur in code that attempts to be secure."}]}, "Content_History": {"Submission": {"Submission_Name": "PLOVER", "Submission_Date": "2006-07-19", "Submission_Version": "Draft 3", "Submission_ReleaseDate": "2006-07-19"}, "Modification": [{"Modification_Name": "Eric Dalci", "Modification_Organization": "Cigital", "Modification_Date": "2008-07-01", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Time_of_Introduction"}, {"Modification_Organization": "KDM Analytics", "Modification_Date": "2008-08-01", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "added/updated white box definitions"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-09-08", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Common_Consequences, Relationships, Other_Notes, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-10-14", "Modification_Version": "1.0.1", "Modification_ReleaseDate": "2008-10-14", "Modification_Comment": "updated Description, Name, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-11-24", "Modification_Version": "1.1", "Modification_ReleaseDate": "2008-11-25", "Modification_Comment": "updated Relationships, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-01-12", "Modification_Version": "1.2", "Modification_ReleaseDate": "2009-01-12", "Modification_Comment": "updated Alternate_Terms, Observed_Examples, Other_Notes, References, Relationship_Notes, Relationships, Research_Gaps"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-05-27", "Modification_Version": "1.4", "Modification_ReleaseDate": "2009-05-27", "Modification_Comment": "updated Demonstrative_Examples"}, {"Modification_Name": "KDM Analytics", "Modification_Date": "2009-07-17", "Modification_Version": "1.5", "Modification_ReleaseDate": "2009-07-27", "Modification_Comment": "Improved the White_Box_Definition"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-07-27", "Modification_Version": "1.5", "Modification_ReleaseDate": "2009-07-27", "Modification_Comment": "updated White_Box_Definitions"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2010-09-27", "Modification_Version": "1.10", "Modification_ReleaseDate": "2010-09-27", "Modification_Comment": "updated Description, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2010-12-13", "Modification_Version": "1.11", "Modification_ReleaseDate": "2010-12-13", "Modification_Comment": "updated Alternate_Terms, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-06-01", "Modification_Version": "1.13", "Modification_ReleaseDate": "2011-06-01", "Modification_Comment": "updated Common_Consequences"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-06-27", "Modification_Version": "2.0", "Modification_ReleaseDate": "2011-06-27", "Modification_Comment": "updated Common_Consequences"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-09-13", "Modification_Version": "2.1", "Modification_ReleaseDate": "2011-09-13", "Modification_Comment": "updated Relationships, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2012-05-11", "Modification_Version": "2.2", "Modification_ReleaseDate": "2012-05-15", "Modification_Comment": "updated Demonstrative_Examples, Observed_Examples, References, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2012-10-30", "Modification_Version": "2.3", "Modification_ReleaseDate": "2012-10-30", "Modification_Comment": "updated Potential_Mitigations"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2014-07-30", "Modification_Version": "2.8", "Modification_ReleaseDate": "2014-07-31", "Modification_Comment": "updated Demonstrative_Examples, Relationships, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2017-11-08", "Modification_Version": "3.0", "Modification_ReleaseDate": "2017-11-08", "Modification_Comment": "updated Applicable_Platforms, Demonstrative_Examples, Likelihood_of_Exploit, References, Relationships, Taxonomy_Mappings, White_Box_Definitions"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2019-06-20", "Modification_Version": "3.3", "Modification_ReleaseDate": "2019-06-20", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2020-02-24", "Modification_Version": "4.0", "Modification_ReleaseDate": "2020-02-24", "Modification_Comment": "updated References, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2022-04-28", "Modification_Version": "4.7", "Modification_ReleaseDate": "2022-04-28", "Modification_Comment": "updated Demonstrative_Examples, References, Relationships, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2022-06-28", "Modification_Version": "4.8", "Modification_ReleaseDate": "2022-06-28", "Modification_Comment": "updated Observed_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-01-31", "Modification_Version": "4.10", "Modification_ReleaseDate": "2023-01-31", "Modification_Comment": "updated Description"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-04-27", "Modification_Version": "4.11", "Modification_ReleaseDate": "2023-04-27", "Modification_Comment": "updated Detection_Factors, References, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-06-29", "Modification_Version": "4.12", "Modification_ReleaseDate": "2023-06-29", "Modification_Comment": "updated Mapping_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-04-03", "Modification_Version": "4.17", "Modification_ReleaseDate": "2025-04-03", "Modification_Comment": "updated Affected_Resources, Observed_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-09-09", "Modification_Version": "4.18", "Modification_ReleaseDate": "2025-09-09", "Modification_Comment": "updated Common_Consequences, Description, Diagram, Modes_of_Introduction"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-12-11", "Modification_Version": "4.19", "Modification_ReleaseDate": "2025-12-11", "Modification_Comment": "updated Weakness_Ordinalities"}], "Previous_Entry_Name": {"@Date": "2008-10-14", "#text": "Time-of-check Time-of-use Race Condition"}}}
