Skip to content

Commit 123889a

Browse files
committed
C++: Fix 'triple DES' false positives.
1 parent 40cf29b commit 123889a

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

cpp/ql/src/semmle/code/cpp/security/Encryption.qll

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ string getAnInsecureHashAlgorithmName() { result = ["SHA1", "MD5"] }
3030
/**
3131
* Gets the regular expression used for matching strings that look like they
3232
* contain an algorithm that is known to be insecure.
33+
*
34+
* Consider using `isInsecureEncryption` rather than accessing this regular
35+
* expression directly.
3336
*/
3437
string getInsecureAlgorithmRegex() {
3538
result =
@@ -49,8 +52,12 @@ string getInsecureAlgorithmRegex() {
4952
* Holds if `name` looks like it might be related to operations with an
5053
* insecure encyption algorithm.
5154
*/
52-
bindingset[name] predicate isInsecureEncryption(string name) {
53-
name.regexpMatch(getInsecureAlgorithmRegex())
55+
bindingset[name]
56+
predicate isInsecureEncryption(string name) {
57+
name.regexpMatch(getInsecureAlgorithmRegex()) and
58+
// Check for evidence that an otherwise matching name may in fact not be
59+
// related to insecure encrpytion, e.g. "Triple-DES" is not "DES".
60+
not name.toUpperCase().regexpMatch(".*TRIPLE.*")
5461
}
5562

5663
/**

cpp/ql/test/query-tests/Security/CWE/CWE-327/BrokenCryptoAlgorithm.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
| test2.cpp:182:38:182:45 | ALGO_DES | This macro invocation specifies a broken or weak cryptographic algorithm. |
66
| test.cpp:38:2:38:31 | ENCRYPT_WITH_DES(data,amount) | This macro invocation specifies a broken or weak cryptographic algorithm. |
77
| test.cpp:39:2:39:31 | ENCRYPT_WITH_RC2(data,amount) | This macro invocation specifies a broken or weak cryptographic algorithm. |
8-
| test.cpp:42:2:42:38 | ENCRYPT_WITH_TRIPLE_DES(data,amount) | This macro invocation specifies a broken or weak cryptographic algorithm. |
98
| test.cpp:51:2:51:32 | DES_DO_ENCRYPTION(data,amount) | This macro invocation specifies a broken or weak cryptographic algorithm. |
109
| test.cpp:52:2:52:31 | RUN_DES_ENCODING(data,amount) | This macro invocation specifies a broken or weak cryptographic algorithm. |
1110
| test.cpp:53:2:53:25 | DES_ENCODE(data,amount) | This macro invocation specifies a broken or weak cryptographic algorithm. |
@@ -14,6 +13,5 @@
1413
| test.cpp:59:12:59:25 | SORT_ORDER_DES | This macro invocation specifies a broken or weak cryptographic algorithm. |
1514
| test.cpp:88:2:88:11 | call to encryptDES | This function call specifies a broken or weak cryptographic algorithm. |
1615
| test.cpp:89:2:89:11 | call to encryptRC2 | This function call specifies a broken or weak cryptographic algorithm. |
17-
| test.cpp:92:2:92:17 | call to encryptTripleDES | This function call specifies a broken or weak cryptographic algorithm. |
1816
| test.cpp:101:2:101:15 | call to do_des_encrypt | This function call specifies a broken or weak cryptographic algorithm. |
1917
| test.cpp:102:2:102:12 | call to DES_Set_Key | This function call specifies a broken or weak cryptographic algorithm. |

cpp/ql/test/query-tests/Security/CWE/CWE-327/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void test_macros(void *data, size_t amount, const char *str)
3939
ENCRYPT_WITH_RC2(data, amount); // BAD
4040
ENCRYPT_WITH_AES(data, amount); // GOOD (good algorithm)
4141
ENCRYPT_WITH_3DES(data, amount); // GOOD (good enough algorithm)
42-
ENCRYPT_WITH_TRIPLE_DES(data, amount); // GOOD (good enough algorithm) [FALSE POSITIVE]
42+
ENCRYPT_WITH_TRIPLE_DES(data, amount); // GOOD (good enough algorithm)
4343
ENCRYPT_WITH_RC20(data, amount); // GOOD (if there ever is an RC20 algorithm, we have no reason to believe it's weak)
4444
ENCRYPT_WITH_DES_REMOVED(data, amount); // GOOD (implementation has been deleted)
4545

@@ -89,7 +89,7 @@ void test_functions(void *data, size_t amount, const char *str)
8989
encryptRC2(data, amount); // BAD
9090
encryptAES(data, amount); // GOOD (good algorithm)
9191
encrypt3DES(data, amount); // GOOD (good enough algorithm)
92-
encryptTripleDES(data, amount); // GOOD (good enough algorithm) [FALSE POSITIVE]
92+
encryptTripleDES(data, amount); // GOOD (good enough algorithm)
9393

9494
DESEncrypt(data, amount); // BAD
9595
RC2Encrypt(data, amount); // BAD

0 commit comments

Comments
 (0)