Skip to content

Commit 08aced8

Browse files
coadaflorinCopilot
andcommitted
Add barrier and barrier guard documentation for Rust
Add barrierModel and barrierGuardModel sections to the Rust library models documentation, following the pattern established in PR #21523 for other languages. Includes: - New extensible predicate descriptions in the overview - Example: barrier for SQL injection using escape_sql - Example: barrier guard for path injection using is_safe_path - Reference material for both barrierModel and barrierGuardModel Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6c83ec6 commit 08aced8

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

docs/codeql/codeql-language-guides/customizing-library-models-for-rust.rst

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ The CodeQL library for Rust analysis exposes the following extensible predicates
5454
- ``sinkModel(path, input, kind, provenance)``. This is used to model sinks where tainted data may be used in a way that makes the code vulnerable.
5555
- ``summaryModel(path, input, output, kind, provenance)``. This is used to model flow through elements.
5656
- ``neutralModel(path, kind, provenance)``. This is similar to a summary model but used to indicate that a callable has no flow for a given category. Manual neutral models (those with a provenance such as ``manual``) can be used to override generated summary, source, or sink models (those with a provenance such as ``df-generated``), so that the generated model will be ignored.
57+
- ``barrierModel(path, output, kind, provenance)``. This is used to model barriers, which are elements that stop the flow of taint.
58+
- ``barrierGuardModel(path, input, acceptingValue, kind, provenance)``. This is used to model barrier guards, which are elements that can stop the flow of taint depending on a conditional check.
5759

5860
The extensible predicates are populated using the models defined in data extension files.
5961

@@ -319,6 +321,75 @@ Since we are adding a neutral model, we need to add a tuple to the ``neutralMode
319321
- The second value ``sink`` is the category of model to suppress. This means that any generated sink model for ``Option::map`` will be ignored. The category can be ``source``, ``sink``, or ``summary``.
320322
- The third value ``manual`` is the provenance of the neutral model.
321323

324+
Example: Add a barrier for SQL injection
325+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
326+
327+
This example shows how to model a barrier that stops the flow of taint. A barrier model is used to define that the flow of taint stops at the modeled element for the specified kind of query.
328+
329+
Consider a hypothetical function ``my_crate::sanitize::escape_sql`` which escapes a SQL string, making it safe to use in a SQL query.
330+
331+
.. code-block:: rust
332+
333+
fn run_query(pool: &sqlx::PgPool, user_input: &str) {
334+
let safe_input = my_crate::sanitize::escape_sql(user_input); // The return value is safe to use in SQL.
335+
let query = format!("SELECT * FROM users WHERE name = '{}'", safe_input);
336+
// ...
337+
}
338+
339+
We need to add a tuple to the ``barrierModel``\(path, output, kind, provenance) extensible predicate by updating a data extension file.
340+
341+
.. code-block:: yaml
342+
343+
extensions:
344+
- addsTo:
345+
pack: codeql/rust-all
346+
extensible: barrierModel
347+
data:
348+
- ["my_crate::sanitize::escape_sql", "ReturnValue", "sql-injection", "manual"]
349+
350+
Since we are adding a barrier, we need to add a tuple to the ``barrierModel`` extensible predicate.
351+
352+
- The first value ``my_crate::sanitize::escape_sql`` is the canonical path of the function.
353+
- The second value ``ReturnValue`` is the access path to the output of the barrier, which means that the return value is considered sanitized.
354+
- The third value ``sql-injection`` is the kind of the barrier. The barrier kind must match the kind used in the query where the barrier should take effect. In this case, it matches the ``sql-injection`` sink kind used by SQL injection queries.
355+
- The fourth value ``manual`` is the provenance of the barrier.
356+
357+
Example: Add a barrier guard
358+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
359+
360+
This example shows how to model a barrier guard that stops the flow of taint when a conditional check is performed on data.
361+
A barrier guard model is used when a function returns a boolean that indicates whether the data is safe to use.
362+
363+
Consider a hypothetical function ``my_crate::validate::is_safe_path`` which returns ``true`` when the given path is safe to use in a file system access.
364+
365+
.. code-block:: rust
366+
367+
fn read_file(user_path: &str) {
368+
if my_crate::validate::is_safe_path(user_path) { // The check guards the use, so the input is safe.
369+
let contents = std::fs::read_to_string(user_path).unwrap();
370+
// ...
371+
}
372+
}
373+
374+
We need to add a tuple to the ``barrierGuardModel``\(path, input, acceptingValue, kind, provenance) extensible predicate by updating a data extension file.
375+
376+
.. code-block:: yaml
377+
378+
extensions:
379+
- addsTo:
380+
pack: codeql/rust-all
381+
extensible: barrierGuardModel
382+
data:
383+
- ["my_crate::validate::is_safe_path", "Argument[0]", "true", "path-injection", "manual"]
384+
385+
Since we are adding a barrier guard, we need to add a tuple to the ``barrierGuardModel`` extensible predicate.
386+
387+
- The first value ``my_crate::validate::is_safe_path`` is the canonical path of the function.
388+
- The second value ``Argument[0]`` is the access path to the input whose flow is blocked. In this case, the first argument to the function (``user_path`` in the example).
389+
- The third value ``true`` is the accepting value of the barrier guard. This is the value that the conditional check must return for the barrier to apply. In this case, when ``is_safe_path`` returns ``true``, the input is considered safe.
390+
- The fourth value ``path-injection`` is the kind of the barrier guard. The barrier guard kind must match the kind used in the query where the barrier guard should take effect. In this case, it matches the ``path-injection`` sink kind used by tainted path queries.
391+
- The fifth value ``manual`` is the provenance of the barrier guard.
392+
322393
.. _threat-models-rust:
323394

