|
| 1 | +.. _analyzing-data-flow-in-swift: |
| 2 | + |
| 3 | +Analyzing data flow in Swift |
| 4 | +============================ |
| 5 | + |
| 6 | +You can use CodeQL to track the flow of data through a Swift program to places where the data is used. |
| 7 | + |
| 8 | +.. include:: ../reusables/swift-beta-note.rst |
| 9 | + |
| 10 | +About this article |
| 11 | +------------------ |
| 12 | + |
| 13 | +This article describes how data flow analysis is implemented in the CodeQL libraries for Swift and includes examples to help you write your own data flow queries. |
| 14 | +The following sections describe how to use the libraries for local data flow, global data flow, and taint tracking. |
| 15 | +For a more general introduction to modeling data flow, see ":ref:`About data flow analysis <about-data-flow-analysis>`." |
| 16 | + |
| 17 | +Local data flow |
| 18 | +--------------- |
| 19 | + |
| 20 | +Local data flow tracks the flow of data within a single function. Local data flow is easier, faster, and more precise than global data flow. Before looking at more complex tracking, you should always consider local tracking because it is sufficient for many queries. |
| 21 | + |
| 22 | +Using local data flow |
| 23 | +~~~~~~~~~~~~~~~~~~~~~ |
| 24 | + |
| 25 | +You can use the local data flow library by importing the ``DataFlow`` module. The library uses the class ``Node`` to represent any element through which data can flow. |
| 26 | +The ``Node`` class has a number of useful subclasses, such as ``ExprNode`` for expressions and ``ParameterNode`` for parameters. You can map between data flow nodes and expressions/control-flow nodes using the member predicates ``asExpr`` and ``getCfgNode``: |
| 27 | + |
| 28 | +.. code-block:: ql |
| 29 | +
|
| 30 | + class Node { |
| 31 | + /** |
| 32 | + * Gets the expression that corresponds to this node, if any. |
| 33 | + */ |
| 34 | + Expr asExpr() { ... } |
| 35 | +
|
| 36 | + /** |
| 37 | + * Gets the control flow node that corresponds to this data flow node. |
| 38 | + */ |
| 39 | + ControlFlowNode getCfgNode() { ... } |
| 40 | +
|
| 41 | + ... |
| 42 | + } |
| 43 | +
|
| 44 | +You can use the predicates ``exprNode`` and ``parameterNode`` to map from expressions and parameters to their data-flow node: |
| 45 | + |
| 46 | +.. code-block:: ql |
| 47 | +
|
| 48 | + /** |
| 49 | + * Gets a node corresponding to expression `e`. |
| 50 | + */ |
| 51 | + ExprNode exprNode(DataFlowExpr e) { result.asExpr() = e } |
| 52 | +
|
| 53 | + /** |
| 54 | + * Gets the node corresponding to the value of parameter `p` at function entry. |
| 55 | + */ |
| 56 | + ParameterNode parameterNode(DataFlowParameter p) { result.getParameter() = p } |
| 57 | +
|
| 58 | +There can be multiple data-flow nodes associated with a single expression node in the AST. |
| 59 | + |
| 60 | +The predicate ``localFlowStep(Node nodeFrom, Node nodeTo)`` holds if there is an immediate data flow edge from the node ``nodeFrom`` to the node ``nodeTo``. |
| 61 | +You can apply the predicate recursively, by using the ``+`` and ``*`` operators, or you can use the predefined recursive predicate ``localFlow``. |
| 62 | + |
| 63 | +For example, you can find flow from an expression ``source`` to an expression ``sink`` in zero or more local steps: |
| 64 | + |
| 65 | +.. code-block:: ql |
| 66 | +
|
| 67 | + DataFlow::localFlow(DataFlow::exprNode(source), DataFlow::exprNode(sink)) |
| 68 | +
|
| 69 | +Using local taint tracking |
| 70 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 71 | + |
| 72 | +Local taint tracking extends local data flow to include flow steps where values are not preserved, such as string manipulation. |
| 73 | +For example: |
| 74 | + |
| 75 | +.. code-block:: swift |
| 76 | +
|
| 77 | + temp = x |
| 78 | + y = temp + ", " + temp |
| 79 | +
|
| 80 | +If ``x`` is a tainted string then ``y`` is also tainted. |
| 81 | + |
| 82 | +The local taint tracking library is in the module ``TaintTracking``. |
| 83 | +Like local data flow, a predicate ``localTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo)`` holds if there is an immediate taint propagation edge from the node ``nodeFrom`` to the node ``nodeTo``. |
| 84 | +You can apply the predicate recursively, by using the ``+`` and ``*`` operators, or you can use the predefined recursive predicate ``localTaint``. |
| 85 | + |
| 86 | +For example, you can find taint propagation from an expression ``source`` to an expression ``sink`` in zero or more local steps: |
| 87 | + |
| 88 | +.. code-block:: ql |
| 89 | +
|
| 90 | + TaintTracking::localTaint(DataFlow::exprNode(source), DataFlow::exprNode(sink)) |
| 91 | +
|
| 92 | +Examples of local data flow |
| 93 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 94 | + |
| 95 | +This query finds the ``format`` argument passed into each call to ``String.init(format:_:)``: |
| 96 | + |
| 97 | +.. code-block:: ql |
| 98 | +
|
| 99 | + import swift |
| 100 | +
|
| 101 | + from CallExpr call, Method method |
| 102 | + where |
| 103 | + call.getStaticTarget() = method and |
| 104 | + method.hasQualifiedName("String", "init(format:_:)") |
| 105 | + select call.getArgument(0).getExpr() |
| 106 | +
|
| 107 | +Unfortunately this will only give the expression in the argument, not the values which could be passed to it. |
| 108 | +So we use local data flow to find all expressions that flow into the argument: |
| 109 | + |
| 110 | +.. code-block:: ql |
| 111 | +
|
| 112 | + import swift |
| 113 | + import codeql.swift.dataflow.DataFlow |
| 114 | +
|
| 115 | + from CallExpr call, Method method, Expr sourceExpr, Expr sinkExpr |
| 116 | + where |
| 117 | + call.getStaticTarget() = method and |
| 118 | + method.hasQualifiedName("String", "init(format:_:)") and |
| 119 | + sinkExpr = call.getArgument(0).getExpr() and |
| 120 | + DataFlow::localFlow(DataFlow::exprNode(sourceExpr), DataFlow::exprNode(sinkExpr)) |
| 121 | + select sourceExpr, sinkExpr |
| 122 | +
|
| 123 | +We can vary the source, for example, making the source the parameter of a function rather than an expression. The following query finds where a parameter is used for the format: |
| 124 | + |
| 125 | +.. code-block:: ql |
| 126 | +
|
| 127 | + import swift |
| 128 | + import codeql.swift.dataflow.DataFlow |
| 129 | +
|
| 130 | + from CallExpr call, Method method, ParamDecl sourceParam, Expr sinkExpr |
| 131 | + where |
| 132 | + call.getStaticTarget() = method and |
| 133 | + method.hasQualifiedName("String", "init(format:_:)") and |
| 134 | + sinkExpr = call.getArgument(0).getExpr() and |
| 135 | + DataFlow::localFlow(DataFlow::parameterNode(sourceParam), DataFlow::exprNode(sinkExpr)) |
| 136 | + select sourceParam, sinkExpr |
| 137 | +
|
| 138 | +The following example finds calls to ``String.init(format:_:)`` where the format string is not a hard-coded string literal: |
| 139 | + |
| 140 | +.. code-block:: ql |
| 141 | +
|
| 142 | + import swift |
| 143 | + import codeql.swift.dataflow.DataFlow |
| 144 | +
|
| 145 | + from CallExpr call, Method method, DataFlow::Node sinkNode |
| 146 | + where |
| 147 | + call.getStaticTarget() = method and |
| 148 | + method.hasQualifiedName("String", "init(format:_:)") and |
| 149 | + sinkNode.asExpr() = call.getArgument(0).getExpr() and |
| 150 | + not exists(StringLiteralExpr sourceLiteral | |
| 151 | + DataFlow::localFlow(DataFlow::exprNode(sourceLiteral), sinkNode) |
| 152 | + ) |
| 153 | + select call, "Format argument to " + method.getName() + " isn't hard-coded." |
| 154 | +
|
| 155 | +Global data flow |
| 156 | +---------------- |
| 157 | + |
| 158 | +Global data flow tracks data flow throughout the entire program, and is therefore more powerful than local data flow. |
| 159 | +However, global data flow is less precise than local data flow, and the analysis typically requires significantly more time and memory to perform. |
| 160 | + |
| 161 | +.. pull-quote:: Note |
| 162 | + |
| 163 | + .. include:: ../reusables/path-problem.rst |
| 164 | + |
| 165 | +Using global data flow |
| 166 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 167 | + |
| 168 | +You can use the global data flow library by implementing the module ``DataFlow::ConfigSig``: |
| 169 | + |
| 170 | +.. code-block:: ql |
| 171 | +
|
| 172 | + import codeql.swift.dataflow.DataFlow |
| 173 | +
|
| 174 | + module MyDataFlowConfiguration implements DataFlow::ConfigSig { |
| 175 | + predicate isSource(DataFlow::Node source) { |
| 176 | + ... |
| 177 | + } |
| 178 | +
|
| 179 | + predicate isSink(DataFlow::Node sink) { |
| 180 | + ... |
| 181 | + } |
| 182 | + } |
| 183 | +
|
| 184 | + module MyDataFlow = DataFlow::Global<MyDataFlowConfiguration>; |
| 185 | +
|
| 186 | +These predicates are defined in the configuration: |
| 187 | + |
| 188 | +- ``isSource`` - defines where data may flow from. |
| 189 | +- ``isSink`` - defines where data may flow to. |
| 190 | +- ``isBarrier`` - optionally, restricts the data flow. |
| 191 | +- ``isAdditionalFlowStep`` - optionally, adds additional flow steps. |
| 192 | + |
| 193 | +The last line (``module MyDataFlow = ...``) instantiates the parameterized module for data flow analysis by passing the configuration to the parameterized module. Data flow analysis can then be performed using ``MyDataFlow::flow(DataFlow::Node source, DataFlow::Node sink)``: |
| 194 | + |
| 195 | +.. code-block:: ql |
| 196 | +
|
| 197 | + from DataFlow::Node source, DataFlow::Node sink |
| 198 | + where MyDataFlow::flow(source, sink) |
| 199 | + select source, "Dataflow to $@.", sink, sink.toString() |
| 200 | +
|
| 201 | +Using global taint tracking |
| 202 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 203 | + |
| 204 | +Global taint tracking is to global data flow what local taint tracking is to local data flow. |
| 205 | +That is, global taint tracking extends global data flow with additional non-value-preserving steps. |
| 206 | +The global taint tracking library uses the same configuration module as the global data flow library. You can perform taint flow analysis using ``TaintTracking::Global``: |
| 207 | + |
| 208 | +.. code-block:: ql |
| 209 | +
|
| 210 | + module MyTaintFlow = TaintTracking::Global<MyDataFlowConfiguration>; |
| 211 | +
|
| 212 | + from DataFlow::Node source, DataFlow::Node sink |
| 213 | + where MyTaintFlow::flow(source, sink) |
| 214 | + select source, "Taint flow to $@.", sink, sink.toString() |
| 215 | +
|
| 216 | +Predefined sources |
| 217 | +~~~~~~~~~~~~~~~~~~ |
| 218 | + |
| 219 | +The data flow library module ``codeql.swift.dataflow.FlowSources`` contains a number of predefined sources that you can use to write security queries to track data flow and taint flow. |
| 220 | + |
| 221 | +- The class ``RemoteFlowSource`` represents data flow from remote network inputs and from other applications. |
| 222 | +- The class ``LocalFlowSource`` represents data flow from local user input. |
| 223 | +- The class ``FlowSource`` includes both of the above. |
| 224 | + |
| 225 | +Examples of global data flow |
| 226 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 227 | + |
| 228 | +The following global taint-tracking query finds places where a string literal is used in a function call argument named "password". |
| 229 | + - Since this is a taint-tracking query, the ``TaintTracking::Global`` module is used. |
| 230 | + - The ``isSource`` predicate defines sources as any ``StringLiteralExpr``. |
| 231 | + - The ``isSink`` predicate defines sinks as arguments to a ``CallExpr`` called "password". |
| 232 | + - The sources and sinks may need tuning to a particular use, for example, if passwords are represented by a type other than ``String`` or passed in arguments of a different name than "password". |
| 233 | + |
| 234 | +.. code-block:: ql |
| 235 | +
|
| 236 | + import swift |
| 237 | + import codeql.swift.dataflow.DataFlow |
| 238 | + import codeql.swift.dataflow.TaintTracking |
| 239 | +
|
| 240 | + module ConstantPasswordConfig implements DataFlow::ConfigSig { |
| 241 | + predicate isSource(DataFlow::Node node) { node.asExpr() instanceof StringLiteralExpr } |
| 242 | +
|
| 243 | + predicate isSink(DataFlow::Node node) { |
| 244 | + // any argument called `password` |
| 245 | + exists(CallExpr call | call.getArgumentWithLabel("password").getExpr() = node.asExpr()) |
| 246 | + } |
| 247 | +
|
| 248 | + module ConstantPasswordFlow = TaintTracking::Global<ConstantPasswordConfig>; |
| 249 | +
|
| 250 | + from DataFlow::Node sourceNode, DataFlow::Node sinkNode |
| 251 | + where ConstantPasswordFlow::flow(sourceNode, sinkNode) |
| 252 | + select sinkNode, "The value $@ is used as a constant password.", sourceNode, sourceNode.toString() |
| 253 | +
|
| 254 | +
|
| 255 | +The following global taint-tracking query finds places where a value from a remote or local user input is used as an argument to the SQLite ``Connection.execute(_:)`` function. |
| 256 | + - Since this is a taint-tracking query, the ``TaintTracking::Global`` module is used. |
| 257 | + - The ``isSource`` predicate defines sources as a ``FlowSource`` (remote or local user input). |
| 258 | + - The ``isSink`` predicate defines sinks as the first argument in any call to ``Connection.execute(_:)``. |
| 259 | + |
| 260 | +.. code-block:: ql |
| 261 | +
|
| 262 | + import swift |
| 263 | + import codeql.swift.dataflow.DataFlow |
| 264 | + import codeql.swift.dataflow.TaintTracking |
| 265 | + import codeql.swift.dataflow.FlowSources |
| 266 | +
|
| 267 | + module SqlInjectionConfig implements DataFlow::ConfigSig { |
| 268 | + predicate isSource(DataFlow::Node node) { node instanceof FlowSource } |
| 269 | +
|
| 270 | + predicate isSink(DataFlow::Node node) { |
| 271 | + exists(CallExpr call | |
| 272 | + call.getStaticTarget().(Method).hasQualifiedName("Connection", "execute(_:)") and |
| 273 | + call.getArgument(0).getExpr() = node.asExpr() |
| 274 | + ) |
| 275 | + } |
| 276 | + } |
| 277 | +
|
| 278 | + module SqlInjectionFlow = TaintTracking::Global<SqlInjectionConfig>; |
| 279 | +
|
| 280 | + from DataFlow::Node sourceNode, DataFlow::Node sinkNode |
| 281 | + where SqlInjectionFlow::flow(sourceNode, sinkNode) |
| 282 | + select sinkNode, "This query depends on a $@.", sourceNode, "user-provided value" |
| 283 | +
|
| 284 | +Further reading |
| 285 | +--------------- |
| 286 | + |
| 287 | +- ":ref:`Exploring data flow with path queries <exploring-data-flow-with-path-queries>`" |
| 288 | + |
| 289 | + |
| 290 | +.. include:: ../reusables/swift-further-reading.rst |
| 291 | +.. include:: ../reusables/codeql-ref-tools-further-reading.rst |
0 commit comments