Skip to content

Commit 2f0be40

Browse files
committed
C++: Exclude results in some common libraries.
1 parent 965d131 commit 2f0be40

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-1240/CustomCryptographicPrimitive.ql

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,34 @@ predicate computeHeuristic(Expr e) {
7676
computeHeuristicType(e.getUnspecifiedType())
7777
}
7878

79+
/**
80+
* Gets the name of an established cryptography library or likely third party directory.
81+
*/
82+
string encryptionLibraryName() {
83+
result =
84+
[
85+
"libssh", "openssl", "boringssl", "mbed", "libsodium", "libsrtp", "third.?party", "library",
86+
"deps"
87+
]
88+
}
89+
90+
/**
91+
* Holds if `f` is a file that is likely to be inside an established
92+
* cryptography library.
93+
*/
94+
predicate isLibrary(File f) {
95+
f.getAbsolutePath().regexpMatch("(?i).*(" + concat(encryptionLibraryName(), "|") + ").*")
96+
or
97+
// assume that any result that would be found outside the source location is in a crypto library
98+
not exists(f.getFile().getRelativePath())
99+
}
100+
79101
from Function f, int amount
80102
where
81103
likelyEncryptionFunction(f) and
82104
amount = strictcount(Expr e | computeHeuristic(e) and e.getEnclosingFunction() = f) and
83105
amount >= 8 and
84-
exists(f.getFile().getRelativePath()) // exclude library files
106+
not isLibrary(f.getFile())
85107
select f,
86108
"This function, \"" + f.getName() +
87109
"\", may be a custom implementation of a cryptographic primitive."
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
| library/tests_library.h:4:6:4:19 | do_aes_encrypt | This function, "do_aes_encrypt", may be a custom implementation of a cryptographic primitive. |
21
| tests_crypto.cpp:11:6:11:18 | encryptString | This function, "encryptString", may be a custom implementation of a cryptographic primitive. |
32
| tests_crypto.cpp:30:6:30:14 | MyEncrypt | This function, "MyEncrypt", may be a custom implementation of a cryptographic primitive. |
43
| tests_crypto.cpp:83:6:83:18 | init_aes_sbox | This function, "init_aes_sbox", may be a custom implementation of a cryptographic primitive. |

0 commit comments

Comments
 (0)