CWE-121 栈缓冲区溢出

Stack-based Buffer Overflow

结构: Simple

Abstraction: Variant

状态: Draft

被利用可能性: High

基本描述

A stack-based buffer overflow condition is a condition where the buffer being overwritten is allocated on the stack (i.e., is a local variable or, rarely, a parameter to a function).

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 788 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 788 cwe_View_ID: 699 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 787 cwe_View_ID: 1000

  • cwe_Nature: ChildOf cwe_CWE_ID: 787 cwe_View_ID: 699

适用平台

Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}]

常见的影响

范围 影响 注释
Availability ['DoS: Crash, Exit, or Restart', 'DoS: Resource Consumption (CPU)', 'DoS: Resource Consumption (Memory)'] Buffer overflows generally lead to crashes. Other attacks leading to lack of availability are possible, including putting the program into an infinite loop.
['Integrity', 'Confidentiality', 'Availability', 'Access Control'] ['Execute Unauthorized Code or Commands', 'Bypass Protection Mechanism'] Buffer overflows often can be used to execute arbitrary code, which is usually outside the scope of a program's implicit security policy.
['Integrity', 'Confidentiality', 'Availability', 'Access Control', 'Other'] ['Execute Unauthorized Code or Commands', 'Bypass Protection Mechanism', 'Other'] When the consequence is arbitrary code execution, this can often be used to subvert any other security service.

可能的缓解方案

MIT-10 Build and Compilation

策略: Compilation or Build Hardening

Run or compile the software using features or extensions that automatically provide a protection mechanism that mitigates or eliminates buffer overflows. For example, certain compilers and extensions provide automatic buffer overflow detection mechanisms that are built into the compiled code. Examples include the Microsoft Visual Studio /GS flag, Fedora/Red Hat FORTIFY_SOURCE GCC flag, StackGuard, and ProPolice.

Architecture and Design

策略:

Use an abstraction library to abstract away risky APIs. Not a complete solution.

Build and Compilation

策略:

Compiler-based canary mechanisms such as StackGuard, ProPolice and the Microsoft Visual Studio /GS flag. Unless this provides automatic bounds checking, it is not a complete solution.

Implementation

策略:

Implement and perform bounds checking on input.

Implementation

策略:

Do not use dangerous functions such as gets. Use safer, equivalent functions which check for boundary errors.

Operation

策略:

Use OS-level preventative functionality, such as ASLR. This is not a complete solution.

示例代码

While buffer overflow examples can be rather complex, it is possible to have very simple, yet still exploitable, stack-based buffer overflows:

bad C

#define BUFSIZE 256
int main(int argc, char **argv) {
char buf[BUFSIZE];
strcpy(buf, argv[1]);
}

The buffer size is fixed, but there is no guarantee the string in argv[1] will not exceed this size and cause an overflow.

This example takes an IP address from a user, verifies that it is well formed and then looks up the hostname and copies it into a buffer.

bad C

void host_lookup(char user_supplied_addr){
struct hostent hp;
in_addr_t addr;
char hostname[64];
in_addr_t inet_addr(const char
cp);

/routine that ensures user_supplied_addr is in the right format for conversion /

validate_addr_form(user_supplied_addr);
addr = inet_addr(user_supplied_addr);
hp = gethostbyaddr( addr, sizeof(struct in_addr), AF_INET);
strcpy(hostname, hp->h_name);
}

This function allocates a buffer of 64 bytes to store the hostname, however there is no guarantee that the hostname will not be larger than 64 bytes. If an attacker specifies an address which resolves to a very large hostname, then we may overwrite sensitive data or even relinquish control flow to the attacker.

Note that this example also contains an unchecked return value (CWE-252) that can lead to a NULL pointer dereference (CWE-476).

Notes

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
CLASP Stack overflow
Software Fault Patterns SFP8 Faulty Buffer Access
CERT C Secure Coding ARR38-C Imprecise Guarantee that library functions do not form invalid pointers
CERT C Secure Coding STR31-C CWE More Specific Guarantee that storage for strings has sufficient space for character data and the null terminator

引用