diff --git a/unified/ql/lib/codeql/unified/internal/ParameterEx.qll b/unified/ql/lib/codeql/unified/internal/ParameterEx.qll index 075f76938b15..52e7b34fbe95 100644 --- a/unified/ql/lib/codeql/unified/internal/ParameterEx.qll +++ b/unified/ql/lib/codeql/unified/internal/ParameterEx.qll @@ -11,6 +11,7 @@ private newtype TParameterEx = TImplicitReceiverParameter(Callable c) { exists(LocalVariable v | v.isImplicitReceiverParameter(c)) } or + TImplicitCalleeParameter(Callable c) or TImplicitFunctionExprParameter(FunctionExpr fe, int i) { i = [0 .. fe.getNumberOfImplicitParameters() - 1] } or @@ -40,6 +41,13 @@ abstract private class ParameterExImpl extends TParameterEx { */ predicate isImplicitReceiverParameter(Callable c) { this = TImplicitReceiverParameter(c) } + /** + * Holds if this entity represents an implicit callee parameter of `c`. + * + * Only used to model variable capture. + */ + predicate isImplicitCalleeParameter(Callable c) { this = TImplicitCalleeParameter(c) } + /** Holds if this entity represents implicit parameter `i` of `fe`. */ predicate isImplicitFunctionExprParameter(FunctionExpr fe, int i) { this = TImplicitFunctionExprParameter(fe, i) @@ -75,6 +83,8 @@ abstract private class ParameterExImpl extends TParameterEx { this.isImplicitFunctionExprParameter(result, _) or this.isDefaultConstructorParameter(result, _, _) + or + this.isImplicitCalleeParameter(result) } /** Gets the external name of this parameter, if any. */ @@ -86,6 +96,11 @@ abstract private class ParameterExImpl extends TParameterEx { /** Gets the type declaration of this parameter, if any. */ abstract AstNode getType(); + /** + * Gets the control flow node associated with this parameter. + */ + abstract ControlFlowNode getControlFlowNode(); + /** Gets a textual representation of this parameter. */ abstract string toString(); @@ -104,6 +119,13 @@ private class AstParameterEx extends ParameterExImpl, TAstParameter { override AstNode getType() { result = p.getType() } + override ControlFlowNode getControlFlowNode() { + result.injects(p.getPattern()) + or + not exists(p.getPattern()) and + result.injects(p) + } + override string toString() { result = p.toString() } override Location getLocation() { result = p.getLocation() } @@ -120,6 +142,10 @@ private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitRece override AstNode getType() { none() } + override ControlFlowNode getControlFlowNode() { + result.(ControlFlow::EntryNode).getEnclosingCallable() = c + } + override string toString() { exists(LocalVariable v | v.isImplicitReceiverParameter(c) and @@ -130,6 +156,26 @@ private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitRece override Location getLocation() { result = c.getLocation() } } +private class ImplicitCalleeParameter extends ParameterExImpl, TImplicitCalleeParameter { + Callable c; + + ImplicitCalleeParameter() { this = TImplicitCalleeParameter(c) } + + override string getExternalName() { none() } + + override predicate hasDefault() { none() } + + override AstNode getType() { none() } + + override ControlFlowNode getControlFlowNode() { + result.(ControlFlow::EntryNode).getEnclosingCallable() = c + } + + override string toString() { result = "implicit callee parameter" } + + override Location getLocation() { result = c.getLocation() } +} + /** * An implicit parameter of a function expression inferred from usage of * implicit parameters inside its body. @@ -148,6 +194,10 @@ private class ImplicitFunctionExprParameterEx extends ParameterExImpl, override AstNode getType() { none() } + override ControlFlowNode getControlFlowNode() { + result.(ControlFlow::EntryNode).getEnclosingCallable() = fe + } + override string toString() { result = "implicit parameter " + i } override Location getLocation() { result = fe.getLocation() } @@ -167,6 +217,10 @@ private class DefaultConstructorParameterEx extends ParameterExImpl, TDefaultCon override predicate hasDefault() { hasDefault = true } + override ControlFlowNode getControlFlowNode() { + result.(ControlFlow::EntryNode).getEnclosingCallable() = cd + } + override string toString() { result = name + " [" + cd.getName() + " default constructor]" } override Location getLocation() { result = cd.getLocation() } diff --git a/unified/ql/lib/codeql/unified/internal/dataflow/CaptureSsa.qll b/unified/ql/lib/codeql/unified/internal/dataflow/CaptureSsa.qll index 1e0103469370..a9fac87ca3d3 100644 --- a/unified/ql/lib/codeql/unified/internal/dataflow/CaptureSsa.qll +++ b/unified/ql/lib/codeql/unified/internal/dataflow/CaptureSsa.qll @@ -137,9 +137,7 @@ Node getNodeFromCaptureSsaNode(CaptureSsaOutput::ClosureNode n) { exists(CaptureSsaOutput::ThisParameterNode thisParam, Callable callable | n = thisParam and callable = thisParam.getCallable() and - result - .(BuilderNode) - .isImplicitParameter(toDataFlowCallable(callable), any(ParameterPosition p | p.isCallee())) + result.(BuilderNode).asParameterEx().isImplicitCalleeParameter(callable) ) } diff --git a/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowGraph.qll b/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowGraph.qll index 5189d6c0bca0..b4b5b4d3705b 100644 --- a/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowGraph.qll +++ b/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowGraph.qll @@ -19,14 +19,14 @@ predicate step(BuilderNode node1, Step step, BuilderNode node2) { ) or exists(Callable callable | - node1.isReceiverParameter(callable) and + node1.asParameterEx().isImplicitReceiverParameter(callable) and step.value() and node2.isLocalVariableWrite(callable, getImplicitReceiverVariable(callable)) ) or - exists(FunctionExpr fe, LocalVariable var, ParameterPosition pos | - var.isImplicitFunctionExprParameter(fe, pos.asPositional()) and - node1.isImplicitParameter(toDataFlowCallable(fe), pos) and + exists(FunctionExpr fe, LocalVariable var, int pos | + var.isImplicitFunctionExprParameter(fe, pos) and + node1.asParameterEx().isImplicitFunctionExprParameter(fe, pos) and step.value() and node2.isLocalVariableWrite(fe, var) ) @@ -239,7 +239,11 @@ predicate step(BuilderNode node1, Step step, BuilderNode node2) { node2.isResultValue(expr) ) or - none() // Temporarily disable compilation errors from unsatisfiable types + exists(Parameter p | + p = node1.asParameterEx().asParameter() and + node2.isIncomingValue(p.getPattern()) and + if exists(p.getDefault()) then step.value() else step.valueMustFlow() + ) } /** Holds if `node` should be included in the debug view. */ diff --git a/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll b/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll index 9038c34d5b07..286229bf5168 100644 --- a/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll +++ b/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll @@ -2,6 +2,7 @@ private import unified private import AllDataFlow private import AllDataFlow as D private import codeql.dataflow.DataFlow +private import codeql.unified.internal.ParameterEx private import codeql.util.Void private import codeql.util.Unit @@ -74,20 +75,31 @@ module DataFlowInput implements InputSig { DataFlowCallable nodeGetEnclosingCallable(Node node) { result = node.getEnclosingCallableEx() } - private predicate isParameterNodeImpl(Node p, DataFlowCallable c, ParameterPosition pos) { - exists(Parameter param | - p.asExpr() = param.getPattern() and - c.asSourceCallable() = param.getEnclosingCallable() + private predicate isParameterNodeImpl(Node n, DataFlowCallable c, ParameterPosition pos) { + exists(ParameterEx p, Callable callable | + p = n.(BuilderNode).asParameterEx() and + callable = c.asSourceCallable() | - pos.asPositional() = param.getPositionalIndex() + exists(Parameter param | + param = p.asParameter() and + callable = param.getDeclaringCallable() + | + pos.asPositional() = param.getPositionalIndex() + or + pos.asNamed() = param.getExternalName() + ) or - pos.asNamed() = param.getExternalName() + p.isImplicitReceiverParameter(callable) and + pos.isReceiver() + or + p.isImplicitCalleeParameter(callable) and + pos.isCallee() + or + p.isImplicitFunctionExprParameter(callable, pos.asPositional()) ) or - p.(BuilderNode).isImplicitParameter(c, pos) - or - FlowSummaryOutput::summaryParameterNode(getSummaryNode(p), pos) and - c = p.getEnclosingCallableEx() + FlowSummaryOutput::summaryParameterNode(getSummaryNode(n), pos) and + c = n.getEnclosingCallableEx() } class ParameterNode extends Node { @@ -280,7 +292,7 @@ module DataFlowInput implements InputSig { predicate isUnreachableInCall(NodeRegion nr, DataFlowCall call) { none() } // TODO predicate allowParameterReturnInSelf(ParameterNode p) { - p.isReceiverParameter(_) + p.(BuilderNode).asParameterEx().isImplicitReceiverParameter(_) or captureSsaAllowParameterReturnInSelf(p) or diff --git a/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowNode.qll b/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowNode.qll index 8075c80efee8..33ed06169077 100644 --- a/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowNode.qll +++ b/unified/ql/lib/codeql/unified/internal/dataflow/DataFlowNode.qll @@ -2,6 +2,7 @@ private import unified private import AllDataFlow private import codeql.unified.internal.ExprPositions private import codeql.unified.internal.LocalNameBinding +private import codeql.unified.internal.ParameterEx private import codeql.util.Boolean private predicate hasIncomingValueAtCfgNode(Expr expr, ControlFlowNode cfgNode) { @@ -122,15 +123,7 @@ newtype TDataFlowNode = not exists(callable.asSummarizedCallable()) // represented by TSummaryNode } or TCallableNode(DataFlowCallable callable) or - TImplicitParameterNode(DataFlowCallable callable, ParameterPosition pos) { - not exists(callable.asSummarizedCallable()) and // represented by TSummaryNode - ( - pos instanceof ImplicitParameterPosition - or - pos.asPositional() = - [0 .. callable.asSourceCallable().(FunctionExpr).getNumberOfImplicitParameters() - 1] - ) - } or + TParameterExNode(ParameterEx p) { exists(toDataFlowCallable(p.getCallable())) } or TImplicitArgumentNode(DataFlowCall call, ImplicitArgumentPosition pos, Boolean isPost) { not call.isSummaryCall(_, _) // represented by TSummaryNode } or @@ -140,7 +133,7 @@ newtype TDataFlowNode = class TDataFlowNodeStage1 = TValueNode or TStrictlyIncomingValue or TExprPostUpdateNode or TLocalVariableRefNode or - TReturnNode or TCallableNode or TImplicitParameterNode or TImplicitArgumentNode; + TReturnNode or TCallableNode or TParameterExNode or TImplicitArgumentNode; class TDataFlowNodeStage2 = TDataFlowNodeStage1 or TLocalSsaNode; @@ -214,26 +207,14 @@ class BuilderNode extends TDataFlowNodeStage1 { */ predicate isReturnNodeEx(DataFlowCallable callable) { this = TReturnNode(callable) } - /** Holds if this node represents an implicit parameter of `callable`. */ - predicate isImplicitParameter(DataFlowCallable callable, ParameterPosition pos) { - this = TImplicitParameterNode(callable, pos) - } + /** Gets the `ParameterEx` associated with this node, if any. */ + ParameterEx asParameterEx() { this = TParameterExNode(result) } /** Holds if this node represents an implicit argument to `call` (or its post-update). */ predicate isImplicitArgument(DataFlowCall call, ImplicitArgumentPosition pos, boolean isPost) { this = TImplicitArgumentNode(call, pos, isPost) } - /** - * Holds if this represents the receiver passed to the given callable. - * - * Note that for non-methods and closures that capture the receiver from the enclosing method, - * this node still exists but will typically not flow anywhere. - */ - predicate isReceiverParameter(Callable callable) { - this.isImplicitParameter(toDataFlowCallable(callable), any(ParameterPosition p | p.isReceiver())) - } - /** Holds if this node represents the receiver argument passed to `call`. */ predicate isReceiverArgumentEx(DataFlowCall call) { this.isImplicitArgument(call, any(ArgumentPosition p | p.isReceiver()), false) @@ -318,12 +299,12 @@ class BuilderNode extends TDataFlowNodeStage1 { result = "[variable " + kind + "] " + v.toString() ) or + exists(ParameterEx p | + this = TParameterExNode(p) and + result = p.toString() + ) + or exists(DataFlowCallable callable | - exists(ParameterPosition pos | - this.isImplicitParameter(callable, pos) and - result = "[" + pos + " param] " + callable.toString() - ) - or this.isCallableEx(callable) and result = "[callable] " + callable.toString() ) @@ -351,11 +332,13 @@ class BuilderNode extends TDataFlowNodeStage1 { result = repr.getLocation() ) or + exists(ParameterEx p | + this = TParameterExNode(p) and + result = p.getLocation() + ) + or exists(DataFlowCallable callable | - this.isImplicitParameter(callable, _) - or - this.isCallableEx(callable) - | + this.isCallableEx(callable) and result = callable.getLocation() ) or @@ -394,10 +377,9 @@ class BuilderNode extends TDataFlowNodeStage1 { performsVariableAccess(repr, var, kind, cfgNode) ) or + cfgNode = this.asParameterEx().getControlFlowNode() + or exists(DataFlowCallable callable | - this.isImplicitParameter(callable, _) and - cfgNode.(ControlFlow::EntryNode).getEnclosingCallable() = callable.asSourceCallable() - or this.isCallableEx(callable) and cfgNode.injects(callable.asSourceCallable()) ) @@ -504,7 +486,7 @@ class Node extends TDataFlowNode { * this node still exists but will typically not flow anywhere. */ predicate isReceiverParameter(Callable callable) { - this.(BuilderNode).isReceiverParameter(callable) + this.(BuilderNode).asParameterEx().isImplicitReceiverParameter(callable) } /** Holds if this node represents the receiver argument passed to `call`. */ @@ -575,7 +557,7 @@ class Node extends TDataFlowNode { result.asSourceCallable() = node.getEnclosingCallable() ) or - this.(BuilderNode).isImplicitParameter(result, _) + result.asSourceCallable() = this.(BuilderNode).asParameterEx().getCallable() or exists(DataFlowCall call | this.(BuilderNode).isImplicitArgument(call, _, _) and diff --git a/unified/ql/test/library-tests/dataflow/test.expected b/unified/ql/test/library-tests/dataflow/test.expected index 1e16a1c65d65..7da1ffa1b167 100644 --- a/unified/ql/test/library-tests/dataflow/test.expected +++ b/unified/ql/test/library-tests/dataflow/test.expected @@ -108,8 +108,7 @@ edges | calls.swift:49:32:49:32 | y | calls.swift:51:14:51:14 | y | provenance | | | calls.swift:53:12:53:25 | source(...) | calls.swift:49:19:49:19 | x | provenance | | | calls.swift:53:28:53:41 | source(...) | calls.swift:49:32:49:32 | y | provenance | | -| calls.swift:61:19:61:19 | x [Return] [field] | calls.swift:66:12:66:12 | [post] b [field] | provenance | | -| calls.swift:62:9:62:9 | [post] x [field] | calls.swift:61:19:61:19 | x [Return] [field] | provenance | | +| calls.swift:62:9:62:9 | [post] x [field] | calls.swift:66:12:66:12 | [post] b [field] | provenance | | | calls.swift:62:9:62:15 | ... .field | calls.swift:62:9:62:9 | [post] x [field] | provenance | | | calls.swift:62:19:62:32 | source(...) | calls.swift:62:9:62:15 | ... .field | provenance | | | calls.swift:66:12:66:12 | [post] b [field] | calls.swift:67:10:67:10 | b [field] | provenance | | @@ -580,8 +579,7 @@ edges | subscripts.swift:143:26:143:40 | source(...) | subscripts.swift:143:5:143:22 | ... .subscript(...) | provenance | | | subscripts.swift:144:10:144:10 | t [field] | subscripts.swift:144:10:144:21 | ... .getField(...) | provenance | | | subscripts.swift:154:24:154:31 | newValue [field] | subscripts.swift:154:24:154:37 | ... .field | provenance | | -| subscripts.swift:157:19:157:19 | s [Return] [field] | subscripts.swift:159:12:159:16 | [post] & ... [field] | provenance | | -| subscripts.swift:157:33:157:33 | [post] s [field] | subscripts.swift:157:19:157:19 | s [Return] [field] | provenance | | +| subscripts.swift:157:33:157:33 | [post] s [field] | subscripts.swift:159:12:159:16 | [post] & ... [field] | provenance | | | subscripts.swift:157:33:157:39 | ... .field | subscripts.swift:157:33:157:33 | [post] s [field] | provenance | | | subscripts.swift:157:43:157:57 | source(...) | subscripts.swift:157:33:157:39 | ... .field | provenance | | | subscripts.swift:159:12:159:16 | [post] & ... [field] | subscripts.swift:159:13:159:16 | [post] ... .subscript(...) [field] | provenance | | @@ -917,7 +915,6 @@ nodes | calls.swift:51:14:51:14 | y | semmle.label | y | | calls.swift:53:12:53:25 | source(...) | semmle.label | source(...) | | calls.swift:53:28:53:41 | source(...) | semmle.label | source(...) | -| calls.swift:61:19:61:19 | x [Return] [field] | semmle.label | x [Return] [field] | | calls.swift:62:9:62:9 | [post] x [field] | semmle.label | [post] x [field] | | calls.swift:62:9:62:15 | ... .field | semmle.label | ... .field | | calls.swift:62:19:62:32 | source(...) | semmle.label | source(...) | @@ -1461,7 +1458,6 @@ nodes | subscripts.swift:144:10:144:21 | ... .getField(...) | semmle.label | ... .getField(...) | | subscripts.swift:154:24:154:31 | newValue [field] | semmle.label | newValue [field] | | subscripts.swift:154:24:154:37 | ... .field | semmle.label | ... .field | -| subscripts.swift:157:19:157:19 | s [Return] [field] | semmle.label | s [Return] [field] | | subscripts.swift:157:33:157:33 | [post] s [field] | semmle.label | [post] s [field] | | subscripts.swift:157:33:157:39 | ... .field | semmle.label | ... .field | | subscripts.swift:157:43:157:57 | source(...) | semmle.label | source(...) | diff --git a/unified/ql/test/library-tests/mad/test.expected b/unified/ql/test/library-tests/mad/test.expected index a839bc8e92d3..952386038622 100644 --- a/unified/ql/test/library-tests/mad/test.expected +++ b/unified/ql/test/library-tests/mad/test.expected @@ -57,7 +57,7 @@ isSource | test.swift:89:9:89:24 | ... .text | local | | test.swift:176:13:176:16 | text | local | | test.swift:177:13:177:21 | ... .text | local | -| test.swift:209:5:213:5 | [self param] func ShorthandTestSub.sourceParams | shorthand-source | +| test.swift:209:5:213:5 | self | shorthand-source | | test.swift:210:11:210:11 | x | shorthand-source | | test.swift:232:13:232:13 | v | subscript-setter-source | | test.swift:253:11:253:11 | x | optional-source | diff --git a/unified/ql/test/library-tests/type-inference/CONSISTENCY/DataFlowConsistency.expected b/unified/ql/test/library-tests/type-inference/CONSISTENCY/DataFlowConsistency.expected index 2933561f6950..b4ea1a018406 100644 --- a/unified/ql/test/library-tests/type-inference/CONSISTENCY/DataFlowConsistency.expected +++ b/unified/ql/test/library-tests/type-inference/CONSISTENCY/DataFlowConsistency.expected @@ -1,5 +1,5 @@ uniqueParameterNodeAtPosition -| basics.swift:67:1:69:1 | func swap | a: | basics.swift:67:21:67:22 | a1 | Parameters with overlapping positions. | -| basics.swift:67:1:69:1 | func swap | a: | basics.swift:67:31:67:32 | a2 | Parameters with overlapping positions. | -| basics.swift:73:1:73:61 | func f | a: | basics.swift:73:10:73:14 | first | Parameters with overlapping positions. | -| basics.swift:73:1:73:61 | func f | a: | basics.swift:73:34:73:39 | second | Parameters with overlapping positions. | +| basics.swift:67:1:69:1 | func swap | a: | basics.swift:67:19:67:26 | Parameter | Parameters with overlapping positions. | +| basics.swift:67:1:69:1 | func swap | a: | basics.swift:67:29:67:36 | Parameter | Parameters with overlapping positions. | +| basics.swift:73:1:73:61 | func f | a: | basics.swift:73:8:73:29 | Parameter | Parameters with overlapping positions. | +| basics.swift:73:1:73:61 | func f | a: | basics.swift:73:32:73:57 | Parameter | Parameters with overlapping positions. | diff --git a/unified/ql/test/query-tests/security/CWE-094/UnsafeJsEval/CONSISTENCY/DataFlowConsistency.expected b/unified/ql/test/query-tests/security/CWE-094/UnsafeJsEval/CONSISTENCY/DataFlowConsistency.expected index aa30eebb8386..f457dee0d4de 100644 --- a/unified/ql/test/query-tests/security/CWE-094/UnsafeJsEval/CONSISTENCY/DataFlowConsistency.expected +++ b/unified/ql/test/query-tests/security/CWE-094/UnsafeJsEval/CONSISTENCY/DataFlowConsistency.expected @@ -1,5 +1,5 @@ uniqueParameterNodeAtPosition -| UnsafeJsEval.swift:31:2:38:2 | func WKWebView.evaluateJavaScript | in: | UnsafeJsEval.swift:33:6:33:10 | frame | Parameters with overlapping positions. | -| UnsafeJsEval.swift:31:2:38:2 | func WKWebView.evaluateJavaScript | in: | UnsafeJsEval.swift:34:6:34:17 | contentWorld | Parameters with overlapping positions. | -| UnsafeJsEval.swift:46:2:54:2 | func WKWebView.callAsyncJavaScript | in: | UnsafeJsEval.swift:49:6:49:10 | frame | Parameters with overlapping positions. | -| UnsafeJsEval.swift:46:2:54:2 | func WKWebView.callAsyncJavaScript | in: | UnsafeJsEval.swift:50:6:50:17 | contentWorld | Parameters with overlapping positions. | +| UnsafeJsEval.swift:31:2:38:2 | func WKWebView.evaluateJavaScript | in: | UnsafeJsEval.swift:33:3:33:30 | Parameter | Parameters with overlapping positions. | +| UnsafeJsEval.swift:31:2:38:2 | func WKWebView.evaluateJavaScript | in: | UnsafeJsEval.swift:34:3:34:33 | Parameter | Parameters with overlapping positions. | +| UnsafeJsEval.swift:46:2:54:2 | func WKWebView.callAsyncJavaScript | in: | UnsafeJsEval.swift:49:3:49:30 | Parameter | Parameters with overlapping positions. | +| UnsafeJsEval.swift:46:2:54:2 | func WKWebView.callAsyncJavaScript | in: | UnsafeJsEval.swift:50:3:50:33 | Parameter | Parameters with overlapping positions. |