Skip to content

Commit

Permalink
Improve source of randomness detection
Browse files Browse the repository at this point in the history
Also sanitize flow out of sinks to avoid overlapping paths
  • Loading branch information
atorralba committed Nov 8, 2023
1 parent cf401d4 commit bafbdc5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions java/ql/lib/semmle/code/java/security/RandomDataSource.qll
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import java
private import semmle.code.java.dataflow.TypeFlow

/**
* A method access that returns random data or writes random data to an argument.
Expand Down Expand Up @@ -43,6 +44,9 @@ abstract class RandomDataSource extends MethodCall {
* in the case where it writes random data to that argument.
*/
abstract Expr getOutput();

/** Gets the type of the source of randomness used by this call. */
RefType getSourceOfRandomness() { boundOrStaticType(this.getQualifier(), result) }
}

/**
Expand Down Expand Up @@ -167,4 +171,18 @@ class ApacheCommonsRandomStringSource extends RandomDataSource {
}

override Expr getOutput() { result = this }

override RefType getSourceOfRandomness() {
if
this.getMethod().hasStringSignature("random(int, int, int, boolean, boolean, char[], Random)")
then boundOrStaticType(this.getArgument(6), result)
else result.hasQualifiedName("java.util", "Random")
}
}

/** Holds if `t` is the static type of `e`, or an upper bound of the runtime type of `e`. */
private predicate boundOrStaticType(Expr e, RefType t) {
exprTypeFlow(e, t, false)
or
t = e.getType()
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class WeakRandomnessSource extends DataFlow::Node { }
private class RandomMethodSource extends WeakRandomnessSource {
RandomMethodSource() {
exists(RandomDataSource s | this.asExpr() = s.getOutput() |
not s.getQualifier().getType() instanceof SafeRandomImplementation
not s.getSourceOfRandomness() instanceof SafeRandomImplementation
)
}
}
Expand Down Expand Up @@ -70,6 +70,8 @@ module WeakRandomnessConfig implements DataFlow::ConfigSig {

predicate isBarrierIn(DataFlow::Node n) { isSource(n) }

predicate isBarrierOut(DataFlow::Node n) { isSink(n) }

predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
n1.asExpr() = n2.asExpr().(BinaryExpr).getAnOperand()
or
Expand Down

0 comments on commit bafbdc5

Please sign in to comment.