CWE-763 对无效指针或索引的释放

admin 2021年11月8日20:02:30评论85 views字数 3721阅读12分24秒阅读模式

CWE-763 对无效指针或索引的释放

Release of Invalid Pointer or Reference

结构: Simple

Abstraction: Base

状态: Incomplete

被利用可能性: unkown

基本描述

The application attempts to return a memory resource to the system, but calls the wrong release function or calls the appropriate release function incorrectly.

扩展描述

This weakness can take several forms, such as:

相关缺陷

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

  • cwe_Nature: ChildOf cwe_CWE_ID: 404 cwe_View_ID: 1003 cwe_Ordinal: Primary

常见的影响

范围 影响 注释
['Integrity', 'Availability', 'Confidentiality'] ['Modify Memory', 'DoS: Crash, Exit, or Restart', 'Execute Unauthorized Code or Commands'] This weakness may result in the corruption of memory, and perhaps instructions, possibly leading to a crash. If the corrupted memory can be effectively controlled, it may be possible to execute arbitrary code.

可能的缓解方案

Implementation

策略:

Only call matching memory management functions. Do not mix and match routines. For example, when you allocate a buffer with malloc(), dispose of the original pointer with free().

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.

示例代码

This code attempts to tokenize a string and place it into an array using the strsep function, which inserts a byte in place of whitespace or a tab character. After finishing the loop, each string in the AP array points to a location within the input string.

bad C

char ap, argv[10], inputstring;
for (ap = argv; (*ap = strsep(&inputstring, " t")) != NULL;)

if (ap != '')

if (++ap >= &argv[10])

break;

/.../
free(ap[4]);

Since strsep is not allocating any new memory, freeing an element in the middle of the array is equivalent to free a pointer in the middle of inputstring.

This example allocates a BarObj object using the new operator in C++, however, the programmer then deallocates the object using free(), which may lead to unexpected behavior.

bad C++

void foo(){

BarObj ptr = new BarObj()
/
do some work with ptr here */

...

free(ptr);

}

Instead, the programmer should have either created the object with one of the malloc family functions, or else deleted the object with the delete operator.

good C++

void foo(){

BarObj ptr = new BarObj()
/
do some work with ptr here */

...

delete ptr;

}

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 SUCCESS (1)
#define FAILURE (0)

int contains_char(char c){

char str;
str = (char
)malloc(20sizeof(char));
strcpy(str, "Search Me!");
while(
str != NULL){

if( str == c ){


/ matched char, free string and return success /

free(str);
return SUCCESS;

}
/
didn't match yet, increment pointer and try next char /

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 SUCCESS (1)
#define FAILURE (0)

int cointains_char(char c){

char str;
int i = 0;
str = (char
)malloc(20sizeof(char));
strcpy(str, "Search Me!");
while( i

if( str[i] == c ){


/ 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中文网

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年11月8日20:02:30
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CWE-763 对无效指针或索引的释放http://cn-sec.com/archives/613616.html

发表评论

匿名网友 填写信息