Skip to content

Commit 9e54d53

Browse files
committed
Rust: Add barrier.
1 parent a34f9be commit 9e54d53

File tree

3 files changed

+18
-28
lines changed

3 files changed

+18
-28
lines changed

rust/ql/lib/codeql/rust/security/HardcodedCryptographicValueExtensions.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,18 @@ module HardcodedCryptographicValue {
8989

9090
override CryptographicValueKind getKind() { result = kind }
9191
}
92+
93+
/**
94+
* A call to `getrandom` that is a barrier.
95+
*/
96+
private class GetRandomBarrier extends Barrier {
97+
GetRandomBarrier() {
98+
exists(CallExpr ce |
99+
ce.getFunction().(PathExpr).getResolvedCrateOrigin() =
100+
"repo:https://github.com/rust-random/getrandom:getrandom" and
101+
ce.getFunction().(PathExpr).getResolvedPath() = ["crate::fill", "crate::getrandom"] and
102+
this.asExpr().getExpr().getParentNode*() = ce.getArgList().getArg(0)
103+
)
104+
}
105+
}
92106
}

rust/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue.expected

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
| test_cipher.rs:125:19:125:19 | 0 | test_cipher.rs:125:19:125:19 | 0 | test_cipher.rs:127:21:127:27 | encrypt | This hard-coded value is used as $@. | test_cipher.rs:127:21:127:27 | encrypt | a nonce |
1515
| test_cipher.rs:129:34:129:37 | 0xff | test_cipher.rs:129:34:129:37 | 0xff | test_cipher.rs:132:19:132:32 | ...::new | This hard-coded value is used as $@. | test_cipher.rs:132:19:132:32 | ...::new | a key |
1616
| test_cipher.rs:131:28:131:31 | 0xff | test_cipher.rs:131:28:131:31 | 0xff | test_cipher.rs:133:21:133:27 | encrypt | This hard-coded value is used as $@. | test_cipher.rs:133:21:133:27 | encrypt | a nonce |
17-
| test_cipher.rs:137:21:137:23 | 0u8 | test_cipher.rs:137:21:137:23 | 0u8 | test_cipher.rs:141:19:141:32 | ...::new | This hard-coded value is used as $@. | test_cipher.rs:141:19:141:32 | ...::new | a key |
18-
| test_cipher.rs:138:23:138:25 | 0u8 | test_cipher.rs:138:23:138:25 | 0u8 | test_cipher.rs:142:21:142:27 | encrypt | This hard-coded value is used as $@. | test_cipher.rs:142:21:142:27 | encrypt | a nonce |
1917
edges
2018
| test_cipher.rs:18:9:18:14 | const1 [&ref, element] | test_cipher.rs:19:73:19:78 | const1 [&ref, element] | provenance | |
2119
| test_cipher.rs:18:28:18:36 | &... [&ref, element] | test_cipher.rs:18:9:18:14 | const1 [&ref, element] | provenance | |
@@ -109,16 +107,6 @@ edges
109107
| test_cipher.rs:132:35:132:38 | key3 [&ref, element] | test_cipher.rs:132:34:132:38 | &key3 [&ref, &ref, element] | provenance | |
110108
| test_cipher.rs:133:29:133:42 | &... [&ref, element] | test_cipher.rs:133:21:133:27 | encrypt | provenance | MaD:94 Sink:MaD:94 Sink:MaD:94 |
111109
| test_cipher.rs:133:30:133:42 | nonce3.into(...) [element] | test_cipher.rs:133:29:133:42 | &... [&ref, element] | provenance | |
112-
| test_cipher.rs:137:9:137:16 | mut key4 [element] | test_cipher.rs:141:35:141:45 | key4.into(...) [element] | provenance | MaD:105 |
113-
| test_cipher.rs:137:20:137:27 | [0u8; 32] [element] | test_cipher.rs:137:9:137:16 | mut key4 [element] | provenance | |
114-
| test_cipher.rs:137:21:137:23 | 0u8 | test_cipher.rs:137:20:137:27 | [0u8; 32] [element] | provenance | |
115-
| test_cipher.rs:138:9:138:18 | mut nonce4 [element] | test_cipher.rs:142:30:142:42 | nonce4.into(...) [element] | provenance | MaD:105 |
116-
| test_cipher.rs:138:22:138:29 | [0u8; 12] [element] | test_cipher.rs:138:9:138:18 | mut nonce4 [element] | provenance | |
117-
| test_cipher.rs:138:23:138:25 | 0u8 | test_cipher.rs:138:22:138:29 | [0u8; 12] [element] | provenance | |
118-
| test_cipher.rs:141:34:141:45 | &... [&ref, element] | test_cipher.rs:141:19:141:32 | ...::new | provenance | MaD:93 Sink:MaD:93 Sink:MaD:93 |
119-
| test_cipher.rs:141:35:141:45 | key4.into(...) [element] | test_cipher.rs:141:34:141:45 | &... [&ref, element] | provenance | |
120-
| test_cipher.rs:142:29:142:42 | &... [&ref, element] | test_cipher.rs:142:21:142:27 | encrypt | provenance | MaD:94 Sink:MaD:94 Sink:MaD:94 |
121-
| test_cipher.rs:142:30:142:42 | nonce4.into(...) [element] | test_cipher.rs:142:29:142:42 | &... [&ref, element] | provenance | |
122110
nodes
123111
| test_cipher.rs:18:9:18:14 | const1 [&ref, element] | semmle.label | const1 [&ref, element] |
124112
| test_cipher.rs:18:28:18:36 | &... [&ref, element] | semmle.label | &... [&ref, element] |
@@ -223,16 +211,4 @@ nodes
223211
| test_cipher.rs:133:21:133:27 | encrypt | semmle.label | encrypt |
224212
| test_cipher.rs:133:29:133:42 | &... [&ref, element] | semmle.label | &... [&ref, element] |
225213
| test_cipher.rs:133:30:133:42 | nonce3.into(...) [element] | semmle.label | nonce3.into(...) [element] |
226-
| test_cipher.rs:137:9:137:16 | mut key4 [element] | semmle.label | mut key4 [element] |
227-
| test_cipher.rs:137:20:137:27 | [0u8; 32] [element] | semmle.label | [0u8; 32] [element] |
228-
| test_cipher.rs:137:21:137:23 | 0u8 | semmle.label | 0u8 |
229-
| test_cipher.rs:138:9:138:18 | mut nonce4 [element] | semmle.label | mut nonce4 [element] |
230-
| test_cipher.rs:138:22:138:29 | [0u8; 12] [element] | semmle.label | [0u8; 12] [element] |
231-
| test_cipher.rs:138:23:138:25 | 0u8 | semmle.label | 0u8 |
232-
| test_cipher.rs:141:19:141:32 | ...::new | semmle.label | ...::new |
233-
| test_cipher.rs:141:34:141:45 | &... [&ref, element] | semmle.label | &... [&ref, element] |
234-
| test_cipher.rs:141:35:141:45 | key4.into(...) [element] | semmle.label | key4.into(...) [element] |
235-
| test_cipher.rs:142:21:142:27 | encrypt | semmle.label | encrypt |
236-
| test_cipher.rs:142:29:142:42 | &... [&ref, element] | semmle.label | &... [&ref, element] |
237-
| test_cipher.rs:142:30:142:42 | nonce4.into(...) [element] | semmle.label | nonce4.into(...) [element] |
238214
subpaths

rust/ql/test/query-tests/security/CWE-798/test_cipher.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ fn test_aes_gcm(
134134

135135
// with barrier
136136

137-
let mut key4 = [0u8;32]; // $ SPURIOUS: Alert[rust/hardcoded-cryptographic-value]
138-
let mut nonce4 = [0u8;12]; // $ SPURIOUS: Alert[rust/hardcoded-cryptographic-value]
137+
let mut key4 = [0u8;32];
138+
let mut nonce4 = [0u8;12];
139139
_ = getrandom::fill(&mut key4).unwrap();
140140
_ = getrandom2::getrandom(&mut nonce4).unwrap();
141-
let cipher4 = Aes256Gcm::new(&key4.into()); // $ Sink
142-
let _ = cipher2.encrypt(&nonce4.into(), b"plaintext".as_ref()).unwrap(); // $ Sink
141+
let cipher4 = Aes256Gcm::new(&key4.into());
142+
let _ = cipher2.encrypt(&nonce4.into(), b"plaintext".as_ref()).unwrap();
143143
}

0 commit comments

Comments
 (0)