Skip to content

Commit d2fd986

Browse files
committed
CPP: Support crement operations in CWE-190.
1 parent f2760f2 commit d2fd986

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ predicate taintedVarAccess(Expr origin, VariableAccess va) {
2121
tainted(origin, va)
2222
}
2323

24-
from Expr origin, BinaryArithmeticOperation op, VariableAccess va, string effect
24+
from Expr origin, Operation op, VariableAccess va, string effect
2525
where taintedVarAccess(origin, va)
2626
and op.getAnOperand() = va
2727
and

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ predicate guardedByAssignDiv(Expr origin) {
4646
tainted(origin, va) and div.getLValue() = va)
4747
}
4848

49-
from Expr origin, BinaryArithmeticOperation op, VariableAccess va, string effect
49+
from Expr origin, Operation op, VariableAccess va, string effect
5050
where taintedVarAccess(origin, va)
5151
and op.getAnOperand() = va
5252
and

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ predicate taintedVarAccess(Expr origin, VariableAccess va) {
4545
tainted(origin, va)
4646
}
4747

48-
from Expr origin, BinaryArithmeticOperation op, VariableAccess va, string effect
48+
from Expr origin, Operation op, VariableAccess va, string effect
4949
where taintedVarAccess(origin, va)
5050
and op.getAnOperand() = va
5151
and

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import semmle.code.cpp.controlflow.Dominance
44
/* Guarding */
55

66
/** is the size of this use guarded using 'abs'? */
7-
predicate guardedAbs(BinaryArithmeticOperation e, Expr use) {
7+
predicate guardedAbs(Operation e, Expr use) {
88
exists(FunctionCall fc |
99
fc.getTarget().getName() = "abs" |
1010
fc.getArgument(0).getAChild*() = use
@@ -13,7 +13,7 @@ predicate guardedAbs(BinaryArithmeticOperation e, Expr use) {
1313
}
1414

1515
/** is the size of this use guarded to be less than something? */
16-
predicate guardedLesser(BinaryArithmeticOperation e, Expr use) {
16+
predicate guardedLesser(Operation e, Expr use) {
1717
exists(IfStmt c, RelationalOperation guard |
1818
use = guard.getLesserOperand().getAChild*() and
1919
guard = c.getControllingExpr().getAChild*() and
@@ -33,7 +33,7 @@ predicate guardedLesser(BinaryArithmeticOperation e, Expr use) {
3333
}
3434

3535
/** is the size of this use guarded to be greater than something? */
36-
predicate guardedGreater(BinaryArithmeticOperation e, Expr use) {
36+
predicate guardedGreater(Operation e, Expr use) {
3737
exists(IfStmt c, RelationalOperation guard |
3838
use = guard.getGreaterOperand().getAChild*() and
3939
guard = c.getControllingExpr().getAChild*() and
@@ -58,24 +58,26 @@ VariableAccess varUse(LocalScopeVariable v) {
5858
}
5959

6060
/** is e not guarded against overflow by use? */
61-
predicate missingGuardAgainstOverflow(BinaryArithmeticOperation e, VariableAccess use) {
61+
predicate missingGuardAgainstOverflow(Operation e, VariableAccess use) {
6262
use = e.getAnOperand() and
6363
exists(LocalScopeVariable v | use.getTarget() = v |
6464
// overflow possible if large
6565
(e instanceof AddExpr and not guardedLesser(e, varUse(v))) or
66+
(e instanceof IncrementOperation and not guardedLesser(e, varUse(v))) or
6667
// overflow possible if large or small
6768
(e instanceof MulExpr and
6869
not (guardedLesser(e, varUse(v)) and guardedGreater(e, varUse(v))))
6970
)
7071
}
7172

7273
/** is e not guarded against underflow by use? */
73-
predicate missingGuardAgainstUnderflow(BinaryArithmeticOperation e, VariableAccess use) {
74+
predicate missingGuardAgainstUnderflow(Operation e, VariableAccess use) {
7475
use = e.getAnOperand() and
7576
exists(LocalScopeVariable v | use.getTarget() = v |
7677
// underflow possible if use is left operand and small
77-
(e instanceof SubExpr and
78-
(use = e.getLeftOperand() and not guardedGreater(e, varUse(v)))) or
78+
(use = e.(SubExpr).getLeftOperand() and not guardedGreater(e, varUse(v))) or
79+
// underflow possible if small
80+
(e instanceof DecrementOperation and not guardedGreater(e, varUse(v))) or
7981
// underflow possible if large or small
8082
(e instanceof MulExpr and
8183
not (guardedLesser(e, varUse(v)) and guardedGreater(e, varUse(v))))

0 commit comments

Comments
 (0)