|
12 | 12 | * external/cwe/cwe-807
|
13 | 13 | */
|
14 | 14 |
|
15 |
| -import semmle.code.cpp.ir.dataflow.internal.DefaultTaintTrackingImpl |
16 |
| -import TaintedWithPath |
| 15 | +import cpp |
| 16 | +import semmle.code.cpp.security.Security |
| 17 | +import semmle.code.cpp.security.FlowSources |
| 18 | +import semmle.code.cpp.ir.dataflow.TaintTracking |
| 19 | +import semmle.code.cpp.ir.IR |
| 20 | +import Flow::PathGraph |
| 21 | + |
| 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 | +} |
17 | 27 |
|
18 | 28 | predicate sensitiveCondition(Expr condition, Expr raise) {
|
19 | 29 | raisesPrivilege(raise) and
|
20 | 30 | exists(IfStmt ifstmt |
|
21 |
| - ifstmt.getCondition() = condition and |
| 31 | + getExprWithoutNot(ifstmt.getCondition()) = condition and |
22 | 32 | raise.getEnclosingStmt().getParentStmt*() = ifstmt
|
23 | 33 | )
|
24 | 34 | }
|
25 | 35 |
|
26 |
| -class Configuration extends TaintTrackingConfiguration { |
27 |
| - override predicate isSink(Element tainted) { sensitiveCondition(tainted, _) } |
| 36 | +private predicate constantInstruction(Instruction instr) { |
| 37 | + instr instanceof ConstantInstruction |
| 38 | + or |
| 39 | + instr instanceof StringConstantInstruction |
| 40 | + or |
| 41 | + constantInstruction(instr.(UnaryInstruction).getUnary()) |
| 42 | +} |
| 43 | + |
| 44 | +predicate isSource(FlowSource source, string sourceType) { sourceType = source.getSourceType() } |
| 45 | + |
| 46 | +module Config implements DataFlow::ConfigSig { |
| 47 | + predicate isSource(DataFlow::Node node) { isSource(node, _) } |
| 48 | + |
| 49 | + predicate isSink(DataFlow::Node node) { |
| 50 | + sensitiveCondition([node.asExpr(), node.asIndirectExpr()], _) |
| 51 | + } |
| 52 | + |
| 53 | + predicate isBarrier(DataFlow::Node node) { |
| 54 | + // Block flow into binary instructions if both operands are non-constant |
| 55 | + exists(BinaryInstruction iTo | |
| 56 | + iTo = node.asInstruction() and |
| 57 | + not constantInstruction(iTo.getLeft()) and |
| 58 | + not constantInstruction(iTo.getRight()) and |
| 59 | + // propagate taint from either the pointer or the offset, regardless of constant-ness |
| 60 | + not iTo instanceof PointerArithmeticInstruction |
| 61 | + ) |
| 62 | + or |
| 63 | + // Block flow through calls to pure functions if two or more operands are non-constant |
| 64 | + exists(Instruction iFrom1, Instruction iFrom2, CallInstruction iTo | |
| 65 | + iTo = node.asInstruction() and |
| 66 | + isPureFunction(iTo.getStaticCallTarget().getName()) and |
| 67 | + iFrom1 = iTo.getAnArgument() and |
| 68 | + iFrom2 = iTo.getAnArgument() and |
| 69 | + not constantInstruction(iFrom1) and |
| 70 | + not constantInstruction(iFrom2) and |
| 71 | + iFrom1 != iFrom2 |
| 72 | + ) |
| 73 | + } |
28 | 74 | }
|
29 | 75 |
|
| 76 | +module Flow = TaintTracking::Global<Config>; |
| 77 | + |
30 | 78 | /*
|
31 | 79 | * Produce an alert if there is an 'if' statement whose condition `condition`
|
32 | 80 | * is influenced by tainted data `source`, and the body contains
|
33 | 81 | * `raise` which escalates privilege.
|
34 | 82 | */
|
35 | 83 |
|
36 |
| -from Expr source, Expr condition, Expr raise, PathNode sourceNode, PathNode sinkNode |
| 84 | +from |
| 85 | + Expr raise, string sourceType, DataFlow::Node source, DataFlow::Node sink, |
| 86 | + Flow::PathNode sourceNode, Flow::PathNode sinkNode |
37 | 87 | where
|
38 |
| - taintedWithPath(source, condition, sourceNode, sinkNode) and |
39 |
| - sensitiveCondition(condition, raise) |
40 |
| -select condition, sourceNode, sinkNode, "Reliance on untrusted input $@ to raise privilege at $@.", |
41 |
| - source, source.toString(), raise, raise.toString() |
| 88 | + source = sourceNode.getNode() and |
| 89 | + sink = sinkNode.getNode() and |
| 90 | + isSource(source, sourceType) and |
| 91 | + sensitiveCondition([sink.asExpr(), sink.asIndirectExpr()], raise) and |
| 92 | + Flow::flowPath(sourceNode, sinkNode) |
| 93 | +select sink, sourceNode, sinkNode, "Reliance on $@ to raise privilege at $@.", source, sourceType, |
| 94 | + raise, raise.toString() |
0 commit comments