Skip to content

Commit c195c64

Browse files
committed
Python: Use type-tracking for integer literal tracking
Like we've done for pretty much everything else. An experiment to see what this means for query performance.
1 parent 2798771 commit c195c64

File tree

4 files changed

+28
-4212
lines changed

4 files changed

+28
-4212
lines changed

config/identical-files.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
"python/ql/src/semmle/python/dataflow/new/internal/DataFlowImpl.qll",
2323
"python/ql/src/semmle/python/dataflow/new/internal/DataFlowImpl2.qll",
2424
"python/ql/src/semmle/python/dataflow/new/internal/DataFlowImpl3.qll",
25-
"python/ql/src/semmle/python/dataflow/new/internal/DataFlowImpl4.qll",
26-
"python/ql/src/semmle/python/dataflow/new/internal/DataFlowImplOnlyInternalUse.qll"
25+
"python/ql/src/semmle/python/dataflow/new/internal/DataFlowImpl4.qll"
2726
],
2827
"DataFlow Java/C++/C#/Python Common": [
2928
"java/ql/src/semmle/code/java/dataflow/internal/DataFlowImplCommon.qll",

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import python
88
private import semmle.python.dataflow.new.DataFlow
9-
private import semmle.python.dataflow.new.DataFlowOnlyInternalUse
109
private import semmle.python.dataflow.new.RemoteFlowSources
1110
private import semmle.python.dataflow.new.TaintTracking
1211
private import semmle.python.Frameworks
@@ -563,19 +562,34 @@ module Cryptography {
563562

564563
/** Provides classes for modeling new key-pair generation APIs. */
565564
module KeyGeneration {
566-
/**
567-
* A data-flow configuration for tracking integer literals.
568-
*/
569-
private class IntegerLiteralTrackerConfiguration extends DataFlowOnlyInternalUse::Configuration {
570-
IntegerLiteralTrackerConfiguration() { this = "IntegerLiteralTrackerConfiguration" }
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+
) {
569+
t.start() and
570+
result.asExpr().(IntegerLiteral).getValue() = keySize and
571+
origin = result
572+
or
573+
// Due to bad performance when using normal setup with we have inlined that code and forced a join
574+
exists(DataFlow::TypeTracker t2 |
575+
exists(DataFlow::StepSummary summary |
576+
keysizeTracker_first_join(t2, keySize, origin, result, summary) and
577+
t = t2.append(summary)
578+
)
579+
)
580+
}
571581

572-
override predicate isSource(DataFlow::Node source) {
573-
source = DataFlow::exprNode(any(IntegerLiteral size))
574-
}
582+
pragma[nomagic]
583+
private predicate keysizeTracker_first_join(
584+
DataFlow::TypeTracker t2, int keySize, DataFlow::Node origin, DataFlow::Node res,
585+
DataFlow::StepSummary summary
586+
) {
587+
DataFlow::StepSummary::step(keysizeTracker(t2, keySize, origin), res, summary)
588+
}
575589

576-
override predicate isSink(DataFlow::Node sink) {
577-
sink = any(KeyGeneration::Range kg).getKeySizeArg()
578-
}
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)
579593
}
580594

581595
/**
@@ -596,11 +610,7 @@ module Cryptography {
596610
* explains how we obtained this specific key size.
597611
*/
598612
int getKeySizeWithOrigin(DataFlow::Node origin) {
599-
exists(IntegerLiteral size, IntegerLiteralTrackerConfiguration config |
600-
origin.asExpr() = size and
601-
config.hasFlow(origin, this.getKeySizeArg()) and
602-
result = size.getValue()
603-
)
613+
this.getKeySizeArg() = keysizeTracker(result, origin)
604614
}
605615

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

python/ql/src/semmle/python/dataflow/new/DataFlowOnlyInternalUse.qll

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)