|
| 1 | +/** |
| 2 | + * @name Weak Randomness |
| 3 | + * @description Using a weak source of randomness may allow an attacker to predict the generated values. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity error |
| 6 | + * @security-severity 8.6 |
| 7 | + * @precision high |
| 8 | + * @id java/weak-randomness |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-330 |
| 11 | + * external/cwe/cwe-338 |
| 12 | + */ |
| 13 | + |
| 14 | +import java |
| 15 | +import semmle.code.java.frameworks.Servlets |
| 16 | +import semmle.code.java.dataflow.TaintTracking |
| 17 | +import semmle.code.java.security.RandomQuery |
| 18 | +import WeakRandomnessFlow::PathGraph |
| 19 | + |
| 20 | +/** |
| 21 | + * The `java.util.Random` class. |
| 22 | + */ |
| 23 | +class TypeRandom extends RefType { |
| 24 | + TypeRandom() { this.hasQualifiedName("java.util", "Random") } |
| 25 | +} |
| 26 | + |
| 27 | +abstract class WeakRandomnessSource extends DataFlow::Node { } |
| 28 | + |
| 29 | +private class JavaRandomSource extends WeakRandomnessSource { |
| 30 | + JavaRandomSource() { |
| 31 | + this.asExpr().getType() instanceof TypeRandom and this.asExpr() instanceof ConstructorCall |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +private class MathRandomMethodAccess extends WeakRandomnessSource { |
| 36 | + MathRandomMethodAccess() { |
| 37 | + exists(MethodAccess ma | this.asExpr() = ma | |
| 38 | + ma.getMethod().hasName("random") and |
| 39 | + ma.getMethod().getDeclaringType().hasQualifiedName("java.lang", "Math") |
| 40 | + ) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +abstract private class SafeRandomImplementation extends RefType { } |
| 45 | + |
| 46 | +private class TypeSecureRandom extends SafeRandomImplementation { |
| 47 | + TypeSecureRandom() { this.hasQualifiedName("java.security", "SecureRandom") } |
| 48 | +} |
| 49 | + |
| 50 | +private class TypeHadoopOsSecureRandom extends SafeRandomImplementation { |
| 51 | + TypeHadoopOsSecureRandom() { |
| 52 | + this.hasQualifiedName("org.apache.hadoop.crypto.random", "OsSecureRandom") |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +abstract class WeakRandomnessAdditionalTaintStep extends Unit { |
| 57 | + abstract predicate step(DataFlow::Node node1, DataFlow::Node node2); |
| 58 | +} |
| 59 | + |
| 60 | +abstract class WeakRandomnessSink extends DataFlow::Node { } |
| 61 | + |
| 62 | +private class CookieSink extends WeakRandomnessSink { |
| 63 | + CookieSink() { |
| 64 | + this.asExpr().getType() instanceof TypeCookie and |
| 65 | + exists(MethodAccess ma | ma.getMethod().hasName("addCookie") | |
| 66 | + ma.getArgument(0) = this.asExpr() |
| 67 | + ) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Holds if there is a method access which converts `bytes` to the string `str`. |
| 73 | + */ |
| 74 | +private predicate covertsBytesToString(DataFlow::Node bytes, DataFlow::Node str) { |
| 75 | + bytes.getType().(Array).getElementType().(PrimitiveType).hasName("byte") and |
| 76 | + str.getType() instanceof TypeString and |
| 77 | + exists(MethodAccess ma | ma = str.asExpr() | bytes.asExpr() = ma.getAnArgument()) |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * A taint-tracking configuration for weak randomness. |
| 82 | + */ |
| 83 | +module WeakRandomnessConfig implements DataFlow::ConfigSig { |
| 84 | + predicate isSource(DataFlow::Node src) { src instanceof WeakRandomnessSource } |
| 85 | + |
| 86 | + predicate isSink(DataFlow::Node sink) { sink instanceof WeakRandomnessSink } |
| 87 | + |
| 88 | + predicate isBarrier(DataFlow::Node n) { n.getTypeBound() instanceof SafeRandomImplementation } |
| 89 | + |
| 90 | + predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) { |
| 91 | + exists(MethodAccess ma, Method m | |
| 92 | + n1.asExpr() = ma.getQualifier() and |
| 93 | + ma.getMethod() = m and |
| 94 | + m.getDeclaringType() instanceof TypeRandom and |
| 95 | + ( |
| 96 | + m.hasName(["nextInt", "nextLong", "nextFloat", "nextDouble", "nextBoolean", "nextGaussian"]) and |
| 97 | + n2.asExpr() = ma |
| 98 | + or |
| 99 | + m.hasName("nextBytes") and |
| 100 | + n2.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr() = ma.getArgument(0) |
| 101 | + ) |
| 102 | + ) |
| 103 | + or |
| 104 | + covertsBytesToString(n1, n2) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +module WeakRandomnessFlow = TaintTracking::Global<WeakRandomnessConfig>; |
| 109 | + |
| 110 | +from WeakRandomnessFlow::PathNode source, WeakRandomnessFlow::PathNode sink |
| 111 | +where WeakRandomnessFlow::flowPath(source, sink) |
| 112 | +select sink.getNode(), source, sink, "Potential weak randomness due to a $@.", source.getNode(), |
| 113 | + "weak randomness source." |
0 commit comments