From e94e1bb65bf5a4aca87426deb5b81e4f3257413e Mon Sep 17 00:00:00 2001 From: Esben Sparre Andreasen Date: Thu, 17 Mar 2022 10:55:55 +0100 Subject: [PATCH] re-optimize without being unsound --- .../AdaptiveThreatModeling.qll | 23 +++++++++++++++++++ .../EndpointScoring.qll | 8 +++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/AdaptiveThreatModeling.qll b/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/AdaptiveThreatModeling.qll index 12c282d6d98..e6d09981a75 100644 --- a/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/AdaptiveThreatModeling.qll +++ b/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/AdaptiveThreatModeling.qll @@ -34,6 +34,7 @@ module ATM { */ pragma[inline] float getScoreForFlow(DataFlow::Node source, DataFlow::Node sink) { + Optimizations::hasFlow(source, sink) and shouldResultBeIncluded(source, sink) and result = unique(float s | s = any(ScoringResults results).getScoreForFlow(source, sink)) } @@ -120,4 +121,26 @@ module ATM { ) } } + + /** + * Predicates for performance improvements that should not affect the semantics. + */ + module Optimizations { + /** + * EXPERIMENTAL. This API may change in the future. + * + * Holds if data may flow from `source` to `sink` for *some* configuration. + * + * This is a variant of `Configuration::hasFlow`, that does not require the precense of a source and a sink. + */ + predicate hasFlow(DataFlow::Node source, DataFlow::Node sink) { + exists(DataFlow::Configuration cfg | + exists(DataFlow::SourcePathNode flowsource, DataFlow::SinkPathNode flowsink | + cfg.hasFlowPath(flowsource, flowsink) and + source = flowsource.getNode() and + sink = flowsink.getNode() + ) + ) + } + } } diff --git a/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/EndpointScoring.qll b/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/EndpointScoring.qll index e70544a746c..f3906e7dfb8 100644 --- a/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/EndpointScoring.qll +++ b/javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/EndpointScoring.qll @@ -9,6 +9,7 @@ private import BaseScoring private import EndpointFeatures as EndpointFeatures private import FeaturizationConfig private import EndpointTypes +private import AdaptiveThreatModeling private string getACompatibleModelChecksum() { availableMlModels(result, "javascript", _, "atm-endpoint-scoring") @@ -23,9 +24,11 @@ module ModelScoring { RelevantFeaturizationConfig() { this = "RelevantFeaturization" } override DataFlow::Node getAnEndpointToFeaturize() { - getCfg().isEffectiveSource(result) + getCfg().isEffectiveSource(result) and + ATM::Optimizations::hasFlow(result, _) or - getCfg().isEffectiveSink(result) + getCfg().isEffectiveSink(result) and + ATM::Optimizations::hasFlow(_, result) } } @@ -146,6 +149,7 @@ module Debugging { query predicate endpointScores = ModelScoring::endpointScores/3; query predicate shouldResultBeIncluded(DataFlow::Node source, DataFlow::Node sink) { + ATM::Optimizations::hasFlow(source, sink) and any(ScoringResults scoringResults).shouldResultBeIncluded(source, sink) } }