Use of Path Manipulation Function without Maximum-sized Buffer

Incomplete Variant
Structure: Simple
Description

The product invokes a function for normalizing paths or file names, but it provides an output buffer that is smaller than the maximum possible size, such as PATH_MAX.

Extended Description

Passing an inadequately-sized output buffer to a path manipulation function can result in a buffer overflow. Such functions include realpath(), readlink(), PathAppend(), and others.

Common Consequences 1
Scope: IntegrityConfidentialityAvailability

Impact: Modify MemoryExecute Unauthorized Code or CommandsDoS: Crash, Exit, or Restart

Potential Mitigations 1
Phase: Implementation
Always specify output buffers large enough to handle the maximum-size possible result from path manipulation functions.
Demonstrative Examples 1
In this example the function creates a directory named "output\<name>" in the current directory and returns a heap-allocated copy of its name.

Code Example:

Bad
C
c
For most values of the current directory and the name parameter, this function will work properly. However, if the name parameter is particularly long, then the second call to PathAppend() could overflow the outputDirectoryName buffer, which is smaller than MAX_PATH bytes.
References 1
Seven Pernicious Kingdoms: A Taxonomy of Software Security Errors
Katrina Tsipenyuk, Brian Chess, and Gary McGraw
NIST Workshop on Software Security Assurance Tools Techniques and MetricsNIST
07-11-2005
ID: REF-6
Applicable Platforms
Languages:
C : UndeterminedC++ : Undetermined
Modes of Introduction
Implementation
Affected Resources
  1. Memory
  2. File or Directory
Taxonomy Mapping
  • 7 Pernicious Kingdoms
  • Software Fault Patterns
Notes
MaintenanceThis entry is at a much lower level of abstraction than most entries because it is function-specific. It also has significant overlap with other entries that can vary depending on the perspective. For example, incorrect usage could trigger either a stack-based overflow (Stack-based Buffer Overflow) or a heap-based overflow (Heap-based Buffer Overflow). The CWE team has not decided how to handle such entries.