Skip to content

Commit 4fc60ae

Browse files
committed
C++: Relax the restrictions on when '%' is a barrier and accept test changes.
1 parent a6f1f8d commit 4fc60ae

File tree

3 files changed

+8
-31
lines changed

3 files changed

+8
-31
lines changed

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@ pragma[inline]
1515
private predicate boundedDiv(Expr e, Expr left) { e = left }
1616

1717
/**
18-
* An operand `e` of a remainder expression `rem` (i.e., `rem` is either a `RemExpr` or
19-
* an `AssignRemExpr`) with left-hand side `left` and right-ahnd side `right` is bounded
20-
* when `e` is `left` and `right` is upper bounded by some number that is less than the maximum integer
21-
* allowed by the result type of `rem`.
18+
* An operand `e` of a remainder expression (i.e., `e` is an operand of either a `RemExpr` or
19+
* a `AssignRemExpr`) is bounded when `e` is the left-hand side of the remainder.
2220
*/
2321
pragma[inline]
24-
private predicate boundedRem(Expr e, Expr rem, Expr left, Expr right) {
25-
e = left and
26-
upperBound(right.getFullyConverted()) < exprMaxVal(rem.getFullyConverted())
27-
}
22+
private predicate boundedRem(Expr e, Expr left) { e = left }
2823

2924
/**
3025
* An operand `e` of a bitwise and expression `andExpr` (i.e., `andExpr` is either an `BitwiseAndExpr`
@@ -50,19 +45,9 @@ predicate bounded(Expr e) {
5045
) and
5146
not convertedExprMightOverflow(e)
5247
or
53-
// For `%` and `&` we require that `e` is bounded by a value that is strictly smaller than the
54-
// maximum possible value of the result type of the operation.
55-
// For example, the function call `rand()` is considered bounded in the following program:
56-
// ```
57-
// int i = rand() % (UINT8_MAX + 1);
58-
// ```
59-
// but not in:
60-
// ```
61-
// unsigned char uc = rand() % (UINT8_MAX + 1);
62-
// ```
63-
exists(RemExpr rem | boundedRem(e, rem, rem.getLeftOperand(), rem.getRightOperand()))
48+
boundedRem(e, any(RemExpr rem).getLeftOperand())
6449
or
65-
exists(AssignRemExpr rem | boundedRem(e, rem, rem.getLValue(), rem.getRValue()))
50+
boundedRem(e, any(AssignRemExpr rem).getLValue())
6651
or
6752
exists(BitwiseAndExpr andExpr |
6853
boundedBitwiseAnd(e, andExpr, andExpr.getAnOperand(), andExpr.getAnOperand())

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ edges
77
| test.c:81:14:81:17 | call to rand | test.c:83:9:83:9 | r |
88
| test.c:81:23:81:26 | call to rand | test.c:83:9:83:9 | r |
99
| test.c:99:14:99:19 | call to rand | test.c:100:5:100:5 | r |
10-
| test.c:115:12:115:15 | call to rand | test.c:116:3:116:4 | r1 |
11-
| test.c:118:13:118:16 | call to rand | test.c:119:3:119:4 | r2 |
1210
| test.cpp:8:9:8:12 | Store | test.cpp:24:11:24:18 | call to get_rand |
1311
| test.cpp:8:9:8:12 | call to rand | test.cpp:8:9:8:12 | Store |
1412
| test.cpp:13:2:13:15 | Chi [[]] | test.cpp:30:13:30:14 | get_rand2 output argument [[]] |
@@ -35,10 +33,6 @@ nodes
3533
| test.c:83:9:83:9 | r | semmle.label | r |
3634
| test.c:99:14:99:19 | call to rand | semmle.label | call to rand |
3735
| test.c:100:5:100:5 | r | semmle.label | r |
38-
| test.c:115:12:115:15 | call to rand | semmle.label | call to rand |
39-
| test.c:116:3:116:4 | r1 | semmle.label | r1 |
40-
| test.c:118:13:118:16 | call to rand | semmle.label | call to rand |
41-
| test.c:119:3:119:4 | r2 | semmle.label | r2 |
4236
| test.cpp:8:9:8:12 | Store | semmle.label | Store |
4337
| test.cpp:8:9:8:12 | call to rand | semmle.label | call to rand |
4438
| test.cpp:13:2:13:15 | Chi [[]] | semmle.label | Chi [[]] |
@@ -62,8 +56,6 @@ nodes
6256
| test.c:83:9:83:9 | r | test.c:81:14:81:17 | call to rand | test.c:83:9:83:9 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:81:14:81:17 | call to rand | Uncontrolled value |
6357
| test.c:83:9:83:9 | r | test.c:81:23:81:26 | call to rand | test.c:83:9:83:9 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:81:23:81:26 | call to rand | Uncontrolled value |
6458
| 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 |
65-
| test.c:116:3:116:4 | r1 | test.c:115:12:115:15 | call to rand | test.c:116:3:116:4 | r1 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:115:12:115:15 | call to rand | Uncontrolled value |
66-
| test.c:119:3:119:4 | r2 | test.c:118:13:118:16 | call to rand | test.c:119:3:119:4 | r2 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:118:13:118:16 | call to rand | Uncontrolled value |
6759
| 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 |
6860
| 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 |
6961
| 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ void add_100(int r) {
113113

114114
void randomTester2(int bound, int min, int max) {
115115
int r1 = rand() % bound;
116-
r1 += 100; // GOOD [FALSE POSITIVE] (`bound` may possibly be MAX_INT in which case this could
117-
// still overflow, but it's most likely fine)
116+
r1 += 100; // GOOD (`bound` may possibly be MAX_INT in which case this could
117+
// still overflow, but it's most likely fine)
118118

119119
int r2 = (rand() % (max - min + 1)) + min;
120-
r2 += 100; // GOOD [FALSE POSITIVE] (This is a common way to clamp the random value between [min, max])
120+
r2 += 100; // GOOD (This is a common way to clamp the random value between [min, max])
121121
}

0 commit comments

Comments
 (0)