Skip to content

Commit 3363235

Browse files
authored
Merge pull request #18414 from geoffw0/sensitive
Rust: Sensitive data library
2 parents af15eba + 9d178ab commit 3363235

File tree

12 files changed

+484
-1
lines changed

12 files changed

+484
-1
lines changed

config/identical-files.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@
247247
"javascript/ql/lib/semmle/javascript/security/internal/SensitiveDataHeuristics.qll",
248248
"python/ql/lib/semmle/python/security/internal/SensitiveDataHeuristics.qll",
249249
"ruby/ql/lib/codeql/ruby/security/internal/SensitiveDataHeuristics.qll",
250-
"swift/ql/lib/codeql/swift/security/internal/SensitiveDataHeuristics.qll"
250+
"swift/ql/lib/codeql/swift/security/internal/SensitiveDataHeuristics.qll",
251+
"rust/ql/lib/codeql/rust/security/internal/SensitiveDataHeuristics.qll"
251252
],
252253
"IncompleteUrlSubstringSanitization": [
253254
"javascript/ql/src/Security/CWE-020/IncompleteUrlSubstringSanitization.qll",

rust/ql/integration-tests/hello-project/summary.expected

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
| Macro calls - resolved | 2 |
1515
| Macro calls - total | 2 |
1616
| Macro calls - unresolved | 0 |
17+
| Sensitive data | 0 |
1718
| Taint sources - active | 0 |
1819
| Taint sources - total | 0 |

rust/ql/integration-tests/hello-workspace/summary.cargo.expected

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
| Macro calls - resolved | 2 |
1515
| Macro calls - total | 2 |
1616
| Macro calls - unresolved | 0 |
17+
| Sensitive data | 0 |
1718
| Taint sources - active | 0 |
1819
| Taint sources - total | 0 |

rust/ql/integration-tests/hello-workspace/summary.rust-project.expected

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
| Macro calls - resolved | 2 |
1515
| Macro calls - total | 2 |
1616
| Macro calls - unresolved | 0 |
17+
| Sensitive data | 0 |
1718
| Taint sources - active | 0 |
1819
| Taint sources - total | 0 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 rust
9+
private import internal.SensitiveDataHeuristics
10+
private import codeql.rust.dataflow.DataFlow
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+
* A function that might produce sensitive data.
26+
*/
27+
private class SensitiveDataFunction extends Function {
28+
SensitiveDataClassification classification;
29+
30+
SensitiveDataFunction() {
31+
HeuristicNames::nameIndicatesSensitiveData(this.getName().getText(), classification)
32+
}
33+
34+
SensitiveDataClassification getClassification() { result = classification }
35+
}
36+
37+
/**
38+
* A function call data flow node that might produce sensitive data.
39+
*/
40+
private class SensitiveDataCall extends SensitiveData {
41+
SensitiveDataClassification classification;
42+
43+
SensitiveDataCall() {
44+
classification =
45+
this.asExpr()
46+
.getAstNode()
47+
.(CallExprBase)
48+
.getStaticTarget()
49+
.(SensitiveDataFunction)
50+
.getClassification()
51+
}
52+
53+
override SensitiveDataClassification getClassification() { result = classification }
54+
}
55+
56+
/**
57+
* A variable that might contain sensitive data.
58+
*/
59+
private class SensitiveDataVariable extends Variable {
60+
SensitiveDataClassification classification;
61+
62+
SensitiveDataVariable() {
63+
HeuristicNames::nameIndicatesSensitiveData(this.getName(), classification)
64+
}
65+
66+
SensitiveDataClassification getClassification() { result = classification }
67+
}
68+
69+
/**
70+
* A variable access data flow node that might produce sensitive data.
71+
*/
72+
private class SensitiveVariableAccess extends SensitiveData {
73+
SensitiveDataClassification classification;
74+
75+
SensitiveVariableAccess() {
76+
classification =
77+
this.asExpr()
78+
.getAstNode()
79+
.(VariableAccess)
80+
.getVariable()
81+
.(SensitiveDataVariable)
82+
.getClassification()
83+
}
84+
85+
override SensitiveDataClassification getClassification() { result = classification }
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/**
2+
* INTERNAL: Do not use.
3+
*
4+
* Provides classes and predicates for identifying strings that may indicate the presence of sensitive data.
5+
* Such that we can share this logic across our CodeQL analysis of different languages.
6+
*
7+
* 'Sensitive' data in general is anything that should not be sent around in unencrypted form.
8+
*/
9+
10+
/**
11+
* A classification of different kinds of sensitive data:
12+
*
13+
* - secret: generic secret or trusted data;
14+
* - id: a user name or other account information;
15+
* - password: a password or authorization key;
16+
* - certificate: a certificate.
17+
* - private: private data such as credit card numbers
18+
*
19+
* While classifications are represented as strings, this should not be relied upon.
20+
* Instead, use the predicates in `SensitiveDataClassification::` to work with
21+
* classifications.
22+
*/
23+
class SensitiveDataClassification extends string {
24+
SensitiveDataClassification() { this in ["secret", "id", "password", "certificate", "private"] }
25+
}
26+
27+
/**
28+
* Provides predicates to select the different kinds of sensitive data we support.
29+
*/
30+
module SensitiveDataClassification {
31+
/** Gets the classification for secret or trusted data. */
32+
SensitiveDataClassification secret() { result = "secret" }
33+
34+
/** Gets the classification for user names or other account information. */
35+
SensitiveDataClassification id() { result = "id" }
36+
37+
/** Gets the classification for passwords or authorization keys. */
38+
SensitiveDataClassification password() { result = "password" }
39+
40+
/** Gets the classification for certificates. */
41+
SensitiveDataClassification certificate() { result = "certificate" }
42+
43+
/** Gets the classification for private data. */
44+
SensitiveDataClassification private() { result = "private" }
45+
}
46+
47+
/**
48+
* INTERNAL: Do not use.
49+
*
50+
* Provides heuristics for identifying names related to sensitive information.
51+
*/
52+
module HeuristicNames {
53+
/**
54+
* Gets a regular expression that identifies strings that may indicate the presence of secret
55+
* or trusted data.
56+
*/
57+
string maybeSecret() { result = "(?is).*((?<!is|is_)secret|(?<!un|un_|is|is_)trusted).*" }
58+
59+
/**
60+
* Gets a regular expression that identifies strings that may indicate the presence of
61+
* user names or other account information.
62+
*/
63+
string maybeAccountInfo() {
64+
result = "(?is).*acc(ou)?nt.*" or
65+
result = "(?is).*(puid|username|userid|session(id|key)).*" or
66+
result = "(?s).*([uU]|^|_|[a-z](?=U))([uU][iI][dD]).*"
67+
}
68+
69+
/**
70+
* Gets a regular expression that identifies strings that may indicate the presence of
71+
* a password or an authorization key.
72+
*/
73+
string maybePassword() {
74+
result = "(?is).*pass(wd|word|code|phrase)(?!.*question).*" or
75+
result = "(?is).*(auth(entication|ori[sz]ation)?)key.*"
76+
}
77+
78+
/**
79+
* Gets a regular expression that identifies strings that may indicate the presence of
80+
* a certificate.
81+
*/
82+
string maybeCertificate() { result = "(?is).*(cert)(?!.*(format|name|ification)).*" }
83+
84+
/**
85+
* Gets a regular expression that identifies strings that may indicate the presence of
86+
* private data.
87+
*/
88+
string maybePrivate() {
89+
result =
90+
"(?is).*(" +
91+
// Inspired by the list on https://cwe.mitre.org/data/definitions/359.html
92+
// Government identifiers, such as Social Security Numbers
93+
"social.?security|employer.?identification|national.?insurance|resident.?id|" +
94+
"passport.?(num|no)|([_-]|\\b)ssn([_-]|\\b)|" +
95+
// Contact information, such as home addresses
96+
"post.?code|zip.?code|home.?addr|" +
97+
// and telephone numbers
98+
"(mob(ile)?|home).?(num|no|tel|phone)|(tel|fax|phone).?(num|no)|telephone|" +
99+
"emergency.?contact|" +
100+
// Geographic location - where the user is (or was)
101+
"latitude|longitude|nationality|" +
102+
// Financial data - such as credit card numbers, salary, bank accounts, and debts
103+
"(credit|debit|bank|visa).?(card|num|no|acc(ou)?nt)|acc(ou)?nt.?(no|num|credit)|" +
104+
"salary|billing|credit.?(rating|score)|([_-]|\\b)ccn([_-]|\\b)|" +
105+
// Communications - e-mail addresses, private e-mail messages, SMS text messages, chat logs, etc.
106+
// "e(mail|_mail)|" + // this seems too noisy
107+
// Health - medical conditions, insurance status, prescription records
108+
"birth.?da(te|y)|da(te|y).?(of.?)?birth|" +
109+
"medical|(health|care).?plan|healthkit|appointment|prescription|" +
110+
"blood.?(type|alcohol|glucose|pressure)|heart.?(rate|rhythm)|body.?(mass|fat)|" +
111+
"menstrua|pregnan|insulin|inhaler|" +
112+
// Relationships - work and family
113+
"employ(er|ee)|spouse|maiden.?name" +
114+
// ---
115+
").*"
116+
}
117+
118+
/**
119+
* Gets a regular expression that identifies strings that may indicate the presence
120+
* of sensitive data, with `classification` describing the kind of sensitive data involved.
121+
*/
122+
string maybeSensitiveRegexp(SensitiveDataClassification classification) {
123+
result = maybeSecret() and classification = SensitiveDataClassification::secret()
124+
or
125+
result = maybeAccountInfo() and classification = SensitiveDataClassification::id()
126+
or
127+
result = maybePassword() and classification = SensitiveDataClassification::password()
128+
or
129+
result = maybeCertificate() and
130+
classification = SensitiveDataClassification::certificate()
131+
or
132+
result = maybePrivate() and
133+
classification = SensitiveDataClassification::private()
134+
}
135+
136+
/**
137+
* Gets a regular expression that identifies strings that may indicate the presence of data
138+
* that is hashed or encrypted, and hence rendered non-sensitive, or contains special characters
139+
* suggesting nouns within the string do not represent the meaning of the whole string (e.g. a URL or a SQL query).
140+
*
141+
* We also filter out common words like `certain` and `concert`, since otherwise these could
142+
* be matched by the certificate regular expressions. Same for `accountable` (account), or
143+
* `secretarial` (secret).
144+
*/
145+
string notSensitiveRegexp() {
146+
result =
147+
"(?is).*([^\\w$.-]|redact|censor|obfuscate|hash|md5|sha|random|((?<!un)(en))?(crypt|(?<!pass)code)|certain|concert|secretar|accountant|accountab).*"
148+
}
149+
150+
/**
151+
* Holds if `name` may indicate the presence of sensitive data, and `name` does not indicate that
152+
* the data is in fact non-sensitive (for example since it is hashed or encrypted).
153+
*
154+
* That is, one of the regexps from `maybeSensitiveRegexp` matches `name` (with the given
155+
* classification), and none of the regexps from `notSensitiveRegexp` matches `name`.
156+
*/
157+
bindingset[name]
158+
predicate nameIndicatesSensitiveData(string name) {
159+
exists(string combinedRegexp |
160+
// Combine all the maybe-sensitive regexps into one using non-capturing groups and |.
161+
combinedRegexp =
162+
"(?:" + strictconcat(string r | r = maybeSensitiveRegexp(_) | r, ")|(?:") + ")"
163+
|
164+
name.regexpMatch(combinedRegexp)
165+
) and
166+
not name.regexpMatch(notSensitiveRegexp())
167+
}
168+
169+
/**
170+
* Holds if `name` may indicate the presence of sensitive data, and
171+
* `name` does not indicate that the data is in fact non-sensitive (for example since
172+
* it is hashed or encrypted). `classification` describes the kind of sensitive data
173+
* involved.
174+
*
175+
* That is, one of the regexps from `maybeSensitiveRegexp` matches `name` (with the
176+
* given classification), and none of the regexps from `notSensitiveRegexp` matches
177+
* `name`.
178+
*
179+
* When the set of names is large, it's worth using `nameIndicatesSensitiveData/1` as a first
180+
* pass, since that combines all the regexps into one, and should be faster. Then call this
181+
* predicate to get the classification(s).
182+
*/
183+
bindingset[name]
184+
predicate nameIndicatesSensitiveData(string name, SensitiveDataClassification classification) {
185+
name.regexpMatch(maybeSensitiveRegexp(classification)) and
186+
not name.regexpMatch(notSensitiveRegexp())
187+
}
188+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Sensitive Data
3+
* @description List all sensitive data found in the database. Sensitive data is anything that
4+
* should not be sent in unencrypted form.
5+
* @kind problem
6+
* @problem.severity info
7+
* @id rust/summary/sensitive-data
8+
* @tags summary
9+
*/
10+
11+
import rust
12+
import codeql.rust.security.SensitiveData
13+
14+
from SensitiveData d
15+
select d, "Sensitive data (" + d.getClassification() + "): " + d.toString()

rust/ql/src/queries/summary/SummaryStats.ql

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import rust
1010
import codeql.rust.Concepts
11+
import codeql.rust.security.SensitiveData
1112
import codeql.rust.Diagnostics
1213
import Stats
1314

@@ -56,4 +57,6 @@ where
5657
key = "Taint sources - total" and value = count(ThreatModelSource s)
5758
or
5859
key = "Taint sources - active" and value = count(ActiveThreatModelSource s)
60+
or
61+
key = "Sensitive data" and value = count(SensitiveData d)
5962
select key, value order by key

rust/ql/test/library-tests/sensitivedata/SensitiveData.expected

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import rust
2+
import codeql.rust.dataflow.DataFlow
3+
import codeql.rust.dataflow.TaintTracking
4+
import codeql.rust.security.SensitiveData
5+
import utils.test.InlineExpectationsTest
6+
7+
/**
8+
* Configuration for flow from any sensitive data source to an argument of the function `sink`.
9+
*/
10+
module SensitiveDataConfig implements DataFlow::ConfigSig {
11+
predicate isSource(DataFlow::Node source) { source instanceof SensitiveData }
12+
13+
predicate isSink(DataFlow::Node sink) {
14+
any(CallExpr call | call.getFunction().(PathExpr).getResolvedPath() = "crate::test::sink")
15+
.getArgList()
16+
.getAnArg() = sink.asExpr().getExpr()
17+
}
18+
}
19+
20+
module SensitiveDataFlow = TaintTracking::Global<SensitiveDataConfig>;
21+
22+
module SensitiveDataTest implements TestSig {
23+
string getARelevantTag() { result = "sensitive" }
24+
25+
predicate hasActualResult(Location location, string element, string tag, string value) {
26+
exists(DataFlow::Node source, DataFlow::Node sink |
27+
SensitiveDataFlow::flow(source, sink) and
28+
location = sink.getLocation() and
29+
element = sink.toString() and
30+
tag = "sensitive" and
31+
value = source.(SensitiveData).getClassification()
32+
)
33+
}
34+
}
35+
36+
import MakeTest<SensitiveDataTest>

0 commit comments

Comments
 (0)