Incomplete Identification of Uploaded File Variables (PHP)

Incomplete Variant
Structure: Simple
Description

The PHP application uses an old method for processing uploaded files by referencing the four global variables that are set for each file (e.g. $varname, $varname_size, $varname_name, $varname_type). These variables could be overwritten by attackers, causing the application to process unauthorized files.

Extended Description

These global variables could be overwritten by POST requests, cookies, or other methods of populating or overwriting these variables. This could be used to read or process arbitrary files by providing values such as "/etc/passwd".

Common Consequences 1
Scope: ConfidentialityIntegrity

Impact: Read Files or DirectoriesModify Files or Directories

Potential Mitigations 3
Phase: Architecture and Design
Use PHP 4 or later.
Phase: Architecture and Design
If you must support older PHP versions, write your own version of is_uploaded_file() and run it against $HTTP_POST_FILES['userfile']))
Phase: Implementation
For later PHP versions, reference uploaded files using the $HTTP_POST_FILES or $_FILES variables, and use is_uploaded_file() or move_uploaded_file() to ensure that you are dealing with an uploaded file.
Demonstrative Examples 2
As of 2006, the "four globals" method is probably in sharp decline, but older PHP applications could have this issue.
In the "four globals" method, PHP sets the following 4 global variables (where "varname" is application-dependent):

Code Example:

Bad
PHP
php
"The global $_FILES exists as of PHP 4.1.0 (Use $HTTP_POST_FILES instead if using an earlier version). These arrays will contain all the uploaded file information."

Code Example:

Bad
PHP
php
** note: 'userfile' is the field name from the web form; this can vary.
Observed Examples 3
CVE-2002-1460Forum does not properly verify whether a file was uploaded or if the associated variables were set by POST, allowing remote attackers to read arbitrary files.
CVE-2002-1759Product doesn't check if the variables for an upload were set by uploading the file, or other methods such as $_POST.
CVE-2002-1710Product does not distinguish uploaded file from other files.
References 1
A Study in Scarlet - section 5, "File Upload"
Shaun Clowes
ID: REF-502
Applicable Platforms
Languages:
PHP : Undetermined
Modes of Introduction
Implementation
Taxonomy Mapping
  • PLOVER
  • Software Fault Patterns