|
| 1 | +.. _analyzing-data-flow-in-ruby: |
| 2 | + |
| 3 | +Analyzing data flow in Ruby |
| 4 | +============================= |
| 5 | + |
| 6 | +You can use CodeQL to track the flow of data through a Ruby program to places where the data is used. |
| 7 | + |
| 8 | +About this article |
| 9 | +------------------ |
| 10 | + |
| 11 | +This article describes how data flow analysis is implemented in the CodeQL libraries for Ruby and includes examples to help you write your own data flow queries. |
| 12 | +The following sections describe how to use the libraries for local data flow, global data flow, and taint tracking. |
| 13 | +For a more general introduction to modeling data flow, see ":ref:`About data flow analysis <about-data-flow-analysis>`." |
| 14 | + |
| 15 | +Local data flow |
| 16 | +--------------- |
| 17 | + |
| 18 | +Local data flow tracks the flow of data within a single method or callable. 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. |
| 19 | + |
| 20 | +Using local data flow |
| 21 | +~~~~~~~~~~~~~~~~~~~~~ |
| 22 | + |
| 23 | +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. |
| 24 | +``Node``\ s are divided into expression nodes (``ExprNode``) and parameter nodes (``ParameterNode``). |
| 25 | +You can map a data flow ``ParameterNode`` to its corresponding ``Parameter`` AST node using the ``asParameter`` member predicate. |
| 26 | +Similarly, you can use the ``asExpr`` member predicate to map a data flow ``ExprNode`` to its corresponding ``ExprCfgNode`` in the control-flow library. |
| 27 | + |
| 28 | +.. code-block:: ql |
| 29 | +
|
| 30 | + class Node { |
| 31 | + /** Gets the expression corresponding to this node, if any. */ |
| 32 | + CfgNodes::ExprCfgNode asExpr() { ... } |
| 33 | +
|
| 34 | + /** Gets the parameter corresponding to this node, if any. */ |
| 35 | + Parameter asParameter() { ... } |
| 36 | +
|
| 37 | + ... |
| 38 | + } |
| 39 | +
|
| 40 | +You can use the predicates ``exprNode`` and ``parameterNode`` to map from expressions and parameters to their data-flow node: |
| 41 | + |
| 42 | +.. code-block:: ql |
| 43 | +
|
| 44 | + /** |
| 45 | + * Gets a node corresponding to expression `e`. |
| 46 | + */ |
| 47 | + ExprNode exprNode(CfgNodes::ExprCfgNode e) { ... } |
| 48 | +
|
| 49 | + /** |
| 50 | + * Gets the node corresponding to the value of parameter `p` at function entry. |
| 51 | + */ |
| 52 | + ParameterNode parameterNode(Parameter p) { ... } |
| 53 | +
|
| 54 | +Note that since ``asExpr`` and ``exprNode`` map between data-flow and control-flow nodes, you then need to call the ``getExpr`` member predicate on the control-flow node to map to the corresponding AST node, |
| 55 | +for example, by writing ``node.asExpr().getExpr()``. |
| 56 | +A control-flow graph considers every way control can flow through code, consequently, there can be multiple data-flow and control-flow nodes associated with a single expression node in the AST. |
| 57 | + |
| 58 | +The predicate ``localFlowStep(Node nodeFrom, Node nodeTo)`` holds if there is an immediate data flow edge from the node ``nodeFrom`` to the node ``nodeTo``. |
| 59 | +You can apply the predicate recursively, by using the ``+`` and ``*`` operators, or you can use the predefined recursive predicate ``localFlow``. |
| 60 | + |
| 61 | +For example, you can find flow from an expression ``source`` to an expression ``sink`` in zero or more local steps: |
| 62 | + |
| 63 | +.. code-block:: ql |
| 64 | +
|
| 65 | + DataFlow::localFlow(source, sink) |
| 66 | +
|
| 67 | +Using local taint tracking |
| 68 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 69 | + |
| 70 | +Local taint tracking extends local data flow to include flow steps where values are not preserved, for example, string manipulation. |
| 71 | +For example: |
| 72 | + |
| 73 | +.. code-block:: ruby |
| 74 | +
|
| 75 | + temp = x |
| 76 | + y = temp + ", " + temp |
| 77 | +
|
| 78 | +If ``x`` is a tainted string then ``y`` is also tainted. |
| 79 | + |
| 80 | +The local taint tracking library is in the module ``TaintTracking``. |
| 81 | +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``. |
| 82 | +You can apply the predicate recursively, by using the ``+`` and ``*`` operators, or you can use the predefined recursive predicate ``localTaint``. |
| 83 | + |
| 84 | +For example, you can find taint propagation from an expression ``source`` to an expression ``sink`` in zero or more local steps: |
| 85 | + |
| 86 | +.. code-block:: ql |
| 87 | +
|
| 88 | + TaintTracking::localTaint(source, sink) |
| 89 | +
|
| 90 | +
|
| 91 | +Using local sources |
| 92 | +~~~~~~~~~~~~~~~~~~~ |
| 93 | + |
| 94 | +When exploring local data flow or taint propagation between two expressions as above, you would normally constrain the expressions to be relevant to your investigation. |
| 95 | +The next section gives some concrete examples, but first it's helpful to introduce the concept of a local source. |
| 96 | + |
| 97 | +A local source is a data-flow node with no local data flow into it. |
| 98 | +As such, it is a local origin of data flow, a place where a new value is created. |
| 99 | +This includes parameters (which only receive values from global data flow) and most expressions (because they are not value-preserving). |
| 100 | +The class ``LocalSourceNode`` represents data-flow nodes that are also local sources. |
| 101 | +It comes with a useful member predicate ``flowsTo(DataFlow::Node node)``, which holds if there is local data flow from the local source to ``node``. |
| 102 | + |
| 103 | +Examples of local data flow |
| 104 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 105 | + |
| 106 | +This query finds the filename argument passed in each call to ``File.open``: |
| 107 | + |
| 108 | +.. code-block:: ql |
| 109 | +
|
| 110 | + import codeql.ruby.DataFlow |
| 111 | + import codeql.ruby.ApiGraphs |
| 112 | + |
| 113 | + from DataFlow::CallNode call |
| 114 | + where call = API::getTopLevelMember("File").getAMethodCall("open") |
| 115 | + select call.getArgument(0) |
| 116 | +
|
| 117 | +Notice the use of the ``API`` module for referring to library methods. |
| 118 | +For more information, see ":doc:`Using API graphs in Ruby <using-api-graphs-in-ruby>`." |
| 119 | + |
| 120 | +Unfortunately this will only give the expression in the argument, not the values which could be passed to it. |
| 121 | +So we use local data flow to find all expressions that flow into the argument: |
| 122 | + |
| 123 | +.. code-block:: ql |
| 124 | +
|
| 125 | + import codeql.ruby.DataFlow |
| 126 | + import codeql.ruby.ApiGraphs |
| 127 | + |
| 128 | + from DataFlow::CallNode call, DataFlow::ExprNode expr |
| 129 | + where |
| 130 | + call = API::getTopLevelMember("File").getAMethodCall("open") and |
| 131 | + DataFlow::localFlow(expr, call.getArgument(0)) |
| 132 | + select call, expr |
| 133 | +
|
| 134 | +Many expressions flow to the same call. |
| 135 | +If you run this query, you may notice that you get several data-flow nodes for an expression as it flows towards a call (notice repeated locations in the ``call`` column). |
| 136 | +We are mostly interested in the "first" of these, what might be called the local source for the file name. |
| 137 | +To restrict the results to local sources for the file name, and to simultaneously make the analysis more efficient, we can use the CodeQL class ``LocalSourceNode``. |
| 138 | +We can update the query to specify that ``expr`` is an instance of a ``LocalSourceNode``. |
| 139 | + |
| 140 | +.. code-block:: ql |
| 141 | +
|
| 142 | + import codeql.ruby.DataFlow |
| 143 | + import codeql.ruby.ApiGraphs |
| 144 | + |
| 145 | + from DataFlow::CallNode call, DataFlow::ExprNode expr |
| 146 | + where |
| 147 | + call = API::getTopLevelMember("File").getAMethodCall("open") and |
| 148 | + DataFlow::localFlow(expr, call.getArgument(0)) and |
| 149 | + expr instanceof DataFlow::LocalSourceNode |
| 150 | + select call, expr |
| 151 | +
|
| 152 | +An alternative approach to limit the results to local sources for the file name is to enforce this by casting. |
| 153 | +That would allow us to use the member predicate ``flowsTo`` on ``LocalSourceNode`` like so: |
| 154 | + |
| 155 | +.. code-block:: ql |
| 156 | +
|
| 157 | + import codeql.ruby.DataFlow |
| 158 | + import codeql.ruby.ApiGraphs |
| 159 | + |
| 160 | + from DataFlow::CallNode call, DataFlow::ExprNode expr |
| 161 | + where |
| 162 | + call = API::getTopLevelMember("File").getAMethodCall("open") and |
| 163 | + expr.(DataFlow::LocalSourceNode).flowsTo(call.getArgument(0)) |
| 164 | + select call, expr |
| 165 | +
|
| 166 | +As an alternative, we can ask more directly that ``expr`` is a local source of the first argument, via the predicate ``getALocalSource``: |
| 167 | + |
| 168 | +.. code-block:: ql |
| 169 | +
|
| 170 | + import codeql.ruby.DataFlow |
| 171 | + import codeql.ruby.ApiGraphs |
| 172 | + |
| 173 | + from DataFlow::CallNode call, DataFlow::ExprNode expr |
| 174 | + where |
| 175 | + call = API::getTopLevelMember("File").getAMethodCall("open") and |
| 176 | + expr = call.getArgument(0).getALocalSource() |
| 177 | + select call, expr |
| 178 | +
|
| 179 | +All these three queries give identical results. |
| 180 | +We now mostly have one expression per call. |
| 181 | + |
| 182 | +We may still have cases of more than one expression flowing to a call, but then they flow through different code paths (possibly due to control-flow splitting). |
| 183 | + |
| 184 | +We might want to make the source more specific, for example, a parameter to a method or block. |
| 185 | +This query finds instances where a parameter is used as the name when opening a file: |
| 186 | + |
| 187 | +.. code-block:: ql |
| 188 | +
|
| 189 | + import codeql.ruby.DataFlow |
| 190 | + import codeql.ruby.ApiGraphs |
| 191 | + |
| 192 | + from DataFlow::CallNode call, DataFlow::ParameterNode p |
| 193 | + where |
| 194 | + call = API::getTopLevelMember("File").getAMethodCall("open") and |
| 195 | + DataFlow::localFlow(p, call.getArgument(0)) |
| 196 | + select call, p |
| 197 | +
|
| 198 | +Using the exact name supplied via the parameter may be too strict. |
| 199 | +If we want to know if the parameter influences the file name, we can use taint tracking instead of data flow. |
| 200 | +This query finds calls to ``File.open`` where the file name is derived from a parameter: |
| 201 | + |
| 202 | +.. code-block:: ql |
| 203 | +
|
| 204 | + import codeql.ruby.DataFlow |
| 205 | + import codeql.ruby.TaintTracking |
| 206 | + import codeql.ruby.ApiGraphs |
| 207 | + |
| 208 | + from DataFlow::CallNode call, DataFlow::ParameterNode p |
| 209 | + where |
| 210 | + call = API::getTopLevelMember("File").getAMethodCall("open") and |
| 211 | + TaintTracking::localTaint(p, call.getArgument(0)) |
| 212 | + select call, p |
| 213 | +
|
| 214 | +Global data flow |
| 215 | +---------------- |
| 216 | + |
| 217 | +Global data flow tracks data flow throughout the entire program, and is therefore more powerful than local data flow. |
| 218 | +However, global data flow is less precise than local data flow, and the analysis typically requires significantly more time and memory to perform. |
| 219 | + |
| 220 | +.. pull-quote:: Note |
| 221 | + |
| 222 | + .. include:: ../reusables/path-problem.rst |
| 223 | + |
| 224 | +Using global data flow |
| 225 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 226 | + |
| 227 | +You can use the global data flow library by extending the class ``DataFlow::Configuration``: |
| 228 | + |
| 229 | +.. code-block:: ql |
| 230 | +
|
| 231 | + import codeql.ruby.DataFlow |
| 232 | +
|
| 233 | + class MyDataFlowConfiguration extends DataFlow::Configuration { |
| 234 | + MyDataFlowConfiguration() { this = "..." } |
| 235 | +
|
| 236 | + override predicate isSource(DataFlow::Node source) { |
| 237 | + ... |
| 238 | + } |
| 239 | +
|
| 240 | + override predicate isSink(DataFlow::Node sink) { |
| 241 | + ... |
| 242 | + } |
| 243 | + } |
| 244 | +
|
| 245 | +These predicates are defined in the configuration: |
| 246 | + |
| 247 | +- ``isSource`` - defines where data may flow from. |
| 248 | +- ``isSink`` - defines where data may flow to. |
| 249 | +- ``isBarrier`` - optionally, restricts the data flow. |
| 250 | +- ``isAdditionalFlowStep`` - optionally, adds additional flow steps. |
| 251 | + |
| 252 | +The characteristic predicate (``MyDataFlowConfiguration()``) defines the name of the configuration, so ``"..."`` must be replaced with a unique name (for instance the class name). |
| 253 | + |
| 254 | +The data flow analysis is performed using the predicate ``hasFlow(DataFlow::Node source, DataFlow::Node sink)``: |
| 255 | + |
| 256 | +.. code-block:: ql |
| 257 | +
|
| 258 | + from MyDataFlowConfiguation dataflow, DataFlow::Node source, DataFlow::Node sink |
| 259 | + where dataflow.hasFlow(source, sink) |
| 260 | + select source, "Dataflow to $@.", sink, sink.toString() |
| 261 | +
|
| 262 | +Using global taint tracking |
| 263 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 264 | + |
| 265 | +Global taint tracking is to global data flow what local taint tracking is to local data flow. |
| 266 | +That is, global taint tracking extends global data flow with additional non-value-preserving steps. |
| 267 | +The global taint tracking library is used by extending the class ``TaintTracking::Configuration``: |
| 268 | + |
| 269 | +.. code-block:: ql |
| 270 | +
|
| 271 | + import codeql.ruby.DataFlow |
| 272 | + import codeql.ruby.TaintTracking |
| 273 | +
|
| 274 | + class MyTaintTrackingConfiguration extends TaintTracking::Configuration { |
| 275 | + MyTaintTrackingConfiguration() { this = "..." } |
| 276 | +
|
| 277 | + override predicate isSource(DataFlow::Node source) { |
| 278 | + ... |
| 279 | + } |
| 280 | +
|
| 281 | + override predicate isSink(DataFlow::Node sink) { |
| 282 | + ... |
| 283 | + } |
| 284 | + } |
| 285 | +
|
| 286 | +These predicates are defined in the configuration: |
| 287 | + |
| 288 | +- ``isSource`` - defines where taint may flow from. |
| 289 | +- ``isSink`` - defines where taint may flow to. |
| 290 | +- ``isSanitizer`` - optionally, restricts the taint flow. |
| 291 | +- ``isAdditionalTaintStep`` - optionally, adds additional taint steps. |
| 292 | + |
| 293 | +Similar to global data flow, the characteristic predicate (``MyTaintTrackingConfiguration()``) defines the unique name of the configuration and the taint analysis is performed using the predicate ``hasFlow(DataFlow::Node source, DataFlow::Node sink)``. |
| 294 | + |
| 295 | +Predefined sources and sinks |
| 296 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 297 | + |
| 298 | +The data flow library contains a number of predefined sources and sinks, providing a good starting point for defining data flow based security queries. |
| 299 | + |
| 300 | +- The class ``RemoteFlowSource`` (defined in module ``codeql.ruby.dataflow.RemoteFlowSources``) represents data flow from remote network inputs. This is useful for finding security problems in networked services. |
| 301 | +- The library ``Concepts`` (defined in module ``codeql.ruby.Concepts``) contains several subclasses of ``DataFlow::Node`` that are security relevant, such as ``FileSystemAccess`` and ``SqlExecution``. |
| 302 | + |
| 303 | +For global flow, it is also useful to restrict sources to instances of ``LocalSourceNode``. |
| 304 | +The predefined sources generally do that. |
| 305 | + |
| 306 | +Class hierarchy |
| 307 | +~~~~~~~~~~~~~~~ |
| 308 | + |
| 309 | +- ``DataFlow::Configuration`` - base class for custom global data flow analysis. |
| 310 | +- ``DataFlow::Node`` - an element behaving as a data-flow node. |
| 311 | + - ``DataFlow::LocalSourceNode`` - a local origin of data, as a data-flow node. |
| 312 | + - ``DataFlow::ExprNode`` - an expression behaving as a data-flow node. |
| 313 | + - ``DataFlow::ParameterNode`` - a parameter data-flow node representing the value of a parameter at method/block entry. |
| 314 | + |
| 315 | + - ``RemoteFlowSource`` - data flow from network/remote input. |
| 316 | + - ``Concepts::SystemCommandExecution`` - a data-flow node that executes an operating system command, for instance by spawning a new process. |
| 317 | + - ``Concepts::FileSystemAccess`` - a data-flow node that performs a file system access, including reading and writing data, creating and deleting files and folders, checking and updating permissions, and so on. |
| 318 | + - ``Concepts::Path::PathNormalization`` - a data-flow node that performs path normalization. This is often needed in order to safely access paths. |
| 319 | + - ``Concepts::CodeExecution`` - a data-flow node that dynamically executes Ruby code. |
| 320 | + - ``Concepts::SqlExecution`` - a data-flow node that executes SQL statements. |
| 321 | + - ``Concepts::HTTP::Server::RouteSetup`` - a data-flow node that sets up a route on a server. |
| 322 | + - ``Concepts::HTTP::Server::HttpResponse`` - a data-flow node that creates an HTTP response on a server. |
| 323 | + |
| 324 | +- ``TaintTracking::Configuration`` - base class for custom global taint tracking analysis. |
| 325 | + |
| 326 | +Examples of global data flow |
| 327 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 328 | + |
| 329 | +The following global taint-tracking query finds path arguments in filesystem accesses that can be controlled by a remote user. |
| 330 | + - Since this is a taint-tracking query, the configuration class extends ``TaintTracking::Configuration``. |
| 331 | + - The ``isSource`` predicate defines sources as any data-flow nodes that are instances of ``RemoteFlowSource``. |
| 332 | + - The ``isSink`` predicate defines sinks as path arguments in any filesystem access, using ``FileSystemAccess`` from the ``Concepts`` library. |
| 333 | + |
| 334 | +.. code-block:: ql |
| 335 | +
|
| 336 | + import codeql.ruby.DataFlow |
| 337 | + import codeql.ruby.TaintTracking |
| 338 | + import codeql.ruby.Concepts |
| 339 | + import codeql.ruby.dataflow.RemoteFlowSources |
| 340 | + |
| 341 | + class RemoteToFileConfiguration extends TaintTracking::Configuration { |
| 342 | + RemoteToFileConfiguration() { this = "RemoteToFileConfiguration" } |
| 343 | + |
| 344 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 345 | + |
| 346 | + override predicate isSink(DataFlow::Node sink) { |
| 347 | + sink = any(FileSystemAccess fa).getAPathArgument() |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + from DataFlow::Node input, DataFlow::Node fileAccess, RemoteToFileConfiguration config |
| 352 | + where config.hasFlow(input, fileAccess) |
| 353 | + select fileAccess, "This file access uses data from $@.", input, "user-controllable input." |
| 354 | +
|
| 355 | +The following global data-flow query finds calls to ``File.open`` where the filename argument comes from an environment variable. |
| 356 | + - Since this is a data-flow query, the configuration class extends ``DataFlow::Configuration``. |
| 357 | + - The ``isSource`` predicate defines sources as expression nodes representing lookups on the ``ENV`` hash. |
| 358 | + - The ``isSink`` predicate defines sinks as the first argument in any call to ``File.open``. |
| 359 | + |
| 360 | +.. code-block:: ql |
| 361 | +
|
| 362 | + import codeql.ruby.DataFlow |
| 363 | + import codeql.ruby.controlflow.CfgNodes |
| 364 | + import codeql.ruby.ApiGraphs |
| 365 | + |
| 366 | + class EnvironmentToFileConfiguration extends DataFlow::Configuration { |
| 367 | + EnvironmentToFileConfiguration() { this = "EnvironmentToFileConfiguration" } |
| 368 | + |
| 369 | + override predicate isSource(DataFlow::Node source) { |
| 370 | + exists(ExprNodes::ConstantReadAccessCfgNode env | |
| 371 | + env.getExpr().getName() = "ENV" and |
| 372 | + env = source.asExpr().(ExprNodes::ElementReferenceCfgNode).getReceiver() |
| 373 | + ) |
| 374 | + } |
| 375 | + |
| 376 | + override predicate isSink(DataFlow::Node sink) { |
| 377 | + sink = API::getTopLevelMember("File").getAMethodCall("open").getArgument(0) |
| 378 | + } |
| 379 | + } |
| 380 | + |
| 381 | + from EnvironmentToFileConfiguration config, DataFlow::Node environment, DataFlow::Node fileOpen |
| 382 | + where config.hasFlow(environment, fileOpen) |
| 383 | + select fileOpen, "This call to 'File.open' uses data from $@.", environment, |
| 384 | + "an environment variable" |
| 385 | +
|
| 386 | +Further reading |
| 387 | +--------------- |
| 388 | + |
| 389 | +- ":ref:`Exploring data flow with path queries <exploring-data-flow-with-path-queries>`" |
| 390 | + |
| 391 | + |
| 392 | +.. include:: ../reusables/ruby-further-reading.rst |
| 393 | +.. include:: ../reusables/codeql-ref-tools-further-reading.rst |
0 commit comments