|
| 1 | +/** Provides classes and predicates to reason about cleartext storage in serializable classes. */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.frameworks.JAXB |
| 5 | +import semmle.code.java.dataflow.DataFlow |
| 6 | +import semmle.code.java.dataflow.DataFlow2 |
| 7 | +import semmle.code.java.security.CleartextStorageQuery |
| 8 | +import semmle.code.java.security.CleartextStoragePropertiesQuery |
| 9 | + |
| 10 | +private class ClassCleartextStorageSink extends CleartextStorageSink { |
| 11 | + ClassCleartextStorageSink() { this.asExpr() = getInstanceInput(_, _) } |
| 12 | +} |
| 13 | + |
| 14 | +/** The instantiation of a storable class, which can be stored to disk. */ |
| 15 | +abstract class ClassStore extends Storable, ClassInstanceExpr { |
| 16 | + /** Gets an input, for example `input` in `instance.password = input`. */ |
| 17 | + override Expr getAnInput() { |
| 18 | + exists(ClassStoreFlowConfig conf, DataFlow::Node instance | |
| 19 | + conf.hasFlow(DataFlow::exprNode(this), instance) and |
| 20 | + result = getInstanceInput(instance, this.getConstructor().getDeclaringType()) |
| 21 | + ) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * The instantiation of a serializable class, which can be stored to disk. |
| 27 | + * |
| 28 | + * Only includes tainted instances where data from a `SensitiveSource` may flow |
| 29 | + * to an input of the `Serializable`. |
| 30 | + */ |
| 31 | +private class Serializable extends ClassStore { |
| 32 | + Serializable() { |
| 33 | + this.getConstructor().getDeclaringType().getASupertype*() instanceof TypeSerializable and |
| 34 | + // `Properties` are `Serializable`, but handled elsewhere. |
| 35 | + not this instanceof Properties and |
| 36 | + // restrict attention to tainted instances |
| 37 | + exists(SensitiveSource data | |
| 38 | + data.flowsToCached(getInstanceInput(_, this.getConstructor().getDeclaringType())) |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + /** Gets a store, for example `outputStream.writeObject(instance)`. */ |
| 43 | + override Expr getAStore() { |
| 44 | + exists(ClassStoreFlowConfig conf, DataFlow::Node n | |
| 45 | + serializableStore(n, result) and |
| 46 | + conf.hasFlow(DataFlow::exprNode(this), n) |
| 47 | + ) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** The instantiation of a marshallable class, which can be stored to disk as XML. */ |
| 52 | +private class Marshallable extends ClassStore { |
| 53 | + Marshallable() { this.getConstructor().getDeclaringType() instanceof JAXBElement } |
| 54 | + |
| 55 | + /** Gets a store, for example `marshaller.marshal(instance)`. */ |
| 56 | + override Expr getAStore() { |
| 57 | + exists(ClassStoreFlowConfig conf, DataFlow::Node n | |
| 58 | + marshallableStore(n, result) and |
| 59 | + conf.hasFlow(DataFlow::exprNode(this), n) |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** Gets an input, for example `input` in `instance.password = input`. */ |
| 65 | +private Expr getInstanceInput(DataFlow::Node instance, RefType t) { |
| 66 | + exists(AssignExpr a, FieldAccess fa | |
| 67 | + instance = DataFlow::getFieldQualifier(fa) and |
| 68 | + a.getDest() = fa and |
| 69 | + a.getSource() = result and |
| 70 | + fa.getField().getDeclaringType() = t |
| 71 | + | |
| 72 | + t.getASourceSupertype*() instanceof TypeSerializable or |
| 73 | + t instanceof JAXBElement |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +private class ClassStoreFlowConfig extends DataFlow2::Configuration { |
| 78 | + ClassStoreFlowConfig() { this = "ClassStoreFlowConfig" } |
| 79 | + |
| 80 | + override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof ClassStore } |
| 81 | + |
| 82 | + override predicate isSink(DataFlow::Node sink) { |
| 83 | + exists(getInstanceInput(sink, _)) or |
| 84 | + serializableStore(sink, _) or |
| 85 | + marshallableStore(sink, _) |
| 86 | + } |
| 87 | + |
| 88 | + override int fieldFlowBranchLimit() { result = 1 } |
| 89 | +} |
| 90 | + |
| 91 | +private predicate serializableStore(DataFlow::Node instance, Expr store) { |
| 92 | + exists(MethodAccess m | |
| 93 | + store = m and |
| 94 | + m.getMethod() instanceof WriteObjectMethod and |
| 95 | + instance.asExpr() = m.getArgument(0) |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +private predicate marshallableStore(DataFlow::Node instance, Expr store) { |
| 100 | + exists(MethodAccess m | |
| 101 | + store = m and |
| 102 | + m.getMethod() instanceof JAXBMarshalMethod and |
| 103 | + instance.asExpr() = m.getArgument(0) |
| 104 | + ) |
| 105 | +} |
0 commit comments