Skip to content

Commit 4610b1b

Browse files
committed
Pyhton: Use type back-tracking for keysize on key-generation
Internal evaluation showed that this didn't perform better than normal (forward) type-tracking, but it feels more like the right approach.
1 parent c195c64 commit 4610b1b

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -562,34 +562,32 @@ module Cryptography {
562562

563563
/** Provides classes for modeling new key-pair generation APIs. */
564564
module KeyGeneration {
565-
/** Gets a reference to an integer literal, as well as the origin of the integer literal. */
566-
private DataFlow::Node keysizeTracker(
567-
DataFlow::TypeTracker t, int keySize, DataFlow::Node origin
568-
) {
565+
/** Gets a back-reference to the keysize argument `arg` that was used to generate a new key-pair. */
566+
DataFlow::LocalSourceNode keysizeBacktracker(DataFlow::TypeBackTracker t, DataFlow::Node arg) {
569567
t.start() and
570-
result.asExpr().(IntegerLiteral).getValue() = keySize and
571-
origin = result
568+
arg = any(KeyGeneration::Range r).getKeySizeArg() and
569+
result = arg.getALocalSource()
572570
or
573571
// Due to bad performance when using normal setup with we have inlined that code and forced a join
574-
exists(DataFlow::TypeTracker t2 |
572+
exists(DataFlow::TypeBackTracker t2 |
575573
exists(DataFlow::StepSummary summary |
576-
keysizeTracker_first_join(t2, keySize, origin, result, summary) and
577-
t = t2.append(summary)
574+
keysizeBacktracker_first_join(t2, arg, result, summary) and
575+
t = t2.prepend(summary)
578576
)
579577
)
580578
}
581579

582580
pragma[nomagic]
583-
private predicate keysizeTracker_first_join(
584-
DataFlow::TypeTracker t2, int keySize, DataFlow::Node origin, DataFlow::Node res,
581+
private predicate keysizeBacktracker_first_join(
582+
DataFlow::TypeBackTracker t2, DataFlow::Node arg, DataFlow::Node res,
585583
DataFlow::StepSummary summary
586584
) {
587-
DataFlow::StepSummary::step(keysizeTracker(t2, keySize, origin), res, summary)
585+
DataFlow::StepSummary::step(res, keysizeBacktracker(t2, arg), summary)
588586
}
589587

590-
/** Gets a reference to an integer literal, as well as the origin of the integer literal. */
591-
private DataFlow::Node keysizeTracker(int keySize, DataFlow::Node origin) {
592-
result = keysizeTracker(DataFlow::TypeTracker::end(), keySize, origin)
588+
/** Gets a back-reference to the keysize argument `arg` that was used to generate a new key-pair. */
589+
DataFlow::LocalSourceNode keysizeBacktracker(DataFlow::Node arg) {
590+
result = keysizeBacktracker(DataFlow::TypeBackTracker::end(), arg)
593591
}
594592

595593
/**
@@ -610,7 +608,8 @@ module Cryptography {
610608
* explains how we obtained this specific key size.
611609
*/
612610
int getKeySizeWithOrigin(DataFlow::Node origin) {
613-
this.getKeySizeArg() = keysizeTracker(result, origin)
611+
origin = keysizeBacktracker(this.getKeySizeArg()) and
612+
result = origin.asExpr().(IntegerLiteral).getValue()
614613
}
615614

616615
/** Gets the minimum key size (in bits) for this algorithm to be considered secure. */

0 commit comments

Comments
 (0)