CWE-761 释放一个不在缓冲区起始位置的指针
Free of Pointer not at Start of Buffer
结构: Simple
Abstraction: Variant
状态: Incomplete
被利用可能性: unkown
基本描述
The application calls free() on a pointer to a memory resource that was allocated on the heap, but the pointer is not at the start of the buffer.
扩展描述
This can cause the application to crash, or in some cases, modify critical program variables or execute code.
This weakness often occurs when the memory is allocated explicitly on the heap with one of the malloc() family functions and free() is called, but pointer arithmetic has caused the pointer to be in the interior or end of the buffer.
相关缺陷
常见的影响
范围 | 影响 | 注释 |
---|---|---|
['Integrity', 'Availability', 'Confidentiality'] | ['Modify Memory', 'DoS: Crash, Exit, or Restart', 'Execute Unauthorized Code or Commands'] |
可能的缓解方案
Implementation
策略:
When utilizing pointer arithmetic to traverse a buffer, use a separate variable to track progress through memory and preserve the originally allocated address for later freeing.
Implementation
策略:
When programming in C++, consider using smart pointers provided by the boost library to help correctly and consistently manage memory.
MIT-4.6 Architecture and Design
策略: Libraries or Frameworks
Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid.
For example, glibc in Linux provides protection against free of invalid pointers.
Architecture and Design
策略:
Use a language that provides abstractions for memory allocation and deallocation.
Testing
策略:
Use a tool that dynamically detects memory management problems, such as valgrind.
示例代码
例
In this example, the programmer dynamically allocates a buffer to hold a string and then searches for a specific character. After completing the search, the programmer attempts to release the allocated memory and return SUCCESS or FAILURE to the caller. Note: for simplification, this example uses a hard-coded "Search Me!" string and a constant string length of 20.
bad C
#define FAILURE (0)
int contains_char(char c){
str = (char)malloc(20sizeof(char));
strcpy(str, "Search Me!");
while( str != NULL){
/ matched char, free string and return success /
free(str);
return SUCCESS;
}
/
str = str + 1;
}
/ we did not match the char in the string, free mem and return failure */
free(str);
return FAILURE;
}
However, if the character is not at the beginning of the string, or if it is not in the string at all, then the pointer will not be at the start of the buffer when the programmer frees it.
Instead of freeing the pointer in the middle of the buffer, the programmer can use an indexing pointer to step through the memory or abstract the memory calculations by using array indexing.
good C
#define FAILURE (0)
int cointains_char(char c){
int i = 0;
str = (char)malloc(20sizeof(char));
strcpy(str, "Search Me!");
while( i
/ matched char, free string and return success /
free(str);
return SUCCESS;
}
/ didn't match yet, increment pointer and try next char /
i = i + 1;
}
/ we did not match the char in the string, free mem and return failure */
free(str);
return FAILURE;
}文章来源于互联网:scap中文网
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论