diff --git a/config/identical-files.json b/config/identical-files.json index 0e9bcbb7d73f7..20aa5db370d71 100644 --- a/config/identical-files.json +++ b/config/identical-files.json @@ -470,7 +470,6 @@ "ruby/ql/lib/codeql/ruby/typetracking/internal/SummaryTypeTracker.qll" ], "AccessPathSyntax": [ - "csharp/ql/lib/semmle/code/csharp/dataflow/internal/AccessPathSyntax.qll", "go/ql/lib/semmle/go/dataflow/internal/AccessPathSyntax.qll", "java/ql/lib/semmle/code/java/dataflow/internal/AccessPathSyntax.qll", "python/ql/lib/semmle/python/dataflow/new/internal/AccessPathSyntax.qll", diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/FlowSummary.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/FlowSummary.qll index 92a095589464c..18e4025f9e67f 100644 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/FlowSummary.qll +++ b/csharp/ql/lib/semmle/code/csharp/dataflow/FlowSummary.qll @@ -1,198 +1,23 @@ /** Provides classes and predicates for defining flow summaries. */ import csharp -private import dotnet private import internal.FlowSummaryImpl as Impl private import internal.DataFlowDispatch as DataFlowDispatch -private import Impl::Public::SummaryComponent as SummaryComponentInternal -class ParameterPosition = DataFlowDispatch::ParameterPosition; +deprecated class ParameterPosition = DataFlowDispatch::ParameterPosition; -class ArgumentPosition = DataFlowDispatch::ArgumentPosition; +deprecated class ArgumentPosition = DataFlowDispatch::ArgumentPosition; -// import all instances below -private module Summaries { - private import semmle.code.csharp.frameworks.EntityFramework -} +deprecated class SummaryComponent = Impl::Private::SummaryComponent; -class SummaryComponent = Impl::Public::SummaryComponent; +deprecated module SummaryComponent = Impl::Private::SummaryComponent; -/** Provides predicates for constructing summary components. */ -module SummaryComponent { - predicate content = SummaryComponentInternal::content/1; +deprecated class SummaryComponentStack = Impl::Private::SummaryComponentStack; - /** Gets a summary component for parameter `i`. */ - SummaryComponent parameter(int i) { - exists(ArgumentPosition pos | - result = SummaryComponentInternal::parameter(pos) and - i = pos.getPosition() - ) - } +deprecated module SummaryComponentStack = Impl::Private::SummaryComponentStack; - /** Gets a summary component for argument `i`. */ - SummaryComponent argument(int i) { - exists(ParameterPosition pos | - result = SummaryComponentInternal::argument(pos) and - i = pos.getPosition() - ) - } - - predicate return = SummaryComponentInternal::return/1; - - /** Gets a summary component that represents a qualifier. */ - SummaryComponent qualifier() { - exists(ParameterPosition pos | - result = SummaryComponentInternal::argument(pos) and - pos.isThisParameter() - ) - } - - /** Gets a summary component that represents an element in a collection. */ - SummaryComponent element() { result = content(any(DataFlow::ElementContent c)) } - - /** Gets a summary component for property `p`. */ - SummaryComponent property(Property p) { - result = content(any(DataFlow::PropertyContent c | c.getProperty() = p.getUnboundDeclaration())) - } - - /** Gets a summary component for field `f`. */ - SummaryComponent field(Field f) { - result = content(any(DataFlow::FieldContent c | c.getField() = f.getUnboundDeclaration())) - } - - /** Gets a summary component that represents the return value of a call. */ - SummaryComponent return() { result = return(any(DataFlowDispatch::NormalReturnKind rk)) } - - predicate syntheticGlobal = SummaryComponentInternal::syntheticGlobal/1; - - class SyntheticGlobal = SummaryComponentInternal::SyntheticGlobal; -} - -class SummaryComponentStack = Impl::Public::SummaryComponentStack; - -/** Provides predicates for constructing stacks of summary components. */ -module SummaryComponentStack { - private import Impl::Public::SummaryComponentStack as SummaryComponentStackInternal - - predicate singleton = SummaryComponentStackInternal::singleton/1; - - predicate push = SummaryComponentStackInternal::push/2; - - /** Gets a singleton stack for argument `i`. */ - SummaryComponentStack argument(int i) { result = singleton(SummaryComponent::argument(i)) } - - predicate return = SummaryComponentStackInternal::return/1; - - /** Gets a singleton stack representing a qualifier. */ - SummaryComponentStack qualifier() { result = singleton(SummaryComponent::qualifier()) } - - /** Gets a stack representing an element of `container`. */ - SummaryComponentStack elementOf(SummaryComponentStack container) { - result = push(SummaryComponent::element(), container) - } - - /** Gets a stack representing a property `p` of `object`. */ - SummaryComponentStack propertyOf(Property p, SummaryComponentStack object) { - result = push(SummaryComponent::property(p), object) - } - - /** Gets a stack representing a field `f` of `object`. */ - SummaryComponentStack fieldOf(Field f, SummaryComponentStack object) { - result = push(SummaryComponent::field(f), object) - } - - /** Gets a singleton stack representing the return value of a call. */ - SummaryComponentStack return() { result = singleton(SummaryComponent::return()) } - - /** Gets a singleton stack representing a synthetic global with name `name`. */ - SummaryComponentStack syntheticGlobal(string synthetic) { - result = singleton(SummaryComponent::syntheticGlobal(synthetic)) - } - - /** - * DEPRECATED: Use the member predicate `getMadRepresentation` instead. - * - * Gets a textual representation of this stack used for flow summaries. - */ - deprecated string getComponentStack(SummaryComponentStack s) { result = s.getMadRepresentation() } -} +deprecated class RequiredSummaryComponentStack = Impl::Private::RequiredSummaryComponentStack; class SummarizedCallable = Impl::Public::SummarizedCallable; -private predicate recordConstructorFlow(Constructor c, int i, Property p) { - c = any(RecordType r).getAMember() and - exists(string name | - c.getParameter(i).getName() = name and - c.getDeclaringType().getAMember(name) = p - ) -} - -private class RecordConstructorFlow extends SummarizedCallable { - RecordConstructorFlow() { recordConstructorFlow(this, _, _) } - - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - exists(int i, Property p | - recordConstructorFlow(this, i, p) and - input = SummaryComponentStack::argument(i) and - output = SummaryComponentStack::propertyOf(p, SummaryComponentStack::return()) and - preservesValue = true - ) - } -} - -class RequiredSummaryComponentStack = Impl::Public::RequiredSummaryComponentStack; - -private class RecordConstructorFlowRequiredSummaryComponentStack extends RequiredSummaryComponentStack -{ - override predicate required(SummaryComponent head, SummaryComponentStack tail) { - exists(Property p | - recordConstructorFlow(_, _, p) and - head = SummaryComponent::property(p) and - tail = SummaryComponentStack::return() - ) - } -} - class Provenance = Impl::Public::Provenance; - -private import semmle.code.csharp.frameworks.system.linq.Expressions - -private SummaryComponent delegateSelf() { - exists(ArgumentPosition pos | - result = SummaryComponentInternal::parameter(pos) and - pos.isDelegateSelf() - ) -} - -private predicate mayInvokeCallback(Callable c, int n) { - c.getParameter(n).getType() instanceof SystemLinqExpressions::DelegateExtType and - not c.fromSource() -} - -private class SummarizedCallableWithCallback extends SummarizedCallable { - private int pos; - - SummarizedCallableWithCallback() { mayInvokeCallback(this, pos) } - - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - input = SummaryComponentStack::argument(pos) and - output = SummaryComponentStack::push(delegateSelf(), input) and - preservesValue = true - } - - override predicate hasProvenance(Provenance provenance) { provenance = "hq-generated" } -} - -private class RequiredComponentStackForCallback extends RequiredSummaryComponentStack { - override predicate required(SummaryComponent head, SummaryComponentStack tail) { - exists(int pos | - mayInvokeCallback(_, pos) and - head = delegateSelf() and - tail = SummaryComponentStack::argument(pos) - ) - } -} diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/AccessPathSyntax.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/AccessPathSyntax.qll deleted file mode 100644 index 0c3dc8427b2b1..0000000000000 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/AccessPathSyntax.qll +++ /dev/null @@ -1,182 +0,0 @@ -/** - * Module for parsing access paths from MaD models, both the identifying access path used - * by dynamic languages, and the input/output specifications for summary steps. - * - * This file is used by the shared data flow library and by the JavaScript libraries - * (which does not use the shared data flow libraries). - */ - -/** - * Convenience-predicate for extracting two capture groups at once. - */ -bindingset[input, regexp] -private predicate regexpCaptureTwo(string input, string regexp, string capture1, string capture2) { - capture1 = input.regexpCapture(regexp, 1) and - capture2 = input.regexpCapture(regexp, 2) -} - -/** Companion module to the `AccessPath` class. */ -module AccessPath { - /** A string that should be parsed as an access path. */ - abstract class Range extends string { - bindingset[this] - Range() { any() } - } - - /** - * Parses an integer constant `n` or interval `n1..n2` (inclusive) and gets the value - * of the constant or any value contained in the interval. - */ - bindingset[arg] - int parseInt(string arg) { - result = arg.toInt() - or - // Match "n1..n2" - exists(string lo, string hi | - regexpCaptureTwo(arg, "(-?\\d+)\\.\\.(-?\\d+)", lo, hi) and - result = [lo.toInt() .. hi.toInt()] - ) - } - - /** - * Parses a lower-bounded interval `n..` and gets the lower bound. - */ - bindingset[arg] - int parseLowerBound(string arg) { result = arg.regexpCapture("(-?\\d+)\\.\\.", 1).toInt() } - - /** - * Parses an integer constant or interval (bounded or unbounded) that explicitly - * references the arity, such as `N-1` or `N-3..N-1`. - * - * Note that expressions of form `N-x` will never resolve to a negative index, - * even if `N` is zero (it will have no result in that case). - */ - bindingset[arg, arity] - private int parseIntWithExplicitArity(string arg, int arity) { - result >= 0 and // do not allow N-1 to resolve to a negative index - exists(string lo | - // N-x - lo = arg.regexpCapture("N-(\\d+)", 1) and - result = arity - lo.toInt() - or - // N-x.. - lo = arg.regexpCapture("N-(\\d+)\\.\\.", 1) and - result = [arity - lo.toInt(), arity - 1] - ) - or - exists(string lo, string hi | - // x..N-y - regexpCaptureTwo(arg, "(-?\\d+)\\.\\.N-(\\d+)", lo, hi) and - result = [lo.toInt() .. arity - hi.toInt()] - or - // N-x..N-y - regexpCaptureTwo(arg, "N-(\\d+)\\.\\.N-(\\d+)", lo, hi) and - result = [arity - lo.toInt() .. arity - hi.toInt()] and - result >= 0 - or - // N-x..y - regexpCaptureTwo(arg, "N-(\\d+)\\.\\.(\\d+)", lo, hi) and - result = [arity - lo.toInt() .. hi.toInt()] and - result >= 0 - ) - } - - /** - * Parses an integer constant or interval (bounded or unbounded) and gets any - * of the integers contained within (of which there may be infinitely many). - * - * Has no result for arguments involving an explicit arity, such as `N-1`. - */ - bindingset[arg, result] - int parseIntUnbounded(string arg) { - result = parseInt(arg) - or - result >= parseLowerBound(arg) - } - - /** - * Parses an integer constant or interval (bounded or unbounded) that - * may reference the arity of a call, such as `N-1` or `N-3..N-1`. - * - * Note that expressions of form `N-x` will never resolve to a negative index, - * even if `N` is zero (it will have no result in that case). - */ - bindingset[arg, arity] - int parseIntWithArity(string arg, int arity) { - result = parseInt(arg) - or - result in [parseLowerBound(arg) .. arity - 1] - or - result = parseIntWithExplicitArity(arg, arity) - } -} - -/** Gets the `n`th token on the access path as a string. */ -private string getRawToken(AccessPath path, int n) { - // Avoid splitting by '.' since tokens may contain dots, e.g. `Field[foo.Bar.x]`. - // Instead use regexpFind to match valid tokens, and supplement with a final length - // check (in `AccessPath.hasSyntaxError`) to ensure all characters were included in a token. - result = path.regexpFind("\\w+(?:\\[[^\\]]*\\])?(?=\\.|$)", n, _) -} - -/** - * A string that occurs as an access path (either identifying or input/output spec) - * which might be relevant for this database. - */ -class AccessPath extends string instanceof AccessPath::Range { - /** Holds if this string is not a syntactically valid access path. */ - predicate hasSyntaxError() { - // If the lengths match, all characters must haven been included in a token - // or seen by the `.` lookahead pattern. - this != "" and - not this.length() = sum(int n | | getRawToken(this, n).length() + 1) - 1 - } - - /** Gets the `n`th token on the access path (if there are no syntax errors). */ - AccessPathToken getToken(int n) { - result = getRawToken(this, n) and - not this.hasSyntaxError() - } - - /** Gets the number of tokens on the path (if there are no syntax errors). */ - int getNumToken() { - result = count(int n | exists(getRawToken(this, n))) and - not this.hasSyntaxError() - } -} - -/** - * An access part token such as `Argument[1]` or `ReturnValue`, appearing in one or more access paths. - */ -class AccessPathToken extends string { - AccessPathToken() { this = getRawToken(_, _) } - - private string getPart(int part) { - result = this.regexpCapture("([^\\[]+)(?:\\[([^\\]]*)\\])?", part) - } - - /** Gets the name of the token, such as `Member` from `Member[x]` */ - string getName() { result = this.getPart(1) } - - /** - * Gets the argument list, such as `1,2` from `Member[1,2]`, - * or has no result if there are no arguments. - */ - string getArgumentList() { result = this.getPart(2) } - - /** Gets the `n`th argument to this token, such as `x` or `y` from `Member[x,y]`. */ - string getArgument(int n) { result = this.getArgumentList().splitAt(",", n).trim() } - - /** Gets the `n`th argument to this `name` token, such as `x` or `y` from `Member[x,y]`. */ - pragma[nomagic] - string getArgument(string name, int n) { name = this.getName() and result = this.getArgument(n) } - - /** Gets an argument to this token, such as `x` or `y` from `Member[x,y]`. */ - string getAnArgument() { result = this.getArgument(_) } - - /** Gets an argument to this `name` token, such as `x` or `y` from `Member[x,y]`. */ - string getAnArgument(string name) { result = this.getArgument(name, _) } - - /** Gets the number of arguments to this token, such as 2 for `Member[x,y]` or zero for `ReturnValue`. */ - int getNumArgument() { result = count(int n | exists(this.getArgument(n))) } -} diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowDispatch.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowDispatch.qll index 0a5877d489743..c2b7b76952ace 100644 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowDispatch.qll +++ b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowDispatch.qll @@ -116,9 +116,7 @@ private module Cached { // No need to include calls that are compiled from source not call.getImplementation().getMethod().compiledFromSource() } or - TSummaryCall( - FlowSummaryImpl::Public::SummarizedCallable c, FlowSummaryImpl::Private::SummaryNode receiver - ) { + TSummaryCall(FlowSummary::SummarizedCallable c, FlowSummaryImpl::Private::SummaryNode receiver) { FlowSummaryImpl::Private::summaryCallbackRange(c, receiver) } @@ -446,7 +444,7 @@ class CilDataFlowCall extends DataFlowCall, TCilCall { * the method `Select`. */ class SummaryCall extends DelegateDataFlowCall, TSummaryCall { - private FlowSummaryImpl::Public::SummarizedCallable c; + private FlowSummary::SummarizedCallable c; private FlowSummaryImpl::Private::SummaryNode receiver; SummaryCall() { this = TSummaryCall(c, receiver) } diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll index fb9a0c6b13cca..0df1dbb9f566c 100644 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll +++ b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll @@ -675,11 +675,11 @@ private predicate fieldOrPropertyStore(Expr e, Content c, Expr src, Expr q, bool f instanceof InstanceFieldOrProperty or exists( - FlowSummaryImpl::Public::SummarizedCallable sc, - FlowSummaryImpl::Public::SummaryComponentStack input + FlowSummaryImpl::Private::SummarizedCallableImpl sc, + FlowSummaryImpl::Private::SummaryComponentStack input | sc.propagatesFlow(input, _, _) and - input.contains(FlowSummary::SummaryComponent::content(f.getContent())) + input.contains(FlowSummaryImpl::Private::SummaryComponent::content(f.getContent())) ) ) | @@ -1392,11 +1392,11 @@ private module ArgumentNodes { } private class SummaryArgumentNode extends FlowSummaryNode, ArgumentNodeImpl { - private DataFlowCall call_; + private SummaryCall call_; private ArgumentPosition pos_; SummaryArgumentNode() { - FlowSummaryImpl::Private::summaryArgumentNode(call_, this.getSummaryNode(), pos_) + FlowSummaryImpl::Private::summaryArgumentNode(call_.getReceiver(), this.getSummaryNode(), pos_) } override predicate argumentOf(DataFlowCall call, ArgumentPosition pos) { @@ -1682,11 +1682,11 @@ private module OutNodes { } private class SummaryOutNode extends FlowSummaryNode, OutNode { - private DataFlowCall call; + private SummaryCall call; private ReturnKind kind_; SummaryOutNode() { - FlowSummaryImpl::Private::summaryOutNode(call, this.getSummaryNode(), kind_) + FlowSummaryImpl::Private::summaryOutNode(call.getReceiver(), this.getSummaryNode(), kind_) } override DataFlowCall getCall(ReturnKind kind) { result = call and kind = kind_ } @@ -1699,7 +1699,7 @@ import OutNodes class FlowSummaryNode extends NodeImpl, TFlowSummaryNode { FlowSummaryImpl::Private::SummaryNode getSummaryNode() { this = TFlowSummaryNode(result) } - FlowSummaryImpl::Public::SummarizedCallable getSummarizedCallable() { + FlowSummary::SummarizedCallable getSummarizedCallable() { result = this.getSummaryNode().getSummarizedCallable() } @@ -2423,7 +2423,10 @@ predicate additionalLambdaFlowStep(Node nodeFrom, Node nodeTo, boolean preserves * by default as a heuristic. */ predicate allowParameterReturnInSelf(ParameterNode p) { - FlowSummaryImpl::Private::summaryAllowParameterReturnInSelf(p) + exists(DataFlowCallable c, ParameterPosition pos | + parameterNode(p, c, pos) and + FlowSummaryImpl::Private::summaryAllowParameterReturnInSelf(c.asSummarizedCallable(), pos) + ) } /** An approximated `Content`. */ diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/ExternalFlow.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/ExternalFlow.qll index 122567111cd3f..b2b43ccc43f76 100644 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/ExternalFlow.qll +++ b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/ExternalFlow.qll @@ -88,13 +88,13 @@ import csharp import ExternalFlowExtensions -private import AccessPathSyntax private import DataFlowDispatch private import DataFlowPrivate private import DataFlowPublic +private import FlowSummaryImpl private import FlowSummaryImpl::Public +private import FlowSummaryImpl::Private private import FlowSummaryImpl::Private::External -private import FlowSummaryImplSpecific private import semmle.code.csharp.commons.QualifiedName private import codeql.mad.ModelValidation as SharedModelVal @@ -380,14 +380,14 @@ Declaration interpretElement( * A callable where there exists a MaD sink model that applies to it. */ class SinkCallable extends Callable { - SinkCallable() { sinkElement(this, _, _, _) } + SinkCallable() { SourceSinkInterpretationInput::sinkElement(this, _, _, _) } } /** * A callable where there exists a MaD source model that applies to it. */ class SourceCallable extends Callable { - SourceCallable() { sourceElement(this, _, _, _) } + SourceCallable() { SourceSinkInterpretationInput::sourceElement(this, _, _, _) } } cached @@ -398,7 +398,9 @@ private module Cached { */ cached predicate sourceNode(Node node, string kind) { - exists(InterpretNode n | isSourceNode(n, kind) and n.asNode() = node) + exists(SourceSinkInterpretationInput::InterpretNode n | + isSourceNode(n, kind) and n.asNode() = node + ) } /** @@ -407,7 +409,9 @@ private module Cached { */ cached predicate sinkNode(Node node, string kind) { - exists(InterpretNode n | isSinkNode(n, kind) and n.asNode() = node) + exists(SourceSinkInterpretationInput::InterpretNode n | + isSinkNode(n, kind) and n.asNode() = node + ) } } @@ -485,3 +489,66 @@ string asPartialNeutralModel(UnboundCallable c) { + parameters + ";" // ) } + +private predicate interpretSummary( + UnboundCallable c, string input, string output, string kind, string provenance +) { + exists( + string namespace, string type, boolean subtypes, string name, string signature, string ext + | + summaryModel(namespace, type, subtypes, name, signature, ext, input, output, kind, provenance) and + c = interpretElement(namespace, type, subtypes, name, signature, ext) + ) +} + +private predicate relevantSummaryElementManual( + UnboundCallable c, string input, string output, string kind, Provenance provenance +) { + interpretSummary(c, input, output, kind, provenance) and + provenance.isManual() +} + +private predicate relevantSummaryElementGenerated( + UnboundCallable c, string input, string output, string kind, Provenance provenance +) { + interpretSummary(c, input, output, kind, provenance) and + provenance.isGenerated() +} + +private class SummarizedCallableAdapter extends SummarizedCallable { + string input_; + string output_; + string kind; + string provenance_; + + SummarizedCallableAdapter() { + relevantSummaryElementManual(this, input_, output_, kind, provenance_) + or + relevantSummaryElementGenerated(this, input_, output_, kind, provenance_) and + not relevantSummaryElementManual(this, _, _, _, _) + } + + override predicate propagatesFlow(string input, string output, boolean preservesValue) { + input = input_ and + output = output_ and + if kind = "value" then preservesValue = true else preservesValue = false + } + + override predicate hasProvenance(Provenance provenance) { provenance = provenance_ } +} + +private class NeutralCallableAdapter extends NeutralCallable { + string kind; + string provenance_; + + NeutralCallableAdapter() { + exists(string namespace, string type, string name, string signature | + neutralModel(namespace, type, name, signature, kind, provenance_) and + this = interpretElement(namespace, type, false, name, signature, "") + ) + } + + override string getKind() { result = kind } + + override predicate hasProvenance(Provenance provenance) { provenance = provenance_ } +} diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImpl.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImpl.qll index 0aa17c521b43f..7835d6fbb1f48 100644 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImpl.qll +++ b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImpl.qll @@ -1,1491 +1,426 @@ /** * Provides classes and predicates for defining flow summaries. - * - * The definitions in this file are language-independent, and language-specific - * definitions are passed in via the `DataFlowImplSpecific` and - * `FlowSummaryImplSpecific` modules. */ -private import FlowSummaryImplSpecific +private import csharp +private import semmle.code.csharp.frameworks.system.linq.Expressions +private import codeql.dataflow.internal.FlowSummaryImpl +private import codeql.dataflow.internal.AccessPathSyntax as AccessPath +private import DataFlowImplSpecific as DataFlowImplSpecific private import DataFlowImplSpecific::Private private import DataFlowImplSpecific::Public -private import DataFlowImplCommon -private import codeql.util.Unit - -/** Provides classes and predicates for defining flow summaries. */ -module Public { - private import Private - - /** - * A component used in a flow summary. - * - * Either a parameter or an argument at a given position, a specific - * content type, or a return kind. - */ - class SummaryComponent extends TSummaryComponent { - /** Gets a textual representation of this component used for MaD models. */ - string getMadRepresentation() { - result = getMadRepresentationSpecific(this) - or - exists(ArgumentPosition pos | - this = TParameterSummaryComponent(pos) and - result = "Parameter[" + getArgumentPosition(pos) + "]" - ) - or - exists(ParameterPosition pos | - this = TArgumentSummaryComponent(pos) and - result = "Argument[" + getParameterPosition(pos) + "]" - ) - or - exists(string synthetic | - this = TSyntheticGlobalSummaryComponent(synthetic) and - result = "SyntheticGlobal[" + synthetic + "]" - ) - or - this = TReturnSummaryComponent(getReturnValueKind()) and result = "ReturnValue" - } - - /** Gets a textual representation of this summary component. */ - string toString() { result = this.getMadRepresentation() } - } - - /** Provides predicates for constructing summary components. */ - module SummaryComponent { - /** Gets a summary component for content `c`. */ - SummaryComponent content(ContentSet c) { result = TContentSummaryComponent(c) } - - /** Gets a summary component where data is not allowed to be stored in `c`. */ - SummaryComponent withoutContent(ContentSet c) { result = TWithoutContentSummaryComponent(c) } - - /** Gets a summary component where data must be stored in `c`. */ - SummaryComponent withContent(ContentSet c) { result = TWithContentSummaryComponent(c) } - - /** Gets a summary component for a parameter at position `pos`. */ - SummaryComponent parameter(ArgumentPosition pos) { result = TParameterSummaryComponent(pos) } - - /** Gets a summary component for an argument at position `pos`. */ - SummaryComponent argument(ParameterPosition pos) { result = TArgumentSummaryComponent(pos) } - - /** Gets a summary component for a return of kind `rk`. */ - SummaryComponent return(ReturnKind rk) { result = TReturnSummaryComponent(rk) } - - /** Gets a summary component for synthetic global `sg`. */ - SummaryComponent syntheticGlobal(SyntheticGlobal sg) { - result = TSyntheticGlobalSummaryComponent(sg) - } - - /** - * A synthetic global. This represents some form of global state, which - * summaries can read and write individually. - */ - abstract class SyntheticGlobal extends string { - bindingset[this] - SyntheticGlobal() { any() } - } - } - - /** - * A (non-empty) stack of summary components. - * - * A stack is used to represent where data is read from (input) or where it - * is written to (output). For example, an input stack `[Field f, Argument 0]` - * means that data is read from field `f` from the `0`th argument, while an - * output stack `[Field g, Return]` means that data is written to the field - * `g` of the returned object. - */ - class SummaryComponentStack extends TSummaryComponentStack { - /** Gets the head of this stack. */ - SummaryComponent head() { - this = TSingletonSummaryComponentStack(result) or - this = TConsSummaryComponentStack(result, _) - } +private import semmle.code.csharp.Unification +private import semmle.code.csharp.dataflow.internal.ExternalFlow - /** Gets the tail of this stack, if any. */ - SummaryComponentStack tail() { this = TConsSummaryComponentStack(_, result) } +module Input implements InputSig { + class SummarizedCallableBase = UnboundCallable; - /** Gets the length of this stack. */ - int length() { - this = TSingletonSummaryComponentStack(_) and result = 1 - or - result = 1 + this.tail().length() - } - - /** Gets the stack obtained by dropping the first `i` elements, if any. */ - SummaryComponentStack drop(int i) { - i = 0 and result = this - or - result = this.tail().drop(i - 1) - } + ArgumentPosition callbackSelfParameterPosition() { result.isDelegateSelf() } - /** Holds if this stack contains summary component `c`. */ - predicate contains(SummaryComponent c) { c = this.drop(_).head() } + ReturnKind getStandardReturnValueKind() { result instanceof NormalReturnKind } - /** Gets the bottom element of this stack. */ - SummaryComponent bottom() { - this = TSingletonSummaryComponentStack(result) or result = this.tail().bottom() - } - - /** Gets a textual representation of this stack used for MaD models. */ - string getMadRepresentation() { - exists(SummaryComponent head, SummaryComponentStack tail | - head = this.head() and - tail = this.tail() and - result = tail.getMadRepresentation() + "." + head.getMadRepresentation() - ) - or - exists(SummaryComponent c | - this = TSingletonSummaryComponentStack(c) and - result = c.getMadRepresentation() - ) - } - - /** Gets a textual representation of this stack. */ - string toString() { result = this.getMadRepresentation() } - } - - /** Provides predicates for constructing stacks of summary components. */ - module SummaryComponentStack { - /** Gets a singleton stack containing `c`. */ - SummaryComponentStack singleton(SummaryComponent c) { - result = TSingletonSummaryComponentStack(c) - } - - /** - * Gets the stack obtained by pushing `head` onto `tail`. - * - * Make sure to override `RequiredSummaryComponentStack::required()` in order - * to ensure that the constructed stack exists. - */ - SummaryComponentStack push(SummaryComponent head, SummaryComponentStack tail) { - result = TConsSummaryComponentStack(head, tail) - } - - /** Gets a singleton stack for an argument at position `pos`. */ - SummaryComponentStack argument(ParameterPosition pos) { - result = singleton(SummaryComponent::argument(pos)) - } - - /** Gets a singleton stack representing a return of kind `rk`. */ - SummaryComponentStack return(ReturnKind rk) { result = singleton(SummaryComponent::return(rk)) } - } - - /** - * A class that exists for QL technical reasons only (the IPA type used - * to represent component stacks needs to be bounded). - */ - class RequiredSummaryComponentStack extends Unit { - /** - * Holds if the stack obtained by pushing `head` onto `tail` is required. - */ - abstract predicate required(SummaryComponent head, SummaryComponentStack tail); - } - - /** - * Gets the valid model origin values. - */ - private string getValidModelOrigin() { - result = - [ - "ai", // AI (machine learning) - "df", // Dataflow (model generator) - "tb", // Type based (model generator) - "hq", // Heuristic query - ] - } - - /** - * A class used to represent provenance values for MaD models. - * - * The provenance value is a string of the form `origin-verification` - * (or just `manual`), where `origin` is a value indicating the - * origin of the model, and `verification` is a value indicating, how - * the model was verified. - * - * Examples could be: - * - `df-generated`: A model produced by the model generator, but not verified by a human. - * - `ai-manual`: A model produced by AI, but verified by a human. - */ - class Provenance extends string { - private string verification; - - Provenance() { - exists(string origin | origin = getValidModelOrigin() | - this = origin + "-" + verification and - verification = ["manual", "generated"] - ) - or - this = verification and verification = "manual" - } - - /** - * Holds if this is a valid generated provenance value. - */ - predicate isGenerated() { verification = "generated" } - - /** - * Holds if this is a valid manual provenance value. - */ - predicate isManual() { verification = "manual" } - } - - /** A callable with a flow summary. */ - abstract class SummarizedCallable extends SummarizedCallableBase { - bindingset[this] - SummarizedCallable() { any() } - - /** - * Holds if data may flow from `input` to `output` through this callable. - * - * `preservesValue` indicates whether this is a value-preserving step - * or a taint-step. - * - * Input specifications are restricted to stacks that end with - * `SummaryComponent::argument(_)`, preceded by zero or more - * `SummaryComponent::return(_)` or `SummaryComponent::content(_)` components. - * - * Output specifications are restricted to stacks that end with - * `SummaryComponent::return(_)` or `SummaryComponent::argument(_)`. - * - * Output stacks ending with `SummaryComponent::return(_)` can be preceded by zero - * or more `SummaryComponent::content(_)` components. - * - * Output stacks ending with `SummaryComponent::argument(_)` can be preceded by an - * optional `SummaryComponent::parameter(_)` component, which in turn can be preceded - * by zero or more `SummaryComponent::content(_)` components. - */ - pragma[nomagic] - predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - none() - } - - /** - * Holds if there exists a generated summary that applies to this callable. - */ - final predicate hasGeneratedModel() { - exists(Provenance p | p.isGenerated() and this.hasProvenance(p)) - } - - /** - * Holds if all the summaries that apply to this callable are auto generated and not manually created. - * That is, only apply generated models, when there are no manual models. - */ - final predicate applyGeneratedModel() { - this.hasGeneratedModel() and - not this.hasManualModel() - } - - /** - * Holds if there exists a manual summary that applies to this callable. - */ - final predicate hasManualModel() { - exists(Provenance p | p.isManual() and this.hasProvenance(p)) - } - - /** - * Holds if there exists a manual summary that applies to this callable. - * Always apply manual models if they exist. - */ - final predicate applyManualModel() { this.hasManualModel() } - - /** - * Holds if there exists a summary that applies to this callable - * that has provenance `provenance`. - */ - predicate hasProvenance(Provenance provenance) { provenance = "manual" } - } - - /** - * A callable where there is no flow via the callable. - */ - class NeutralSummaryCallable extends NeutralCallable { - NeutralSummaryCallable() { this.getKind() = "summary" } - } - - /** - * A callable that has a neutral model. - */ - class NeutralCallable extends NeutralCallableBase { - private string kind; - private Provenance provenance; - - NeutralCallable() { neutralElement(this, kind, provenance) } - - /** - * Holds if the neutral is auto generated. - */ - final predicate hasGeneratedModel() { provenance.isGenerated() } - - /** - * Holds if there exists a manual neutral that applies to this callable. - */ - final predicate hasManualModel() { provenance.isManual() } - - /** - * Holds if the neutral has provenance `p`. - */ - predicate hasProvenance(Provenance p) { p = provenance } - - /** - * Gets the kind of the neutral. - */ - string getKind() { result = kind } - } -} - -/** - * Provides predicates for compiling flow summaries down to atomic local steps, - * read steps, and store steps. - */ -module Private { - private import Public - import AccessPathSyntax - - newtype TSummaryComponent = - TContentSummaryComponent(ContentSet c) or - TParameterSummaryComponent(ArgumentPosition pos) or - TArgumentSummaryComponent(ParameterPosition pos) or - TReturnSummaryComponent(ReturnKind rk) or - TSyntheticGlobalSummaryComponent(SummaryComponent::SyntheticGlobal sg) or - TWithoutContentSummaryComponent(ContentSet c) or - TWithContentSummaryComponent(ContentSet c) - - private TParameterSummaryComponent callbackSelfParam() { - result = TParameterSummaryComponent(callbackSelfParameterPosition()) - } - - newtype TSummaryComponentStack = - TSingletonSummaryComponentStack(SummaryComponent c) or - TConsSummaryComponentStack(SummaryComponent head, SummaryComponentStack tail) { - any(RequiredSummaryComponentStack x).required(head, tail) - or - any(RequiredSummaryComponentStack x).required(TParameterSummaryComponent(_), tail) and - head = callbackSelfParam() - or - derivedFluentFlowPush(_, _, _, head, tail, _) - } - - pragma[nomagic] - private predicate summary( - SummarizedCallable c, SummaryComponentStack input, SummaryComponentStack output, - boolean preservesValue - ) { - c.propagatesFlow(input, output, preservesValue) + string encodeParameterPosition(ParameterPosition pos) { + result = pos.getPosition().toString() or - // observe side effects of callbacks on input arguments - c.propagatesFlow(output, input, preservesValue) and - preservesValue = true and - isCallbackParameter(input) and - isContentOfArgument(output, _) + pos.isThisParameter() and + result = "this" or - // flow from the receiver of a callback into the instance-parameter - exists(SummaryComponentStack s, SummaryComponentStack callbackRef | - c.propagatesFlow(s, _, _) or c.propagatesFlow(_, s, _) - | - callbackRef = s.drop(_) and - (isCallbackParameter(callbackRef) or callbackRef.head() = TReturnSummaryComponent(_)) and - input = callbackRef.tail() and - output = TConsSummaryComponentStack(callbackSelfParam(), input) and - preservesValue = true - ) - or - exists(SummaryComponentStack arg, SummaryComponentStack return | - derivedFluentFlow(c, input, arg, return, preservesValue) - | - arg.length() = 1 and - output = return - or - exists(SummaryComponent head, SummaryComponentStack tail | - derivedFluentFlowPush(c, input, arg, head, tail, 0) and - output = SummaryComponentStack::push(head, tail) - ) - ) - or - // Chain together summaries where values get passed into callbacks along the way - exists(SummaryComponentStack mid, boolean preservesValue1, boolean preservesValue2 | - c.propagatesFlow(input, mid, preservesValue1) and - c.propagatesFlow(mid, output, preservesValue2) and - mid.drop(mid.length() - 2) = - SummaryComponentStack::push(TParameterSummaryComponent(_), - SummaryComponentStack::singleton(TArgumentSummaryComponent(_))) and - preservesValue = preservesValue1.booleanAnd(preservesValue2) - ) - } - - /** - * Holds if `c` has a flow summary from `input` to `arg`, where `arg` - * writes to (contents of) arguments at position `pos`, and `c` has a - * value-preserving flow summary from the arguments at position `pos` - * to a return value (`return`). - * - * In such a case, we derive flow from `input` to (contents of) the return - * value. - * - * As an example, this simplifies modeling of fluent methods: - * for `StringBuilder.append(x)` with a specified value flow from qualifier to - * return value and taint flow from argument 0 to the qualifier, then this - * allows us to infer taint flow from argument 0 to the return value. - */ - pragma[nomagic] - private predicate derivedFluentFlow( - SummarizedCallable c, SummaryComponentStack input, SummaryComponentStack arg, - SummaryComponentStack return, boolean preservesValue - ) { - exists(ParameterPosition pos | - summary(c, input, arg, preservesValue) and - isContentOfArgument(arg, pos) and - summary(c, SummaryComponentStack::argument(pos), return, true) and - return.bottom() = TReturnSummaryComponent(_) - ) + pos.isDelegateSelf() and + result = "delegate-self" } - pragma[nomagic] - private predicate derivedFluentFlowPush( - SummarizedCallable c, SummaryComponentStack input, SummaryComponentStack arg, - SummaryComponent head, SummaryComponentStack tail, int i - ) { - derivedFluentFlow(c, input, arg, tail, _) and - head = arg.drop(i).head() and - i = arg.length() - 2 + string encodeArgumentPosition(ArgumentPosition pos) { + result = pos.getPosition().toString() or - exists(SummaryComponent head0, SummaryComponentStack tail0 | - derivedFluentFlowPush(c, input, arg, head0, tail0, i + 1) and - head = arg.drop(i).head() and - tail = SummaryComponentStack::push(head0, tail0) - ) - } - - private predicate isCallbackParameter(SummaryComponentStack s) { - s.head() = TParameterSummaryComponent(_) and exists(s.tail()) - } - - private predicate isContentOfArgument(SummaryComponentStack s, ParameterPosition pos) { - s.head() = TContentSummaryComponent(_) and isContentOfArgument(s.tail(), pos) - or - s = SummaryComponentStack::argument(pos) - } - - private predicate outputState(SummarizedCallable c, SummaryComponentStack s) { - summary(c, _, s, _) + pos.isQualifier() and + result = "this" or - exists(SummaryComponentStack out | - outputState(c, out) and - out.head() = TContentSummaryComponent(_) and - s = out.tail() - ) - or - // Add the argument node corresponding to the requested post-update node - inputState(c, s) and isCallbackParameter(s) + pos.isDelegateSelf() and + result = "delegate-self" } - private predicate inputState(SummarizedCallable c, SummaryComponentStack s) { - summary(c, s, _, _) + string encodeContent(ContentSet c, string arg) { + c = TElementContent() and result = "Element" and arg = "" or - exists(SummaryComponentStack inp | inputState(c, inp) and s = inp.tail()) + exists(Field f | c = TFieldContent(f) and result = "Field" and arg = f.getFullyQualifiedName()) or - exists(SummaryComponentStack out | - outputState(c, out) and - out.head() = TParameterSummaryComponent(_) and - s = out.tail() + exists(Property p | + c = TPropertyContent(p) and result = "Property" and arg = p.getFullyQualifiedName() ) or - // Add the post-update node corresponding to the requested argument node - outputState(c, s) and isCallbackParameter(s) - or - // Add the parameter node for parameter side-effects - outputState(c, s) and s = SummaryComponentStack::argument(_) - } - - private newtype TSummaryNodeState = - TSummaryNodeInputState(SummaryComponentStack s) { inputState(_, s) } or - TSummaryNodeOutputState(SummaryComponentStack s) { outputState(_, s) } - - /** - * A state used to break up (complex) flow summaries into atomic flow steps. - * For a flow summary - * - * ```ql - * propagatesFlow( - * SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - * ) - * ``` - * - * the following states are used: - * - * - `TSummaryNodeInputState(SummaryComponentStack s)`: - * this state represents that the components in `s` _have been read_ from the - * input. - * - `TSummaryNodeOutputState(SummaryComponentStack s)`: - * this state represents that the components in `s` _remain to be written_ to - * the output. - */ - private class SummaryNodeState extends TSummaryNodeState { - /** Holds if this state is a valid input state for `c`. */ - pragma[nomagic] - predicate isInputState(SummarizedCallable c, SummaryComponentStack s) { - this = TSummaryNodeInputState(s) and - inputState(c, s) - } - - /** Holds if this state is a valid output state for `c`. */ - pragma[nomagic] - predicate isOutputState(SummarizedCallable c, SummaryComponentStack s) { - this = TSummaryNodeOutputState(s) and - outputState(c, s) - } - - /** Gets a textual representation of this state. */ - string toString() { - exists(SummaryComponentStack s | - this = TSummaryNodeInputState(s) and - result = "read: " + s - ) - or - exists(SummaryComponentStack s | - this = TSummaryNodeOutputState(s) and - result = "to write: " + s - ) - } + exists(SyntheticField f | + c = TSyntheticFieldContent(f) and result = "SyntheticField" and arg = f + ) } - private newtype TSummaryNode = - TSummaryInternalNode(SummarizedCallable c, SummaryNodeState state) { - summaryNodeRange(c, state) - } or - TSummaryParameterNode(SummarizedCallable c, ParameterPosition pos) { - summaryParameterNodeRange(c, pos) - } - - abstract class SummaryNode extends TSummaryNode { - abstract string toString(); - - abstract SummarizedCallable getSummarizedCallable(); + string encodeReturn(ReturnKind rk, string arg) { + result = "Argument" and + arg = rk.(OutRefReturnKind).getPosition().toString() } - private class SummaryInternalNode extends SummaryNode, TSummaryInternalNode { - private SummarizedCallable c; - private SummaryNodeState state; - - SummaryInternalNode() { this = TSummaryInternalNode(c, state) } - - override string toString() { result = "[summary] " + state + " in " + c } - - override SummarizedCallable getSummarizedCallable() { result = c } + string encodeWithoutContent(ContentSet c, string arg) { + result = "WithoutElement" and + c = TElementContent() and + arg = "" } - private class SummaryParamNode extends SummaryNode, TSummaryParameterNode { - private SummarizedCallable c; - private ParameterPosition pos; - - SummaryParamNode() { this = TSummaryParameterNode(c, pos) } - - override string toString() { result = "[summary param] " + pos + " in " + c } - - override SummarizedCallable getSummarizedCallable() { result = c } + string encodeWithContent(ContentSet c, string arg) { + result = "WithElement" and + c = TElementContent() and + arg = "" } +} - /** - * Holds if `state` represents having read from a parameter at position - * `pos` in `c`. In this case we are not synthesizing a data-flow node, - * but instead assume that a relevant parameter node already exists. - */ - private predicate parameterReadState( - SummarizedCallable c, SummaryNodeState state, ParameterPosition pos - ) { - state.isInputState(c, SummaryComponentStack::argument(pos)) - } +private import Make as Impl - /** - * Holds if a synthesized summary node is needed for the state `state` in summarized - * callable `c`. - */ - private predicate summaryNodeRange(SummarizedCallable c, SummaryNodeState state) { - state.isInputState(c, _) and - not parameterReadState(c, state, _) - or - state.isOutputState(c, _) +private module TypesInput implements Impl::Private::TypesInputSig { + DataFlowType getSyntheticGlobalType(Impl::Private::SyntheticGlobal sg) { + exists(sg) and + result.asGvnType() = Gvn::getGlobalValueNumber(any(ObjectType t)) } - pragma[noinline] - private SummaryNode summaryNodeInputState(SummarizedCallable c, SummaryComponentStack s) { - exists(SummaryNodeState state | state.isInputState(c, s) | - result = TSummaryInternalNode(c, state) + DataFlowType getContentType(ContentSet c) { + exists(Type t | result.asGvnType() = Gvn::getGlobalValueNumber(t) | + t = c.(FieldContent).getField().getType() or - exists(ParameterPosition pos | - parameterReadState(c, state, pos) and - result = TSummaryParameterNode(c, pos) - ) + t = c.(PropertyContent).getProperty().getType() + or + t = c.(SyntheticFieldContent).getField().getType() + or + c instanceof ElementContent and + t instanceof ObjectType // we don't know what the actual element type is ) } - pragma[noinline] - private SummaryNode summaryNodeOutputState(SummarizedCallable c, SummaryComponentStack s) { - exists(SummaryNodeState state | - state.isOutputState(c, s) and - result = TSummaryInternalNode(c, state) + DataFlowType getParameterType(UnboundCallable c, ParameterPosition pos) { + exists(Type t | result.asGvnType() = Gvn::getGlobalValueNumber(t) | + exists(int i | + pos.getPosition() = i and + t = c.getParameter(i).getType() + ) + or + pos.isThisParameter() and + t = c.getDeclaringType() ) } - /** - * Holds if a write targets `post`, which is a post-update node for a - * parameter at position `pos` in `c`. - */ - private predicate isParameterPostUpdate( - SummaryNode post, SummarizedCallable c, ParameterPosition pos - ) { - post = summaryNodeOutputState(c, SummaryComponentStack::argument(pos)) - } - - /** Holds if a parameter node at position `pos` is required for `c`. */ - private predicate summaryParameterNodeRange(SummarizedCallable c, ParameterPosition pos) { - parameterReadState(c, _, pos) - or - // Same as `isParameterPostUpdate(_, c, pos)`, but can be used in a negative context - any(SummaryNodeState state).isOutputState(c, SummaryComponentStack::argument(pos)) - } - - private predicate callbackOutput( - SummarizedCallable c, SummaryComponentStack s, SummaryNode receiver, ReturnKind rk - ) { - any(SummaryNodeState state).isInputState(c, s) and - s.head() = TReturnSummaryComponent(rk) and - receiver = summaryNodeInputState(c, s.tail()) - } - - private predicate callbackInput( - SummarizedCallable c, SummaryComponentStack s, SummaryNode receiver, ArgumentPosition pos - ) { - any(SummaryNodeState state).isOutputState(c, s) and - s.head() = TParameterSummaryComponent(pos) and - receiver = summaryNodeInputState(c, s.tail()) - } - - /** Holds if a call targeting `receiver` should be synthesized inside `c`. */ - predicate summaryCallbackRange(SummarizedCallable c, SummaryNode receiver) { - callbackOutput(c, _, receiver, _) - or - callbackInput(c, _, receiver, _) - } - - /** - * Gets the type of synthesized summary node `n`. - * - * The type is computed based on the language-specific predicates - * `getContentType()`, `getReturnType()`, `getCallbackParameterType()`, and - * `getCallbackReturnType()`. - */ - DataFlowType summaryNodeType(SummaryNode n) { - exists(SummaryNode pre | - summaryPostUpdateNode(n, pre) and - result = summaryNodeType(pre) - ) - or - exists(SummarizedCallable c, SummaryComponentStack s, SummaryComponent head | head = s.head() | - n = summaryNodeInputState(c, s) and + DataFlowType getReturnType(UnboundCallable c, ReturnKind rk) { + exists(Type t | result.asGvnType() = Gvn::getGlobalValueNumber(t) | + rk instanceof NormalReturnKind and ( - exists(ContentSet cont | result = getContentType(cont) | - head = TContentSummaryComponent(cont) or - head = TWithContentSummaryComponent(cont) - ) - or - head = TWithoutContentSummaryComponent(_) and - result = summaryNodeType(summaryNodeInputState(c, s.tail())) - or - exists(ReturnKind rk | - head = TReturnSummaryComponent(rk) and - result = - getCallbackReturnType(summaryNodeType(summaryNodeInputState(pragma[only_bind_out](c), - s.tail())), rk) - ) - or - exists(SummaryComponent::SyntheticGlobal sg | - head = TSyntheticGlobalSummaryComponent(sg) and - result = getSyntheticGlobalType(sg) - ) + t = c.(Constructor).getDeclaringType() or - exists(ParameterPosition pos | - head = TArgumentSummaryComponent(pos) and - result = getParameterType(c, pos) - ) + not c instanceof Constructor and + t = c.getReturnType() ) or - n = summaryNodeOutputState(c, s) and - ( - exists(ContentSet cont | - head = TContentSummaryComponent(cont) and result = getContentType(cont) - ) - or - s.length() = 1 and - exists(ReturnKind rk | - head = TReturnSummaryComponent(rk) and - result = getReturnType(c, rk) - ) - or - exists(ArgumentPosition pos | head = TParameterSummaryComponent(pos) | - result = - getCallbackParameterType(summaryNodeType(summaryNodeInputState(pragma[only_bind_out](c), - s.tail())), pos) - ) - or - exists(SummaryComponent::SyntheticGlobal sg | - head = TSyntheticGlobalSummaryComponent(sg) and - result = getSyntheticGlobalType(sg) - ) - ) + t = c.getParameter(rk.(OutRefReturnKind).getPosition()).getType() ) } - /** Holds if summary node `p` is a parameter with position `pos`. */ - predicate summaryParameterNode(SummaryNode p, ParameterPosition pos) { - p = TSummaryParameterNode(_, pos) - } - - /** Holds if summary node `out` contains output of kind `rk` from call `c`. */ - predicate summaryOutNode(DataFlowCall c, SummaryNode out, ReturnKind rk) { - exists(SummarizedCallable callable, SummaryComponentStack s, SummaryNode receiver | - callbackOutput(callable, s, receiver, rk) and - out = summaryNodeInputState(callable, s) and - c = summaryDataFlowCall(receiver) + DataFlowType getCallbackParameterType(DataFlowType t, ArgumentPosition pos) { + exists(SystemLinqExpressions::DelegateExtType dt | + t.asGvnType() = Gvn::getGlobalValueNumber(dt) and + result.asGvnType() = + Gvn::getGlobalValueNumber(dt.getDelegateType().getParameter(pos.getPosition()).getType()) ) + or + pos.isDelegateSelf() and + result = t } - /** Holds if summary node `arg` is at position `pos` in the call `c`. */ - predicate summaryArgumentNode(DataFlowCall c, SummaryNode arg, ArgumentPosition pos) { - exists(SummarizedCallable callable, SummaryComponentStack s, SummaryNode receiver | - callbackInput(callable, s, receiver, pos) and - arg = summaryNodeOutputState(callable, s) and - c = summaryDataFlowCall(receiver) + DataFlowType getCallbackReturnType(DataFlowType t, ReturnKind rk) { + rk instanceof NormalReturnKind and + exists(SystemLinqExpressions::DelegateExtType dt | + t.asGvnType() = Gvn::getGlobalValueNumber(dt) and + result.asGvnType() = Gvn::getGlobalValueNumber(dt.getDelegateType().getReturnType()) ) } +} - /** Holds if summary node `post` is a post-update node with pre-update node `pre`. */ - predicate summaryPostUpdateNode(SummaryNode post, SummaryNode pre) { - exists(SummarizedCallable c, ParameterPosition pos | - isParameterPostUpdate(post, c, pos) and - pre = TSummaryParameterNode(c, pos) - ) - or - exists(SummarizedCallable callable, SummaryComponentStack s | - callbackInput(callable, s, _, _) and - pre = summaryNodeOutputState(callable, s) and - post = summaryNodeInputState(callable, s) - ) +private module StepsInput implements Impl::Private::StepsInputSig { + DataFlowCall getACall(Public::SummarizedCallable sc) { + sc = viableCallable(result).asSummarizedCallable() } +} + +module SourceSinkInterpretationInput implements + Impl::Private::External::SourceSinkInterpretationInputSig +{ + private import csharp as Cs - /** Holds if summary node `ret` is a return node of kind `rk`. */ - predicate summaryReturnNode(SummaryNode ret, ReturnKind rk) { - exists(SummaryComponentStack s | - ret = summaryNodeOutputState(_, s) and - s = TSingletonSummaryComponentStack(TReturnSummaryComponent(rk)) + class Element = Cs::Element; + + predicate sourceElement(Element e, string output, string kind, string provenance) { + exists( + string namespace, string type, boolean subtypes, string name, string signature, string ext + | + sourceModel(namespace, type, subtypes, name, signature, ext, output, kind, provenance) and + e = interpretElement(namespace, type, subtypes, name, signature, ext) ) } - /** - * Holds if flow is allowed to pass from parameter `p`, to a return - * node, and back out to `p`. - */ - predicate summaryAllowParameterReturnInSelf(ParamNode p) { - exists(SummarizedCallable c, ParameterPosition ppos | - p.isParameterOf(inject(c), pragma[only_bind_into](ppos)) + predicate sinkElement(Element e, string input, string kind, string provenance) { + exists( + string namespace, string type, boolean subtypes, string name, string signature, string ext | - exists(SummaryComponentStack inputContents, SummaryComponentStack outputContents | - summary(c, inputContents, outputContents, _) and - inputContents.bottom() = pragma[only_bind_into](TArgumentSummaryComponent(ppos)) and - outputContents.bottom() = pragma[only_bind_into](TArgumentSummaryComponent(ppos)) - ) + sinkModel(namespace, type, subtypes, name, signature, ext, input, kind, provenance) and + e = interpretElement(namespace, type, subtypes, name, signature, ext) ) } - /** Provides a compilation of flow summaries to atomic data-flow steps. */ - module Steps { - /** - * Holds if there is a local step from `pred` to `succ`, which is synthesized - * from a flow summary. - */ - predicate summaryLocalStep(SummaryNode pred, SummaryNode succ, boolean preservesValue) { - exists( - SummarizedCallable c, SummaryComponentStack inputContents, - SummaryComponentStack outputContents - | - summary(c, inputContents, outputContents, preservesValue) and - pred = summaryNodeInputState(c, inputContents) and - succ = summaryNodeOutputState(c, outputContents) - | - preservesValue = true - or - preservesValue = false and not summary(c, inputContents, outputContents, true) - ) - or - exists(SummarizedCallable c, SummaryComponentStack s | - pred = summaryNodeInputState(c, s.tail()) and - succ = summaryNodeInputState(c, s) and - s.head() = [SummaryComponent::withContent(_), SummaryComponent::withoutContent(_)] and - preservesValue = true - ) - } + class SourceOrSinkElement = Element; - /** - * Holds if there is a read step of content `c` from `pred` to `succ`, which - * is synthesized from a flow summary. - */ - predicate summaryReadStep(SummaryNode pred, ContentSet c, SummaryNode succ) { - exists(SummarizedCallable sc, SummaryComponentStack s | - pred = summaryNodeInputState(sc, s.tail()) and - succ = summaryNodeInputState(sc, s) and - SummaryComponent::content(c) = s.head() - ) - } - - /** - * Holds if there is a store step of content `c` from `pred` to `succ`, which - * is synthesized from a flow summary. - */ - predicate summaryStoreStep(SummaryNode pred, ContentSet c, SummaryNode succ) { - exists(SummarizedCallable sc, SummaryComponentStack s | - pred = summaryNodeOutputState(sc, s) and - succ = summaryNodeOutputState(sc, s.tail()) and - SummaryComponent::content(c) = s.head() - ) - } + private newtype TInterpretNode = + TElement_(Element n) or + TNode_(Node n) or + TDataFlowCall_(DataFlowCall c) - /** - * Holds if there is a jump step from `pred` to `succ`, which is synthesized - * from a flow summary. - */ - predicate summaryJumpStep(SummaryNode pred, SummaryNode succ) { - exists(SummaryComponentStack s | - s = SummaryComponentStack::singleton(SummaryComponent::syntheticGlobal(_)) and - pred = summaryNodeOutputState(_, s) and - succ = summaryNodeInputState(_, s) - ) - } + /** An entity used to interpret a source/sink specification. */ + class InterpretNode extends TInterpretNode { + /** Gets the element that this node corresponds to, if any. */ + SourceOrSinkElement asElement() { this = TElement_(result) } - /** - * Holds if values stored inside content `c` are cleared at `n`. `n` is a - * synthesized summary node, so in order for values to be cleared at calls - * to the relevant method, it is important that flow does not pass over - * the argument, either via use-use flow or def-use flow. - * - * Example: - * - * ``` - * a.b = taint; - * a.clearB(); // assume we have a flow summary for `clearB` that clears `b` on the qualifier - * sink(a.b); - * ``` - * - * In the above, flow should not pass from `a` on the first line (or the second - * line) to `a` on the third line. Instead, there will be synthesized flow from - * `a` on line 2 to the post-update node for `a` on that line (via an intermediate - * node where field `b` is cleared). - */ - predicate summaryClearsContent(SummaryNode n, ContentSet c) { - exists(SummarizedCallable sc, SummaryNodeState state, SummaryComponentStack stack | - n = TSummaryInternalNode(sc, state) and - state.isInputState(sc, stack) and - stack.head() = SummaryComponent::withoutContent(c) - ) - } + /** Gets the data-flow node that this node corresponds to, if any. */ + Node asNode() { this = TNode_(result) } - /** - * Holds if the value that is being tracked is expected to be stored inside - * content `c` at `n`. - */ - predicate summaryExpectsContent(SummaryNode n, ContentSet c) { - exists(SummarizedCallable sc, SummaryNodeState state, SummaryComponentStack stack | - n = TSummaryInternalNode(sc, state) and - state.isInputState(sc, stack) and - stack.head() = SummaryComponent::withContent(c) - ) - } + /** Gets the call that this node corresponds to, if any. */ + DataFlowCall asCall() { this = TDataFlowCall_(result) } - pragma[noinline] - private predicate viableParam( - DataFlowCall call, SummarizedCallable sc, ParameterPosition ppos, SummaryParamNode p - ) { - exists(DataFlowCallable c | - c = inject(sc) and - p = TSummaryParameterNode(sc, ppos) and - c = viableCallable(call) - ) - } + /** Gets the callable that this node corresponds to, if any. */ + DataFlowCallable asCallable() { result.getUnderlyingCallable() = this.asElement() } - pragma[nomagic] - private SummaryParamNode summaryArgParam(DataFlowCall call, ArgNode arg, SummarizedCallable sc) { - exists(ParameterPosition ppos | - argumentPositionMatch(call, arg, ppos) and - viableParam(call, sc, ppos, result) - ) - } + /** Gets the target of this call, if any. */ + Element getCallTarget() { result = this.asCall().(NonDelegateDataFlowCall).getATarget(_) } - /** - * Holds if `p` can reach `n` in a summarized callable, using only value-preserving - * local steps. `clearsOrExpects` records whether any node on the path from `p` to - * `n` either clears or expects contents. - */ - private predicate paramReachesLocal(SummaryParamNode p, SummaryNode n, boolean clearsOrExpects) { - viableParam(_, _, _, p) and - n = p and - clearsOrExpects = false + /** Gets a textual representation of this node. */ + string toString() { + result = this.asElement().toString() or - exists(SummaryNode mid, boolean clearsOrExpectsMid | - paramReachesLocal(p, mid, clearsOrExpectsMid) and - summaryLocalStep(mid, n, true) and - if - summaryClearsContent(n, _) or - summaryExpectsContent(n, _) - then clearsOrExpects = true - else clearsOrExpects = clearsOrExpectsMid - ) - } - - /** - * Holds if use-use flow starting from `arg` should be prohibited. - * - * This is the case when `arg` is the argument of a call that targets a - * flow summary where the corresponding parameter either clears contents - * or expects contents. - */ - pragma[nomagic] - predicate prohibitsUseUseFlow(ArgNode arg, SummarizedCallable sc) { - exists(SummaryParamNode p, ParameterPosition ppos, SummaryNode ret | - paramReachesLocal(p, ret, true) and - p = summaryArgParam(_, arg, sc) and - p = TSummaryParameterNode(_, pragma[only_bind_into](ppos)) and - isParameterPostUpdate(ret, _, pragma[only_bind_into](ppos)) - ) + result = this.asNode().toString() + or + result = this.asCall().toString() } - pragma[nomagic] - private predicate summaryReturnNodeExt(SummaryNode ret, ReturnKindExt rk) { - summaryReturnNode(ret, rk.(ValueReturnKind).getKind()) + /** Gets the location of this node. */ + Location getLocation() { + result = this.asElement().getLocation() or - exists(SummaryParamNode p, SummaryNode pre, ParameterPosition pos | - paramReachesLocal(p, pre, _) and - summaryPostUpdateNode(ret, pre) and - p = TSummaryParameterNode(_, pos) and - rk.(ParamUpdateReturnKind).getPosition() = pos - ) + result = this.asNode().getLocation() + or + result = this.asCall().getLocation() } + } - bindingset[ret] - private SummaryParamNode summaryArgParamRetOut( - ArgNode arg, SummaryNode ret, OutNodeExt out, SummarizedCallable sc - ) { - exists(DataFlowCall call, ReturnKindExt rk | - result = summaryArgParam(call, arg, sc) and - summaryReturnNodeExt(ret, pragma[only_bind_into](rk)) and - out = pragma[only_bind_into](rk).getAnOutNode(call) - ) - } + /** Provides additional sink specification logic. */ + predicate interpretOutput(string c, InterpretNode mid, InterpretNode node) { + exists(Node n | n = node.asNode() | + (c = "Parameter" or c = "") and + n.asParameter() = mid.asElement() + or + c = "" and + n.asExpr().(AssignableRead).getTarget().getUnboundDeclaration() = mid.asElement() + ) + } - /** - * Holds if `arg` flows to `out` using a simple value-preserving flow - * summary, that is, a flow summary without reads and stores. - * - * NOTE: This step should not be used in global data-flow/taint-tracking, but may - * be useful to include in the exposed local data-flow/taint-tracking relations. - */ - predicate summaryThroughStepValue(ArgNode arg, Node out, SummarizedCallable sc) { - exists(ReturnKind rk, SummaryNode ret, DataFlowCall call | - summaryLocalStep(summaryArgParam(call, arg, sc), ret, true) and - summaryReturnNode(ret, pragma[only_bind_into](rk)) and - out = getAnOutNode(call, pragma[only_bind_into](rk)) - ) - } + /** Provides additional source specification logic. */ + predicate interpretInput(string c, InterpretNode mid, InterpretNode node) { + c = "" and + exists(Assignable a | + node.asNode().asExpr() = a.getAnAssignedValue() and + a.getUnboundDeclaration() = mid.asElement() + ) + } +} - /** - * Holds if `arg` flows to `out` using a simple flow summary involving taint - * step, that is, a flow summary without reads and stores. - * - * NOTE: This step should not be used in global data-flow/taint-tracking, but may - * be useful to include in the exposed local data-flow/taint-tracking relations. - */ - predicate summaryThroughStepTaint(ArgNode arg, Node out, SummarizedCallable sc) { - exists(SummaryNode ret | - summaryLocalStep(summaryArgParamRetOut(arg, ret, out, sc), ret, false) - ) - } +module Private { + import Impl::Private + import Impl::Private::Types - /** - * Holds if there is a read(+taint) of `c` from `arg` to `out` using a - * flow summary. - * - * NOTE: This step should not be used in global data-flow/taint-tracking, but may - * be useful to include in the exposed local data-flow/taint-tracking relations. - */ - predicate summaryGetterStep(ArgNode arg, ContentSet c, Node out, SummarizedCallable sc) { - exists(SummaryNode mid, SummaryNode ret | - summaryReadStep(summaryArgParamRetOut(arg, ret, out, sc), c, mid) and - summaryLocalStep(mid, ret, _) - ) - } + module Steps = Impl::Private::Steps; - /** - * Holds if there is a (taint+)store of `arg` into content `c` of `out` using a - * flow summary. - * - * NOTE: This step should not be used in global data-flow/taint-tracking, but may - * be useful to include in the exposed local data-flow/taint-tracking relations. - */ - predicate summarySetterStep(ArgNode arg, ContentSet c, Node out, SummarizedCallable sc) { - exists(SummaryNode mid, SummaryNode ret | - summaryLocalStep(summaryArgParamRetOut(arg, ret, out, sc), mid, _) and - summaryStoreStep(mid, c, ret) - ) - } - } - - /** - * Provides a means of translating externally (e.g., MaD) defined flow - * summaries into a `SummarizedCallable`s. - */ module External { - /** Holds if `spec` is a relevant external specification. */ - private predicate relevantSpec(string spec) { - summaryElement(_, spec, _, _, _) or - summaryElement(_, _, spec, _, _) or - sourceElement(_, spec, _, _) or - sinkElement(_, spec, _, _) - } + import Impl::Private::External + import Impl::Private::External::SourceSinkInterpretation + } - private class AccessPathRange extends AccessPath::Range { - AccessPathRange() { relevantSpec(this) } - } + private module SummaryComponentInternal = Impl::Private::SummaryComponent; - /** Holds if specification component `token` parses as parameter `pos`. */ - predicate parseParam(AccessPathToken token, ArgumentPosition pos) { - token.getName() = "Parameter" and - pos = parseParamBody(token.getAnArgument()) - } + /** Provides predicates for constructing summary components. */ + module SummaryComponent { + predicate content = SummaryComponentInternal::content/1; - /** Holds if specification component `token` parses as argument `pos`. */ - predicate parseArg(AccessPathToken token, ParameterPosition pos) { - token.getName() = "Argument" and - pos = parseArgBody(token.getAnArgument()) + /** Gets a summary component for parameter `i`. */ + SummaryComponent parameter(int i) { + exists(ArgumentPosition pos | + result = SummaryComponentInternal::parameter(pos) and + i = pos.getPosition() + ) } - /** Holds if specification component `token` parses as synthetic global `sg`. */ - predicate parseSynthGlobal(AccessPathToken token, string sg) { - token.getName() = "SyntheticGlobal" and - sg = token.getAnArgument() + /** Gets a summary component for argument `i`. */ + SummaryComponent argument(int i) { + exists(ParameterPosition pos | + result = SummaryComponentInternal::argument(pos) and + i = pos.getPosition() + ) } - private class SyntheticGlobalFromAccessPath extends SummaryComponent::SyntheticGlobal { - SyntheticGlobalFromAccessPath() { parseSynthGlobal(_, this) } - } + predicate return = SummaryComponentInternal::return/1; - private SummaryComponent interpretComponent(AccessPathToken token) { + /** Gets a summary component that represents a qualifier. */ + SummaryComponent qualifier() { exists(ParameterPosition pos | - parseArg(token, pos) and result = SummaryComponent::argument(pos) - ) - or - exists(ArgumentPosition pos | - parseParam(token, pos) and result = SummaryComponent::parameter(pos) - ) - or - token = "ReturnValue" and result = SummaryComponent::return(getReturnValueKind()) - or - exists(string sg | - parseSynthGlobal(token, sg) and result = SummaryComponent::syntheticGlobal(sg) + result = SummaryComponentInternal::argument(pos) and + pos.isThisParameter() ) - or - result = interpretComponentSpecific(token) } - /** - * Holds if `spec` specifies summary component stack `stack`. - */ - predicate interpretSpec(AccessPath spec, SummaryComponentStack stack) { - interpretSpec(spec, spec.getNumToken(), stack) - } + /** Gets a summary component that represents an element in a collection. */ + SummaryComponent element() { result = content(any(DataFlow::ElementContent c)) } - /** Holds if the first `n` tokens of `spec` resolves to `stack`. */ - private predicate interpretSpec(AccessPath spec, int n, SummaryComponentStack stack) { - n = 1 and - stack = SummaryComponentStack::singleton(interpretComponent(spec.getToken(0))) - or - exists(SummaryComponent head, SummaryComponentStack tail | - interpretSpec(spec, n, head, tail) and - stack = SummaryComponentStack::push(head, tail) - ) + /** Gets a summary component for property `p`. */ + SummaryComponent property(Property p) { + result = + content(any(DataFlow::PropertyContent c | c.getProperty() = p.getUnboundDeclaration())) } - /** Holds if the first `n` tokens of `spec` resolves to `head` followed by `tail` */ - private predicate interpretSpec( - AccessPath spec, int n, SummaryComponent head, SummaryComponentStack tail - ) { - interpretSpec(spec, n - 1, tail) and - head = interpretComponent(spec.getToken(n - 1)) + /** Gets a summary component for field `f`. */ + SummaryComponent field(Field f) { + result = content(any(DataFlow::FieldContent c | c.getField() = f.getUnboundDeclaration())) } - private class MkStack extends RequiredSummaryComponentStack { - override predicate required(SummaryComponent head, SummaryComponentStack tail) { - interpretSpec(_, _, head, tail) - } - } + /** Gets a summary component that represents the return value of a call. */ + SummaryComponent return() { result = return(any(NormalReturnKind rk)) } - private class SummarizedCallableExternal extends SummarizedCallable { - SummarizedCallableExternal() { summaryElement(this, _, _, _, _) } - - private predicate relevantSummaryElementGenerated( - AccessPath inSpec, AccessPath outSpec, string kind - ) { - exists(Provenance provenance | - provenance.isGenerated() and - summaryElement(this, inSpec, outSpec, kind, provenance) - ) and - not this.applyManualModel() - } - - private predicate relevantSummaryElement(AccessPath inSpec, AccessPath outSpec, string kind) { - exists(Provenance provenance | - provenance.isManual() and - summaryElement(this, inSpec, outSpec, kind, provenance) - ) - or - this.relevantSummaryElementGenerated(inSpec, outSpec, kind) - } - - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - exists(AccessPath inSpec, AccessPath outSpec, string kind | - this.relevantSummaryElement(inSpec, outSpec, kind) and - interpretSpec(inSpec, input) and - interpretSpec(outSpec, output) - | - kind = "value" and preservesValue = true - or - kind = "taint" and preservesValue = false - ) - } - - override predicate hasProvenance(Provenance provenance) { - summaryElement(this, _, _, _, provenance) - } - } + predicate syntheticGlobal = SummaryComponentInternal::syntheticGlobal/1; - /** Holds if component `c` of specification `spec` cannot be parsed. */ - predicate invalidSpecComponent(AccessPath spec, string c) { - c = spec.getToken(_) and - not exists(interpretComponent(c)) - } + class SyntheticGlobal = Impl::Private::SyntheticGlobal; + } - /** Holds if `provenance` is not a valid provenance value. */ - bindingset[provenance] - predicate invalidProvenance(string provenance) { not provenance instanceof Provenance } - - /** - * Holds if token `part` of specification `spec` has an invalid index. - * E.g., `Argument[-1]`. - */ - predicate invalidIndexComponent(AccessPath spec, AccessPathToken part) { - part = spec.getToken(_) and - part.getName() = ["Parameter", "Argument"] and - AccessPath::parseInt(part.getArgumentList()) < 0 - } + private module SummaryComponentStackInternal = Impl::Private::SummaryComponentStack; - private predicate inputNeedsReference(AccessPathToken c) { - c.getName() = "Argument" or - inputNeedsReferenceSpecific(c) - } + /** Provides predicates for constructing stacks of summary components. */ + module SummaryComponentStack { + predicate singleton = SummaryComponentStackInternal::singleton/1; - private predicate outputNeedsReference(AccessPathToken c) { - c.getName() = ["Argument", "ReturnValue"] or - outputNeedsReferenceSpecific(c) - } + predicate push = SummaryComponentStackInternal::push/2; - private predicate sourceElementRef(InterpretNode ref, AccessPath output, string kind) { - exists(SourceOrSinkElement e | - sourceElement(e, output, kind, _) and - if outputNeedsReference(output.getToken(0)) - then e = ref.getCallTarget() - else e = ref.asElement() - ) - } + /** Gets a singleton stack for argument `i`. */ + SummaryComponentStack argument(int i) { result = singleton(SummaryComponent::argument(i)) } - private predicate sinkElementRef(InterpretNode ref, AccessPath input, string kind) { - exists(SourceOrSinkElement e | - sinkElement(e, input, kind, _) and - if inputNeedsReference(input.getToken(0)) - then e = ref.getCallTarget() - else e = ref.asElement() - ) - } + predicate return = SummaryComponentStackInternal::return/1; - /** Holds if the first `n` tokens of `output` resolve to the given interpretation. */ - private predicate interpretOutput( - AccessPath output, int n, InterpretNode ref, InterpretNode node - ) { - sourceElementRef(ref, output, _) and - n = 0 and - ( - if output = "" - then - // Allow language-specific interpretation of the empty access path - interpretOutputSpecific("", ref, node) - else node = ref - ) - or - exists(InterpretNode mid, AccessPathToken c | - interpretOutput(output, n - 1, ref, mid) and - c = output.getToken(n - 1) - | - exists(ArgumentPosition apos, ParameterPosition ppos | - node.asNode().(PostUpdateNode).getPreUpdateNode().(ArgNode).argumentOf(mid.asCall(), apos) and - parameterMatch(ppos, apos) - | - c = "Argument" or parseArg(c, ppos) - ) - or - exists(ArgumentPosition apos, ParameterPosition ppos | - node.asNode().(ParamNode).isParameterOf(mid.asCallable(), ppos) and - parameterMatch(ppos, apos) - | - c = "Parameter" or parseParam(c, apos) - ) - or - c = "ReturnValue" and - node.asNode() = getAnOutNodeExt(mid.asCall(), TValueReturn(getReturnValueKind())) - or - interpretOutputSpecific(c, mid, node) - ) + /** Gets a singleton stack representing a qualifier. */ + SummaryComponentStack qualifier() { result = singleton(SummaryComponent::qualifier()) } + + /** Gets a stack representing an element of `container`. */ + SummaryComponentStack elementOf(SummaryComponentStack container) { + result = push(SummaryComponent::element(), container) } - /** Holds if the first `n` tokens of `input` resolve to the given interpretation. */ - private predicate interpretInput(AccessPath input, int n, InterpretNode ref, InterpretNode node) { - sinkElementRef(ref, input, _) and - n = 0 and - ( - if input = "" - then - // Allow language-specific interpretation of the empty access path - interpretInputSpecific("", ref, node) - else node = ref - ) - or - exists(InterpretNode mid, AccessPathToken c | - interpretInput(input, n - 1, ref, mid) and - c = input.getToken(n - 1) - | - exists(ArgumentPosition apos, ParameterPosition ppos | - node.asNode().(ArgNode).argumentOf(mid.asCall(), apos) and - parameterMatch(ppos, apos) - | - c = "Argument" or parseArg(c, ppos) - ) - or - exists(ReturnNodeExt ret | - c = "ReturnValue" and - ret = node.asNode() and - ret.getKind().(ValueReturnKind).getKind() = getReturnValueKind() and - mid.asCallable() = getNodeEnclosingCallable(ret) - ) - or - interpretInputSpecific(c, mid, node) - ) + /** Gets a stack representing a property `p` of `object`. */ + SummaryComponentStack propertyOf(Property p, SummaryComponentStack object) { + result = push(SummaryComponent::property(p), object) } - /** - * Holds if `node` is specified as a source with the given kind in a MaD flow - * model. - */ - predicate isSourceNode(InterpretNode node, string kind) { - exists(InterpretNode ref, AccessPath output | - sourceElementRef(ref, output, kind) and - interpretOutput(output, output.getNumToken(), ref, node) - ) + /** Gets a stack representing a field `f` of `object`. */ + SummaryComponentStack fieldOf(Field f, SummaryComponentStack object) { + result = push(SummaryComponent::field(f), object) } - /** - * Holds if `node` is specified as a sink with the given kind in a MaD flow - * model. - */ - predicate isSinkNode(InterpretNode node, string kind) { - exists(InterpretNode ref, AccessPath input | - sinkElementRef(ref, input, kind) and - interpretInput(input, input.getNumToken(), ref, node) - ) + /** Gets a singleton stack representing the return value of a call. */ + SummaryComponentStack return() { result = singleton(SummaryComponent::return()) } + + /** Gets a singleton stack representing a synthetic global with name `name`. */ + SummaryComponentStack syntheticGlobal(string synthetic) { + result = singleton(SummaryComponent::syntheticGlobal(synthetic)) } } +} - /** Provides a query predicate for outputting a set of relevant flow summaries. */ - module TestOutput { - /** A flow summary to include in the `summary/1` query predicate. */ - abstract class RelevantSummarizedCallable instanceof SummarizedCallable { - /** Gets the string representation of this callable used by `summary/1`. */ - abstract string getCallableCsv(); - - /** Holds if flow is propagated between `input` and `output`. */ - predicate relevantSummary( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - super.propagatesFlow(input, output, preservesValue) - } - - string toString() { result = super.toString() } - } +module Public = Impl::Public; - /** A model to include in the `neutral/1` query predicate. */ - abstract class RelevantNeutralCallable instanceof NeutralCallable { - /** Gets the string representation of this callable used by `neutral/1`. */ - abstract string getCallableCsv(); +// import all instances below +private module BidirectionalImports { + private import semmle.code.csharp.dataflow.internal.ExternalFlow + private import semmle.code.csharp.frameworks.EntityFramework +} - /** - * Gets the kind of the neutral. - */ - string getKind() { result = super.getKind() } +private predicate recordConstructorFlow(Constructor c, int i, Property p) { + c = any(RecordType r).getAMember() and + exists(string name | + c.getParameter(i).getName() = name and + c.getDeclaringType().getAMember(name) = p + ) +} - string toString() { result = super.toString() } - } +private class RecordConstructorFlow extends Impl::Private::SummarizedCallableImpl { + RecordConstructorFlow() { recordConstructorFlow(this, _, _) } - /** Render the kind in the format used in flow summaries. */ - private string renderKind(boolean preservesValue) { - preservesValue = true and result = "value" - or - preservesValue = false and result = "taint" - } + predicate propagatesFlowImpl( + Impl::Private::SummaryComponentStack input, Impl::Private::SummaryComponentStack output, + boolean preservesValue + ) { + exists(int i, Property p | + recordConstructorFlow(this, i, p) and + input = Private::SummaryComponentStack::argument(i) and + output = + Private::SummaryComponentStack::propertyOf(p, Private::SummaryComponentStack::return()) and + preservesValue = true + ) + } - private string renderProvenance(SummarizedCallable c) { - if c.applyManualModel() then result = "manual" else c.hasProvenance(result) - } + override predicate propagatesFlow( + Impl::Private::SummaryComponentStack input, Impl::Private::SummaryComponentStack output, + boolean preservesValue + ) { + this.propagatesFlowImpl(input, output, preservesValue) + } - private string renderProvenanceNeutral(NeutralCallable c) { - if c.hasManualModel() then result = "manual" else c.hasProvenance(result) - } + override predicate hasProvenance(Public::Provenance provenance) { provenance = "manual" } +} - /** - * A query predicate for outputting flow summaries in semi-colon separated format in QL tests. - * The syntax is: "namespace;type;overrides;name;signature;ext;inputspec;outputspec;kind;provenance", - * ext is hardcoded to empty. - */ - query predicate summary(string csv) { - exists( - RelevantSummarizedCallable c, SummaryComponentStack input, SummaryComponentStack output, - boolean preservesValue - | - c.relevantSummary(input, output, preservesValue) and - csv = - c.getCallableCsv() // Callable information - + input.getMadRepresentation() + ";" // input - + output.getMadRepresentation() + ";" // output - + renderKind(preservesValue) + ";" // kind - + renderProvenance(c) // provenance - ) - } +// see `SummarizedCallableImpl` qldoc +private class RecordConstructorFlowAdapter extends Impl::Public::SummarizedCallable instanceof RecordConstructorFlow +{ + override predicate propagatesFlow(string input, string output, boolean preservesValue) { none() } - /** - * Holds if a neutral model `csv` exists (semi-colon separated format). Used for testing purposes. - * The syntax is: "namespace;type;name;signature;kind;provenance"", - */ - query predicate neutral(string csv) { - exists(RelevantNeutralCallable c | - csv = - c.getCallableCsv() // Callable information - + c.getKind() + ";" // kind - + renderProvenanceNeutral(c) // provenance - ) - } + override predicate hasProvenance(Public::Provenance provenance) { + RecordConstructorFlow.super.hasProvenance(provenance) } +} - /** - * Provides query predicates for rendering the generated data flow graph for - * a summarized callable. - * - * Import this module into a `.ql` file of `@kind graph` to render the graph. - * The graph is restricted to callables from `RelevantSummarizedCallable`. - */ - module RenderSummarizedCallable { - /** A summarized callable to include in the graph. */ - abstract class RelevantSummarizedCallable instanceof SummarizedCallable { - string toString() { result = super.toString() } - } +private class RecordConstructorFlowRequiredSummaryComponentStack extends Impl::Private::RequiredSummaryComponentStack +{ + override predicate required( + Impl::Private::SummaryComponent head, Impl::Private::SummaryComponentStack tail + ) { + exists(Property p | + recordConstructorFlow(_, _, p) and + head = Private::SummaryComponent::property(p) and + tail = Private::SummaryComponentStack::return() + ) + } +} - private newtype TNodeOrCall = - MkNode(SummaryNode n) { - exists(RelevantSummarizedCallable c | - n = TSummaryInternalNode(c, _) - or - n = TSummaryParameterNode(c, _) - ) - } or - MkCall(DataFlowCall call) { - call = summaryDataFlowCall(_) and - call.getEnclosingCallable() = inject(any(RelevantSummarizedCallable c)) - } - - private class NodeOrCall extends TNodeOrCall { - SummaryNode asNode() { this = MkNode(result) } - - DataFlowCall asCall() { this = MkCall(result) } - - string toString() { - result = this.asNode().toString() - or - result = this.asCall().toString() - } - - /** - * Holds if this element is at the specified location. - * The location spans column `startcolumn` of line `startline` to - * column `endcolumn` of line `endline` in file `filepath`. - * For more information, see - * [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). - */ - predicate hasLocationInfo( - string filepath, int startline, int startcolumn, int endline, int endcolumn - ) { - filepath = "" and - startline = 0 and - startcolumn = 0 and - endline = 0 and - endcolumn = 0 - } - } +private import semmle.code.csharp.frameworks.system.linq.Expressions - query predicate nodes(NodeOrCall n, string key, string val) { - key = "semmle.label" and val = n.toString() - } +private predicate mayInvokeCallback(Callable c, int n) { + c.getParameter(n).getType() instanceof SystemLinqExpressions::DelegateExtType and + not c.fromSource() +} - private predicate edgesComponent(NodeOrCall a, NodeOrCall b, string value) { - exists(boolean preservesValue | - Private::Steps::summaryLocalStep(a.asNode(), b.asNode(), preservesValue) and - if preservesValue = true then value = "value" else value = "taint" - ) - or - exists(ContentSet c | - Private::Steps::summaryReadStep(a.asNode(), c, b.asNode()) and - value = "read (" + c + ")" - or - Private::Steps::summaryStoreStep(a.asNode(), c, b.asNode()) and - value = "store (" + c + ")" - or - Private::Steps::summaryClearsContent(a.asNode(), c) and - b = a and - value = "clear (" + c + ")" - or - Private::Steps::summaryExpectsContent(a.asNode(), c) and - b = a and - value = "expect (" + c + ")" - ) - or - summaryPostUpdateNode(b.asNode(), a.asNode()) and - value = "post-update" - or - b.asCall() = summaryDataFlowCall(a.asNode()) and - value = "receiver" - or - exists(ArgumentPosition pos | - summaryArgumentNode(b.asCall(), a.asNode(), pos) and - value = "argument (" + pos + ")" - ) - } +private class SummarizedCallableWithCallback extends Public::SummarizedCallable { + private int pos; - query predicate edges(NodeOrCall a, NodeOrCall b, string key, string value) { - key = "semmle.label" and - value = strictconcat(string s | edgesComponent(a, b, s) | s, " / ") - } + SummarizedCallableWithCallback() { mayInvokeCallback(this, pos) } + + override predicate propagatesFlow(string input, string output, boolean preservesValue) { + input = "Argument[" + pos + "]" and + output = "Argument[" + pos + "].Parameter[delegate-self]" and + preservesValue = true } + + override predicate hasProvenance(Public::Provenance provenance) { provenance = "hq-generated" } } diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImplSpecific.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImplSpecific.qll deleted file mode 100644 index eadad66d75556..0000000000000 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImplSpecific.qll +++ /dev/null @@ -1,340 +0,0 @@ -/** - * Provides C# specific classes and predicates for defining flow summaries. - */ - -private import csharp -private import dotnet -private import semmle.code.csharp.frameworks.system.linq.Expressions -private import DataFlowDispatch -private import DataFlowPrivate -private import DataFlowPublic -private import DataFlowImplCommon -private import FlowSummaryImpl::Private -private import FlowSummaryImpl::Public -private import semmle.code.csharp.Unification -private import ExternalFlow -private import semmle.code.csharp.dataflow.FlowSummary as FlowSummary - -/** - * A class of callables that are candidates for flow summary modeling. - */ -class SummarizedCallableBase = UnboundCallable; - -/** - * A class of callables that are candidates for neutral modeling. - */ -class NeutralCallableBase = UnboundCallable; - -/** - * A module for importing frameworks that define synthetic globals. - */ -private module SyntheticGlobals { - private import semmle.code.csharp.frameworks.EntityFramework -} - -DataFlowCallable inject(SummarizedCallable c) { result.asSummarizedCallable() = c } - -/** Gets the parameter position of the instance parameter. */ -ArgumentPosition callbackSelfParameterPosition() { result.isDelegateSelf() } - -/** Gets the synthesized data-flow call for `receiver`. */ -SummaryCall summaryDataFlowCall(SummaryNode receiver) { receiver = result.getReceiver() } - -/** Gets the type of content `c`. */ -DataFlowType getContentType(Content c) { - exists(Type t | result.asGvnType() = Gvn::getGlobalValueNumber(t) | - t = c.(FieldContent).getField().getType() - or - t = c.(PropertyContent).getProperty().getType() - or - t = c.(SyntheticFieldContent).getField().getType() - or - c instanceof ElementContent and - t instanceof ObjectType // we don't know what the actual element type is - ) -} - -/** Gets the type of the parameter at the given position. */ -DataFlowType getParameterType(SummarizedCallable c, ParameterPosition pos) { - exists(Type t | result.asGvnType() = Gvn::getGlobalValueNumber(t) | - exists(int i | - pos.getPosition() = i and - t = c.getParameter(i).getType() - ) - or - pos.isThisParameter() and - t = c.getDeclaringType() - ) -} - -/** Gets the return type of kind `rk` for callable `c`. */ -DataFlowType getReturnType(DotNet::Callable c, ReturnKind rk) { - exists(Type t | result.asGvnType() = Gvn::getGlobalValueNumber(t) | - rk instanceof NormalReturnKind and - ( - t = c.(Constructor).getDeclaringType() - or - not c instanceof Constructor and - t = c.getReturnType() - ) - or - t = c.getParameter(rk.(OutRefReturnKind).getPosition()).getType() - ) -} - -/** - * Gets the type of the parameter matching arguments at position `pos` in a - * synthesized call that targets a callback of type `t`. - */ -DataFlowType getCallbackParameterType(DataFlowType t, ArgumentPosition pos) { - exists(SystemLinqExpressions::DelegateExtType dt | - t.asGvnType() = Gvn::getGlobalValueNumber(dt) and - result.asGvnType() = - Gvn::getGlobalValueNumber(dt.getDelegateType().getParameter(pos.getPosition()).getType()) - ) - or - pos.isDelegateSelf() and - result = t -} - -/** - * Gets the return type of kind `rk` in a synthesized call that targets a - * callback of type `t`. - */ -DataFlowType getCallbackReturnType(DataFlowType t, ReturnKind rk) { - rk instanceof NormalReturnKind and - exists(SystemLinqExpressions::DelegateExtType dt | - t.asGvnType() = Gvn::getGlobalValueNumber(dt) and - result.asGvnType() = Gvn::getGlobalValueNumber(dt.getDelegateType().getReturnType()) - ) -} - -/** Gets the type of synthetic global `sg`. */ -DataFlowType getSyntheticGlobalType(SummaryComponent::SyntheticGlobal sg) { - exists(sg) and - result.asGvnType() = Gvn::getGlobalValueNumber(any(ObjectType t)) -} - -/** - * Holds if an external flow summary exists for `c` with input specification - * `input`, output specification `output`, kind `kind`, and provenance `provenance`. - */ -predicate summaryElement(Callable c, string input, string output, string kind, string provenance) { - exists( - string namespace, string type, boolean subtypes, string name, string signature, string ext - | - summaryModel(namespace, type, subtypes, name, signature, ext, input, output, kind, provenance) and - c = interpretElement(namespace, type, subtypes, name, signature, ext) - ) -} - -/** - * Holds if a neutral model exists for `c` of kind `kind` - * and with provenance `provenance`. - */ -predicate neutralElement(Callable c, string kind, string provenance) { - exists(string namespace, string type, string name, string signature | - neutralModel(namespace, type, name, signature, kind, provenance) and - c = interpretElement(namespace, type, false, name, signature, "") - ) -} - -/** - * Holds if an external source specification exists for `e` with output specification - * `output`, kind `kind`, and provenance `provenance`. - */ -predicate sourceElement(Element e, string output, string kind, string provenance) { - exists( - string namespace, string type, boolean subtypes, string name, string signature, string ext - | - sourceModel(namespace, type, subtypes, name, signature, ext, output, kind, provenance) and - e = interpretElement(namespace, type, subtypes, name, signature, ext) - ) -} - -/** - * Holds if an external sink specification exists for `e` with input specification - * `input`, kind `kind` and provenance `provenance`. - */ -predicate sinkElement(Element e, string input, string kind, string provenance) { - exists( - string namespace, string type, boolean subtypes, string name, string signature, string ext - | - sinkModel(namespace, type, subtypes, name, signature, ext, input, kind, provenance) and - e = interpretElement(namespace, type, subtypes, name, signature, ext) - ) -} - -/** Gets the summary component for specification component `c`, if any. */ -bindingset[c] -SummaryComponent interpretComponentSpecific(AccessPathToken c) { - c = "Element" and result = SummaryComponent::content(any(ElementContent ec)) - or - c = "WithoutElement" and result = SummaryComponent::withoutContent(any(ElementContent ec)) - or - c = "WithElement" and result = SummaryComponent::withContent(any(ElementContent ec)) - or - // Qualified names may contain commas,such as in `Tuple<,>`, so get the entire argument list - // rather than an individual argument. - exists(Field f | - c.getName() = "Field" and - c.getArgumentList() = f.getFullyQualifiedName() and - result = SummaryComponent::content(any(FieldContent fc | fc.getField() = f)) - ) - or - exists(Property p | - c.getName() = "Property" and - c.getArgumentList() = p.getFullyQualifiedName() and - result = SummaryComponent::content(any(PropertyContent pc | pc.getProperty() = p)) - ) - or - exists(SyntheticField f | - c.getAnArgument("SyntheticField") = f and - result = SummaryComponent::content(any(SyntheticFieldContent sfc | sfc.getField() = f)) - ) -} - -/** Gets the textual representation of the content in the format used for MaD models. */ -private string getContentSpecific(Content c) { - c = TElementContent() and result = "Element" - or - exists(Field f | c = TFieldContent(f) and result = "Field[" + f.getFullyQualifiedName() + "]") - or - exists(Property p | - c = TPropertyContent(p) and result = "Property[" + p.getFullyQualifiedName() + "]" - ) - or - exists(SyntheticField f | c = TSyntheticFieldContent(f) and result = "SyntheticField[" + f + "]") -} - -/** Gets the textual representation of a summary component in the format used for MaD models. */ -string getMadRepresentationSpecific(SummaryComponent sc) { - exists(Content c | sc = TContentSummaryComponent(c) and result = getContentSpecific(c)) - or - sc = TWithoutContentSummaryComponent(_) and result = "WithoutElement" - or - sc = TWithContentSummaryComponent(_) and result = "WithElement" - or - exists(OutRefReturnKind rk | - sc = TReturnSummaryComponent(rk) and - result = "Argument[" + rk.getPosition() + "]" - ) -} - -/** Gets the textual representation of a parameter position in the format used for flow summaries. */ -string getParameterPosition(ParameterPosition pos) { - result = pos.getPosition().toString() - or - pos.isThisParameter() and - result = "this" - or - pos.isDelegateSelf() and - result = "delegate-self" -} - -/** Gets the textual representation of an argument position in the format used for flow summaries. */ -string getArgumentPosition(ArgumentPosition pos) { - result = pos.getPosition().toString() - or - pos.isQualifier() and - result = "this" - or - pos.isDelegateSelf() and - result = "delegate-self" -} - -/** Holds if input specification component `c` needs a reference. */ -predicate inputNeedsReferenceSpecific(string c) { none() } - -/** Holds if output specification component `c` needs a reference. */ -predicate outputNeedsReferenceSpecific(string c) { none() } - -class SourceOrSinkElement = Element; - -/** Gets the return kind corresponding to specification `"ReturnValue"`. */ -NormalReturnKind getReturnValueKind() { any() } - -private newtype TInterpretNode = - TElement_(Element n) or - TNode_(Node n) or - TDataFlowCall_(DataFlowCall c) - -/** An entity used to interpret a source/sink specification. */ -class InterpretNode extends TInterpretNode { - /** Gets the element that this node corresponds to, if any. */ - SourceOrSinkElement asElement() { this = TElement_(result) } - - /** Gets the data-flow node that this node corresponds to, if any. */ - Node asNode() { this = TNode_(result) } - - /** Gets the call that this node corresponds to, if any. */ - DataFlowCall asCall() { this = TDataFlowCall_(result) } - - /** Gets the callable that this node corresponds to, if any. */ - DataFlowCallable asCallable() { result.getUnderlyingCallable() = this.asElement() } - - /** Gets the target of this call, if any. */ - Callable getCallTarget() { result = this.asCall().(NonDelegateDataFlowCall).getATarget(_) } - - /** Gets a textual representation of this node. */ - string toString() { - result = this.asElement().toString() - or - result = this.asNode().toString() - or - result = this.asCall().toString() - } - - /** Gets the location of this node. */ - Location getLocation() { - result = this.asElement().getLocation() - or - result = this.asNode().getLocation() - or - result = this.asCall().getLocation() - } -} - -/** Provides additional sink specification logic required for attributes. */ -predicate interpretOutputSpecific(string c, InterpretNode mid, InterpretNode node) { - exists(Node n | n = node.asNode() | - (c = "Parameter" or c = "") and - n.asParameter() = mid.asElement() - or - c = "" and - n.asExpr().(AssignableRead).getTarget().getUnboundDeclaration() = mid.asElement() - ) -} - -/** Provides additional sink specification logic required for attributes. */ -predicate interpretInputSpecific(string c, InterpretNode mid, InterpretNode n) { - c = "" and - exists(Assignable a | - n.asNode().asExpr() = a.getAnAssignedValue() and - a.getUnboundDeclaration() = mid.asElement() - ) -} - -/** Gets the argument position obtained by parsing `X` in `Parameter[X]`. */ -bindingset[s] -ArgumentPosition parseParamBody(string s) { - result.getPosition() = AccessPath::parseInt(s) - or - s = "this" and - result.isQualifier() - or - s = "delegate-self" and - result.isDelegateSelf() -} - -/** Gets the parameter position obtained by parsing `X` in `Argument[X]`. */ -bindingset[s] -ParameterPosition parseArgBody(string s) { - result.getPosition() = AccessPath::parseInt(s) - or - s = "this" and - result.isThisParameter() - or - s = "delegate-self" and - result.isDelegateSelf() -} diff --git a/csharp/ql/lib/semmle/code/csharp/frameworks/EntityFramework.qll b/csharp/ql/lib/semmle/code/csharp/frameworks/EntityFramework.qll index 362f73b36129c..28cab54928b61 100644 --- a/csharp/ql/lib/semmle/code/csharp/frameworks/EntityFramework.qll +++ b/csharp/ql/lib/semmle/code/csharp/frameworks/EntityFramework.qll @@ -9,7 +9,8 @@ private import semmle.code.csharp.frameworks.System private import semmle.code.csharp.frameworks.system.data.Entity private import semmle.code.csharp.frameworks.system.collections.Generic private import semmle.code.csharp.frameworks.Sql -private import semmle.code.csharp.dataflow.FlowSummary +private import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Public +private import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Private private import semmle.code.csharp.dataflow.internal.DataFlowPrivate as DataFlowPrivate /** @@ -85,9 +86,23 @@ module EntityFramework { } /** A flow summary for EntityFramework. */ - abstract class EFSummarizedCallable extends SummarizedCallable { + abstract class EFSummarizedCallable extends SummarizedCallableImpl { bindingset[this] EFSummarizedCallable() { any() } + + override predicate hasProvenance(Provenance provenance) { provenance = "manual" } + } + + // see `SummarizedCallableImpl` qldoc + private class EFSummarizedCallableAdapter extends SummarizedCallable instanceof EFSummarizedCallable + { + override predicate propagatesFlow(string input, string output, boolean preservesValue) { + none() + } + + override predicate hasProvenance(Provenance provenance) { + EFSummarizedCallable.super.hasProvenance(provenance) + } } /** The class ``Microsoft.EntityFrameworkCore.DbQuery`1`` or ``System.Data.Entity.DbQuery`1``. */ diff --git a/csharp/ql/test/library-tests/dataflow/external-models/validatemodels.ql b/csharp/ql/test/library-tests/dataflow/external-models/validatemodels.ql index c538d5c29e180..507bc89a7f23b 100644 --- a/csharp/ql/test/library-tests/dataflow/external-models/validatemodels.ql +++ b/csharp/ql/test/library-tests/dataflow/external-models/validatemodels.ql @@ -1,6 +1,6 @@ import csharp import semmle.code.csharp.dataflow.internal.ExternalFlow -import semmle.code.csharp.dataflow.internal.AccessPathSyntax +import codeql.dataflow.internal.AccessPathSyntax import ModelValidation private predicate getRelevantAccessPath(string path) { @@ -10,6 +10,4 @@ private predicate getRelevantAccessPath(string path) { sourceModel(_, _, _, _, _, _, path, _, _) } -private class AccessPathsExternal extends AccessPath::Range { - AccessPathsExternal() { getRelevantAccessPath(this) } -} +import AccessPath diff --git a/csharp/ql/test/library-tests/dataflow/library/FlowSummaries.ql b/csharp/ql/test/library-tests/dataflow/library/FlowSummaries.ql index 82889478a1ff8..d39dd556fcd14 100644 --- a/csharp/ql/test/library-tests/dataflow/library/FlowSummaries.ql +++ b/csharp/ql/test/library-tests/dataflow/library/FlowSummaries.ql @@ -1,13 +1,31 @@ -private import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl -private import semmle.code.csharp.dataflow.internal.ExternalFlow import shared.FlowSummaries +private import semmle.code.csharp.dataflow.internal.ExternalFlow +// private class Test extends Callable instanceof SummarizedCallable { +// Test() { this.getFullyQualifiedName() = "Microsoft.AspNetCore.Connections.ConnectionItems.Add" } +// private predicate sdfs(string input, string output, boolean preservesValue) { +// SummarizedCallable.super.propagatesFlow(input, output, preservesValue) +// } +// } private class IncludeAllSummarizedCallable extends IncludeSummarizedCallable { IncludeAllSummarizedCallable() { exists(this) } + // private predicate sdfs( + // string s, SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue + // ) { + // this.getFullyQualifiedName() = "Microsoft.AspNetCore.Connections.ConnectionItems.Add" and + // s = this.getCallableCsv() and + // this.propagatesFlow(input, output, preservesValue) + // } + // private predicate sdfs2(string s) { + // this.getFullyQualifiedName() = "Microsoft.AspNetCore.Connections.ConnectionItems.Add" and + // s = this.getCallableCsv() + // } + // namespace = "Microsoft.AspNetCore.Connections" and + // type = "ConnectionItems" and + // name = "Add" and } -private class IncludeNeutralSummarizedCallable extends RelevantNeutralCallable instanceof FlowSummaryImpl::Public::NeutralSummaryCallable -{ +private class IncludeNeutralSummarizedCallable extends RelevantNeutralCallable { /** Gets a string representing the callable in semi-colon separated format for use in flow summaries. */ final override string getCallableCsv() { result = asPartialNeutralModel(this) } } diff --git a/csharp/ql/test/library-tests/dataflow/library/FlowSummariesFiltered.ql b/csharp/ql/test/library-tests/dataflow/library/FlowSummariesFiltered.ql index fcfe037f1ed21..c3584afcbc330 100644 --- a/csharp/ql/test/library-tests/dataflow/library/FlowSummariesFiltered.ql +++ b/csharp/ql/test/library-tests/dataflow/library/FlowSummariesFiltered.ql @@ -1,8 +1,9 @@ import shared.FlowSummaries private import semmle.code.csharp.dataflow.internal.ExternalFlow -class IncludeFilteredSummarizedCallable extends IncludeSummarizedCallable instanceof SummarizedCallable -{ +class IncludeFilteredSummarizedCallable extends IncludeSummarizedCallable { + IncludeFilteredSummarizedCallable() { exists(this) } + /** * Holds if flow is propagated between `input` and `output` and * if there is no summary for a callable in a `base` class or interface @@ -14,7 +15,7 @@ class IncludeFilteredSummarizedCallable extends IncludeSummarizedCallable instan super.propagatesFlow(input, output, preservesValue) and not exists(IncludeSummarizedCallable rsc | isBaseCallableOrPrototype(rsc) and - rsc.(SummarizedCallable).propagatesFlow(input, output, preservesValue) and + rsc.propagatesFlow(input, output, preservesValue) and this.(UnboundCallable).overridesOrImplementsUnbound(rsc) ) } diff --git a/csharp/ql/test/shared/FlowSummaries.qll b/csharp/ql/test/shared/FlowSummaries.qll index c650052f903fe..d85bbc5abae5d 100644 --- a/csharp/ql/test/shared/FlowSummaries.qll +++ b/csharp/ql/test/shared/FlowSummaries.qll @@ -1,4 +1,6 @@ -import semmle.code.csharp.dataflow.FlowSummary +// import semmle.code.csharp.dataflow.FlowSummary +import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Private +import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Public import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Private::TestOutput private import semmle.code.csharp.dataflow.internal.ExternalFlow diff --git a/shared/dataflow/codeql/dataflow/internal/FlowSummaryImpl.qll b/shared/dataflow/codeql/dataflow/internal/FlowSummaryImpl.qll index 6befc75bcb94a..e73bf51670692 100644 --- a/shared/dataflow/codeql/dataflow/internal/FlowSummaryImpl.qll +++ b/shared/dataflow/codeql/dataflow/internal/FlowSummaryImpl.qll @@ -3,6 +3,7 @@ */ private import codeql.dataflow.DataFlow as DF +private import codeql.util.Location private import DataFlowImpl private import AccessPathSyntax as AccessPathSyntax @@ -74,17 +75,6 @@ signature module InputSig { none() } - /** - * Gets the return kind corresponding to the unknown token `token`. - * - * The token is unknown because it could not be reverse-encoded using the - * encoding predicates above. - */ - bindingset[token] - default Lang::ReturnKind decodeUnknownReturn(AccessPathSyntax::AccessPathTokenBase token) { - none() - } - /** * Gets the without-content corresponding to the unknown token `token`. * @@ -512,7 +502,26 @@ module Make Input> { abstract predicate required(SummaryComponent head, SummaryComponentStack tail); } - /** A callable with a flow summary. */ + /** + * A callable with a flow summary. + * + * This interface is not meant to be used directly, instead use the public + * `SummarizedCallable` interface. However, _if_ you need to use this, make + * sure that that all classes `C` that extend `SummarizedCallableImpl` also + * extend `SummarizedCallable`, using the following adapter pattern: + * + * ```ql + * private class CAdapter extends SummarizedCallable instanceof C { + * override predicate propagatesFlow(string input, string output, boolean preservesValue) { + * none() + * } + * + * override predicate hasProvenance(Provenance provenance) { + * C.super.hasProvenance(provenance) + * } + * } + * ``` + */ abstract class SummarizedCallableImpl extends SummarizedCallableBaseFinal { bindingset[this] SummarizedCallableImpl() { any() } @@ -1260,11 +1269,29 @@ module Make Input> { * summaries into a `SummarizedCallable`s. */ module External { - private SummaryComponent decodeToken(AccessPathToken token) { + private ContentSet decodeContent(AccessPathToken token) { + exists(string name | name = encodeContent(result, token.getAnArgument(name))) + or + token = encodeContent(result, "") + } + + private ContentSet decodeWithoutContent(AccessPathToken token) { + exists(string name | name = encodeWithoutContent(result, token.getAnArgument(name))) + or + token = encodeWithoutContent(result, "") + } + + private ContentSet decodeWithContent(AccessPathToken token) { + exists(string name | name = encodeWithContent(result, token.getAnArgument(name))) or + token = encodeWithContent(result, "") + } + + private SummaryComponent interpretComponent(AccessPathToken token) { exists(ContentSet c | - exists(string name | name = encodeContent(c, token.getAnArgument(name))) + c = decodeContent(token) or - token = encodeContent(c, "") + not exists(decodeContent(token)) and + c = decodeUnknownContent(token) | result = SummaryComponent::content(c) ) @@ -1293,36 +1320,24 @@ module Make Input> { ) or exists(ContentSet c | - exists(string name | name = encodeWithoutContent(c, token.getAnArgument(name))) + c = decodeWithoutContent(token) or - token = encodeWithoutContent(c, "") + not exists(decodeWithoutContent(token)) and + c = decodeUnknownWithoutContent(token) | result = SummaryComponent::withoutContent(c) ) or exists(ContentSet c | - exists(string name | name = encodeWithContent(c, token.getAnArgument(name))) or - token = encodeWithContent(c, "") + c = decodeWithContent(token) + or + not exists(decodeWithContent(token)) and + c = decodeUnknownWithContent(token) | result = SummaryComponent::withContent(c) ) } - private SummaryComponent interpretComponent(AccessPathToken token) { - result = decodeToken(token) - or - not exists(decodeToken(token)) and - ( - result = SummaryComponent::content(decodeUnknownContent(token)) - or - result = SummaryComponent::return(decodeUnknownReturn(token)) - or - result = SummaryComponent::withoutContent(decodeUnknownWithoutContent(token)) - or - result = SummaryComponent::withContent(decodeUnknownWithContent(token)) - ) - } - /** * Holds if `spec` specifies summary component stack `stack`. */ @@ -1415,9 +1430,11 @@ module Make Input> { AccessPathSyntax::parseInt(part.getArgumentList()) < 0 } - signature module SourceSinkInterpretationInputSig { + signature module SourceSinkInterpretationInputSig { class Element { string toString(); + + Location getLocation(); } /** @@ -1438,6 +1455,8 @@ module Make Input> { class InterpretNode { string toString(); + Location getLocation(); + /** Gets the element that this node corresponds to, if any. */ SourceOrSinkElement asElement(); @@ -1473,7 +1492,8 @@ module Make Input> { * Should eventually be replaced with API graphs like in dynamic languages. */ module SourceSinkInterpretation< - SourceSinkInterpretationInputSig SourceSinkInterpretationInput> + LocationSig Location, + SourceSinkInterpretationInputSig SourceSinkInterpretationInput> { private import SourceSinkInterpretationInput @@ -1488,7 +1508,7 @@ module Make Input> { private class SourceSinkAccessPath = AccessPath::AccessPath; - private predicate outputNeedsReferenceExt(AccessPathToken c) { + private predicate outputNeedsReferenceExt(SourceSinkAccessPathToken c) { c.getName() = ["Argument", "ReturnValue"] or outputNeedsReference(c) } @@ -1536,22 +1556,24 @@ module Make Input> { interpretOutput(output, n - 1, ref, mid) and c = output.getToken(n - 1) | - exists(ArgumentPosition apos, ParameterPosition ppos | + exists(ArgumentPosition apos | node.asNode() .(PostUpdateNode) .getPreUpdateNode() .(ArgNode) - .argumentOf(mid.asCall(), apos) and - parameterMatch(ppos, apos) + .argumentOf(mid.asCall(), apos) | - c = "Argument" or parseArg(c, ppos) + c = "Argument" + or + exists(ParameterPosition ppos | parameterMatch(ppos, apos) and parseArg(c, ppos)) ) or - exists(ArgumentPosition apos, ParameterPosition ppos | - node.asNode().(ParamNode).isParameterOf(mid.asCallable(), ppos) and - parameterMatch(ppos, apos) + exists(ParameterPosition ppos | + node.asNode().(ParamNode).isParameterOf(mid.asCallable(), ppos) | - c = "Parameter" or parseParam(c, apos) + c = "Parameter" + or + exists(ArgumentPosition apos | parameterMatch(ppos, apos) and parseParam(c, apos)) ) or c = "ReturnValue" and @@ -1580,11 +1602,10 @@ module Make Input> { interpretInput(input, n - 1, ref, mid) and c = input.getToken(n - 1) | - exists(ArgumentPosition apos, ParameterPosition ppos | - node.asNode().(ArgNode).argumentOf(mid.asCall(), apos) and - parameterMatch(ppos, apos) - | - c = "Argument" or parseArg(c, ppos) + exists(ArgumentPosition apos | node.asNode().(ArgNode).argumentOf(mid.asCall(), apos) | + c = "Argument" + or + exists(ParameterPosition ppos | parameterMatch(ppos, apos) and parseArg(c, ppos)) ) or exists(ReturnNodeExt ret | @@ -1624,8 +1645,10 @@ module Make Input> { /** Provides a query predicate for outputting a set of relevant flow summaries. */ module TestOutput { + final private class SummarizedCallableImplFinal = SummarizedCallableImpl; + /** A flow summary to include in the `summary/1` query predicate. */ - abstract class RelevantSummarizedCallable instanceof SummarizedCallableImpl { + abstract class RelevantSummarizedCallable extends SummarizedCallableImplFinal { /** Gets the string representation of this callable used by `summary/1`. */ abstract string getCallableCsv(); @@ -1635,8 +1658,6 @@ module Make Input> { ) { super.propagatesFlow(input, output, preservesValue) } - - string toString() { result = super.toString() } } /** A model to include in the `neutral/1` query predicate. */ @@ -1660,7 +1681,11 @@ module Make Input> { } private string renderProvenance(SummarizedCallable c) { - if c.applyManualModel() then result = "manual" else c.hasProvenance(result) + if c.applyManualModel() + then result = "manual" + else ( + c.hasProvenance(result) and result != "manual" + ) } private string renderProvenanceNeutral(NeutralCallable c) {