Skip to content

Commit 631dc98

Browse files
authored
Merge pull request github#14904 from MathiasVP/taint-through-int-to-bool-casts
C++: Add Taint through int -> bool casts
2 parents 9eaebfc + 2681617 commit 631dc98

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/TaintTrackingUtil.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ private predicate operandToInstructionTaintStep(Operand opFrom, Instruction inst
7272
or
7373
instrTo.(FieldAddressInstruction).getField().getDeclaringType() instanceof Union
7474
)
75+
or
76+
// Taint from int to boolean casts. This ensures that we have flow to `!x` in:
77+
// ```cpp
78+
// x = integer_source();
79+
// if(!x) { ... }
80+
// ```
81+
exists(Operand zero |
82+
zero.getDef().(ConstantValueInstruction).getValue() = "0" and
83+
instrTo.(CompareNEInstruction).hasOperands(opFrom, zero)
84+
)
7585
}
7686

7787
/**

cpp/ql/src/Security/CWE/CWE-807/TaintedCondition.ql

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,10 @@ import semmle.code.cpp.ir.dataflow.TaintTracking
1919
import semmle.code.cpp.ir.IR
2020
import Flow::PathGraph
2121

22-
Expr getExprWithoutNot(Expr expr) {
23-
result = expr and not expr instanceof NotExpr
24-
or
25-
result = getExprWithoutNot(expr.(NotExpr).getOperand()) and expr instanceof NotExpr
26-
}
27-
2822
predicate sensitiveCondition(Expr condition, Expr raise) {
2923
raisesPrivilege(raise) and
3024
exists(IfStmt ifstmt |
31-
getExprWithoutNot(ifstmt.getCondition()) = condition and
25+
ifstmt.getCondition() = condition and
3226
raise.getEnclosingStmt().getParentStmt*() = ifstmt
3327
)
3428
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
edges
2-
| test.cpp:20:29:20:47 | call to getenv | test.cpp:24:11:24:16 | call to strcmp |
3-
| test.cpp:20:29:20:47 | call to getenv indirection | test.cpp:24:11:24:16 | call to strcmp |
2+
| test.cpp:20:29:20:47 | call to getenv | test.cpp:24:10:24:35 | ! ... |
3+
| test.cpp:20:29:20:47 | call to getenv indirection | test.cpp:24:10:24:35 | ! ... |
44
nodes
55
| test.cpp:20:29:20:47 | call to getenv | semmle.label | call to getenv |
66
| test.cpp:20:29:20:47 | call to getenv indirection | semmle.label | call to getenv indirection |
7-
| test.cpp:24:11:24:16 | call to strcmp | semmle.label | call to strcmp |
7+
| test.cpp:24:10:24:35 | ! ... | semmle.label | ! ... |
88
subpaths
99
#select
10-
| test.cpp:24:11:24:16 | call to strcmp | test.cpp:20:29:20:47 | call to getenv | test.cpp:24:11:24:16 | call to strcmp | Reliance on $@ to raise privilege at $@. | test.cpp:20:29:20:47 | call to getenv | an environment variable | test.cpp:25:9:25:27 | ... = ... | ... = ... |
11-
| test.cpp:24:11:24:16 | call to strcmp | test.cpp:20:29:20:47 | call to getenv indirection | test.cpp:24:11:24:16 | call to strcmp | Reliance on $@ to raise privilege at $@. | test.cpp:20:29:20:47 | call to getenv indirection | an environment variable | test.cpp:25:9:25:27 | ... = ... | ... = ... |
10+
| test.cpp:24:10:24:35 | ! ... | test.cpp:20:29:20:47 | call to getenv | test.cpp:24:10:24:35 | ! ... | Reliance on $@ to raise privilege at $@. | test.cpp:20:29:20:47 | call to getenv | an environment variable | test.cpp:25:9:25:27 | ... = ... | ... = ... |
11+
| test.cpp:24:10:24:35 | ! ... | test.cpp:20:29:20:47 | call to getenv indirection | test.cpp:24:10:24:35 | ! ... | Reliance on $@ to raise privilege at $@. | test.cpp:20:29:20:47 | call to getenv indirection | an environment variable | test.cpp:25:9:25:27 | ... = ... | ... = ... |

0 commit comments

Comments
 (0)