Description
The product attempts to return a memory resource to the system, but it calls the wrong release function or calls the appropriate release function incorrectly.
This weakness can take several forms, such as:
Potential Impact
Integrity, Availability, Confidentiality
Modify Memory, DoS: Crash, Exit, or Restart, Execute Unauthorized Code or Commands
Demonstrative Examples
char **ap, *argv[10], *inputstring;for (ap = argv; (*ap = strsep(&inputstring, " \t")) != NULL;)
if (**ap != '\0')if (++ap >= &argv[10])break;
/.../free(ap[4]);void foo(){
BarObj *ptr = new BarObj()
/* do some work with ptr here */
...
free(ptr);
}void foo(){
BarObj *ptr = new BarObj()
/* do some work with ptr here */
...
delete ptr;
}#define SUCCESS (1)#define FAILURE (0)
int contains_char(char c){
char *str;str = (char*)malloc(20*sizeof(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;
}#define SUCCESS (1)#define FAILURE (0)
int cointains_char(char c){
char *str;int i = 0;str = (char*)malloc(20*sizeof(char));strcpy(str, "Search Me!");while( i < strlen(str) ){
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;
}//hardcode input length for simplicity
char* input = (char*) malloc(40*sizeof(char));char *tok;char* sep = " \t";
get_user_input( input );
/* The following loop will parse and process each token in the input string */
tok = strtok( input, sep);while( NULL != tok ){
if( isMalformed( tok ) ){
/* ignore and discard bad data */
free( tok );
}else{add_to_command_queue( tok );}tok = strtok( NULL, sep));
}//hardcode input length for simplicity
char* input = (char*) malloc(40*sizeof(char));char *tok, *command;char* sep = " \t";
get_user_input( input );
/* The following loop will parse and process each token in the input string */
tok = strtok( input, sep);while( NULL != tok ){
if( !isMalformed( command ) ){
/* copy and enqueue good data */
command = (char*) malloc( (strlen(tok) + 1) * sizeof(char) );strcpy( command, tok );add_to_command_queue( command );
}tok = strtok( NULL, sep));
}
free( input )Mitigations & Prevention
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().
When programming in C++, consider using smart pointers provided by the boost library to help correctly and consistently manage memory.
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.
Use a language that provides abstractions for memory allocation and deallocation.
Detection Methods
- Fuzzing High — Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with random inputs, it is often capable of generating unexpected results such as crashes, memory corruption,
- Automated Dynamic Analysis Moderate — Use tools that are integrated during compilation to insert runtime error-checking mechanisms related to memory safety errors, such as AddressSanitizer (ASan) for C/C++ [REF-1518] or valgrind [REF-480].
Real-World CVE Examples
| CVE ID | Description |
|---|---|
| CVE-2019-11930 | function "internally calls 'calloc' and returns a pointer at an index... inside the allocated buffer. This led to freeing invalid memory." |
Related Weaknesses
Taxonomy Mappings
- Software Fault Patterns: SFP12 — Faulty Memory Release
Frequently Asked Questions
What is CWE-763?
CWE-763 (Release of Invalid Pointer or Reference) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. The product attempts to return a memory resource to the system, but it calls the wrong release function or calls the appropriate release function incorrectly.
How can CWE-763 be exploited?
Attackers can exploit CWE-763 (Release of Invalid Pointer or Reference) to modify memory, dos: crash, exit, or restart, execute unauthorized code or commands. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-763?
Key mitigations include: 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().
What is the severity of CWE-763?
CWE-763 is classified as a Base-level weakness (Medium abstraction). It has been observed in 1 real-world CVEs.