Skip to content

Commit fd13055

Browse files
committed
Shared: Generate value-preserving summaries when possible.
1 parent b91a2cc commit fd13055

File tree

2 files changed

+98
-24
lines changed

2 files changed

+98
-24
lines changed

shared/mad/codeql/mad/modelgenerator/internal/ModelGeneratorImpl.qll

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private import codeql.dataflow.internal.ContentDataFlowImpl
1111
private import codeql.dataflow.internal.DataFlowImplCommon as DataFlowImplCommon
1212
private import codeql.util.Location
1313
private import ModelPrinting
14+
private import codeql.util.Unit
1415

1516
/**
1617
* Provides language-specific model generator parameters.
@@ -464,14 +465,22 @@ module MakeModelGenerator<
464465
override string toString() { result = "TaintStore(" + step + ")" }
465466
}
466467

467-
/**
468-
* A data flow configuration for tracking flow through APIs.
469-
* The sources are the parameters of an API and the sinks are the return values (excluding `this`) and parameters.
470-
*
471-
* This can be used to generate Flow summaries for APIs from parameter to return.
472-
*/
473-
private module PropagateFlowConfig implements DataFlow::StateConfigSig {
474-
class FlowState = TaintState;
468+
private signature module PropagateFlowConfigInputSig {
469+
class FlowState;
470+
471+
FlowState initialState();
472+
473+
default predicate isAdditionalFlowStep(
474+
DataFlow::Node node1, FlowState state1, DataFlow::Node node2, FlowState state2
475+
) {
476+
none()
477+
}
478+
}
479+
480+
private module PropagateFlowConfig<PropagateFlowConfigInputSig PropagateFlowConfigInput>
481+
implements DataFlow::StateConfigSig
482+
{
483+
import PropagateFlowConfigInput
475484

476485
predicate isSource(DataFlow::Node source, FlowState state) {
477486
source instanceof DataFlow::ParameterNode and
@@ -480,7 +489,7 @@ module MakeModelGenerator<
480489
c instanceof DataFlowSummaryTargetApi and
481490
not isUninterestingForHeuristicDataFlowModels(c)
482491
) and
483-
state.(TaintRead).getStep() = 0
492+
state = initialState()
484493
}
485494

486495
predicate isSink(DataFlow::Node sink, FlowState state) {
@@ -494,6 +503,31 @@ module MakeModelGenerator<
494503
not exists(captureQualifierFlow(getAsExprEnclosingCallable(sink)))
495504
}
496505

506+
predicate isAdditionalFlowStep = PropagateFlowConfigInput::isAdditionalFlowStep/4;
507+
508+
predicate isBarrier(DataFlow::Node n) {
509+
exists(Type t | t = n.(NodeExtended).getType() and not isRelevantType(t))
510+
}
511+
512+
DataFlow::FlowFeature getAFeature() {
513+
result instanceof DataFlow::FeatureEqualSourceSinkCallContext
514+
}
515+
}
516+
517+
/**
518+
* A module used to construct a data flow configuration for tracking taint-
519+
* flow through APIs.
520+
* The sources are the parameters of an API and the sinks are the return
521+
* values (excluding `this`) and parameters.
522+
*
523+
* This can be used to generate flow summaries for APIs from parameter to
524+
* return.
525+
*/
526+
module PropagateFlowConfigInputTaintInput implements PropagateFlowConfigInputSig {
527+
class FlowState = TaintState;
528+
529+
FlowState initialState() { result.(TaintRead).getStep() = 0 }
530+
497531
predicate isAdditionalFlowStep(
498532
DataFlow::Node node1, FlowState state1, DataFlow::Node node2, FlowState state2
499533
) {
@@ -515,41 +549,79 @@ module MakeModelGenerator<
515549
state1.(TaintRead).getStep() + 1 = state2.(TaintRead).getStep()
516550
)
517551
}
552+
}
518553

519-
predicate isBarrier(DataFlow::Node n) {
520-
exists(Type t | t = n.(NodeExtended).getType() and not isRelevantType(t))
521-
}
554+
/**
555+
* A data flow configuration for tracking taint-flow through APIs.
556+
* The sources are the parameters of an API and the sinks are the return
557+
* values (excluding `this`) and parameters.
558+
*
559+
* This can be used to generate flow summaries for APIs from parameter to
560+
* return.
561+
*/
562+
private module PropagateTaintFlowConfig =
563+
PropagateFlowConfig<PropagateFlowConfigInputTaintInput>;
522564

