|
| 1 | +import java |
| 2 | +import semmle.code.java.dataflow.FlowSources |
| 3 | +import semmle.code.java.dataflow.TaintTracking2 |
| 4 | +import DataFlow |
| 5 | + |
| 6 | +/** |
| 7 | + * A taint-tracking configuration for disabling revocation checking. |
| 8 | + */ |
| 9 | +class DisabledRevocationCheckingConfig extends TaintTracking::Configuration { |
| 10 | + DisabledRevocationCheckingConfig() { this = "DisabledRevocationCheckingConfig" } |
| 11 | + |
| 12 | + override predicate isSource(DataFlow::Node source) { |
| 13 | + exists(BooleanLiteral b | b.getBooleanValue() = false | source.asExpr() = b) |
| 14 | + } |
| 15 | + |
| 16 | + override predicate isSink(DataFlow::Node sink) { sink instanceof SetRevocationEnabledSink } |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * A sink that disables revocation checking, |
| 21 | + * i.e. calling `PKIXParameters.setRevocationEnabled(false)` |
| 22 | + * without setting a custom revocation checker in `PKIXParameters`. |
| 23 | + */ |
| 24 | +class SetRevocationEnabledSink extends DataFlow::ExprNode { |
| 25 | + SetRevocationEnabledSink() { |
| 26 | + exists(MethodAccess setRevocationEnabledCall | |
| 27 | + setRevocationEnabledCall.getMethod() instanceof SetRevocationEnabledMethod and |
| 28 | + setRevocationEnabledCall.getArgument(0) = getExpr() and |
| 29 | + not exists( |
| 30 | + SettingRevocationCheckerConfig config, DataFlow2::PathNode source, DataFlow2::PathNode sink |
| 31 | + | |
| 32 | + config.hasFlowPath(source, sink) and |
| 33 | + sink.getNode().(SettingRevocationCheckerSink).getVariable() = |
| 34 | + setRevocationEnabledCall.getQualifier().(VarAccess).getVariable() |
| 35 | + ) |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * A dataflow config for tracking a custom revocation checker. |
| 42 | + */ |
| 43 | +class SettingRevocationCheckerConfig extends DataFlow2::Configuration { |
| 44 | + SettingRevocationCheckerConfig() { |
| 45 | + this = "DisabledRevocationChecking::SettingRevocationCheckerConfig" |
| 46 | + } |
| 47 | + |
| 48 | + override predicate isSource(DataFlow::Node source) { |
| 49 | + source instanceof GetRevocationCheckerSource |
| 50 | + } |
| 51 | + |
| 52 | + override predicate isSink(DataFlow::Node sink) { sink instanceof SettingRevocationCheckerSink } |
| 53 | + |
| 54 | + override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 55 | + createSingletonListStep(node1, node2) or |
| 56 | + convertArrayToListStep(node1, node2) or |
| 57 | + addToListStep(node1, node2) |
| 58 | + } |
| 59 | + |
| 60 | + override int fieldFlowBranchLimit() { result = 0 } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * A source that creates a custom revocation checker, |
| 65 | + * i.e. `CertPathValidator.getRevocationChecker()`. |
| 66 | + */ |
| 67 | +class GetRevocationCheckerSource extends DataFlow::ExprNode { |
| 68 | + GetRevocationCheckerSource() { |
| 69 | + exists(MethodAccess ma | ma.getMethod() instanceof GetRevocationCheckerMethod | |
| 70 | + ma = asExpr() or ma.getQualifier() = asExpr() |
| 71 | + ) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * A sink that sets a custom revocation checker in `PKIXParameters`, |
| 77 | + * i.e. `PKIXParameters.addCertPathChecker()` or `PKIXParameters.setCertPathCheckers()`. |
| 78 | + */ |
| 79 | +class SettingRevocationCheckerSink extends DataFlow::ExprNode { |
| 80 | + MethodAccess ma; |
| 81 | + |
| 82 | + SettingRevocationCheckerSink() { |
| 83 | + ( |
| 84 | + ma.getMethod() instanceof AddCertPathCheckerMethod or |
| 85 | + ma.getMethod() instanceof SetCertPathCheckersMethod |
| 86 | + ) and |
| 87 | + ma.getArgument(0) = asExpr() |
| 88 | + } |
| 89 | + |
| 90 | + Variable getVariable() { result = ma.getQualifier().(VarAccess).getVariable() } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Holds if `node1` to `node2` is a dataflow step that creates a singleton list, |
| 95 | + * i.e. `Collections.singletonList(element)`. |
| 96 | + */ |
| 97 | +predicate createSingletonListStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 98 | + exists(StaticMethodAccess ma, Method m | m = ma.getMethod() | |
| 99 | + m.getDeclaringType() instanceof Collections and |
| 100 | + m.hasName("singletonList") and |
| 101 | + ma.getArgument(0) = node1.asExpr() and |
| 102 | + (ma = node2.asExpr() or ma.getQualifier() = node2.asExpr()) |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Holds if `node1` to `node2` is a dataflow step that converts an array to a list,class |
| 108 | + * i.e. `Arrays.asList(element)`. |
| 109 | + */ |
| 110 | +predicate convertArrayToListStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 111 | + exists(StaticMethodAccess ma, Method m | m = ma.getMethod() | |
| 112 | + m.getDeclaringType() instanceof Arrays and |
| 113 | + m.hasName("asList") and |
| 114 | + ma.getArgument(0) = node1.asExpr() and |
| 115 | + (ma = node2.asExpr() or ma.getQualifier() = node2.asExpr()) |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Holds if `node1` to `node2` is a dataflow step that adds an element to a list, |
| 121 | + * i.e. `list.add(element)` or `list.addAll(elements)`. |
| 122 | + */ |
| 123 | +predicate addToListStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 124 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 125 | + m.getDeclaringType() instanceof List and |
| 126 | + ( |
| 127 | + m.hasName("add") or |
| 128 | + m.hasName("addAll") |
| 129 | + ) and |
| 130 | + ma.getArgument(0) = node1.asExpr() and |
| 131 | + (ma = node2.asExpr() or ma.getQualifier() = node2.asExpr()) |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +class SetRevocationEnabledMethod extends Method { |
| 136 | + SetRevocationEnabledMethod() { |
| 137 | + getDeclaringType() instanceof PKIXParameters and |
| 138 | + hasName("setRevocationEnabled") |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +class GetRevocationCheckerMethod extends Method { |
| 143 | + GetRevocationCheckerMethod() { |
| 144 | + getDeclaringType() instanceof CertPathValidator and |
| 145 | + hasName("getRevocationChecker") |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +class AddCertPathCheckerMethod extends Method { |
| 150 | + AddCertPathCheckerMethod() { |
| 151 | + getDeclaringType() instanceof PKIXParameters and |
| 152 | + hasName("addCertPathChecker") |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +class SetCertPathCheckersMethod extends Method { |
| 157 | + SetCertPathCheckersMethod() { |
| 158 | + getDeclaringType() instanceof PKIXParameters and |
| 159 | + hasName("setCertPathCheckers") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +class PKIXParameters extends RefType { |
| 164 | + PKIXParameters() { hasQualifiedName("java.security.cert", "PKIXParameters") } |
| 165 | +} |
| 166 | + |
| 167 | +class CertPathValidator extends RefType { |
| 168 | + CertPathValidator() { hasQualifiedName("java.security.cert", "CertPathValidator") } |
| 169 | +} |
| 170 | + |
| 171 | +class Collections extends RefType { |
| 172 | + Collections() { hasQualifiedName("java.util", "Collections") } |
| 173 | +} |
| 174 | + |
| 175 | +class Arrays extends RefType { |
| 176 | + Arrays() { hasQualifiedName("java.util", "Arrays") } |
| 177 | +} |
| 178 | + |
| 179 | +class List extends ParameterizedInterface { |
| 180 | + List() { getGenericType().hasQualifiedName("java.util", "List") } |
| 181 | +} |
0 commit comments