Skip to content

Commit 238c483

Browse files
committed
C++: Make any non-overflowing arithmetic operation a barrier.
1 parent 18e5d3c commit 238c483

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

cpp/ql/src/Security/CWE/CWE-190/ArithmeticUncontrolled.ql

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ predicate boundedDiv(Expr e, Expr left) { e = left }
3636

3737
/**
3838
* An operand `e` of a remainder expression `rem` (i.e., `rem` is either a `RemExpr` or
39-
* an `AssignRemExpr`) with left-hand side `left` and right-ahnd side `right` is bounded
39+
* an `AssignRemExpr`) with left-hand side `left` and right-hand side `right` is bounded
4040
* when `e` is `left` and `right` is upper bounded by some number that is less than the maximum integer
4141
* allowed by the result type of `rem`.
4242
*/
@@ -59,10 +59,15 @@ predicate boundedBitwiseAnd(Expr e, Expr andExpr, Expr operand1, Expr operand2)
5959
}
6060

6161
/**
62-
* Holds if `fc` is a part of the left operand of a binary operation that greatly reduces the range
63-
* of possible values.
62+
* Holds if `e` is an operand of an operation that greatly reduces the range of possible values.
6463
*/
6564
predicate bounded(Expr e) {
65+
(
66+
e instanceof UnaryArithmeticOperation or
67+
e instanceof BinaryArithmeticOperation
68+
) and
69+
not convertedExprMightOverflow(e)
70+
or
6671
// For `%` and `&` we require that `e` is bounded by a value that is strictly smaller than the
6772
// maximum possible value of the result type of the operation.
6873
// For example, the function call `rand()` is considered bounded in the following program:

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ edges
1919
| test.c:99:14:99:19 | call to rand | test.c:100:5:100:5 | r |
2020
| test.c:99:14:99:19 | call to rand | test.c:100:5:100:5 | r |
2121
| test.c:99:14:99:19 | call to rand | test.c:100:5:100:5 | r |
22-
| test.c:104:13:104:16 | call to rand | test.c:106:5:106:11 | r |
23-
| test.c:104:13:104:16 | call to rand | test.c:106:5:106:11 | r |
24-
| test.c:106:5:106:11 | r | test.c:110:18:110:18 | r |
25-
| test.c:110:18:110:18 | r | test.c:111:3:111:3 | r |
26-
| test.c:110:18:110:18 | r | test.c:111:3:111:3 | r |
2722
| test.cpp:8:9:8:12 | Store | test.cpp:24:11:24:18 | call to get_rand |
2823
| test.cpp:8:9:8:12 | call to rand | test.cpp:8:9:8:12 | Store |
2924
| test.cpp:8:9:8:12 | call to rand | test.cpp:8:9:8:12 | Store |
@@ -67,13 +62,6 @@ nodes
6762
| test.c:100:5:100:5 | r | semmle.label | r |
6863
| test.c:100:5:100:5 | r | semmle.label | r |
6964
| test.c:100:5:100:5 | r | semmle.label | r |
70-
| test.c:104:13:104:16 | call to rand | semmle.label | call to rand |
71-
| test.c:104:13:104:16 | call to rand | semmle.label | call to rand |
72-
| test.c:106:5:106:11 | r | semmle.label | r |
73-
| test.c:110:18:110:18 | r | semmle.label | r |
74-
| test.c:111:3:111:3 | r | semmle.label | r |
75-
| test.c:111:3:111:3 | r | semmle.label | r |
76-
| test.c:111:3:111:3 | r | semmle.label | r |
7765
| test.cpp:8:9:8:12 | Store | semmle.label | Store |
7866
| test.cpp:8:9:8:12 | call to rand | semmle.label | call to rand |
7967
| test.cpp:8:9:8:12 | call to rand | semmle.label | call to rand |
@@ -105,7 +93,6 @@ nodes
10593
| test.c:45:5:45:5 | r | test.c:44:13:44:16 | call to rand | test.c:45:5:45:5 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:44:13:44:16 | call to rand | Uncontrolled value |
10694
| test.c:77:9:77:9 | r | test.c:75:13:75:19 | ... ^ ... | test.c:77:9:77:9 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:75:13:75:19 | ... ^ ... | Uncontrolled value |
10795
| test.c:100:5:100:5 | r | test.c:99:14:99:19 | call to rand | test.c:100:5:100:5 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:99:14:99:19 | call to rand | Uncontrolled value |
108-
| test.c:111:3:111:3 | r | test.c:104:13:104:16 | call to rand | test.c:111:3:111:3 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:104:13:104:16 | call to rand | Uncontrolled value |
10996
| test.cpp:25:7:25:7 | r | test.cpp:8:9:8:12 | call to rand | test.cpp:25:7:25:7 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:8:9:8:12 | call to rand | Uncontrolled value |
11097
| test.cpp:31:7:31:7 | r | test.cpp:13:10:13:13 | call to rand | test.cpp:31:7:31:7 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:13:10:13:13 | call to rand | Uncontrolled value |
11198
| test.cpp:37:7:37:7 | r | test.cpp:18:9:18:12 | call to rand | test.cpp:37:7:37:7 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:18:9:18:12 | call to rand | Uncontrolled value |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,5 @@ void randomTester() {
108108
}
109109

110110
void add_100(int r) {
111-
r += 100; // GOOD [FALSE POSITIVE]
111+
r += 100; // GOOD
112112
}

0 commit comments

Comments
 (0)