Skip to content

Commit fb2ec15

Browse files
committed
C++: Add double-free query documentation.
1 parent cc12e74 commit fb2ec15

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

cpp/ql/src/Critical/DoubleFree.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int* f() {
2+
int *buff = malloc(SIZE*sizeof(int));
3+
do_stuff(buff);
4+
free(buff);
5+
int *new_buffer = malloc(SIZE*sizeof(int));
6+
free(buff); // BAD: If new_buffer is assigned the same address as buff,
7+
// the memory allocator will free the new buffer memory region,
8+
// leading to use-after-free problems and memory corruption.
9+
return new_buffer;
10+
}

cpp/ql/src/Critical/DoubleFree.qhelp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
7+
<overview>
8+
<p>
9+
Dereferencing a pointer after it has been deallocated may result in memory corruption which can
10+
lead to security vulnerabilities.
11+
</p>
12+
13+
<include src="dataFlowWarning.inc.qhelp" />
14+
15+
</overview>
16+
<recommendation>
17+
<p>
18+
Ensure that all execution paths deallocate the allocated memory at most once. If possible, reassign
19+
the pointer to a null value after deallocating it. This will both prevent double-free vulnerabilities, and
20+
increase the likelihood of the operating system raising a runtime error if the pointer is subsequently
21+
dereferenced after being deallocated.
22+
</p>
23+
24+
</recommendation>
25+
<example><sample src="DoubleFree.cpp" />
26+
</example>
27+
<references>
28+
29+
<li>
30+
OWASP:
31+
<a href="https://owasp.org/www-community/vulnerabilities/Doubly_freeing_memory">Doubly freeing memory</a>.
32+
</li>
33+
34+
</references>
35+
</qhelp>

0 commit comments

Comments
 (0)