Skip to content

Commit aafdf1a

Browse files
committed
Rust: Update StreamCipherInit to use getCanonicalPath.
1 parent ed3a33f commit aafdf1a

File tree

3 files changed

+28
-44
lines changed

3 files changed

+28
-44
lines changed

rust/ql/lib/codeql/rust/frameworks/rustcrypto/RustCrypto.qll

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
private import rust
66
private import codeql.rust.Concepts
77
private import codeql.rust.dataflow.DataFlow
8+
import codeql.rust.internal.TypeInference
9+
import codeql.rust.internal.Type
810

911
bindingset[algorithmName]
1012
private string simplifyAlgorithmName(string algorithmName) {
@@ -21,28 +23,21 @@ class StreamCipherInit extends Cryptography::CryptographicOperation::Range {
2123

2224
StreamCipherInit() {
2325
// a call to `cipher::KeyInit::new`, `cipher::KeyInit::new_from_slice`,
24-
// `cipher::KeyIvInit::new`, `cipher::KeyIvInit::new_from_slices` or `rc2::Rc2::new_with_eff_key_len`.
25-
exists(PathExpr p, string rawAlgorithmName |
26-
this.asExpr().getExpr().(CallExpr).getFunction() = p and
27-
p.getResolvedCrateOrigin().matches("%/RustCrypto%") and
28-
p.getPath().getText() = ["new", "new_from_slice", "new_from_slices", "new_with_eff_key_len"] and
29-
(
30-
rawAlgorithmName = p.getPath().getQualifier().getText() or
26+
// `cipher::KeyIvInit::new`, `cipher::KeyIvInit::new_from_slices`, `rc2::Rc2::new_with_eff_key_len` or similar.
27+
exists(CallExprBase ce, string rawAlgorithmName |
28+
ce = this.asExpr().getExpr() and
29+
ce.getStaticTarget()
30+
.getCanonicalPath()
31+
.matches(["%::new", "%::new_from_slice", "%::new_with_eff_key_len", "%::new_from_slices"]) and
32+
// extract the algorithm name from the type of `ce` or its receiver.
33+
exists(Type t, TypePath tp |
34+
t = inferType([ce, ce.(MethodCallExpr).getReceiver()], tp) and
3135
rawAlgorithmName =
32-
p.getPath()
33-
.getQualifier()
34-
.getSegment()
35-
.getGenericArgList()
36-
.getGenericArg(0)
37-
.(TypeArg)
38-
.getTypeRepr()
39-
.(PathTypeRepr)
40-
.getPath()
41-
.getSegment()
42-
.getIdentifier()
43-
.getText()
36+
t.(StructType).asItemNode().(Addressable).getCanonicalPath().splitAt("::")
4437
) and
45-
algorithmName = simplifyAlgorithmName(rawAlgorithmName)
38+
algorithmName = simplifyAlgorithmName(rawAlgorithmName) and
39+
// only match a known cryptographic algorithm
40+
any(Cryptography::CryptographicAlgorithm alg).matchesName(algorithmName)
4641
)
4742
}
4843

rust/ql/test/query-tests/security/CWE-327/BrokenCryptoAlgorithm.expected

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,9 @@
22
| test_cipher.rs:23:27:23:60 | ...::new_from_slice(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:23:27:23:60 | ...::new_from_slice(...) | The cryptographic algorithm RC4 |
33
| test_cipher.rs:26:27:26:48 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:26:27:26:48 | ...::new(...) | The cryptographic algorithm RC4 |
44
| test_cipher.rs:29:27:29:48 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:29:27:29:48 | ...::new(...) | The cryptographic algorithm RC4 |
5-
| test_cipher.rs:59:23:59:42 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:59:23:59:42 | ...::new(...) | The cryptographic algorithm DES |
6-
| test_cipher.rs:63:23:63:47 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:63:23:63:47 | ...::new(...) | The cryptographic algorithm DES |
75
| test_cipher.rs:67:23:67:46 | ...::new_from_slice(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:67:23:67:46 | ...::new_from_slice(...) | The cryptographic algorithm DES |
8-
| test_cipher.rs:71:23:71:42 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:71:23:71:42 | ...::new(...) | The cryptographic algorithm DES |
9-
| test_cipher.rs:75:27:75:46 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:75:27:75:46 | ...::new(...) | The cryptographic algorithm DES |
10-
| test_cipher.rs:80:24:80:48 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:80:24:80:48 | ...::new(...) | The cryptographic algorithm 3DES |
11-
| test_cipher.rs:84:24:84:48 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:84:24:84:48 | ...::new(...) | The cryptographic algorithm 3DES |
12-
| test_cipher.rs:88:24:88:48 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:88:24:88:48 | ...::new(...) | The cryptographic algorithm 3DES |
136
| test_cipher.rs:92:24:92:52 | ...::new_from_slice(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:92:24:92:52 | ...::new_from_slice(...) | The cryptographic algorithm 3DES |
14-
| test_cipher.rs:97:23:97:42 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:97:23:97:42 | ...::new(...) | The cryptographic algorithm RC2 |
15-
| test_cipher.rs:101:23:101:46 | ...::new_from_slice(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:101:23:101:46 | ...::new_from_slice(...) | The cryptographic algorithm RC2 |
7+
| test_cipher.rs:92:24:92:52 | ...::new_from_slice(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:92:24:92:52 | ...::new_from_slice(...) | The cryptographic algorithm DES |
168
| test_cipher.rs:105:23:105:56 | ...::new_with_eff_key_len(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:105:23:105:56 | ...::new_with_eff_key_len(...) | The cryptographic algorithm RC2 |
179
| test_cipher.rs:110:23:110:50 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:110:23:110:50 | ...::new(...) | The cryptographic algorithm RC5 |
1810
| test_cipher.rs:114:23:114:55 | ...::new_from_slice(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:114:23:114:55 | ...::new_from_slice(...) | The cryptographic algorithm RC5 |
19-
| test_cipher.rs:132:23:132:76 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:132:23:132:76 | ...::new(...) | The cryptographic algorithm DES |
20-
| test_cipher.rs:138:23:138:76 | ...::new_from_slices(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:138:23:138:76 | ...::new_from_slices(...) | The cryptographic algorithm DES |
21-
| test_cipher.rs:141:23:141:76 | ...::new(...) | $@ is broken or weak, and should not be used. | test_cipher.rs:141:23:141:76 | ...::new(...) | The cryptographic algorithm DES |

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,36 @@ fn test_block_cipher(
5656
aes_cipher3.decrypt_block(block128.into());
5757

5858
// des (broken)
59-
let des_cipher1 = Des::new(key.into()); // $ Alert[rust/weak-cryptographic-algorithm]
59+
let des_cipher1 = Des::new(key.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
6060
des_cipher1.encrypt_block(data.into());
6161
des_cipher1.decrypt_block(data.into());
6262

63-
let des_cipher2 = des::Des::new(key.into()); // $ Alert[rust/weak-cryptographic-algorithm]
63+
let des_cipher2 = des::Des::new(key.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
6464
des_cipher2.encrypt_block(data.into());
6565
des_cipher2.decrypt_block(data.into());
6666

6767
let des_cipher3 = Des::new_from_slice(key).expect("fail"); // $ Alert[rust/weak-cryptographic-algorithm]
6868
des_cipher3.encrypt_block(data.into());
6969
des_cipher3.decrypt_block(data.into());
7070

71-
let des_cipher4 = Des::new(key.into()); // $ Alert[rust/weak-cryptographic-algorithm]
71+
let des_cipher4 = Des::new(key.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
7272
des_cipher4.encrypt_block_b2b(input.into(), data.into());
7373
des_cipher4.decrypt_block_b2b(input.into(), data.into());
7474

75-
let mut des_cipher5 = Des::new(key.into()); // $ Alert[rust/weak-cryptographic-algorithm]
75+
let mut des_cipher5 = Des::new(key.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
7676
des_cipher5.encrypt_block_mut(data.into());
7777
des_cipher5.decrypt_block_mut(data.into());
7878

7979
// triple des (broken)
80-
let tdes_cipher1 = TdesEde2::new(key.into()); // $ Alert[rust/weak-cryptographic-algorithm]
80+
let tdes_cipher1 = TdesEde2::new(key.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
8181
tdes_cipher1.encrypt_block(data.into());
8282
tdes_cipher1.decrypt_block(data.into());
8383

84-
let tdes_cipher2 = TdesEde3::new(key.into()); // $ Alert[rust/weak-cryptographic-algorithm]
84+
let tdes_cipher2 = TdesEde3::new(key.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
8585
tdes_cipher2.encrypt_block(data.into());
8686
tdes_cipher2.decrypt_block(data.into());
8787

88-
let tdes_cipher3 = TdesEee2::new(key.into()); // $ Alert[rust/weak-cryptographic-algorithm]
88+
let tdes_cipher3 = TdesEee2::new(key.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
8989
tdes_cipher3.encrypt_block(data.into());
9090
tdes_cipher3.decrypt_block(data.into());
9191

@@ -94,11 +94,11 @@ fn test_block_cipher(
9494
tdes_cipher4.decrypt_block(data.into());
9595

9696
// rc2 (broken)
97-
let rc2_cipher1 = Rc2::new(key.into()); // $ Alert[rust/weak-cryptographic-algorithm]
97+
let rc2_cipher1 = Rc2::new(key.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
9898
rc2_cipher1.encrypt_block(data.into());
9999
rc2_cipher1.decrypt_block(data.into());
100100

101-
let rc2_cipher2 = Rc2::new_from_slice(key).expect("fail"); // $ Alert[rust/weak-cryptographic-algorithm]
101+
let rc2_cipher2 = Rc2::new_from_slice(key).expect("fail"); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
102102
rc2_cipher2.encrypt_block(data.into());
103103
rc2_cipher2.decrypt_block(data.into());
104104

@@ -129,15 +129,15 @@ fn test_cbc(
129129
_ = aes_cipher1.encrypt_padded_mut::<aes::cipher::block_padding::Pkcs7>(data, data_len).unwrap();
130130

131131
// des (broken)
132-
let des_cipher1 = cbc::Encryptor::<des::Des>::new(key.into(), iv.into()); // $ Alert[rust/weak-cryptographic-algorithm]
132+
let des_cipher1 = cbc::Encryptor::<des::Des>::new(key.into(), iv.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
133133
_ = des_cipher1.encrypt_padded_mut::<des::cipher::block_padding::Pkcs7>(data, data_len).unwrap();
134134

135135
let des_cipher2 = MyDesEncryptor::new(key.into(), iv.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
136136
_ = des_cipher2.encrypt_padded_mut::<des::cipher::block_padding::Pkcs7>(data, data_len).unwrap();
137137

138-
let des_cipher3 = cbc::Encryptor::<des::Des>::new_from_slices(&key, &iv).unwrap(); // $ Alert[rust/weak-cryptographic-algorithm]
138+
let des_cipher3 = cbc::Encryptor::<des::Des>::new_from_slices(&key, &iv).unwrap(); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
139139
_ = des_cipher3.encrypt_padded_mut::<des::cipher::block_padding::Pkcs7>(data, data_len).unwrap();
140140

141-
let des_cipher4 = cbc::Encryptor::<des::Des>::new(key.into(), iv.into()); // $ Alert[rust/weak-cryptographic-algorithm]
141+
let des_cipher4 = cbc::Encryptor::<des::Des>::new(key.into(), iv.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
142142
_ = des_cipher4.encrypt_padded_b2b_mut::<des::cipher::block_padding::Pkcs7>(input, data).unwrap();
143143
}

0 commit comments

Comments
 (0)