Skip to content

Commit 633fc92

Browse files
authored
Add files via upload
1 parent 15049ca commit 633fc92

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if(len<0) return 1;
2+
memset(dest, source, len); // GOOD: variable `len` checked before call
3+
4+
...
5+
6+
memset(dest, source, len); // BAD: variable `len` checked after call
7+
if(len<0) return 1;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Checking the function argument after calling the function itself. This situation looks suspicious and requires the attention of the developer. It may be necessary to add validation before calling the function</p>
7+
8+
9+
</overview>
10+
<recommendation>
11+
12+
<p>We recommend checking before calling the function.</p>
13+
14+
</recommendation>
15+
<example>
16+
<p>The following example demonstrates an erroneous and fixed use of function argument validation.</p>
17+
<sample src="LateCheckOfFunctionArgument.c" />
18+
19+
</example>
20+
<references>
21+
22+
<li>
23+
CWE Common Weakness Enumeration:
24+
<a href="https://cwe.mitre.org/data/definitions/20.html"> CWE-20: Improper Input Validation</a>.
25+
</li>
26+
27+
</references>
28+
</qhelp>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @name Late Check Of Function Argument
3+
* @description --Checking the function argument after calling the function itself.
4+
* --This situation looks suspicious and requires the attention of the developer.
5+
* --It may be necessary to add validation before calling the function.
6+
* @kind problem
7+
* @id cpp/late-check-of-function-argument
8+
* @problem.severity warning
9+
* @precision medium
10+
* @tags correctness
11+
* security
12+
* external/cwe/cwe-20
13+
*/
14+
15+
import cpp
16+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
17+
18+
predicate numberArgument(Function f, int size) {
19+
f.hasGlobalOrStdName("write") and size = 2
20+
or
21+
f.hasGlobalOrStdName("read") and size = 2
22+
or
23+
f.hasGlobalOrStdName("lseek") and size = 1
24+
or
25+
f.hasGlobalOrStdName("memmove") and size = 2
26+
or
27+
f.hasGlobalOrStdName("memset") and size = 2
28+
or
29+
f.hasGlobalOrStdName("memcpy") and size = 2
30+
or
31+
f.hasGlobalOrStdName("memcmp") and size = 2
32+
or
33+
f.hasGlobalOrStdName("strncat") and size = 2
34+
or
35+
f.hasGlobalOrStdName("strncpy") and size = 2
36+
or
37+
f.hasGlobalOrStdName("strncmp") and size = 2
38+
or
39+
f.hasGlobalOrStdName("snprintf") and size = 1
40+
or
41+
f.hasGlobalOrStdName("strndup") and size = 2
42+
or
43+
f.hasGlobalOrStdName("read") and size = 2
44+
}
45+
46+
class IfCompareWithZero extends IfStmt {
47+
IfCompareWithZero() { this.getCondition().(RelationalOperation).getAChild().getValue() = "0" }
48+
49+
Expr noZerroOperand() {
50+
if this.getCondition().(RelationalOperation).getGreaterOperand().getValue() = "0"
51+
then result = this.getCondition().(RelationalOperation).getLesserOperand()
52+
else result = this.getCondition().(RelationalOperation).getGreaterOperand()
53+
}
54+
}
55+
56+
from FunctionCall fc, IfCompareWithZero ifc, int na
57+
where
58+
numberArgument(fc.getTarget(), na) and
59+
na >= 0 and
60+
globalValueNumber(fc.getArgument(na)) = globalValueNumber(ifc.noZerroOperand()) and
61+
dominates(fc, ifc) and
62+
not exists(IfStmt ifc1 |
63+
dominates(ifc1, fc) and
64+
globalValueNumber(fc.getArgument(na)) = globalValueNumber(ifc1.getCondition().getAChild*())
65+
)
66+
select fc, "Argument '$@' will be checked later.", fc.getArgument(na), fc.getArgument(na).toString()

0 commit comments

Comments
 (0)