From fcb6c19c2a64929278be85fd63676c14e7912a8d Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Thu, 7 Dec 2023 11:40:35 +0100 Subject: [PATCH] Java: Use `FlowSummaryImpl` from `dataflow` pack --- .../code/java/dataflow/ExternalFlow.qll | 79 +- .../semmle/code/java/dataflow/FlowSummary.qll | 70 +- .../dataflow/internal/AccessPathSyntax.qll | 182 -- .../java/dataflow/internal/DataFlowNodes.qll | 8 +- .../dataflow/internal/DataFlowPrivate.qll | 5 +- .../dataflow/internal/FlowSummaryImpl.qll | 1659 +++-------------- .../internal/FlowSummaryImplSpecific.qll | 354 ---- .../code/java/dispatch/WrappedInvocation.qll | 19 +- .../semmle/code/java/frameworks/Stream.qll | 41 +- .../code/java/frameworks/android/Intent.qll | 26 +- java/ql/src/Metrics/Summaries/TopJdkApis.qll | 2 +- java/ql/src/Telemetry/ExternalApi.qll | 2 +- .../external-models/validatemodels.ql | 15 +- 13 files changed, 400 insertions(+), 2062 deletions(-) delete mode 100644 java/ql/lib/semmle/code/java/dataflow/internal/AccessPathSyntax.qll delete mode 100644 java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImplSpecific.qll diff --git a/java/ql/lib/semmle/code/java/dataflow/ExternalFlow.qll b/java/ql/lib/semmle/code/java/dataflow/ExternalFlow.qll index 48ee41630d9b0..c1f493c345af8 100644 --- a/java/ql/lib/semmle/code/java/dataflow/ExternalFlow.qll +++ b/java/ql/lib/semmle/code/java/dataflow/ExternalFlow.qll @@ -89,12 +89,13 @@ import java private import semmle.code.java.dataflow.DataFlow::DataFlow +private import FlowSummary as FlowSummary private import internal.DataFlowPrivate +private import internal.FlowSummaryImpl +private import internal.FlowSummaryImpl::Public +private import internal.FlowSummaryImpl::Private private import internal.FlowSummaryImpl::Private::External -private import internal.FlowSummaryImplSpecific as FlowSummaryImplSpecific -private import internal.AccessPathSyntax private import internal.ExternalFlowExtensions as Extensions -private import FlowSummary private import codeql.mad.ModelValidation as SharedModelVal /** @@ -234,6 +235,21 @@ predicate modelCoverage(string package, int pkgs, string kind, string part, int /** Provides a query predicate to check the MaD models for validation errors. */ module ModelValidation { + private import codeql.dataflow.internal.AccessPathSyntax as AccessPathSyntax + + private predicate getRelevantAccessPath(string path) { + summaryModel(_, _, _, _, _, _, path, _, _, _) or + summaryModel(_, _, _, _, _, _, _, path, _, _) or + sinkModel(_, _, _, _, _, _, path, _, _) or + sourceModel(_, _, _, _, _, _, path, _, _) + } + + private module MkAccessPath = AccessPathSyntax::AccessPath; + + class AccessPath = MkAccessPath::AccessPath; + + class AccessPathToken = MkAccessPath::AccessPathToken; + private string getInvalidModelInput() { exists(string pred, AccessPath input, AccessPathToken part | sinkModel(_, _, _, _, _, _, input, _, _) and pred = "sink" @@ -478,7 +494,9 @@ private module Cached { */ cached predicate sourceNode(Node node, string kind) { - exists(FlowSummaryImplSpecific::InterpretNode n | isSourceNode(n, kind) and n.asNode() = node) + exists(SourceSinkInterpretationInput::InterpretNode n | + isSourceNode(n, kind) and n.asNode() = node + ) } /** @@ -487,8 +505,59 @@ private module Cached { */ cached predicate sinkNode(Node node, string kind) { - exists(FlowSummaryImplSpecific::InterpretNode n | isSinkNode(n, kind) and n.asNode() = node) + exists(SourceSinkInterpretationInput::InterpretNode n | + isSinkNode(n, kind) and n.asNode() = node + ) } } import Cached + +private predicate relevantSummaryElementManual( + FlowSummary::SummarizedCallableBase c, string input, string output, string kind, + Provenance provenance +) { + summaryElement(c, input, output, kind, provenance) and + provenance.isManual() +} + +private predicate relevantSummaryElementGenerated( + FlowSummary::SummarizedCallableBase c, string input, string output, string kind, + Provenance provenance +) { + summaryElement(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() { neutralElement(this, kind, provenance_) } + + override string getKind() { result = kind } + + override predicate hasProvenance(Provenance provenance) { provenance = provenance_ } +} diff --git a/java/ql/lib/semmle/code/java/dataflow/FlowSummary.qll b/java/ql/lib/semmle/code/java/dataflow/FlowSummary.qll index d3c9fe3b08b56..1dd0c2c11fa8c 100644 --- a/java/ql/lib/semmle/code/java/dataflow/FlowSummary.qll +++ b/java/ql/lib/semmle/code/java/dataflow/FlowSummary.qll @@ -6,63 +6,13 @@ import java private import internal.FlowSummaryImpl as Impl private import internal.DataFlowUtil -class SummaryComponent = Impl::Public::SummaryComponent; +deprecated class SummaryComponent = Impl::Private::SummaryComponent; -/** Provides predicates for constructing summary components. */ -module SummaryComponent { - import Impl::Public::SummaryComponent +deprecated module SummaryComponent = Impl::Private::SummaryComponent; - /** Gets a summary component that represents a qualifier. */ - SummaryComponent qualifier() { result = argument(-1) } +deprecated class SummaryComponentStack = Impl::Private::SummaryComponentStack; - /** Gets a summary component for field `f`. */ - SummaryComponent field(Field f) { result = content(any(FieldContent c | c.getField() = f)) } - - /** Gets a summary component for `Element`. */ - SummaryComponent element() { result = content(any(CollectionContent c)) } - - /** Gets a summary component for `ArrayElement`. */ - SummaryComponent arrayElement() { result = content(any(ArrayContent c)) } - - /** Gets a summary component for `MapValue`. */ - SummaryComponent mapValue() { result = content(any(MapValueContent c)) } - - /** Gets a summary component that represents the return value of a call. */ - SummaryComponent return() { result = return(_) } -} - -class SummaryComponentStack = Impl::Public::SummaryComponentStack; - -/** Provides predicates for constructing stacks of summary components. */ -module SummaryComponentStack { - import Impl::Public::SummaryComponentStack - - /** Gets a singleton stack representing a qualifier. */ - SummaryComponentStack qualifier() { result = singleton(SummaryComponent::qualifier()) } - - /** Gets a stack representing a field `f` of `object`. */ - SummaryComponentStack fieldOf(Field f, SummaryComponentStack object) { - result = push(SummaryComponent::field(f), object) - } - - /** Gets a stack representing `Element` of `object`. */ - SummaryComponentStack elementOf(SummaryComponentStack object) { - result = push(SummaryComponent::element(), object) - } - - /** Gets a stack representing `ArrayElement` of `object`. */ - SummaryComponentStack arrayElementOf(SummaryComponentStack object) { - result = push(SummaryComponent::arrayElement(), object) - } - - /** Gets a stack representing `MapValue` of `object`. */ - SummaryComponentStack mapValueOf(SummaryComponentStack object) { - result = push(SummaryComponent::mapValue(), object) - } - - /** Gets a singleton stack representing a (normal) return. */ - SummaryComponentStack return() { result = singleton(SummaryComponent::return()) } -} +deprecated module SummaryComponentStack = Impl::Private::SummaryComponentStack; /** A synthetic callable with a set of concrete call sites and a flow summary. */ abstract class SyntheticCallable extends string { @@ -77,11 +27,7 @@ abstract class SyntheticCallable extends string { * * See `SummarizedCallable::propagatesFlow` for details. */ - predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - none() - } + abstract predicate propagatesFlow(string input, string output, boolean preservesValue); /** * Gets the type of the parameter at the specified position with -1 indicating @@ -180,11 +126,9 @@ class SummarizedCallable = Impl::Public::SummarizedCallable; * to `SummarizedCallable`. */ private class SummarizedSyntheticCallableAdapter extends SummarizedCallable, TSyntheticCallable { - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { + override predicate propagatesFlow(string input, string output, boolean preservesValue) { this.asSyntheticCallable().propagatesFlow(input, output, preservesValue) } } -class RequiredSummaryComponentStack = Impl::Public::RequiredSummaryComponentStack; +deprecated class RequiredSummaryComponentStack = Impl::Private::RequiredSummaryComponentStack; diff --git a/java/ql/lib/semmle/code/java/dataflow/internal/AccessPathSyntax.qll b/java/ql/lib/semmle/code/java/dataflow/internal/AccessPathSyntax.qll deleted file mode 100644 index 0c3dc8427b2b1..0000000000000 --- a/java/ql/lib/semmle/code/java/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/java/ql/lib/semmle/code/java/dataflow/internal/DataFlowNodes.qll b/java/ql/lib/semmle/code/java/dataflow/internal/DataFlowNodes.qll index 415929f4f72c1..8bcbf0635a35a 100644 --- a/java/ql/lib/semmle/code/java/dataflow/internal/DataFlowNodes.qll +++ b/java/ql/lib/semmle/code/java/dataflow/internal/DataFlowNodes.qll @@ -491,16 +491,16 @@ module Private { override string toString() { result = this.getSummaryNode().toString() } /** Holds if this summary node is the `i`th argument of `call`. */ - predicate isArgumentOf(DataFlowCall call, int i) { - FlowSummaryImpl::Private::summaryArgumentNode(call, this.getSummaryNode(), i) + predicate isArgumentOf(SummaryCall call, int i) { + FlowSummaryImpl::Private::summaryArgumentNode(call.getReceiver(), this.getSummaryNode(), i) } /** Holds if this summary node is a return node. */ predicate isReturn() { FlowSummaryImpl::Private::summaryReturnNode(this.getSummaryNode(), _) } /** Holds if this summary node is an out node for `call`. */ - predicate isOut(DataFlowCall call) { - FlowSummaryImpl::Private::summaryOutNode(call, this.getSummaryNode(), _) + predicate isOut(SummaryCall call) { + FlowSummaryImpl::Private::summaryOutNode(call.getReceiver(), this.getSummaryNode(), _) } } diff --git a/java/ql/lib/semmle/code/java/dataflow/internal/DataFlowPrivate.qll b/java/ql/lib/semmle/code/java/dataflow/internal/DataFlowPrivate.qll index f5466b2d739b5..2442671ac523f 100644 --- a/java/ql/lib/semmle/code/java/dataflow/internal/DataFlowPrivate.qll +++ b/java/ql/lib/semmle/code/java/dataflow/internal/DataFlowPrivate.qll @@ -578,7 +578,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) + ) or CaptureFlow::heuristicAllowInstanceParameterReturnInSelf(p.(InstanceParameterNode).getCallable()) } diff --git a/java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImpl.qll b/java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImpl.qll index 0aa17c521b43f..d5364567d88dd 100644 --- a/java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImpl.qll +++ b/java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImpl.qll @@ -1,1491 +1,408 @@ /** * 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 java +private import codeql.dataflow.internal.FlowSummaryImpl +private import codeql.dataflow.internal.AccessPathSyntax as AccessPath +private import DataFlowDispatch +private import DataFlowPrivate +private import DataFlowUtil +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, _) - } - - /** Gets the tail of this stack, if any. */ - SummaryComponentStack tail() { this = TConsSummaryComponentStack(_, result) } - - /** 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) - } - - /** Holds if this stack contains summary component `c`. */ - predicate contains(SummaryComponent c) { c = this.drop(_).head() } - - /** 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 } - } -} +private import semmle.code.java.dataflow.ExternalFlow +private import semmle.code.java.dataflow.FlowSummary as FlowSummary /** - * Provides predicates for compiling flow summaries down to atomic local steps, - * read steps, and store steps. + * A module for importing frameworks that define synthetic globals. */ -module Private { - private import Public - import AccessPathSyntax +private module SyntheticGlobals { + private import semmle.code.java.frameworks.android.Intent +} - 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) +bindingset[pos] +private string positionToString(int pos) { + if pos = -1 then result = "this" else result = pos.toString() +} - private TParameterSummaryComponent callbackSelfParam() { - result = TParameterSummaryComponent(callbackSelfParameterPosition()) - } +module Input implements InputSig { + class SummarizedCallableBase = FlowSummary::SummarizedCallableBase; - 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, _) - } + ArgumentPosition callbackSelfParameterPosition() { result = -1 } - pragma[nomagic] - private predicate summary( - SummarizedCallable c, SummaryComponentStack input, SummaryComponentStack output, - boolean preservesValue - ) { - c.propagatesFlow(input, output, preservesValue) - or - // observe side effects of callbacks on input arguments - c.propagatesFlow(output, input, preservesValue) and - preservesValue = true and - isCallbackParameter(input) and - isContentOfArgument(output, _) - 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) - ) - } + ReturnKind getStandardReturnValueKind() { any() } - /** - * 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(_) - ) - } + string encodeParameterPosition(ParameterPosition pos) { result = positionToString(pos) } - 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 - 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) - } + string encodeArgumentPosition(ArgumentPosition pos) { result = positionToString(pos) } - private predicate outputState(SummarizedCallable c, SummaryComponentStack s) { - summary(c, _, s, _) - or - exists(SummaryComponentStack out | - outputState(c, out) and - out.head() = TContentSummaryComponent(_) and - s = out.tail() + string encodeContent(ContentSet c, string arg) { + exists(Field f, string package, string className, string fieldName | + f = c.(FieldContent).getField() and + f.hasQualifiedName(package, className, fieldName) and + result = "Field" and + arg = package + "." + className + "." + fieldName ) or - // Add the argument node corresponding to the requested post-update node - inputState(c, s) and isCallbackParameter(s) - } - - private predicate inputState(SummarizedCallable c, SummaryComponentStack s) { - summary(c, s, _, _) + exists(SyntheticField f | + f = c.(SyntheticFieldContent).getField() and result = "SyntheticField" and arg = f + ) or - exists(SummaryComponentStack inp | inputState(c, inp) and s = inp.tail()) + c instanceof ArrayContent and result = "ArrayElement" and arg = "" or - exists(SummaryComponentStack out | - outputState(c, out) and - out.head() = TParameterSummaryComponent(_) and - s = out.tail() - ) + c instanceof CollectionContent and result = "Element" and arg = "" or - // Add the post-update node corresponding to the requested argument node - outputState(c, s) and isCallbackParameter(s) + c instanceof MapKeyContent and result = "MapKey" and arg = "" or - // Add the parameter node for parameter side-effects - outputState(c, s) and s = SummaryComponentStack::argument(_) + c instanceof MapValueContent and result = "MapValue" and arg = "" } - 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 - ) - } + string encodeWithoutContent(ContentSet c, string arg) { + result = "WithoutElement" and + c instanceof CollectionContent and + arg = "" } - 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 encodeWithContent(ContentSet c, string arg) { + result = "WithElement" and + c instanceof CollectionContent and + arg = "" } - 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 } + bindingset[token] + ParameterPosition decodeUnknownParameterPosition(AccessPath::AccessPathTokenBase token) { + // needed to support `Argument[x..y]` ranges and `Argument[-1]` + token.getName() = "Argument" and + result = AccessPath::parseInt(token.getAnArgument()) } - 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 } + bindingset[token] + ArgumentPosition decodeUnknownArgumentPosition(AccessPath::AccessPathTokenBase token) { + // needed to support `Parameter[x..y]` ranges and `Parameter[-1]` + token.getName() = "Parameter" and + result = AccessPath::parseInt(token.getAnArgument()) } +} - /** - * 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 instanceof TypeObject } - pragma[noinline] - private SummaryNode summaryNodeInputState(SummarizedCallable c, SummaryComponentStack s) { - exists(SummaryNodeState state | state.isInputState(c, s) | - result = TSummaryInternalNode(c, state) - or - exists(ParameterPosition pos | - parameterReadState(c, state, pos) and - result = TSummaryParameterNode(c, pos) - ) - ) - } + DataFlowType getContentType(ContentSet c) { result = c.(Content).getType() } - pragma[noinline] - private SummaryNode summaryNodeOutputState(SummarizedCallable c, SummaryComponentStack s) { - exists(SummaryNodeState state | - state.isOutputState(c, s) and - result = TSummaryInternalNode(c, state) - ) + DataFlowType getParameterType(Impl::Public::SummarizedCallable c, ParameterPosition pos) { + result = getErasedRepr(c.getParameterType(pos)) } - /** - * 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)) + DataFlowType getReturnType(Impl::Public::SummarizedCallable c, ReturnKind rk) { + result = getErasedRepr(c.getReturnType()) and + exists(rk) } - /** Holds if a parameter node at position `pos` is required for `c`. */ - private predicate summaryParameterNodeRange(SummarizedCallable c, ParameterPosition pos) { - parameterReadState(c, _, pos) + DataFlowType getCallbackParameterType(DataFlowType t, ArgumentPosition pos) { + result = getErasedRepr(t.(FunctionalInterface).getRunMethod().getParameterType(pos)) or - // Same as `isParameterPostUpdate(_, c, pos)`, but can be used in a negative context - any(SummaryNodeState state).isOutputState(c, SummaryComponentStack::argument(pos)) + result = getErasedRepr(t.(FunctionalInterface)) and pos = -1 } - 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()) + DataFlowType getCallbackReturnType(DataFlowType t, ReturnKind rk) { + result = getErasedRepr(t.(FunctionalInterface).getRunMethod().getReturnType()) and + exists(rk) } +} - 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()) +private module StepsInput implements Impl::Private::StepsInputSig { + DataFlowCall getACall(Public::SummarizedCallable sc) { + sc = viableCallable(result).asSummarizedCallable() } +} - /** Holds if a call targeting `receiver` should be synthesized inside `c`. */ - predicate summaryCallbackRange(SummarizedCallable c, SummaryNode receiver) { - callbackOutput(c, _, receiver, _) - or - callbackInput(c, _, receiver, _) - } +private predicate relatedArgSpec(Callable c, string spec) { + exists( + string namespace, string type, boolean subtypes, string name, string signature, string ext + | + summaryModel(namespace, type, subtypes, name, signature, ext, spec, _, _, _) or + summaryModel(namespace, type, subtypes, name, signature, ext, _, spec, _, _) or + sourceModel(namespace, type, subtypes, name, signature, ext, spec, _, _) or + sinkModel(namespace, type, subtypes, name, signature, ext, spec, _, _) + | + c = interpretElement(namespace, type, subtypes, name, signature, ext) + ) +} - /** - * 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) - ) +/** + * Holds if `defaultsCallable` is a Kotlin default-parameter proxy for `originalCallable`, and + * `originalCallable` has a model, and `defaultsArgSpec` is `originalArgSpec` adjusted to account + * for the additional dispatch receiver parameter that occurs in the default-parameter proxy's argument + * list. When no adjustment is required (e.g. for constructors, or non-argument-based specs), `defaultArgsSpec` + * equals `originalArgSpec`. + * + * Note in the case where `originalArgSpec` uses an integer range, like `Argument[1..3]...`, this will produce multiple + * results for `defaultsArgSpec`, like `{Argument[2]..., Argument[3]..., Argument[4]...}`. + */ +private predicate correspondingKotlinParameterDefaultsArgSpec( + Callable originalCallable, Callable defaultsCallable, string originalArgSpec, + string defaultsArgSpec +) { + relatedArgSpec(originalCallable, originalArgSpec) and + defaultsCallable = originalCallable.getKotlinParameterDefaultsProxy() and + ( + originalCallable instanceof Constructor and originalArgSpec = defaultsArgSpec or - exists(SummarizedCallable c, SummaryComponentStack s, SummaryComponent head | head = s.head() | - n = summaryNodeInputState(c, s) 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) - ) - or - exists(ParameterPosition pos | - head = TArgumentSummaryComponent(pos) and - result = getParameterType(c, pos) - ) - ) - or - n = summaryNodeOutputState(c, s) and + originalCallable instanceof Method and + exists(string regex | + // Note I use a regex and not AccessPathToken because this feeds summaryElement et al, + // which would introduce mutual recursion with the definition of AccessPathToken. + regex = "Argument\\[([0-9,\\. ]+)\\](.*)" and ( - exists(ContentSet cont | - head = TContentSummaryComponent(cont) and result = getContentType(cont) + exists(string oldArgNumber, string rest, int paramOffset | + oldArgNumber = originalArgSpec.regexpCapture(regex, 1) and + rest = originalArgSpec.regexpCapture(regex, 2) and + paramOffset = + defaultsCallable.getNumberOfParameters() - + (originalCallable.getNumberOfParameters() + 2) and + exists(int oldArgParsed | + oldArgParsed = AccessPath::parseInt(oldArgNumber.splitAt(",").trim()) + | + if + ktExtensionFunctions(originalCallable, _, _) and + ktExtensionFunctions(defaultsCallable, _, _) and + oldArgParsed = 0 + then defaultsArgSpec = "Argument[" + paramOffset + "]" // 1 if dispatch receiver is present, 0 otherwise. + else defaultsArgSpec = "Argument[" + (oldArgParsed + paramOffset) + "]" + rest + ) ) 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) - ) + not originalArgSpec.regexpMatch(regex) and + defaultsArgSpec = originalArgSpec ) ) - } - - /** 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) - ) - } - - /** 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) - ) - } + ) +} - /** 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) - ) - } +module SourceSinkInterpretationInput implements + Impl::Private::External::SourceSinkInterpretationInputSig +{ + private import java as J - /** 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 = J::Element; - /** - * 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 sourceElement(Element e, string output, string kind) { + exists( + string namespace, string type, boolean subtypes, string name, string signature, string ext, + SourceOrSinkElement baseSource, string originalOutput | - 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)) + sourceModel(namespace, type, subtypes, name, signature, ext, originalOutput, kind, _) and + baseSource = interpretElement(namespace, type, subtypes, name, signature, ext) and + ( + e = baseSource and output = originalOutput + or + correspondingKotlinParameterDefaultsArgSpec(baseSource, e, originalOutput, output) ) ) } - /** 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 + predicate sinkElement(Element e, string input, string kind) { + exists( + string namespace, string type, boolean subtypes, string name, string signature, string ext, + SourceOrSinkElement baseSink, string originalInput + | + sinkModel(namespace, type, subtypes, name, signature, ext, originalInput, kind, _) and + baseSink = interpretElement(namespace, type, subtypes, name, signature, ext) and + ( + e = baseSink and originalInput = input or - preservesValue = false and not summary(c, inputContents, outputContents, true) + correspondingKotlinParameterDefaultsArgSpec(baseSink, e, originalInput, input) ) - 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 - ) - } + ) + } - /** - * 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() - ) - } + class SourceOrSinkElement = Element; - /** - * 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(SourceOrSinkElement n) or + TNode(Node n) - /** - * 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() { result.asCall() = this.asElement() } - 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.asCallable() = 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().asCall().getCallee().getSourceDeclaration() } - /** - * 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 - ) + result = this.asNode().toString() } - /** - * 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)) - ) - } - - 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 - ) - } - - 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) - ) - } - - /** - * 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)) - ) - } - - /** - * 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) - ) - } - - /** - * 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, _) - ) - } - - /** - * 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) - ) + result = this.asNode().getLocation() } } - /** - * 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, _, _) - } - - private class AccessPathRange extends AccessPath::Range { - AccessPathRange() { relevantSpec(this) } - } - - /** Holds if specification component `token` parses as parameter `pos`. */ - predicate parseParam(AccessPathToken token, ArgumentPosition pos) { - token.getName() = "Parameter" and - pos = parseParamBody(token.getAnArgument()) - } - - /** Holds if specification component `token` parses as argument `pos`. */ - predicate parseArg(AccessPathToken token, ParameterPosition pos) { - token.getName() = "Argument" and - pos = parseArgBody(token.getAnArgument()) - } - - /** Holds if specification component `token` parses as synthetic global `sg`. */ - predicate parseSynthGlobal(AccessPathToken token, string sg) { - token.getName() = "SyntheticGlobal" and - sg = token.getAnArgument() - } - - private class SyntheticGlobalFromAccessPath extends SummaryComponent::SyntheticGlobal { - SyntheticGlobalFromAccessPath() { parseSynthGlobal(_, this) } - } - - private SummaryComponent interpretComponent(AccessPathToken token) { - 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) - ) - or - result = interpretComponentSpecific(token) - } - - /** - * Holds if `spec` specifies summary component stack `stack`. - */ - predicate interpretSpec(AccessPath spec, SummaryComponentStack stack) { - interpretSpec(spec, spec.getNumToken(), stack) - } - - /** 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))) + /** Provides additional sink specification logic required for annotations. */ + bindingset[c] + predicate interpretOutput(string c, InterpretNode mid, InterpretNode node) { + exists(Node n, Top ast | + n = node.asNode() and + ast = mid.asElement() + | + (c = "Parameter" or c = "") and + node.asNode().asParameter() = mid.asElement() or - exists(SummaryComponent head, SummaryComponentStack tail | - interpretSpec(spec, n, head, tail) and - stack = SummaryComponentStack::push(head, tail) - ) - } - - /** 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)) - } - - private class MkStack extends RequiredSummaryComponentStack { - override predicate required(SummaryComponent head, SummaryComponentStack tail) { - interpretSpec(_, _, head, tail) - } - } - - 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) - } + c = "" and + n.asExpr().(FieldRead).getField() = ast + ) + } - 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 - ) - } + /** Provides additional source specification logic required for annotations. */ + bindingset[c] + predicate interpretInput(string c, InterpretNode mid, InterpretNode n) { + exists(FieldWrite fw | + c = "" and + fw.getField() = mid.asElement() and + n.asNode().asExpr() = fw.getASource() + ) + } +} - override predicate hasProvenance(Provenance provenance) { - summaryElement(this, _, _, _, provenance) - } - } +module Private { + import Impl::Private + import Impl::Private::Types - /** Holds if component `c` of specification `spec` cannot be parsed. */ - predicate invalidSpecComponent(AccessPath spec, string c) { - c = spec.getToken(_) and - not exists(interpretComponent(c)) - } + module Steps = Impl::Private::Steps; - /** Holds if `provenance` is not a valid provenance value. */ - bindingset[provenance] - predicate invalidProvenance(string provenance) { not provenance instanceof Provenance } + module External { + import Impl::Private::External + import Impl::Private::External::SourceSinkInterpretation /** - * Holds if token `part` of specification `spec` has an invalid index. - * E.g., `Argument[-1]`. + * Holds if an external flow summary exists for `c` with input specification + * `input`, output specification `output`, kind `kind`, and provenance `provenance`. */ - predicate invalidIndexComponent(AccessPath spec, AccessPathToken part) { - part = spec.getToken(_) and - part.getName() = ["Parameter", "Argument"] and - AccessPath::parseInt(part.getArgumentList()) < 0 - } - - private predicate inputNeedsReference(AccessPathToken c) { - c.getName() = "Argument" or - inputNeedsReferenceSpecific(c) - } - - private predicate outputNeedsReference(AccessPathToken c) { - c.getName() = ["Argument", "ReturnValue"] or - outputNeedsReferenceSpecific(c) - } - - 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() - ) - } - - 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() - ) - } - - /** Holds if the first `n` tokens of `output` resolve to the given interpretation. */ - private predicate interpretOutput( - AccessPath output, int n, InterpretNode ref, InterpretNode node + predicate summaryElement( + Input::SummarizedCallableBase c, string input, string output, string kind, string provenance ) { - 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) - ) - } - - /** 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( + string namespace, string type, boolean subtypes, string name, string signature, string ext, + string originalInput, string originalOutput, Callable baseCallable | - 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) + summaryModel(namespace, type, subtypes, name, signature, ext, originalInput, originalOutput, + kind, provenance) and + baseCallable = interpretElement(namespace, type, subtypes, name, signature, ext) and + ( + c.asCallable() = baseCallable and input = originalInput and output = originalOutput + or + correspondingKotlinParameterDefaultsArgSpec(baseCallable, c.asCallable(), originalInput, + input) and + correspondingKotlinParameterDefaultsArgSpec(baseCallable, c.asCallable(), originalOutput, + output) ) - or - interpretInputSpecific(c, mid, node) - ) - } - - /** - * 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) ) } /** - * Holds if `node` is specified as a sink with the given kind in a MaD flow - * model. + * Holds if a neutral model exists for `c` of kind `kind` + * and with provenance `provenance`. */ - predicate isSinkNode(InterpretNode node, string kind) { - exists(InterpretNode ref, AccessPath input | - sinkElementRef(ref, input, kind) and - interpretInput(input, input.getNumToken(), ref, node) + predicate neutralElement(Input::SummarizedCallableBase c, string kind, string provenance) { + exists(string namespace, string type, string name, string signature | + neutralModel(namespace, type, name, signature, kind, provenance) and + c.asCallable() = interpretElement(namespace, type, false, name, signature, "") ) } } - /** 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(); + /** Provides predicates for constructing summary components. */ + module SummaryComponent { + import Impl::Private::SummaryComponent - /** Holds if flow is propagated between `input` and `output`. */ - predicate relevantSummary( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - super.propagatesFlow(input, output, preservesValue) - } + /** Gets a summary component that represents a qualifier. */ + SummaryComponent qualifier() { result = argument(-1) } - string toString() { result = super.toString() } - } + /** Gets a summary component for field `f`. */ + SummaryComponent field(Field f) { result = content(any(FieldContent c | c.getField() = f)) } - /** 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(); + /** Gets a summary component for `Element`. */ + SummaryComponent element() { result = content(any(CollectionContent c)) } - /** - * Gets the kind of the neutral. - */ - string getKind() { result = super.getKind() } + /** Gets a summary component for `ArrayElement`. */ + SummaryComponent arrayElement() { result = content(any(ArrayContent c)) } - string toString() { result = super.toString() } - } + /** Gets a summary component for `MapValue`. */ + SummaryComponent mapValue() { result = content(any(MapValueContent c)) } - /** 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" - } + /** Gets a summary component that represents the return value of a call. */ + SummaryComponent return() { result = return(_) } - private string renderProvenance(SummarizedCallable c) { - if c.applyManualModel() then result = "manual" else c.hasProvenance(result) - } + class SyntheticGlobal = Impl::Private::SyntheticGlobal; + } - private string renderProvenanceNeutral(NeutralCallable c) { - if c.hasManualModel() then result = "manual" else c.hasProvenance(result) - } + module SummaryComponentStack { + import Impl::Private::SummaryComponentStack - /** - * 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 - ) - } + /** Gets a singleton stack representing a qualifier. */ + SummaryComponentStack qualifier() { result = singleton(SummaryComponent::qualifier()) } - /** - * 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 - ) + /** Gets a stack representing a field `f` of `object`. */ + SummaryComponentStack fieldOf(Field f, SummaryComponentStack object) { + result = push(SummaryComponent::field(f), object) } - } - /** - * 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() } + /** Gets a stack representing `Element` of `object`. */ + SummaryComponentStack elementOf(SummaryComponentStack object) { + result = push(SummaryComponent::element(), object) } - 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 - } + /** Gets a stack representing `ArrayElement` of `object`. */ + SummaryComponentStack arrayElementOf(SummaryComponentStack object) { + result = push(SummaryComponent::arrayElement(), object) } - query predicate nodes(NodeOrCall n, string key, string val) { - key = "semmle.label" and val = n.toString() + /** Gets a stack representing `MapValue` of `object`. */ + SummaryComponentStack mapValueOf(SummaryComponentStack object) { + result = push(SummaryComponent::mapValue(), object) } - 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 + ")" - ) - } + /** Gets a singleton stack representing a (normal) return. */ + SummaryComponentStack return() { result = singleton(SummaryComponent::return()) } + } - query predicate edges(NodeOrCall a, NodeOrCall b, string key, string value) { - key = "semmle.label" and - value = strictconcat(string s | edgesComponent(a, b, s) | s, " / ") - } + /** Gets the argument position obtained by parsing `X` in `Parameter[X]`. */ + bindingset[s] + ArgumentPosition parseParamBody(string s) { + result = AccessPath::parseInt(s) + or + s = "this" and result = -1 + } + + /** Gets the parameter position obtained by parsing `X` in `Argument[X]`. */ + bindingset[s] + ParameterPosition parseArgBody(string s) { + result = AccessPath::parseInt(s) + or + s = "this" and result = -1 } } + +module Public = Impl::Public; diff --git a/java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImplSpecific.qll b/java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImplSpecific.qll deleted file mode 100644 index 996e791d44217..0000000000000 --- a/java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImplSpecific.qll +++ /dev/null @@ -1,354 +0,0 @@ -/** - * Provides Java specific classes and predicates for defining flow summaries. - */ - -private import java -private import DataFlowDispatch -private import DataFlowPrivate -private import DataFlowUtil -private import FlowSummaryImpl::Private -private import FlowSummaryImpl::Public -private import semmle.code.java.dataflow.ExternalFlow -private import semmle.code.java.dataflow.FlowSummary as FlowSummary -private import semmle.code.java.dataflow.internal.AccessPathSyntax as AccessPathSyntax - -class SummarizedCallableBase = FlowSummary::SummarizedCallableBase; - -/** - * A class of callables that are candidates for neutral modeling. - */ -class NeutralCallableBase extends Callable { - NeutralCallableBase() { this.isSourceDeclaration() } - - /** Gets a call that targets this neutral. */ - Call getACall() { result.getCallee().getSourceDeclaration() = this } -} - -/** - * A module for importing frameworks that define synthetic globals. - */ -private module SyntheticGlobals { - private import semmle.code.java.frameworks.android.Intent -} - -DataFlowCallable inject(SummarizedCallable c) { result.asSummarizedCallable() = c } - -/** Gets the parameter position of the instance parameter. */ -ArgumentPosition callbackSelfParameterPosition() { result = -1 } - -/** Gets the synthesized data-flow call for `receiver`. */ -SummaryCall summaryDataFlowCall(SummaryNode receiver) { result.getReceiver() = receiver } - -/** Gets the type of content `c`. */ -DataFlowType getContentType(Content c) { result = c.getType() } - -/** Gets the type of the parameter at the given position. */ -DataFlowType getParameterType(SummarizedCallable c, ParameterPosition pos) { - result = getErasedRepr(c.getParameterType(pos)) -} - -/** Gets the return type of kind `rk` for callable `c`. */ -DataFlowType getReturnType(SummarizedCallable c, ReturnKind rk) { - result = getErasedRepr(c.getReturnType()) and - exists(rk) -} - -/** - * Gets the type of the `i`th parameter in a synthesized call that targets a - * callback of type `t`. - */ -DataFlowType getCallbackParameterType(DataFlowType t, int i) { - result = getErasedRepr(t.(FunctionalInterface).getRunMethod().getParameterType(i)) - or - result = getErasedRepr(t.(FunctionalInterface)) and i = -1 -} - -/** - * Gets the return type of kind `rk` in a synthesized call that targets a - * callback of type `t`. - */ -DataFlowType getCallbackReturnType(DataFlowType t, ReturnKind rk) { - result = getErasedRepr(t.(FunctionalInterface).getRunMethod().getReturnType()) and - exists(rk) -} - -/** Gets the type of synthetic global `sg`. */ -DataFlowType getSyntheticGlobalType(SummaryComponent::SyntheticGlobal sg) { - exists(sg) and - result instanceof TypeObject -} - -private predicate relatedArgSpec(Callable c, string spec) { - exists( - string namespace, string type, boolean subtypes, string name, string signature, string ext - | - summaryModel(namespace, type, subtypes, name, signature, ext, spec, _, _, _) or - summaryModel(namespace, type, subtypes, name, signature, ext, _, spec, _, _) or - sourceModel(namespace, type, subtypes, name, signature, ext, spec, _, _) or - sinkModel(namespace, type, subtypes, name, signature, ext, spec, _, _) - | - c = interpretElement(namespace, type, subtypes, name, signature, ext) - ) -} - -/** - * Holds if `defaultsCallable` is a Kotlin default-parameter proxy for `originalCallable`, and - * `originalCallable` has a model, and `defaultsArgSpec` is `originalArgSpec` adjusted to account - * for the additional dispatch receiver parameter that occurs in the default-parameter proxy's argument - * list. When no adjustment is required (e.g. for constructors, or non-argument-based specs), `defaultArgsSpec` - * equals `originalArgSpec`. - * - * Note in the case where `originalArgSpec` uses an integer range, like `Argument[1..3]...`, this will produce multiple - * results for `defaultsArgSpec`, like `{Argument[2]..., Argument[3]..., Argument[4]...}`. - */ -private predicate correspondingKotlinParameterDefaultsArgSpec( - Callable originalCallable, Callable defaultsCallable, string originalArgSpec, - string defaultsArgSpec -) { - relatedArgSpec(originalCallable, originalArgSpec) and - defaultsCallable = originalCallable.getKotlinParameterDefaultsProxy() and - ( - originalCallable instanceof Constructor and originalArgSpec = defaultsArgSpec - or - originalCallable instanceof Method and - exists(string regex | - // Note I use a regex and not AccessPathToken because this feeds summaryElement et al, - // which would introduce mutual recursion with the definition of AccessPathToken. - regex = "Argument\\[([0-9,\\. ]+)\\](.*)" and - ( - exists(string oldArgNumber, string rest, int paramOffset | - oldArgNumber = originalArgSpec.regexpCapture(regex, 1) and - rest = originalArgSpec.regexpCapture(regex, 2) and - paramOffset = - defaultsCallable.getNumberOfParameters() - - (originalCallable.getNumberOfParameters() + 2) and - exists(int oldArgParsed | - oldArgParsed = AccessPathSyntax::AccessPath::parseInt(oldArgNumber.splitAt(",").trim()) - | - if - ktExtensionFunctions(originalCallable, _, _) and - ktExtensionFunctions(defaultsCallable, _, _) and - oldArgParsed = 0 - then defaultsArgSpec = "Argument[" + paramOffset + "]" // 1 if dispatch receiver is present, 0 otherwise. - else defaultsArgSpec = "Argument[" + (oldArgParsed + paramOffset) + "]" + rest - ) - ) - or - not originalArgSpec.regexpMatch(regex) and - defaultsArgSpec = originalArgSpec - ) - ) - ) -} - -/** - * Holds if an external flow summary exists for `c` with input specification - * `input`, output specification `output`, kind `kind`, and provenance `provenance`. - */ -predicate summaryElement( - SummarizedCallableBase c, string input, string output, string kind, string provenance -) { - exists( - string namespace, string type, boolean subtypes, string name, string signature, string ext, - string originalInput, string originalOutput, Callable baseCallable - | - summaryModel(namespace, type, subtypes, name, signature, ext, originalInput, originalOutput, - kind, provenance) and - baseCallable = interpretElement(namespace, type, subtypes, name, signature, ext) and - ( - c.asCallable() = baseCallable and input = originalInput and output = originalOutput - or - correspondingKotlinParameterDefaultsArgSpec(baseCallable, c.asCallable(), originalInput, input) and - correspondingKotlinParameterDefaultsArgSpec(baseCallable, c.asCallable(), originalOutput, - output) - ) - ) -} - -/** - * Holds if a neutral model exists for `c` of kind `kind` - * and with provenance `provenance`. - */ -predicate neutralElement(NeutralCallableBase 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, "") - ) -} - -/** Gets the summary component for specification component `c`, if any. */ -bindingset[c] -SummaryComponent interpretComponentSpecific(AccessPathToken c) { - exists(Content content | parseContent(c, content) and result = SummaryComponent::content(content)) - or - c = "WithoutElement" and result = SummaryComponent::withoutContent(any(CollectionContent cc)) - or - c = "WithElement" and result = SummaryComponent::withContent(any(CollectionContent cc)) -} - -/** Gets the summary component for specification component `c`, if any. */ -private string getContentSpecific(Content c) { - exists(Field f, string package, string className, string fieldName | - f = c.(FieldContent).getField() and - f.hasQualifiedName(package, className, fieldName) and - result = "Field[" + package + "." + className + "." + fieldName + "]" - ) - or - exists(SyntheticField f | - f = c.(SyntheticFieldContent).getField() and result = "SyntheticField[" + f + "]" - ) - or - c instanceof ArrayContent and result = "ArrayElement" - or - c instanceof CollectionContent and result = "Element" - or - c instanceof MapKeyContent and result = "MapKey" - or - c instanceof MapValueContent and result = "MapValue" -} - -/** Gets the textual representation of the content 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" -} - -bindingset[pos] -private string positionToString(int pos) { - if pos = -1 then result = "this" else result = pos.toString() -} - -/** Gets the textual representation of a parameter position in the format used for flow summaries. */ -string getParameterPosition(ParameterPosition pos) { result = positionToString(pos) } - -/** Gets the textual representation of an argument position in the format used for flow summaries. */ -string getArgumentPosition(ArgumentPosition pos) { result = positionToString(pos) } - -/** 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 = Top; - -/** - * Holds if an external source specification exists for `e` with output specification - * `output`, kind `kind`, and provenance `provenance`. - */ -predicate sourceElement(SourceOrSinkElement e, string output, string kind, string provenance) { - exists( - string namespace, string type, boolean subtypes, string name, string signature, string ext, - SourceOrSinkElement baseSource, string originalOutput - | - sourceModel(namespace, type, subtypes, name, signature, ext, originalOutput, kind, provenance) and - baseSource = interpretElement(namespace, type, subtypes, name, signature, ext) and - ( - e = baseSource and output = originalOutput - or - correspondingKotlinParameterDefaultsArgSpec(baseSource, e, originalOutput, output) - ) - ) -} - -/** - * Holds if an external sink specification exists for `e` with input specification - * `input`, kind `kind` and provenance `provenance`. - */ -predicate sinkElement(SourceOrSinkElement e, string input, string kind, string provenance) { - exists( - string namespace, string type, boolean subtypes, string name, string signature, string ext, - SourceOrSinkElement baseSink, string originalInput - | - sinkModel(namespace, type, subtypes, name, signature, ext, originalInput, kind, provenance) and - baseSink = interpretElement(namespace, type, subtypes, name, signature, ext) and - ( - e = baseSink and originalInput = input - or - correspondingKotlinParameterDefaultsArgSpec(baseSink, e, originalInput, input) - ) - ) -} - -/** Gets the return kind corresponding to specification `"ReturnValue"`. */ -ReturnKind getReturnValueKind() { any() } - -private newtype TInterpretNode = - TElement(SourceOrSinkElement n) or - TNode(Node n) - -/** 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() { result.asCall() = this.asElement() } - - /** Gets the callable that this node corresponds to, if any. */ - DataFlowCallable asCallable() { result.asCallable() = this.asElement() } - - /** Gets the target of this call, if any. */ - Callable getCallTarget() { result = this.asCall().asCall().getCallee().getSourceDeclaration() } - - /** Gets a textual representation of this node. */ - string toString() { - result = this.asElement().toString() - or - result = this.asNode().toString() - } - - /** Gets the location of this node. */ - Location getLocation() { - result = this.asElement().getLocation() - or - result = this.asNode().getLocation() - } -} - -/** Provides additional sink specification logic required for annotations. */ -pragma[inline] -predicate interpretOutputSpecific(string c, InterpretNode mid, InterpretNode node) { - exists(Node n, Top ast | - n = node.asNode() and - ast = mid.asElement() - | - (c = "Parameter" or c = "") and - node.asNode().asParameter() = mid.asElement() - or - c = "" and - n.asExpr().(FieldRead).getField() = ast - ) -} - -/** Provides additional source specification logic required for annotations. */ -pragma[inline] -predicate interpretInputSpecific(string c, InterpretNode mid, InterpretNode n) { - exists(FieldWrite fw | - c = "" and - fw.getField() = mid.asElement() and - n.asNode().asExpr() = fw.getASource() - ) -} - -/** Gets the argument position obtained by parsing `X` in `Parameter[X]`. */ -bindingset[s] -ArgumentPosition parseParamBody(string s) { - result = AccessPath::parseInt(s) - or - s = "this" and result = -1 -} - -/** Gets the parameter position obtained by parsing `X` in `Argument[X]`. */ -bindingset[s] -ParameterPosition parseArgBody(string s) { - result = AccessPath::parseInt(s) - or - s = "this" and result = -1 -} diff --git a/java/ql/lib/semmle/code/java/dispatch/WrappedInvocation.qll b/java/ql/lib/semmle/code/java/dispatch/WrappedInvocation.qll index f41c79f920652..97e49812ae457 100644 --- a/java/ql/lib/semmle/code/java/dispatch/WrappedInvocation.qll +++ b/java/ql/lib/semmle/code/java/dispatch/WrappedInvocation.qll @@ -60,7 +60,6 @@ Method getRunnerTarget(MethodCall ma) { } import semmle.code.java.dataflow.FlowSummary -import semmle.code.java.dataflow.internal.FlowSummaryImplSpecific as ImplSpecific private predicate mayInvokeCallback(SrcMethod m, int n) { m.getParameterType(n).(RefType).getSourceDeclaration() instanceof FunctionalInterface and @@ -72,23 +71,11 @@ private class SummarizedCallableWithCallback extends SummarizedCallable { SummarizedCallableWithCallback() { mayInvokeCallback(this.asCallable(), pos) } - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - input = SummaryComponentStack::argument(pos) and - output = SummaryComponentStack::push(SummaryComponent::parameter(-1), input) and + override predicate propagatesFlow(string input, string output, boolean preservesValue) { + input = "Argument[" + pos + "]" and + output = "Argument[" + pos + "].Parameter[-1]" 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 = SummaryComponent::parameter(-1) and - tail = SummaryComponentStack::argument(pos) - ) - } -} diff --git a/java/ql/lib/semmle/code/java/frameworks/Stream.qll b/java/ql/lib/semmle/code/java/frameworks/Stream.qll index 96d74d1473a78..a449f8bd99a64 100644 --- a/java/ql/lib/semmle/code/java/frameworks/Stream.qll +++ b/java/ql/lib/semmle/code/java/frameworks/Stream.qll @@ -32,11 +32,9 @@ private class CollectToContainer extends SyntheticCallable { ]) } - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - input = SummaryComponentStack::elementOf(SummaryComponentStack::qualifier()) and - output = SummaryComponentStack::elementOf(SummaryComponentStack::return()) and + override predicate propagatesFlow(string input, string output, boolean preservesValue) { + input = "Argument[this].Element" and + output = "ReturnValue.Element" and preservesValue = true } } @@ -46,11 +44,9 @@ private class CollectToJoining extends SyntheticCallable { override Call getACall() { result.(CollectCall).getArgument(0).(Collector).hasName("joining") } - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - input = SummaryComponentStack::elementOf(SummaryComponentStack::qualifier()) and - output = SummaryComponentStack::return() and + override predicate propagatesFlow(string input, string output, boolean preservesValue) { + input = "Argument[this].Element" and + output = "ReturnValue" and preservesValue = false } @@ -70,28 +66,9 @@ private class CollectToGroupingBy extends SyntheticCallable { ) } - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { - input = SummaryComponentStack::elementOf(SummaryComponentStack::qualifier()) and - output = - SummaryComponentStack::elementOf(SummaryComponentStack::mapValueOf(SummaryComponentStack::return())) and + override predicate propagatesFlow(string input, string output, boolean preservesValue) { + input = "Argument[this].Element" and + output = "ReturnValue.MapValue.Element" and preservesValue = true } } - -private class RequiredComponentStackForCollect extends RequiredSummaryComponentStack { - override predicate required(SummaryComponent head, SummaryComponentStack tail) { - head = SummaryComponent::element() and - tail = SummaryComponentStack::qualifier() - or - head = SummaryComponent::element() and - tail = SummaryComponentStack::return() - or - head = SummaryComponent::element() and - tail = SummaryComponentStack::mapValueOf(SummaryComponentStack::return()) - or - head = SummaryComponent::mapValue() and - tail = SummaryComponentStack::return() - } -} diff --git a/java/ql/lib/semmle/code/java/frameworks/android/Intent.qll b/java/ql/lib/semmle/code/java/frameworks/android/Intent.qll index 1aba2be33c065..58767a2d9bfbe 100644 --- a/java/ql/lib/semmle/code/java/frameworks/android/Intent.qll +++ b/java/ql/lib/semmle/code/java/frameworks/android/Intent.qll @@ -5,6 +5,7 @@ private import semmle.code.java.dataflow.ExternalFlow private import semmle.code.java.dataflow.FlowSteps private import semmle.code.java.dataflow.FlowSummary private import semmle.code.java.dataflow.internal.BaseSSA as BaseSsa +private import semmle.code.java.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl /** The class `android.content.Intent`. */ class TypeIntent extends Class { @@ -332,12 +333,10 @@ private class StartActivitiesSyntheticCallable extends SyntheticCallable { result.targetsComponentType(targetType) } - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { + override predicate propagatesFlow(string input, string output, boolean preservesValue) { exists(ActivityIntentSyntheticGlobal glob | glob.getTargetType() = targetType | - input = SummaryComponentStack::arrayElementOf(SummaryComponentStack::argument(0)) and - output = SummaryComponentStack::singleton(SummaryComponent::syntheticGlobal(glob)) and + input = "Argument[0].ArrayElement" and + output = "SyntheticGlobal[" + glob + "]" and preservesValue = true ) } @@ -358,18 +357,16 @@ private class GetIntentSyntheticCallable extends SyntheticCallable { result.getEnclosingCallable().getDeclaringType() = targetType } - override predicate propagatesFlow( - SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue - ) { + override predicate propagatesFlow(string input, string output, boolean preservesValue) { exists(ActivityIntentSyntheticGlobal glob | glob.getTargetType() = targetType | - input = SummaryComponentStack::singleton(SummaryComponent::syntheticGlobal(glob)) and - output = SummaryComponentStack::return() and + input = "SyntheticGlobal[" + glob + "]" and + output = "ReturnValue" and preservesValue = true ) } } -private class ActivityIntentSyntheticGlobal extends SummaryComponent::SyntheticGlobal { +private class ActivityIntentSyntheticGlobal extends FlowSummaryImpl::Private::SyntheticGlobal { AndroidComponent targetType; ActivityIntentSyntheticGlobal() { @@ -382,13 +379,6 @@ private class ActivityIntentSyntheticGlobal extends SummaryComponent::SyntheticG AndroidComponent getTargetType() { result = targetType } } -private class RequiredComponentStackForStartActivities extends RequiredSummaryComponentStack { - override predicate required(SummaryComponent head, SummaryComponentStack tail) { - head = SummaryComponent::arrayElement() and - tail = SummaryComponentStack::argument(0) - } -} - /** * A value-preserving step from the intent argument of a `sendBroadcast` call to * the intent parameter in the `onReceive` method of the receiver the diff --git a/java/ql/src/Metrics/Summaries/TopJdkApis.qll b/java/ql/src/Metrics/Summaries/TopJdkApis.qll index 1ba2a0aeed0b8..f9a516fc372c5 100644 --- a/java/ql/src/Metrics/Summaries/TopJdkApis.qll +++ b/java/ql/src/Metrics/Summaries/TopJdkApis.qll @@ -304,7 +304,7 @@ class TopJdkApi extends Callable { /** Holds if this API has a manual neutral summary model. */ private predicate hasManualNeutralSummary() { - this.(FlowSummaryImpl::Public::NeutralSummaryCallable).hasManualModel() + this = any(FlowSummaryImpl::Public::NeutralSummaryCallable n | n.hasManualModel()).asCallable() } /** Holds if this API has a manual MaD model. */ diff --git a/java/ql/src/Telemetry/ExternalApi.qll b/java/ql/src/Telemetry/ExternalApi.qll index b6e0de2f842b5..388908a26a9d4 100644 --- a/java/ql/src/Telemetry/ExternalApi.qll +++ b/java/ql/src/Telemetry/ExternalApi.qll @@ -79,7 +79,7 @@ class ExternalApi extends Callable { /** Holds if this API is a known neutral. */ pragma[nomagic] - predicate isNeutral() { this instanceof FlowSummaryImpl::Public::NeutralCallable } + predicate isNeutral() { this = any(FlowSummaryImpl::Public::NeutralCallable n).asCallable() } /** * Holds if this API is supported by existing CodeQL libraries, that is, it is either a diff --git a/java/ql/test/library-tests/dataflow/external-models/validatemodels.ql b/java/ql/test/library-tests/dataflow/external-models/validatemodels.ql index 84312d3d4e4d4..3f0a4ab9c1c7f 100644 --- a/java/ql/test/library-tests/dataflow/external-models/validatemodels.ql +++ b/java/ql/test/library-tests/dataflow/external-models/validatemodels.ql @@ -1,15 +1,2 @@ import java -import semmle.code.java.dataflow.ExternalFlow -import semmle.code.java.dataflow.internal.AccessPathSyntax -import ModelValidation - -private predicate getRelevantAccessPath(string path) { - summaryModel(_, _, _, _, _, _, path, _, _, _) or - summaryModel(_, _, _, _, _, _, _, path, _, _) or - sinkModel(_, _, _, _, _, _, path, _, _) or - sourceModel(_, _, _, _, _, _, path, _, _) -} - -private class AccessPathsExternal extends AccessPath::Range { - AccessPathsExternal() { getRelevantAccessPath(this) } -} +import semmle.code.java.dataflow.ExternalFlow::ModelValidation