|
| 1 | +/** |
| 2 | + * @name Creating biased random numbers from cryptographically secure source. |
| 3 | + * @description Some mathematical operations on random numbers can cause bias in |
| 4 | + * the results and compromise security. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity warning |
| 7 | + * @precision high |
| 8 | + * @id js/biased-cryptographic-random |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-327 |
| 11 | + */ |
| 12 | + |
| 13 | +import javascript |
| 14 | +private import semmle.javascript.dataflow.InferredTypes |
| 15 | +private import semmle.javascript.dataflow.internal.StepSummary |
| 16 | + |
| 17 | +/** |
| 18 | + * Gets a Buffer/TypedArray containing cryptographically secure random numbers. |
| 19 | + */ |
| 20 | +private DataFlow::SourceNode randomBufferSource() { |
| 21 | + result = DataFlow::moduleMember("crypto", ["randomBytes", "randomFillSync"]).getACall() |
| 22 | + or |
| 23 | + exists(DataFlow::CallNode call | |
| 24 | + call = DataFlow::moduleMember("crypto", ["randomFill", "randomFillSync"]) and |
| 25 | + result = call.getArgument(0).getALocalSource() |
| 26 | + ) |
| 27 | + or |
| 28 | + result = DataFlow::globalVarRef("crypto").getAMethodCall("getRandomValues") |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Gets the pseudo-property used to track elements inside a Buffer. |
| 33 | + * The API for `Set` is close enough to the API for `Buffer` that we can reuse the type-tracking steps. |
| 34 | + */ |
| 35 | +private string prop() { result = DataFlow::PseudoProperties::setElement() } |
| 36 | + |
| 37 | +/** |
| 38 | + * Gets a reference to a cryptographically secure random number, type tracked using `t`. |
| 39 | + */ |
| 40 | +private DataFlow::SourceNode goodRandom(DataFlow::TypeTracker t) { |
| 41 | + t.startInProp(prop()) and |
| 42 | + result = randomBufferSource() |
| 43 | + or |
| 44 | + // Loading a number from a `Buffer`. |
| 45 | + exists(DataFlow::TypeTracker t2 | t = t2.append(LoadStep(prop())) | |
| 46 | + // the random generators return arrays/Buffers of random numbers, we therefore track through an indexed read. |
| 47 | + exists(DataFlow::PropRead read | |
| 48 | + read.getBase() = goodRandom(t2) and |
| 49 | + exists(read.getPropertyNameExpr()) |
| 50 | + ) |
| 51 | + or |
| 52 | + // reading a number from a Buffer. |
| 53 | + exists(DataFlow::MethodCallNode call | |
| 54 | + call.getReceiver().getALocalSource() = goodRandom(t.continue()) and |
| 55 | + call |
| 56 | + .getMethodName() |
| 57 | + .regexpMatch("read(BigInt|BigUInt|Double|Float|Int|UInt)(8|16|32|64)?(BE|LE)?") |
| 58 | + ) |
| 59 | + ) |
| 60 | + or |
| 61 | + exists(DataFlow::TypeTracker t2 | result = goodRandom(t2).track(t2, t)) |
| 62 | + or |
| 63 | + // re-using the collection steps for `Set`. |
| 64 | + exists(DataFlow::TypeTracker t2 | |
| 65 | + result = CollectionsTypeTracking::collectionStep(goodRandom(t2), t, t2) |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Gets a reference to a cryptographically random number. |
| 71 | + */ |
| 72 | +DataFlow::SourceNode goodRandom() { result = goodRandom(DataFlow::TypeTracker::end()) } |
| 73 | + |
| 74 | +/** |
| 75 | + * Gets a node that that produces a biased result from otherwise cryptographically secure random numbers. |
| 76 | + */ |
| 77 | +DataFlow::Node badCrypto(string description) { |
| 78 | + // addition and multiplication - always bad when both the lhs and rhs are random. |
| 79 | + exists(BinaryExpr binop | result.asExpr() = binop | |
| 80 | + goodRandom().flowsToExpr(binop.getLeftOperand()) and |
| 81 | + goodRandom().flowsToExpr(binop.getRightOperand()) and |
| 82 | + ( |
| 83 | + binop.getOperator() = "+" and description = "addition" |
| 84 | + or |
| 85 | + binop.getOperator() = "*" and description = "multiplication" |
| 86 | + ) |
| 87 | + ) |
| 88 | + or |
| 89 | + // division - always bad |
| 90 | + exists(DivExpr div | result.asExpr() = div | |
| 91 | + goodRandom().flowsToExpr(div.getLeftOperand()) and |
| 92 | + description = "division" |
| 93 | + ) |
| 94 | + or |
| 95 | + // modulo - only bad if not by a power of 2 - and the result is not checked for bias |
| 96 | + exists(ModExpr mod, DataFlow::SourceNode random | |
| 97 | + result.asExpr() = mod and mod.getOperator() = "%" |
| 98 | + | |
| 99 | + description = "modulo" and |
| 100 | + goodRandom() = random and |
| 101 | + random.flowsToExpr(mod.getLeftOperand()) and |
| 102 | + not mod.getRightOperand().getIntValue() = [2, 4, 8, 16, 32, 64, 128] and |
| 103 | + // not exists a comparison that checks if the result is potentially biased. |
| 104 | + not exists(BinaryExpr comparison | comparison.getOperator() = [">", "<", "<=", ">="] | |
| 105 | + AccessPath::getAnAliasedSourceNode(random).flowsToExpr(comparison.getAnOperand()) |
| 106 | + or |
| 107 | + exists(DataFlow::PropRead otherRead | |
| 108 | + otherRead = random.(DataFlow::PropRead).getBase().getALocalSource().getAPropertyRead() and |
| 109 | + not exists(otherRead.getPropertyName()) and |
| 110 | + otherRead.flowsToExpr(comparison.getAnOperand()) |
| 111 | + ) |
| 112 | + ) |
| 113 | + ) |
| 114 | + or |
| 115 | + // create a number from a string - always a bad idea. |
| 116 | + exists(DataFlow::CallNode number, StringOps::ConcatenationRoot root | result = number | |
| 117 | + number = DataFlow::globalVarRef(["Number", "parseInt", "parseFloat"]).getACall() and |
| 118 | + root = number.getArgument(0) and |
| 119 | + goodRandom().flowsTo(root.getALeaf()) and |
| 120 | + exists(root.getALeaf().getStringValue()) and |
| 121 | + description = "string concatenation" |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +from DataFlow::Node node, string description |
| 126 | +where node = badCrypto(description) |
| 127 | +select node, |
| 128 | + "Using " + description + " on cryptographically random numbers produces biased results." |
0 commit comments