Race Condition Enabling Link Following

The product checks the status of a file or directory before accessing it, which produces a race condition in which the file can be replaced with a link before the access is performed, causing the product to access the wrong file.


Description

While developers might expect that there is a very narrow time window between the time of check and time of use, there is still a race condition. An attacker could cause the product to slow down (e.g. with memory consumption), causing the time window to become larger. Alternately, in some situations, the attacker could win the race by performing a large number of attacks.

Demonstrations

The following examples help to illustrate the nature of this weakness and describe methods or techniques which can be used to mitigate the risk.

Note that the examples here are by no means exhaustive and any given weakness may have many subtle varieties, each of which may require different detection methods or runtime controls.

Example One

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

function readFile($filename){

  $user = getCurrentUser();

  //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 (CWE-778).

See Also

Comprehensive Categorization: Concurrency

Weaknesses in this category are related to concurrency.

SEI CERT C Coding Standard - Guidelines 50. POSIX (POS)

Weaknesses in this category are related to the rules and recommendations in the POSIX (POS) section of the SEI CERT C Coding Standard.

SFP Secondary Cluster: Race Condition Window

This category identifies Software Fault Patterns (SFPs) within the Race Condition Window cluster (SFP20).

Comprehensive CWE Dictionary

This view (slice) covers all the elements in CWE.

Weaknesses Introduced During Implementation

This view (slice) lists weaknesses that can be introduced during implementation.

Weaknesses Introduced During Design

This view (slice) lists weaknesses that can be introduced during design.


Common Weakness Enumeration content on this website is copyright of The MITRE Corporation unless otherwise specified. Use of the Common Weakness Enumeration and the associated references on this website are subject to the Terms of Use as specified by The MITRE Corporation.