|
| 1 | +/** |
| 2 | + * @name Constant password |
| 3 | + * @description Finds places where a string literal is used in a function call |
| 4 | + * argument that looks like a password. |
| 5 | + * @id rust/examples/simple-constant-password |
| 6 | + * @tags example |
| 7 | + */ |
| 8 | + |
| 9 | +import rust |
| 10 | +import codeql.rust.dataflow.DataFlow |
| 11 | +import codeql.rust.dataflow.TaintTracking |
| 12 | + |
| 13 | +/** |
| 14 | + * A data flow configuration for tracking flow from a string literal to a function |
| 15 | + * call argument that looks like a password. For example: |
| 16 | + * ``` |
| 17 | + * fn set_password(password: &str) { ... } |
| 18 | + * |
| 19 | + * ... |
| 20 | + * |
| 21 | + * let pwd = "123456"; // source |
| 22 | + * set_password(pwd); // sink (argument 0) |
| 23 | + * ``` |
| 24 | + */ |
| 25 | +module ConstantPasswordConfig implements DataFlow::ConfigSig { |
| 26 | + predicate isSource(DataFlow::Node node) { |
| 27 | + // `node` is a string literal |
| 28 | + node.asExpr().getExpr() instanceof StringLiteralExpr |
| 29 | + } |
| 30 | + |
| 31 | + predicate isSink(DataFlow::Node node) { |
| 32 | + // `node` is an argument whose corresponding parameter name matches the pattern "pass%" |
| 33 | + exists(CallExpr call, Function target, int argIndex, Variable v | |
| 34 | + call.getStaticTarget() = target and |
| 35 | + v.getParameter() = target.getParam(argIndex) and |
| 36 | + v.getText().matches("pass%") and |
| 37 | + call.getArg(argIndex) = node.asExpr().getExpr() |
| 38 | + ) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// instantiate the data flow configuration as a global taint tracking module |
| 43 | +module ConstantPasswordFlow = TaintTracking::Global<ConstantPasswordConfig>; |
| 44 | + |
| 45 | +// report flows from sources to sinks |
| 46 | +from DataFlow::Node sourceNode, DataFlow::Node sinkNode |
| 47 | +where ConstantPasswordFlow::flow(sourceNode, sinkNode) |
| 48 | +select sinkNode, "The value $@ is used as a constant password.", sourceNode, sourceNode.toString() |
0 commit comments