|
| 1 | +/** |
| 2 | + * @name Insufficient hash iterations |
| 3 | + * @description Using hash functions with < 1000 iterations is not secure, because that scheme leads to password cracking attacks due to having an insufficient level of computational effort. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity error |
| 6 | + * @security-severity 7.8 |
| 7 | + * @precision high |
| 8 | + * @id swift/insufficient-hash-iterations |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-916 |
| 11 | + */ |
| 12 | + |
| 13 | +import swift |
| 14 | +import codeql.swift.dataflow.DataFlow |
| 15 | +import codeql.swift.dataflow.TaintTracking |
| 16 | +import DataFlow::PathGraph |
| 17 | + |
| 18 | +/** |
| 19 | + * An `Expr` that is used to initialize a password-based encryption key. |
| 20 | + */ |
| 21 | +abstract class IterationsSource extends Expr { } |
| 22 | + |
| 23 | +/** |
| 24 | + * A literal integer that is 1000 or less is a source of taint for iterations. |
| 25 | + */ |
| 26 | +class IntLiteralSource extends IterationsSource instanceof IntegerLiteralExpr { |
| 27 | + IntLiteralSource() { this.getStringValue().toInt() >= 1000 } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * A class for all ways to set the iterations of hash function. |
| 32 | + */ |
| 33 | +class InsufficientHashIterationsSink extends Expr { |
| 34 | + InsufficientHashIterationsSink() { |
| 35 | + // `iterations` arg in `init` is a sink |
| 36 | + exists(ClassOrStructDecl c, AbstractFunctionDecl f, CallExpr call | |
| 37 | + c.getFullName() = "PKCS5.PBKDF1" and |
| 38 | + c.getAMember() = f and |
| 39 | + f.getName().matches("init(%iterations:%") and |
| 40 | + call.getStaticTarget() = f and |
| 41 | + call.getArgument(2).getExpr() = this |
| 42 | + ) |
| 43 | + or |
| 44 | + exists(ClassOrStructDecl c, AbstractFunctionDecl f, CallExpr call | |
| 45 | + c.getFullName() = "PKCS5.PBKDF2" and |
| 46 | + c.getAMember() = f and |
| 47 | + f.getName().matches("init(%iterations:%") and |
| 48 | + call.getStaticTarget() = f and |
| 49 | + call.getArgument(3).getExpr() = this |
| 50 | + ) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * A dataflow configuration from the hash iterations source to expressions that use |
| 56 | + * it to initialize hash functions. |
| 57 | + */ |
| 58 | +class InsufficientHashIterationsConfig extends TaintTracking::Configuration { |
| 59 | + InsufficientHashIterationsConfig() { this = "InsufficientHashIterationsConfig" } |
| 60 | + |
| 61 | + override predicate isSource(DataFlow::Node node) { node.asExpr() instanceof IterationsSource } |
| 62 | + |
| 63 | + override predicate isSink(DataFlow::Node node) { |
| 64 | + node.asExpr() instanceof InsufficientHashIterationsSink |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// The query itself |
| 69 | +from |
| 70 | + InsufficientHashIterationsConfig config, DataFlow::PathNode sourceNode, |
| 71 | + DataFlow::PathNode sinkNode |
| 72 | +where config.hasFlowPath(sourceNode, sinkNode) |
| 73 | +select sinkNode.getNode(), sourceNode, sinkNode, |
| 74 | + "The hash function '" + sinkNode.getNode().toString() + |
| 75 | + "' has been initialized with an insufficient number of iterations from $@.", sourceNode, |
| 76 | + sourceNode.getNode().toString() |
0 commit comments