324395
Threat models
@@ -418,6 +489,49 @@ Example:
418489
data:
419490
- ["<core::option::Option>::map", "sink", "manual"]
420491
492+
barrierModel(path, output, kind, provenance)
493+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
494+
495+
Adds a new barrier that stops the flow of taint at the specified element.
496+
497+
- **path**: Canonical path of the function or method.
498+
- **output**: Access path leading to the output of the barrier (the value that is considered sanitized).
499+
- **kind**: Kind of barrier to add. The barrier kind must match the kind used in the query where the barrier should take effect.
500+
- **provenance**: Origin of the model. Use ``manual`` for custom models.
501+
502+
Example:
503+
504+
.. code-block:: yaml
505+
506+
extensions:
507+
- addsTo:
508+
pack: codeql/rust-all
509+
extensible: barrierModel
510+
data:
511+
- ["my_crate::sanitize::escape_sql", "ReturnValue", "sql-injection", "manual"]
512+
513+
barrierGuardModel(path, input, acceptingValue, kind, provenance)
514+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
515+
516+
Adds a new barrier guard that stops the flow of taint when a conditional check is performed on data.
517+
518+
- **path**: Canonical path of the function or method.
519+
- **input**: Access path to the input whose flow is blocked.
520+
- **acceptingValue**: The value that the conditional check must return for the barrier to apply. Usually ``"true"`` or ``"false"``.
521+
- **kind**: Kind of barrier guard to add. The barrier guard kind must match the kind used in the query where the barrier guard should take effect.
522+
- **provenance**: Origin of the model. Use ``manual`` for custom models.
523+
524+
Example:
525+
526+
.. code-block:: yaml
527+
528+
extensions:
529+
- addsTo:
530+
pack: codeql/rust-all
531+
extensible: barrierGuardModel
532+
data:
533+
- ["my_crate::validate::is_safe_path", "Argument[0]", "true", "path-injection", "manual"]
534+
421535
Access paths
422536
------------
423537

0 commit comments

Comments
 (0)