Skip to content

Commit 41c93d9

Browse files
committed
C++: Remove FPs from right shifts and explicitly bounded random functions.
1 parent 10755ec commit 41c93d9

File tree

3 files changed

+15
-23
lines changed

3 files changed

+15
-23
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ import semmle.code.cpp.security.TaintTracking
1818
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
1919
import TaintedWithPath
2020

21+
string getAMinPattern() { result = ["%min%", "l%"] }
22+
23+
string getAMaxPattern() { result = ["%max%", "%bound%", "h%"] }
24+
2125
predicate isUnboundedRandCall(FunctionCall fc) {
22-
fc.getTarget().getName() = "rand" and not bounded(fc)
26+
exists(Function func | func = fc.getTarget() |
27+
func.getName() = "rand" and
28+
not bounded(fc) and
29+
not func.getAParameter().getName().toLowerCase().matches([getAMinPattern(), getAMaxPattern()])
30+
)
2331
}
2432

2533
/**
@@ -84,6 +92,10 @@ predicate bounded(Expr e) {
8492
boundedDiv(e, any(DivExpr div).getLeftOperand())
8593
or
8694
boundedDiv(e, any(AssignDivExpr div).getLValue())
95+
or
96+
boundedDiv(e, any(RShiftExpr shift).getLeftOperand())
97+
or
98+
boundedDiv(e, any(AssignRShiftExpr div).getLValue())
8799
}
88100

89101
predicate isUnboundedRandCallOrParent(Expr e) {

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ edges
3636
| test.cpp:36:13:36:13 | Chi | test.cpp:37:7:37:7 | r |
3737
| test.cpp:36:13:36:13 | Chi | test.cpp:37:7:37:7 | r |
3838
| test.cpp:36:13:36:13 | get_rand3 output argument [array content] | test.cpp:36:13:36:13 | Chi |
39-
| test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r |
40-
| test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r |
41-
| test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r |
42-
| test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r |
43-
| test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r |
44-
| test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r |
45-
| test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r |
46-
| test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r |
4739
nodes
4840
| test.c:18:13:18:16 | call to rand | semmle.label | call to rand |
4941
| test.c:18:13:18:16 | call to rand | semmle.label | call to rand |
@@ -95,16 +87,6 @@ nodes
9587
| test.cpp:37:7:37:7 | r | semmle.label | r |
9688
| test.cpp:37:7:37:7 | r | semmle.label | r |
9789
| test.cpp:37:7:37:7 | r | semmle.label | r |
98-
| test.cpp:45:11:45:14 | call to rand | semmle.label | call to rand |
99-
| test.cpp:45:11:45:14 | call to rand | semmle.label | call to rand |
100-
| test.cpp:46:3:46:3 | r | semmle.label | r |
101-
| test.cpp:46:3:46:3 | r | semmle.label | r |
102-
| test.cpp:46:3:46:3 | r | semmle.label | r |
103-
| test.cpp:48:24:48:27 | call to rand | semmle.label | call to rand |
104-
| test.cpp:48:24:48:27 | call to rand | semmle.label | call to rand |
105-
| test.cpp:49:2:49:11 | unsigned_r | semmle.label | unsigned_r |
106-
| test.cpp:49:2:49:11 | unsigned_r | semmle.label | unsigned_r |
107-
| test.cpp:49:2:49:11 | unsigned_r | semmle.label | unsigned_r |
10890
#select
10991
| test.c:21:17:21:17 | r | test.c:18:13:18:16 | call to rand | test.c:21:17:21:17 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:18:13:18:16 | call to rand | Uncontrolled value |
11092
| test.c:35:5:35:5 | r | test.c:34:13:34:18 | call to rand | test.c:35:5:35:5 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:34:13:34:18 | call to rand | Uncontrolled value |
@@ -114,5 +96,3 @@ nodes
11496
| 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 |
11597
| 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 |
11698
| 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 |
117-
| test.cpp:46:3:46:3 | r | test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:45:11:45:14 | call to rand | Uncontrolled value |
118-
| test.cpp:49:2:49:11 | unsigned_r | test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:48:24:48:27 | call to rand | Uncontrolled value |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ unsigned rand(int max);
4343

4444
void test_with_bounded_randomness() {
4545
int r = rand(0, 10);
46-
r++; // GOOD [FALSE POSITIVE]
46+
r++; // GOOD
4747

4848
unsigned unsigned_r = rand(10);
49-
unsigned_r++; // GOOD [FALSE POSITIVE]
49+
unsigned_r++; // GOOD
5050
}

0 commit comments

Comments
 (0)