Race Condition Enabling Link Following

Draft Base
Structure: Simple
Description

This vulnerability occurs when a program checks a file's status before using it, creating a brief window where an attacker can replace that file with a malicious link. This causes the program to follow the link and access an unintended, potentially dangerous location.

Extended Description

Developers often assume the gap between checking a file and using it is too small to exploit, but this race condition is very real. Attackers can widen this window by slowing the system down—for example, by consuming memory—or simply by launching repeated attacks until one succeeds. This flaw is particularly dangerous because it bypasses intended security checks. The program verifies a legitimate file, but in that instant, an attacker swaps it for a link pointing to a sensitive system file or a malicious payload, tricking the application into performing unauthorized actions.

Common Consequences 1
Scope: ConfidentialityIntegrity

Impact: Read Files or DirectoriesModify Files or Directories

Demonstrative Examples 1

ID : DX-49

This code prints the contents of a file if a user has permission.

Code Example:

Bad
PHP
php

//resolve file if its a symbolic link* if(is_link($filename)){ ``` $filename = readlink($filename); } if(fileowner($filename) == $user){ echo file_get_contents($realFile); return; } else{ echo 'Access denied'; return false; } }

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 (Insufficient Logging).
References 1
The Art of Software Security Assessment
Mark Dowd, John McDonald, and Justin Schuh
Addison Wesley
2006
ID: REF-62
Applicable Platforms
Languages:
Not Language-Specific : Undetermined
Modes of Introduction
Architecture and Design
Implementation
Related Attack Patterns
Taxonomy Mapping
  • PLOVER
  • CERT C Secure Coding
  • Software Fault Patterns
Notes
RelationshipThis is already covered by the "Link Following" weakness (Improper Link Resolution Before File Access ('Link Following')). It is included here because so many people associate race conditions with link problems; however, not all link following issues involve race conditions.