Skip to content

Commit 02d670a

Browse files
committed
Rust: Make hasConstant behaviour on arrays consistent.
1 parent c0baa61 commit 02d670a

3 files changed

Lines changed: 17 additions & 19 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ module HardcodedCryptographicValue {
6262
abstract class Barrier extends DataFlow::Node { }
6363

6464
/**
65-
* Holds if `e` is a literal or a combination of literals that may be a constant.
65+
* Holds if `e` is a literal or an expression that contains a constant. For example:
66+
* ```
67+
* ["hello", "world", s]
68+
* ```
6669
*/
6770
private predicate hasConstant(Expr e) {
6871
e instanceof LiteralExpr // e.g. `0`
6972
or
70-
forex(Expr elem | elem = e.(ArrayListExpr).getExpr(_) | hasConstant(elem)) // e.g. `[0, 0, 0, 0]`
73+
exists(Expr elem | elem = e.(ArrayListExpr).getExpr(_) | hasConstant(elem)) // e.g. `[0, 0, 0, 0]`
7174
or
7275
hasConstant(e.(ArrayRepeatExpr).getRepeatOperand()) // e.g. `[0; 10]`
7376
or

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
| test_cookie.rs:17:28:17:34 | [0; 64] | test_cookie.rs:17:28:17:34 | [0; 64] | test_cookie.rs:18:26:18:32 | &array1 | This hard-coded value is used as $@. | test_cookie.rs:18:26:18:32 | &array1 | a key |
1010
| test_cookie.rs:21:28:21:34 | [0; 64] | test_cookie.rs:21:28:21:34 | [0; 64] | test_cookie.rs:22:26:22:32 | &array2 | This hard-coded value is used as $@. | test_cookie.rs:22:26:22:32 | &array2 | a key |
1111
| test_cookie.rs:25:16:29:5 | match ... { ... } | test_cookie.rs:25:16:29:5 | match ... { ... } | test_cookie.rs:30:26:30:40 | str3.as_bytes() | This hard-coded value is used as $@. | test_cookie.rs:30:26:30:40 | str3.as_bytes() | a key |
12-
| test_cookie.rs:34:9:34:9 | 1 | test_cookie.rs:34:9:34:9 | 1 | test_cookie.rs:38:26:38:32 | &array4 | This hard-coded value is used as $@. | test_cookie.rs:38:26:38:32 | &array4 | a key |
13-
| test_cookie.rs:35:9:35:9 | 2 | test_cookie.rs:35:9:35:9 | 2 | test_cookie.rs:38:26:38:32 | &array4 | This hard-coded value is used as $@. | test_cookie.rs:38:26:38:32 | &array4 | a key |
12+
| test_cookie.rs:33:27:37:5 | [...] | test_cookie.rs:33:27:37:5 | [...] | test_cookie.rs:38:26:38:32 | &array4 | This hard-coded value is used as $@. | test_cookie.rs:38:26:38:32 | &array4 | a key |
1413
| test_cookie.rs:54:28:54:36 | [0u8; 64] | test_cookie.rs:54:28:54:36 | [0u8; 64] | test_cookie.rs:58:34:58:39 | array2 | This hard-coded value is used as $@. | test_cookie.rs:58:34:58:39 | array2 | a key |
1514
| test_cookie.rs:65:23:65:25 | 0u8 | test_cookie.rs:65:23:65:25 | 0u8 | test_cookie.rs:69:34:69:39 | array3 | This hard-coded value is used as $@. | test_cookie.rs:69:34:69:39 | array3 | a key |
1615
| test_heuristic.rs:38:25:38:30 | 0xFFFF | test_heuristic.rs:38:25:38:30 | 0xFFFF | test_heuristic.rs:81:22:81:31 | MY_CONST_1 | This hard-coded value is used as $@. | test_heuristic.rs:81:22:81:31 | MY_CONST_1 | a salt |
@@ -72,12 +71,10 @@ edges
7271
| test_cookie.rs:25:16:29:5 | match ... { ... } | test_cookie.rs:25:9:25:12 | str3 | provenance | |
7372
| test_cookie.rs:30:26:30:29 | str3 | test_cookie.rs:30:26:30:40 | str3.as_bytes() [&ref] | provenance | MaD:13 |
7473
| test_cookie.rs:30:26:30:40 | str3.as_bytes() [&ref] | test_cookie.rs:30:26:30:40 | str3.as_bytes() | provenance | Sink:MaD:5 |
75-
| test_cookie.rs:33:9:33:14 | array4 [element] | test_cookie.rs:38:27:38:32 | array4 [element] | provenance | |
76-
| test_cookie.rs:33:27:37:5 | [...] [element] | test_cookie.rs:33:9:33:14 | array4 [element] | provenance | |
77-
| test_cookie.rs:34:9:34:9 | 1 | test_cookie.rs:33:27:37:5 | [...] [element] | provenance | |
78-
| test_cookie.rs:35:9:35:9 | 2 | test_cookie.rs:33:27:37:5 | [...] [element] | provenance | |
79-
| test_cookie.rs:38:26:38:32 | &array4 [&ref, element] | test_cookie.rs:38:26:38:32 | &array4 | provenance | Sink:MaD:5 |
80-
| test_cookie.rs:38:27:38:32 | array4 [element] | test_cookie.rs:38:26:38:32 | &array4 [&ref, element] | provenance | |
74+
| test_cookie.rs:33:9:33:14 | array4 | test_cookie.rs:38:27:38:32 | array4 | provenance | |
75+
| test_cookie.rs:33:27:37:5 | [...] | test_cookie.rs:33:9:33:14 | array4 | provenance | |
76+
| test_cookie.rs:38:26:38:32 | &array4 [&ref] | test_cookie.rs:38:26:38:32 | &array4 | provenance | Sink:MaD:5 |
77+
| test_cookie.rs:38:27:38:32 | array4 | test_cookie.rs:38:26:38:32 | &array4 [&ref] | provenance | |
8178
| test_cookie.rs:54:9:54:14 | array2 | test_cookie.rs:58:34:58:39 | array2 | provenance | Sink:MaD:1 |
8279
| test_cookie.rs:54:18:54:37 | ...::from(...) | test_cookie.rs:54:9:54:14 | array2 | provenance | |
8380
| test_cookie.rs:54:28:54:36 | [0u8; 64] | test_cookie.rs:54:18:54:37 | ...::from(...) | provenance | MaD:8 |
@@ -171,13 +168,11 @@ nodes
171168
| test_cookie.rs:30:26:30:29 | str3 | semmle.label | str3 |
172169
| test_cookie.rs:30:26:30:40 | str3.as_bytes() | semmle.label | str3.as_bytes() |
173170
| test_cookie.rs:30:26:30:40 | str3.as_bytes() [&ref] | semmle.label | str3.as_bytes() [&ref] |
174-
| test_cookie.rs:33:9:33:14 | array4 [element] | semmle.label | array4 [element] |
175-
| test_cookie.rs:33:27:37:5 | [...] [element] | semmle.label | [...] [element] |
176-
| test_cookie.rs:34:9:34:9 | 1 | semmle.label | 1 |
177-
| test_cookie.rs:35:9:35:9 | 2 | semmle.label | 2 |
171+
| test_cookie.rs:33:9:33:14 | array4 | semmle.label | array4 |
172+
| test_cookie.rs:33:27:37:5 | [...] | semmle.label | [...] |
178173
| test_cookie.rs:38:26:38:32 | &array4 | semmle.label | &array4 |
179-
| test_cookie.rs:38:26:38:32 | &array4 [&ref, element] | semmle.label | &array4 [&ref, element] |
180-
| test_cookie.rs:38:27:38:32 | array4 [element] | semmle.label | array4 [element] |
174+
| test_cookie.rs:38:26:38:32 | &array4 [&ref] | semmle.label | &array4 [&ref] |
175+
| test_cookie.rs:38:27:38:32 | array4 | semmle.label | array4 |
181176
| test_cookie.rs:54:9:54:14 | array2 | semmle.label | array2 |
182177
| test_cookie.rs:54:18:54:37 | ...::from(...) | semmle.label | ...::from(...) |
183178
| test_cookie.rs:54:28:54:36 | [0u8; 64] | semmle.label | [0u8; 64] |

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ fn test_cookie_jar(array_var: &[u8], val: u64) {
3131
_ = jar.signed_mut(&key3);
3232

3333
let array4: [u8; 3] = [
34-
1, // $ Alert[rust/hard-coded-cryptographic-value]
35-
2, // $ Alert[rust/hard-coded-cryptographic-value]
34+
1,
35+
2,
3636
val as u8
37-
];
37+
]; // $ Alert[rust/hard-coded-cryptographic-value]
3838
let key4 = Key::from(&array4); // $ Sink
3939
_ = jar.signed_mut(&key4);
4040
}

0 commit comments

Comments
 (0)