Skip to content

Commit d6d76fa

Browse files
authored
Merge pull request github#15183 from egregius313/egregius313/java/fix-weak-hashing-adddition
Java: Fix minor error in `java/potentially-weak-cryptographic-algorithm`
2 parents 9c039c4 + be50696 commit d6d76fa

File tree

5 files changed

+46
-25
lines changed

5 files changed

+46
-25
lines changed

java/ql/lib/semmle/code/java/security/MaybeBrokenCryptoAlgorithmQuery.qll

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,47 @@ private import semmle.code.java.dataflow.RangeUtils
1010
private import semmle.code.java.dispatch.VirtualDispatch
1111
private import semmle.code.java.frameworks.Properties
1212

13+
/** A reference to an insecure cryptographic algorithm. */
14+
abstract class InsecureAlgorithm extends Expr {
15+
/** Gets the string representation of this insecure cryptographic algorithm. */
16+
abstract string getStringValue();
17+
}
18+
1319
private class ShortStringLiteral extends StringLiteral {
1420
ShortStringLiteral() { this.getValue().length() < 100 }
1521
}
1622

1723
/**
1824
* A string literal that may refer to an insecure cryptographic algorithm.
1925
*/
20-
class InsecureAlgoLiteral extends ShortStringLiteral {
26+
class InsecureAlgoLiteral extends InsecureAlgorithm, ShortStringLiteral {
2127
InsecureAlgoLiteral() {
22-
// Algorithm identifiers should be at least two characters.
23-
this.getValue().length() > 1 and
2428
exists(string s | s = this.getValue() |
29+
// Algorithm identifiers should be at least two characters.
30+
s.length() > 1 and
2531
not s.regexpMatch(getSecureAlgorithmRegex()) and
2632
// Exclude results covered by another query.
2733
not s.regexpMatch(getInsecureAlgorithmRegex())
2834
)
2935
}
36+
37+
override string getStringValue() { result = this.getValue() }
38+
}
39+
40+
/**
41+
* A property access that may refer to an insecure cryptographic algorithm.
42+
*/
43+
class InsecureAlgoProperty extends InsecureAlgorithm, PropertiesGetPropertyMethodCall {
44+
string value;
45+
46+
InsecureAlgoProperty() {
47+
value = this.getPropertyValue() and
48+
// Since properties pairs are not included in the java/weak-cryptographic-algorithm,
49+
// the check for values from properties files can be less strict than `InsecureAlgoLiteral`.
50+
not value.regexpMatch(getSecureAlgorithmRegex())
51+
}
52+
53+
override string getStringValue() { result = value }
3054
}
3155

3256
private predicate objectToString(MethodCall ma) {
@@ -41,15 +65,7 @@ private predicate objectToString(MethodCall ma) {
4165
* A taint-tracking configuration to reason about the use of potentially insecure cryptographic algorithms.
4266
*/
4367
module InsecureCryptoConfig implements DataFlow::ConfigSig {
44-
predicate isSource(DataFlow::Node n) {
45-
n.asExpr() instanceof InsecureAlgoLiteral
46-
or
47-
exists(PropertiesGetPropertyMethodCall mc | n.asExpr() = mc |
48-
// Since properties pairs are not included in the java/weak-crypto-algorithm,
49-
// The check for values from properties files can be less strict than `InsecureAlgoLiteral`.
50-
not mc.getPropertyValue().regexpMatch(getSecureAlgorithmRegex())
51-
)
52-
}
68+
predicate isSource(DataFlow::Node n) { n.asExpr() instanceof InsecureAlgorithm }
5369

5470
predicate isSink(DataFlow::Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
5571

java/ql/src/Security/CWE/CWE-327/MaybeBrokenCryptoAlgorithm.ql

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,10 @@ import semmle.code.java.frameworks.Properties
1818
import semmle.code.java.security.MaybeBrokenCryptoAlgorithmQuery
1919
import InsecureCryptoFlow::PathGraph
2020

21-
/**
22-
* Get the string value represented by the given expression.
23-
*
24-
* If the value is a string literal, get the literal value.
25-
* If the value is a call to `java.util.Properties::getProperty`, get the potential values of the property.
26-
*/
27-
string getStringValue(DataFlow::Node algo) {
28-
result = algo.asExpr().(StringLiteral).getValue()
29-
or
30-
result = algo.asExpr().(PropertiesGetPropertyMethodCall).getPropertyValue()
31-
}
32-
3321
from InsecureCryptoFlow::PathNode source, InsecureCryptoFlow::PathNode sink, CryptoAlgoSpec c
3422
where
3523
sink.getNode().asExpr() = c.getAlgoSpec() and
3624
InsecureCryptoFlow::flowPath(source, sink)
3725
select c, source, sink,
3826
"Cryptographic algorithm $@ may not be secure, consider using a different algorithm.", source,
39-
getStringValue(source.getNode())
27+
source.getNode().asExpr().(InsecureAlgorithm).getStringValue()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
edges
2+
| WeakHashing.java:21:86:21:90 | "MD5" : String | WeakHashing.java:21:56:21:91 | getProperty(...) |
23
nodes
34
| Test.java:19:45:19:49 | "DES" | semmle.label | "DES" |
45
| Test.java:42:33:42:37 | "RC2" | semmle.label | "RC2" |
6+
| WeakHashing.java:21:56:21:91 | getProperty(...) | semmle.label | getProperty(...) |
7+
| WeakHashing.java:21:86:21:90 | "MD5" : String | semmle.label | "MD5" : String |
58
subpaths
69
#select
710
| Test.java:19:20:19:50 | getInstance(...) | Test.java:19:45:19:49 | "DES" | Test.java:19:45:19:49 | "DES" | Cryptographic algorithm $@ is weak and should not be used. | Test.java:19:45:19:49 | "DES" | DES |
811
| Test.java:42:14:42:38 | getInstance(...) | Test.java:42:33:42:37 | "RC2" | Test.java:42:33:42:37 | "RC2" | Cryptographic algorithm $@ is weak and should not be used. | Test.java:42:33:42:37 | "RC2" | RC2 |
12+
| WeakHashing.java:21:30:21:92 | getInstance(...) | WeakHashing.java:21:86:21:90 | "MD5" : String | WeakHashing.java:21:56:21:91 | getProperty(...) | Cryptographic algorithm $@ is weak and should not be used. | WeakHashing.java:21:86:21:90 | "MD5" | MD5 |

java/ql/test/query-tests/security/CWE-327/semmle/tests/MaybeBrokenCryptoAlgorithm.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ edges
22
nodes
33
| Test.java:34:48:34:52 | "foo" | semmle.label | "foo" |
44
| WeakHashing.java:15:55:15:83 | getProperty(...) | semmle.label | getProperty(...) |
5+
| WeakHashing.java:18:56:18:95 | getProperty(...) | semmle.label | getProperty(...) |
6+
| WeakHashing.java:21:56:21:91 | getProperty(...) | semmle.label | getProperty(...) |
57
subpaths
68
#select
79
| Test.java:34:21:34:53 | new SecretKeySpec(...) | Test.java:34:48:34:52 | "foo" | Test.java:34:48:34:52 | "foo" | Cryptographic algorithm $@ may not be secure, consider using a different algorithm. | Test.java:34:48:34:52 | "foo" | foo |
810
| WeakHashing.java:15:29:15:84 | getInstance(...) | WeakHashing.java:15:55:15:83 | getProperty(...) | WeakHashing.java:15:55:15:83 | getProperty(...) | Cryptographic algorithm $@ may not be secure, consider using a different algorithm. | WeakHashing.java:15:55:15:83 | getProperty(...) | MD5 |
11+
| WeakHashing.java:18:30:18:96 | getInstance(...) | WeakHashing.java:18:56:18:95 | getProperty(...) | WeakHashing.java:18:56:18:95 | getProperty(...) | Cryptographic algorithm $@ may not be secure, consider using a different algorithm. | WeakHashing.java:18:56:18:95 | getProperty(...) | MD5 |
12+
| WeakHashing.java:21:30:21:92 | getInstance(...) | WeakHashing.java:21:56:21:91 | getProperty(...) | WeakHashing.java:21:56:21:91 | getProperty(...) | Cryptographic algorithm $@ may not be secure, consider using a different algorithm. | WeakHashing.java:21:56:21:91 | getProperty(...) | MD5 |

java/ql/test/query-tests/security/CWE-327/semmle/tests/WeakHashing.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ void hashing() throws NoSuchAlgorithmException, IOException {
1414
// BAD: Using a weak hashing algorithm
1515
MessageDigest bad = MessageDigest.getInstance(props.getProperty("hashAlg1"));
1616

17+
// BAD: Using a weak hashing algorithm even with a secure default
18+
MessageDigest bad2 = MessageDigest.getInstance(props.getProperty("hashAlg1", "SHA-256"));
19+
20+
// BAD: Using a strong hashing algorithm but with a weak default
21+
MessageDigest bad3 = MessageDigest.getInstance(props.getProperty("hashAlg2", "MD5"));
22+
1723
// GOOD: Using a strong hashing algorithm
1824
MessageDigest ok = MessageDigest.getInstance(props.getProperty("hashAlg2"));
25+
26+
// OK: Property does not exist and default is secure
27+
MessageDigest ok2 = MessageDigest.getInstance(props.getProperty("hashAlg3", "SHA-256"));
1928
}
2029
}

0 commit comments

Comments
 (0)