Skip to content

Commit dfa223a

Browse files
committed
Python: Better IntegerLiteral tracking for weak crypto key
1 parent a658334 commit dfa223a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

python/ql/src/semmle/python/Concepts.qll

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,21 @@ module Cryptography {
562562

563563
/** Provides classes for modeling new key-pair generation APIs. */
564564
module KeyGeneration {
565+
/**
566+
* A data-flow configuration for tracking integer literals.
567+
*/
568+
private class IntegerLiteralTrackerConfiguration extends DataFlow::Configuration {
569+
IntegerLiteralTrackerConfiguration() { this = "IntegerLiteralTrackerConfiguration" }
570+
571+
override predicate isSource(DataFlow::Node source) {
572+
source = DataFlow::exprNode(any(IntegerLiteral size))
573+
}
574+
575+
override predicate isSink(DataFlow::Node sink) {
576+
sink = any(KeyGeneration::Range kg).getKeySizeArg()
577+
}
578+
}
579+
565580
/**
566581
* A data-flow node that generates a new key-pair for use with public-key cryptography.
567582
*
@@ -580,8 +595,9 @@ module Cryptography {
580595
* explains how we obtained this specific key size.
581596
*/
582597
int getKeySizeWithOrigin(DataFlow::Node origin) {
583-
exists(IntegerLiteral size | origin = DataFlow::exprNode(size) |
584-
origin.(DataFlow::LocalSourceNode).flowsTo(this.getKeySizeArg()) and
598+
exists(IntegerLiteral size, IntegerLiteralTrackerConfiguration config |
599+
origin.asExpr() = size and
600+
config.hasFlow(origin, this.getKeySizeArg()) and
585601
result = size.getValue()
586602
)
587603
}

python/ql/test/query-tests/Security/CWE-326/WeakCryptoKey.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
| weak_crypto.py:74:1:74:37 | ControlFlowNode for rsa_gen_key() | Creation of an RSA key uses $@ bits, which is below 2048 and considered breakable. | weak_crypto.py:12:12:12:15 | ControlFlowNode for IntegerLiteral | 1024 |
77
| weak_crypto.py:76:1:76:22 | ControlFlowNode for Attribute() | Creation of an DSA key uses $@ bits, which is below 2048 and considered breakable. | weak_crypto.py:16:12:16:15 | ControlFlowNode for IntegerLiteral | 1024 |
88
| weak_crypto.py:77:1:77:22 | ControlFlowNode for Attribute() | Creation of an RSA key uses $@ bits, which is below 2048 and considered breakable. | weak_crypto.py:12:12:12:15 | ControlFlowNode for IntegerLiteral | 1024 |
9+
| weak_crypto.py:84:12:84:29 | ControlFlowNode for Attribute() | Creation of an RSA key uses $@ bits, which is below 2048 and considered breakable. | weak_crypto.py:12:12:12:15 | ControlFlowNode for IntegerLiteral | 1024 |

0 commit comments

Comments
 (0)