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.
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.
Potential Impact
Integrity, Confidentiality, Availability
Modify Memory, Execute Unauthorized Code or Commands, DoS: Crash, Exit, or Restart
Demonstrative Examples
char *createOutputDirectory(char *name) {
char outputDirectoryName[128];if (getCurrentDirectory(128, outputDirectoryName) == 0) {return null;}if (!PathAppend(outputDirectoryName, "output")) {return null;}if (!PathAppend(outputDirectoryName, name)) {
return null;
}if (SHCreateDirectoryEx(NULL, outputDirectoryName, NULL) != ERROR_SUCCESS) {
return null;
}return StrDup(outputDirectoryName);
}Mitigations & Prevention
Always specify output buffers large enough to handle the maximum-size possible result from path manipulation functions.
Detection Methods
- Automated Static Analysis — 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 sea
Related Weaknesses
Taxonomy Mappings
- 7 Pernicious Kingdoms: — Often Misused: File System
- Software Fault Patterns: SFP9 — Faulty String Expansion
Frequently Asked Questions
What is CWE-785?
CWE-785 (Use of Path Manipulation Function without Maximum-sized Buffer) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. 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.
How can CWE-785 be exploited?
Attackers can exploit CWE-785 (Use of Path Manipulation Function without Maximum-sized Buffer) to modify memory, execute unauthorized code or commands, dos: crash, exit, or restart. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-785?
Key mitigations include: Always specify output buffers large enough to handle the maximum-size possible result from path manipulation functions.
What is the severity of CWE-785?
CWE-785 is classified as a Variant-level weakness (Low-Medium abstraction). Its actual severity depends on the specific context and how the weakness manifests in your application.