523-
DataFlow::FlowFeature getAFeature() {
524-
result instanceof DataFlow::FeatureEqualSourceSinkCallContext
525-
}
565+
module PropagateTaintFlow = TaintTracking::GlobalWithState<PropagateTaintFlowConfig>;
566+
567+
/**
568+
* A module used to construct a data flow configuration for tracking
569+
* data flow through APIs.
570+
* The sources are the parameters of an API and the sinks are the return
571+
* values (excluding `this`) and parameters.
572+
*
573+
* This can be used to generate value-preserving flow summaries for APIs
574+
* from parameter to return.
575+
*/
576+
module PropagateFlowConfigInputDataFlowInput implements PropagateFlowConfigInputSig {
577+
class FlowState = Unit;
578+
579+
FlowState initialState() { any() }
526580
}
527581

528-
module PropagateFlow = TaintTracking::GlobalWithState<PropagateFlowConfig>;
582+
/**
583+
* A data flow configuration for tracking data flow through APIs.
584+
* The sources are the parameters of an API and the sinks are the return
585+
* values (excluding `this`) and parameters.
586+
*
587+
* This can be used to generate flow summaries for APIs from parameter to
588+
* return.
589+
*/
590+
private module PropagateDataFlowConfig =
591+
PropagateFlowConfig<PropagateFlowConfigInputDataFlowInput>;
592+
593+
module PropagateDataFlow = DataFlow::GlobalWithState<PropagateDataFlowConfig>;
529594

530595
/**
531596
* Gets the summary model(s) of `api`, if there is flow from parameters to return value or parameter.
532597
*/
598+
bindingset[preservesValue]
533599
string captureThroughFlow0(
534-
DataFlowSummaryTargetApi api, DataFlow::ParameterNode p, ReturnNodeExt returnNodeExt
600+
DataFlowSummaryTargetApi api, DataFlow::ParameterNode p, ReturnNodeExt returnNodeExt,
601+
boolean preservesValue
535602
) {
536603
exists(string input, string output |
537604
getEnclosingCallable(p) = api and
538605
getEnclosingCallable(returnNodeExt) = api and
539606
input = parameterNodeAsInput(p) and
540607
output = getOutput(returnNodeExt) and
541608
input != output and
542-
result = ModelPrinting::asLiftedTaintModel(api, input, output)
609+
result = ModelPrinting::asLiftedTaintModel(api, input, output, preservesValue)
543610
)
544611
}
545612

546613
/**
547614
* Gets the summary model(s) of `api`, if there is flow from parameters to return value or parameter.
548615
*/
549616
private string captureThroughFlow(DataFlowSummaryTargetApi api) {
550-
exists(DataFlow::ParameterNode p, ReturnNodeExt returnNodeExt |
551-
PropagateFlow::flow(p, returnNodeExt) and
552-
result = captureThroughFlow0(api, p, returnNodeExt)
617+
exists(DataFlow::ParameterNode p, ReturnNodeExt returnNodeExt, boolean preservesValue |
618+
PropagateDataFlow::flow(p, returnNodeExt) and preservesValue = true
619+
or
620+
not PropagateDataFlow::flow(p, returnNodeExt) and
621+
PropagateTaintFlow::flow(p, returnNodeExt) and
622+
preservesValue = false
623+
|
624+
result = captureThroughFlow0(api, p, returnNodeExt, preservesValue)
553625
)
554626
}
555627

shared/mad/codeql/mad/modelgenerator/internal/ModelPrinting.qll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ module ModelPrintingImpl<ModelPrintingLangSig Lang> {
8686
/**
8787
* Gets the lifted taint summary model for `api` with `input` and `output`.
8888
*/
89-
bindingset[input, output]
90-
string asLiftedTaintModel(Printing::SummaryApi api, string input, string output) {
91-
result = asModel(api, input, output, false, true)
89+
bindingset[input, output, preservesValue]
90+
string asLiftedTaintModel(
91+
Printing::SummaryApi api, string input, string output, boolean preservesValue
92+
) {
93+
result = asModel(api, input, output, preservesValue, true)
9294
}
9395

9496
/**

0 commit comments

Comments
 (0)