C++: Add double-free query documentation.

This commit is contained in:
Mathias Vorreiter Pedersen
2023-04-11 14:13:52 +01:00
parent cc12e74c23
commit fb2ec15dad
2 changed files with 45 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
int* f() {
int *buff = malloc(SIZE*sizeof(int));
do_stuff(buff);
free(buff);
int *new_buffer = malloc(SIZE*sizeof(int));
free(buff); // BAD: If new_buffer is assigned the same address as buff,
// the memory allocator will free the new buffer memory region,
// leading to use-after-free problems and memory corruption.
return new_buffer;
}

View File

@@ -0,0 +1,35 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
Dereferencing a pointer after it has been deallocated may result in memory corruption which can
lead to security vulnerabilities.
</p>
<include src="dataFlowWarning.inc.qhelp" />
</overview>
<recommendation>
<p>
Ensure that all execution paths deallocate the allocated memory at most once. If possible, reassign
the pointer to a null value after deallocating it. This will both prevent double-free vulnerabilities, and
increase the likelihood of the operating system raising a runtime error if the pointer is subsequently
dereferenced after being deallocated.
</p>
</recommendation>
<example><sample src="DoubleFree.cpp" />
</example>
<references>
<li>
OWASP:
<a href="https://owasp.org/www-community/vulnerabilities/Doubly_freeing_memory">Doubly freeing memory</a>.
</li>
</references>
</qhelp>