Skip to content

Commit d145799

Browse files
committed
C++: Use range analysis in Overflow.qll
1 parent 2d618d6 commit d145799

File tree

4 files changed

+7
-9
lines changed

4 files changed

+7
-9
lines changed

cpp/ql/src/semmle/code/cpp/security/Overflow.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import cpp
77
import semmle.code.cpp.controlflow.Dominance
8+
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
89

910
/**
1011
* Holds if the value of `use` is guarded using `abs`.
@@ -94,9 +95,10 @@ predicate guardedGreater(Operation e, Expr use) {
9495
VariableAccess varUse(LocalScopeVariable v) { result = v.getAnAccess() }
9596

9697
/**
97-
* Holds if `e` is not guarded against overflow by `use`.
98+
* Holds if `e` potentially overflows and `use` is an operand of `e` that is not guarded.
9899
*/
99100
predicate missingGuardAgainstOverflow(Operation e, VariableAccess use) {
101+
convertedExprMightOverflow(e) and
100102
use = e.getAnOperand() and
101103
exists(LocalScopeVariable v | use.getTarget() = v |
102104
// overflow possible if large
@@ -115,9 +117,10 @@ predicate missingGuardAgainstOverflow(Operation e, VariableAccess use) {
115117
}
116118

117119
/**
118-
* Holds if `e` is not guarded against underflow by `use`.
120+
* Holds if `e` potentially underflows and `use` is an operand of `e` that is not guarded.
119121
*/
120122
predicate missingGuardAgainstUnderflow(Operation e, VariableAccess use) {
123+
convertedExprMightOverflowNegatively(e) and
121124
use = e.getAnOperand() and
122125
exists(LocalScopeVariable v | use.getTarget() = v |
123126
// underflow possible if use is left operand and small

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/extreme/ArithmeticWithExtremeValues.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33
| test.c:50:3:50:5 | sc3 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:49:9:49:16 | 127 | Extreme value |
44
| test.c:59:3:59:5 | sc6 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:58:9:58:16 | 127 | Extreme value |
55
| test.c:63:3:63:5 | sc8 | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:62:9:62:16 | - ... | Extreme value |
6-
| test.c:75:3:75:5 | sc1 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:74:9:74:16 | 127 | Extreme value |
7-
| test.c:76:3:76:5 | sc1 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:74:9:74:16 | 127 | Extreme value |
86
| test.c:124:9:124:9 | x | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:118:17:118:23 | 2147483647 | Extreme value |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ void test_negatives() {
7272
signed char sc1, sc2, sc3, sc4, sc5, sc6, sc7, sc8;
7373

7474
sc1 = CHAR_MAX;
75-
sc1 += 0; // GOOD [FALSE POSITIVE]
76-
sc1 += -1; // GOOD [FALSE POSITIVE]
75+
sc1 += 0; // GOOD
76+
sc1 += -1; // GOOD
7777
sc2 = CHAR_MIN;
7878
sc2 += -1; // BAD [NOT DETECTED]
7979
sc3 = CHAR_MIN;

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/tainted/ArithmeticTainted.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
| test2.cpp:14:11:14:11 | v | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test2.cpp:25:22:25:23 | & ... | User-provided value |
22
| test2.cpp:14:11:14:11 | v | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test2.cpp:25:22:25:23 | & ... | User-provided value |
3-
| test3.c:15:10:15:10 | x | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test3.c:11:15:11:18 | argv | User-provided value |
4-
| test3.c:15:14:15:14 | y | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test3.c:11:15:11:18 | argv | User-provided value |
5-
| test3.c:15:18:15:18 | z | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test3.c:11:15:11:18 | argv | User-provided value |
63
| test5.cpp:17:6:17:18 | call to getTaintedInt | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test5.cpp:9:7:9:9 | buf | User-provided value |
74
| test5.cpp:19:6:19:6 | y | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test5.cpp:9:7:9:9 | buf | User-provided value |
85
| test5.cpp:19:6:19:6 | y | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test5.cpp:9:7:9:9 | buf | User-provided value |

0 commit comments

Comments
 (0)