Skip to content

Commit 510e280

Browse files
authored
Merge pull request #23131 from geoffw0/unifiedsensitive
Unified: Add a unified Swift sensitive data library and use it in unified/swift/cleartext-logging
2 parents fe4820a + 388f6e3 commit 510e280

4 files changed

Lines changed: 650 additions & 14 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Provides classes and predicates for identifying sensitive data.
3+
*
4+
* 'Sensitive' data is anything that should not be sent in unencrypted form. This library tries to
5+
* guess where sensitive data may either be stored in a variable or produced by a method.
6+
*/
7+
8+
import unified
9+
import codeql.concepts.internal.SensitiveDataHeuristics
10+
private import codeql.unified.internal.typeinference.TypeInference as TypeInference
11+
12+
/**
13+
* A data flow node that might contain sensitive data.
14+
*/
15+
cached
16+
abstract class SensitiveData extends DataFlow::Node {
17+
/**
18+
* Gets the classification of sensitive data this expression might contain.
19+
*/
20+
cached
21+
abstract SensitiveDataClassification getClassification();
22+
}
23+
24+
/**
25+
* An identifier that might contain sensitive data.
26+
*/
27+
private class SensitiveDataIdentifier extends SensitiveData {
28+
SensitiveDataClassification classification;
29+
30+
SensitiveDataIdentifier() {
31+
exists(IdentifierExpr e | e = this.asExpr() |
32+
HeuristicNames::nameIndicatesSensitiveData(e.getValue(), classification)
33+
)
34+
}
35+
36+
override SensitiveDataClassification getClassification() { result = classification }
37+
}
38+
39+
private Expr memberAccessParent(MemberAccessExpr e) { result = e.getParent() }
40+
41+
/**
42+
* A member access that might contain sensitive data. This includes a member itself and
43+
* components of a receiver chain, for example:
44+
* ```
45+
* object.password
46+
* password.value
47+
* password.getValue()
48+
* ```
49+
*/
50+
private class SensitiveDataMemberAccess extends SensitiveData {
51+
SensitiveDataClassification classification;
52+
53+
SensitiveDataMemberAccess() {
54+
exists(MemberAccessExpr e | memberAccessParent*(e) = this.asExpr() |
55+
HeuristicNames::nameIndicatesSensitiveData(e.getMemberName(), classification)
56+
)
57+
}
58+
59+
override SensitiveDataClassification getClassification() { result = classification }
60+
}
61+
62+
/**
63+
* A function call that might produce sensitive data.
64+
*/
65+
private class SensitiveDataCall extends SensitiveData {
66+
SensitiveDataClassification classification;
67+
68+
SensitiveDataCall() {
69+
exists(CallExpr call, string name |
70+
call = this.asExpr() and
71+
name =
72+
[
73+
call.getCallee().(Identifier).getValue(),
74+
call.getCallee().(MemberAccessExpr).getMemberName()
75+
] and
76+
HeuristicNames::nameIndicatesSensitiveData(name, classification)
77+
)
78+
}
79+
80+
override SensitiveDataClassification getClassification() { result = classification }
81+
}

‎unified/ql/src/queries/security/CWE-312/CleartextLogging.ql‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,19 @@
1717
// FIXME: This is a deliberately dumb and noisy version of the query used to exercise data flow early on.
1818
//
1919
import unified
20-
import codeql.concepts.internal.SensitiveDataHeuristics
20+
import codeql.unified.security.SensitiveData
2121

2222
private string getNameFromExpr(Expr e) {
2323
result = e.(IdentifierExpr).getValue()
2424
or
2525
result = e.(MemberAccessExpr).getMemberName()
2626
}
2727

28-
module DummyConfig implements DataFlow::ConfigSig {
29-
predicate isSource(DataFlow::Node node) {
30-
exists(string name |
31-
name = getNameFromExpr(node.asExpr()) and
32-
HeuristicNames::nameIndicatesSensitiveData(name)
33-
)
34-
}
28+
/**
29+
* A taint-tracking configuration for cleartext logging vulnerabilities.
30+
*/
31+
module CleartextLoggingConfig implements DataFlow::ConfigSig {
32+
predicate isSource(DataFlow::Node node) { node instanceof SensitiveData }
3533

3634
predicate isSink(DataFlow::Node node) {
3735
exists(CallExpr call |
@@ -43,10 +41,11 @@ module DummyConfig implements DataFlow::ConfigSig {
4341
predicate isBarrierIn(DataFlow::Node node) { isSource(node) }
4442
}
4543

46-
module DummyFlow = TaintTracking::Global<DummyConfig>;
44+
module CleartextLoggingFlow = TaintTracking::Global<CleartextLoggingConfig>;
4745

48-
import DummyFlow::PathGraph
46+
import CleartextLoggingFlow::PathGraph
4947

50-
from DummyFlow::PathNode source, DummyFlow::PathNode sink
51-
where DummyFlow::flowPath(source, sink)
52-
select sink.getNode(), source, sink, "Logging of $@", source.getNode(), "sensitive data"
48+
from CleartextLoggingFlow::PathNode source, CleartextLoggingFlow::PathNode sink
49+
where CleartextLoggingFlow::flowPath(source, sink)
50+
select source.getNode(), source, sink, "This sensitive information is to $@ to a log file.", sink,
51+
"written"

0 commit comments

Comments
 (0)