|
| 1 | +/** |
| 2 | + * @name Weak cryptography |
| 3 | + * @description Finds usage of a static (hardcoded) IV. (CNG) |
| 4 | + * @kind problem |
| 5 | + * @id cpp/weak-crypto/cng/hardcoded-iv |
| 6 | + * @problem.severity error |
| 7 | + * @precision high |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-327 |
| 10 | + */ |
| 11 | + |
| 12 | +import cpp |
| 13 | +import semmle.code.cpp.dataflow.new.DataFlow |
| 14 | + |
| 15 | +/** |
| 16 | + * Gets const element of `ArrayAggregateLiteral`. |
| 17 | + */ |
| 18 | +Expr getConstElement(ArrayAggregateLiteral lit) { |
| 19 | + exists(int n | |
| 20 | + result = lit.getElementExpr(n, _) and |
| 21 | + result.isConstant() |
| 22 | + ) |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Gets the last element in an `ArrayAggregateLiteral`. |
| 27 | + */ |
| 28 | +Expr getLastElement(ArrayAggregateLiteral lit) { |
| 29 | + exists(int n | |
| 30 | + result = lit.getElementExpr(n, _) and |
| 31 | + not exists(lit.getElementExpr(n + 1, _)) |
| 32 | + ) |
| 33 | +} |
| 34 | + |
| 35 | +module CngBCryptEncryptHardcodedIVConfiguration implements DataFlow::ConfigSig { |
| 36 | + predicate isSource(DataFlow::Node source) { |
| 37 | + exists(AggregateLiteral lit | |
| 38 | + getLastElement(lit) = source.asDefinition() and |
| 39 | + exists(getConstElement(lit)) |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + predicate isSink(DataFlow::Node sink) { |
| 44 | + exists(FunctionCall call | |
| 45 | + // BCryptEncrypt 5h argument specifies the IV |
| 46 | + sink.asIndirectExpr() = call.getArgument(4) and |
| 47 | + call.getTarget().hasGlobalName("BCryptEncrypt") |
| 48 | + ) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +module Flow = DataFlow::Global<CngBCryptEncryptHardcodedIVConfiguration>; |
| 53 | + |
| 54 | +from DataFlow::Node sl, DataFlow::Node fc, AggregateLiteral lit |
| 55 | +where |
| 56 | + Flow::flow(sl, fc) and |
| 57 | + getLastElement(lit) = sl.asDefinition() |
| 58 | +select lit, "Calling BCryptEncrypt with a hard-coded IV on function " |
0 commit comments