|
| 1 | +import java |
| 2 | +import semmle.code.java.dataflow.FlowSources |
| 3 | +import DataFlow |
| 4 | +import experimental.semmle.code.java.frameworks.Jndi |
| 5 | +import experimental.semmle.code.java.frameworks.spring.SpringJndi |
| 6 | +import experimental.semmle.code.java.frameworks.Shiro |
| 7 | + |
| 8 | +/** |
| 9 | + * A taint-tracking configuration for unvalidated user input that is used in JNDI lookup. |
| 10 | + */ |
| 11 | +class JndiInjectionFlowConfig extends TaintTracking::Configuration { |
| 12 | + JndiInjectionFlowConfig() { this = "JndiInjectionFlowConfig" } |
| 13 | + |
| 14 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 15 | + |
| 16 | + override predicate isSink(DataFlow::Node sink) { sink instanceof JndiInjectionSink } |
| 17 | + |
| 18 | + override predicate isSanitizer(DataFlow::Node node) { |
| 19 | + node.getType() instanceof PrimitiveType or node.getType() instanceof BoxedType |
| 20 | + } |
| 21 | + |
| 22 | + override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 23 | + compositeNameStep(node1, node2) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * JNDI sink for JNDI injection vulnerabilities, i.e. 1st argument to `lookup`, `lookupLink`, |
| 29 | + * `doLookup`, `rename`, `list` or `listBindings` method from `InitialContext`. |
| 30 | + */ |
| 31 | +predicate jndiSinkMethod(Method m, int index) { |
| 32 | + m.getDeclaringType().getAnAncestor() instanceof TypeInitialContext and |
| 33 | + ( |
| 34 | + m.hasName("lookup") or |
| 35 | + m.hasName("lookupLink") or |
| 36 | + m.hasName("doLookup") or |
| 37 | + m.hasName("rename") or |
| 38 | + m.hasName("list") or |
| 39 | + m.hasName("listBindings") |
| 40 | + ) and |
| 41 | + index = 0 |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Spring sink for JNDI injection vulnerabilities, i.e. 1st argument to `lookup` method from |
| 46 | + * Spring's `JndiTemplate`. |
| 47 | + */ |
| 48 | +predicate springSinkMethod(Method m, int index) { |
| 49 | + m.getDeclaringType() instanceof TypeSpringJndiTemplate and |
| 50 | + m.hasName("lookup") and |
| 51 | + index = 0 |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Apache Shiro sink for JNDI injection vulnerabilities, i.e. 1st argument to `lookup` method from |
| 56 | + * Shiro's `JndiTemplate`. |
| 57 | + */ |
| 58 | +predicate shiroSinkMethod(Method m, int index) { |
| 59 | + m.getDeclaringType() instanceof TypeShiroJndiTemplate and |
| 60 | + m.hasName("lookup") and |
| 61 | + index = 0 |
| 62 | +} |
| 63 | + |
| 64 | +/** Holds if parameter at index `index` in method `m` is JNDI injection sink. */ |
| 65 | +predicate jndiInjectionSinkMethod(Method m, int index) { |
| 66 | + jndiSinkMethod(m, index) or |
| 67 | + springSinkMethod(m, index) or |
| 68 | + shiroSinkMethod(m, index) |
| 69 | +} |
| 70 | + |
| 71 | +/** A data flow sink for unvalidated user input that is used in JNDI lookup. */ |
| 72 | +class JndiInjectionSink extends DataFlow::ExprNode { |
| 73 | + JndiInjectionSink() { |
| 74 | + exists(MethodAccess ma, Method m, int index | |
| 75 | + ma.getMethod() = m and |
| 76 | + ma.getArgument(index) = this.getExpr() and |
| 77 | + jndiInjectionSinkMethod(m, index) |
| 78 | + ) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Holds if `n1` to `n2` is a dataflow step that converts between `String` and `CompositeName`, |
| 84 | + * i.e. `new CompositeName(tainted)`. |
| 85 | + */ |
| 86 | +predicate compositeNameStep(ExprNode n1, ExprNode n2) { |
| 87 | + exists(ConstructorCall cc | cc.getConstructedType() instanceof TypeCompositeName | |
| 88 | + n1.asExpr() = cc.getAnArgument() and |
| 89 | + n2.asExpr() = cc |
| 90 | + ) |
| 91 | +} |
0 commit comments