Skip to content

Commit 6379463

Browse files
committed
Merge branch 'main' into improve-tainted-arithmetic
2 parents 9b94f3a + 298f70f commit 6379463

File tree

2 files changed

+21
-4
lines changed
  • cpp/ql
    • src/Security/CWE/CWE-190
    • test/query-tests/Security/CWE/CWE-190/semmle/uncontrolled

2 files changed

+21
-4
lines changed

cpp/ql/src/Security/CWE/CWE-190/Bounded.qll

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@ private predicate boundedBitwiseAnd(Expr e, Expr andExpr, Expr operand1, Expr op
3939
}
4040

4141
/**
42-
* Holds if `e` is an operand of a binary operation that greatly reduces the range of possible
43-
* output values. For instance, if `e` is the left operand of a remainder expression.
42+
* Holds if `e` is an arithmetic expression that cannot overflow, or if `e` is an operand of an
43+
* operation that may greatly reduces the range of possible values.
4444
*/
4545
predicate bounded(Expr e) {
46+
(
47+
e instanceof UnaryArithmeticOperation or
48+
e instanceof BinaryArithmeticOperation or
49+
e instanceof AssignArithmeticOperation
50+
) and
51+
not convertedExprMightOverflow(e)
52+
or
4653
// For `%` and `&` we require that `e` is bounded by a value that is strictly smaller than the
4754
// maximum possible value of the result type of the operation.
4855
// For example, the function call `rand()` is considered bounded in the following program:

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/uncontrolled/test.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
int rand(void);
55
void trySlice(int start, int end);
6+
void add_100(int);
67

78
#define RAND() rand()
89
#define RANDN(n) (rand() % n)
910
#define RAND2() (rand() ^ rand())
10-
11-
11+
#define RAND_MAX 32767
1212

1313

1414

@@ -99,4 +99,14 @@ void randomTester() {
9999
*ptr_r = RAND();
100100
r -= 100; // BAD
101101
}
102+
103+
{
104+
int r = rand();
105+
r = ((2.0 / (RAND_MAX + 1)) * r - 1.0);
106+
add_100(r);
107+
}
102108
}
109+
110+
void add_100(int r) {
111+
r += 100; // GOOD
112+
}

0 commit comments

Comments
 (0)