Skip to content

Commit 9c3b7e8

Browse files
committed
Add files via upload
1 parent 15049ca commit 9c3b7e8

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if(len>0 & memset(buf,0,len)) return 1; // BAD: `memset` will be called regardless of the value of the `len` variable. moreover, one cannot be sure that it will happen after verification
2+
...
3+
if(len>0 && memset(buf,0,len)) return 1; // GOOD: `memset` will be called after the `len` variable has been checked.
4+
...
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>Using bitwise operations can be a mistake in some situations. For example, if parameters are evaluated in an expression and the function should be called only upon certain test results. These bitwise operations look suspicious and require developer attention.</p>
7+
8+
9+
</overview>
10+
<recommendation>
11+
12+
<p>We recommend that you evaluate the correctness of using the specified bit operations.</p>
13+
14+
</recommendation>
15+
<example>
16+
<p>The following example demonstrates the erroneous and fixed use of bit and logical operations.</p>
17+
<sample src="InsufficientControlFlowManagementWhenUsingBitOperations.c" />
18+
19+
</example>
20+
<references>
21+
22+
<li>
23+
CWE Common Weakness Enumeration:
24+
<a href="https://cwe.mitre.org/data/definitions/691.html"> CWE-691: Insufficient Control Flow Management</a>.
25+
</li>
26+
27+
</references>
28+
</qhelp>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @name Errors When Using Bit Operations
3+
* @description --Using bitwise operations can be a mistake in some situations.
4+
* --For example, if parameters are evaluated in an expression and the function should be called only upon certain test results.
5+
* --These bitwise operations look suspicious and require developer attention.
6+
* @kind problem
7+
* @id cpp/errors-when-using-bit-operations
8+
* @problem.severity warning
9+
* @precision medium
10+
* @tags correctness
11+
* security
12+
* external/cwe/cwe-691
13+
*/
14+
15+
import cpp
16+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
17+
18+
/**
19+
* Dangerous uses of bit operations.
20+
* For example: `if(intA>0 & intA<10 & charBuf&myFunc(charBuf[intA]))`.
21+
* In this case, the function will be called in any case, and even the sequence of the call is not guaranteed.
22+
*/
23+
class DangerousBitOperations extends Expr {
24+
FunctionCall bfc;
25+
26+
/**
27+
* The assignment indicates the conscious use of the bit operator.
28+
* Use in comparison, conversion, or return value indicates conscious use of the bit operator.
29+
* The use of shifts and bitwise operations on any element of an expression indicates a conscious use of the bitwise operator.
30+
*/
31+
DangerousBitOperations() {
32+
bfc = this.(BinaryBitwiseOperation).getRightOperand() and
33+
not this.getParent*() instanceof AssignExpr and
34+
not this.getParent*() instanceof Initializer and
35+
not this.getParent*() instanceof ReturnStmt and
36+
not this.getParent*() instanceof EqualityOperation and
37+
not this.getParent*() instanceof UnaryLogicalOperation and
38+
not this.getParent*() instanceof BinaryLogicalOperation and
39+
not this.(BinaryBitwiseOperation).getAChild*() instanceof BitwiseXorExpr and
40+
not this.(BinaryBitwiseOperation).getAChild*() instanceof LShiftExpr and
41+
not this.(BinaryBitwiseOperation).getAChild*() instanceof RShiftExpr
42+
}
43+
44+
/** Holds when part of a bit expression is used in a logical operation. */
45+
predicate useInLogicalOperations() {
46+
exists(BinaryLogicalOperation blop, Expr exp |
47+
blop.getAChild*() = exp and
48+
exp.(FunctionCall).getTarget() = bfc.getTarget() and
49+
not exp.getParent() instanceof ComparisonOperation and
50+
not exp.getParent() instanceof BinaryBitwiseOperation
51+
)
52+
}
53+
54+
/** Holds when part of a bit expression is used as part of another supply. For example, as an argument to another function. */
55+
predicate useInOtherCalls() {
56+
bfc.hasQualifier() or
57+
bfc.getTarget() instanceof Operator or
58+
exists(FunctionCall fc | fc.getAnArgument().getAChild*() = this) or
59+
bfc.getTarget() instanceof BuiltInFunction
60+
}
61+
62+
/** Holds when the bit expression contains both arguments and a function call. */
63+
predicate dangerousArgumentChecking() {
64+
not this.(BinaryBitwiseOperation).getLeftOperand() instanceof Call and
65+
globalValueNumber(this.(BinaryBitwiseOperation).getLeftOperand().getAChild*()) =
66+
globalValueNumber(bfc.getAnArgument())
67+
}
68+
69+
/** Holds when function calls are present in the bit expression. */
70+
predicate functionCallsInBitsExpression() {
71+
this.(BinaryBitwiseOperation).getLeftOperand().getAChild*() instanceof FunctionCall
72+
}
73+
}
74+
75+
from DangerousBitOperations dbo
76+
where
77+
not dbo.useInOtherCalls() and
78+
dbo.useInLogicalOperations() and
79+
(not dbo.functionCallsInBitsExpression() or dbo.dangerousArgumentChecking())
80+
select dbo, "this bit expression needs your attention"

0 commit comments

Comments
 (0)