Skip to content

Commit 00a940f

Browse files
authored
Merge pull request #16524 from catenacyber/deref-null-result
Adds another rule for null deref
2 parents 8119a27 + eda8157 commit 00a940f

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
char * create (int arg) {
2+
if (arg > 42) {
3+
// this function may return NULL
4+
return NULL;
5+
}
6+
char * r = malloc(arg);
7+
snprintf(r, arg -1, "Hello");
8+
return r;
9+
}
10+
11+
void process(char *str) {
12+
// str is dereferenced
13+
if (str[0] == 'H') {
14+
printf("Hello H\n");
15+
}
16+
}
17+
18+
void test(int arg) {
19+
// first function returns a pointer that may be NULL
20+
char *str = create(arg);
21+
// str is not checked for nullness before being passed to process function
22+
process(str);
23+
}
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+
6+
<overview>
7+
<p>This rule finds a dereference of a function parameter, whose value comes from another function call that may return NULL, without checks in the meantime.</p>
8+
</overview>
9+
10+
<recommendation>
11+
<p>A check should be added between the return of the function which may return NULL, and its use by the function dereferencing ths pointer.</p>
12+
</recommendation>
13+
14+
<example>
15+
<sample src="DerefNullResult.cpp" />
16+
</example>
17+
18+
<references>
19+
<li>
20+
<a href="https://www.owasp.org/index.php/Null_Dereference">
21+
Null Dereference
22+
</a>
23+
</li>
24+
</references>
25+
26+
</qhelp>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @name Null dereference from a function result
3+
* @description A function parameter is dereferenced,
4+
* while it comes from a function that may return NULL,
5+
* and is not checked for nullness by the caller.
6+
* @kind problem
7+
* @id cpp/deref-null-result
8+
* @problem.severity recommendation
9+
* @tags reliability
10+
* security
11+
* external/cwe/cwe-476
12+
*/
13+
14+
import cpp
15+
import semmle.code.cpp.dataflow.new.DataFlow
16+
17+
from Function nuller, Parameter pd, FunctionCall fc, Variable v
18+
where
19+
mayReturnNull(nuller) and
20+
functionDereferences(pd.getFunction(), pd.getIndex()) and
21+
// there is a function call which will deref parameter pd
22+
fc.getTarget() = pd.getFunction() and
23+
// the parameter pd comes from a variable v
24+
DataFlow::localFlow(DataFlow::exprNode(v.getAnAccess()),
25+
DataFlow::exprNode(fc.getArgument(pd.getIndex()))) and
26+
// this variable v was assigned by a call to the nuller function
27+
unique( | | v.getAnAssignedValue()) = nuller.getACallToThisFunction() and
28+
// this variable v is not accessed for an operation (check for NULLness)
29+
not exists(VariableAccess vc |
30+
vc.getTarget() = v and
31+
(vc.getParent() instanceof Operation or vc.getParent() instanceof IfStmt)
32+
)
33+
select fc, "This function call may deref $@ when it can be NULL from $@", v, v.getName(), nuller,
34+
nuller.getName()

0 commit comments

Comments
 (0)