Skip to content

Commit f2b2300

Browse files
authored
Add files via upload
1 parent a7030c7 commit f2b2300

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
...
2+
buf = malloc(intSize);
3+
...
4+
free(buf);
5+
buf = NULL; // GOOD
6+
...
7+
8+
...
9+
buf = malloc(intSize);
10+
...
11+
free(buf);
12+
if(buf) free(buf); // BAD: the cleanup function does not zero out the pointer
13+
...
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Double freeing of a previously allocated resource can lead to various vulnerabilities in the program. Requires the attention of developers.</p>
7+
8+
</overview>
9+
<recommendation>
10+
<p>We recommend that you exclude situations of possible double release.</p>
11+
12+
</recommendation>
13+
<example>
14+
<p>The following example demonstrates an erroneous and corrected use of freeing a pointer.</p>
15+
<sample src="DoubleFree.c" />
16+
17+
</example>
18+
<references>
19+
20+
<li>
21+
CERT C Coding Standard:
22+
<a href="https://wiki.sei.cmu.edu/confluence/display/c/MEM30-C.+Do+not+access+freed+memory">MEM30-C. Do not access freed memory</a>.
23+
</li>
24+
25+
</references>
26+
</qhelp>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @name Errors When Double Free
3+
* @description Double freeing of a previously allocated resource can lead to various vulnerabilities in the program
4+
* and requires the attention of the developer.
5+
* @kind problem
6+
* @id cpp/errors-when-double-free
7+
* @problem.severity warning
8+
* @precision medium
9+
* @tags security
10+
* external/cwe/cwe-415
11+
*/
12+
13+
import cpp
14+
15+
/**
16+
* The function allows `getASuccessor` to be called recursively.
17+
* This provides a stop in situations of possible influence on the pointer.
18+
*/
19+
ControlFlowNode recursASuccessor(FunctionCall fc, LocalScopeVariable v) {
20+
result = fc
21+
or
22+
exists(ControlFlowNode mid |
23+
mid = recursASuccessor(fc, v) and
24+
result = mid.getASuccessor() and
25+
not result = v.getAnAssignedValue() and
26+
not result.(AddressOfExpr).getOperand() = v.getAnAccess() and
27+
not (
28+
not result instanceof DeallocationExpr and
29+
result.(FunctionCall).getAnArgument().(VariableAccess).getTarget() = v
30+
) and
31+
(
32+
fc.getTarget().hasGlobalOrStdName("realloc") and
33+
(
34+
not fc.getParent*() instanceof IfStmt and
35+
not result instanceof IfStmt
36+
)
37+
or
38+
not fc.getTarget().hasGlobalOrStdName("realloc")
39+
)
40+
)
41+
}
42+
43+
from FunctionCall fc
44+
where
45+
exists(FunctionCall fc2, LocalScopeVariable v |
46+
freeCall(fc, v.getAnAccess()) and
47+
freeCall(fc2, v.getAnAccess()) and
48+
fc != fc2 and
49+
recursASuccessor(fc, v) = fc2
50+
)
51+
select fc.getArgument(0), "This pointer may be cleared again later."

0 commit comments

Comments
 (0)