|
| 1 | +/** |
| 2 | + * Defines extensible predicates for contributing library models from data extensions. |
| 3 | + * |
| 4 | + * The extensible relations have the following columns: |
| 5 | + * |
| 6 | + * - Sources: |
| 7 | + * `crate; path; output; kind; provenance` |
| 8 | + * - Sinks: |
| 9 | + * `crate; path; input; kind; provenance` |
| 10 | + * - Summaries: |
| 11 | + * `crate; path; input; output; kind; provenance` |
| 12 | + * |
| 13 | + * The interpretation of a row is similar to API-graphs with a left-to-right |
| 14 | + * reading. |
| 15 | + * |
| 16 | + * 1. The `crate` column selects a crate. |
| 17 | + * 2. The `path` column selects a function with the given canonical path within |
| 18 | + * the crate. |
| 19 | + * 3. The `input` column specifies how data enters the element selected by the |
| 20 | + * first 2 columns, and the `output` column specifies how data leaves the |
| 21 | + * element selected by the first 2 columns. Both `input` and `output` are |
| 22 | + * `.`-separated lists of "access path tokens" to resolve, starting at the |
| 23 | + * selected function. |
| 24 | + * |
| 25 | + * The following tokens are supported: |
| 26 | + * - `Argument[n]`: the `n`-th argument to a call. May be a range of form `x..y` (inclusive) |
| 27 | + * and/or a comma-separated list. |
| 28 | + * - `Parameter[n]`: the `n`-th parameter of a callback. May be a range of form `x..y` (inclusive) |
| 29 | + * and/or a comma-separated list. |
| 30 | + * - `ReturnValue`: the value returned by a function call. |
| 31 | + * - `ArrayElement`: an element of an array. |
| 32 | + * - `Variant[v::f]`: field `f` of the variant with canonical path `v`, for example |
| 33 | + * `Variant[crate::ihex::Record::Data::value]`. |
| 34 | + * - `Variant[v(i)]`: position `i` inside the variant with canonical path `v`, for example |
| 35 | + * `Variant[crate::option::Option::Some(0)]`. |
| 36 | + * - `Struct[s::f]`: field `f` of the struct with canonical path `v`, for example |
| 37 | + * `Struct[crate::process::Child::stdin]`. |
| 38 | + * - `Tuple[i]`: the `i`th element of a tuple. |
| 39 | + * 4. The `kind` column is a tag that can be referenced from QL to determine to |
| 40 | + * which classes the interpreted elements should be added. For example, for |
| 41 | + * sources `"remote"` indicates a default remote flow source, and for summaries |
| 42 | + * `"taint"` indicates a default additional taint step and `"value"` indicates a |
| 43 | + * globally applicable value-preserving step. |
| 44 | + * 5. The `provenance` column is mainly used internally, and should be set to `"manual"` for |
| 45 | + * all custom models. |
| 46 | + */ |
| 47 | + |
| 48 | +private import rust |
| 49 | +private import codeql.rust.dataflow.FlowSummary |
| 50 | + |
| 51 | +/** |
| 52 | + * Holds if in a call to the function with canonical path `path`, defined in the |
| 53 | + * crate `crate`, the value referred to by `output` is a flow source of the given |
| 54 | + * `kind`. |
| 55 | + * |
| 56 | + * `output = "ReturnValue"` simply means the result of the call itself. |
| 57 | + * |
| 58 | + * For more information on the `kind` parameter, see |
| 59 | + * https://github.com/github/codeql/blob/main/docs/codeql/reusables/threat-model-description.rst. |
| 60 | + */ |
| 61 | +extensible predicate sourceModel( |
| 62 | + string crate, string path, string output, string kind, string provenance, |
| 63 | + QlBuiltins::ExtensionId madId |
| 64 | +); |
| 65 | + |
| 66 | +/** |
| 67 | + * Holds if in a call to the function with canonical path `path`, defined in the |
| 68 | + * crate `crate`, the value referred to by `input` is a flow sink of the given |
| 69 | + * `kind`. |
| 70 | + * |
| 71 | + * For example, `input = Argument[0]` means the first argument of the call. |
| 72 | + * |
| 73 | + * The following kinds are supported: |
| 74 | + * |
| 75 | + * - `sql-injection`: a flow sink for SQL injection. |
| 76 | + */ |
| 77 | +extensible predicate sinkModel( |
| 78 | + string crate, string path, string input, string kind, string provenance, |
| 79 | + QlBuiltins::ExtensionId madId |
| 80 | +); |
| 81 | + |
| 82 | +/** |
| 83 | + * Holds if in a call to the function with canonical path `path`, defined in the |
| 84 | + * crate `crate`, the value referred to by `input` can flow to the value referred |
| 85 | + * to by `output`. |
| 86 | + * |
| 87 | + * `kind` should be either `value` or `taint`, for value-preserving or taint-preserving |
| 88 | + * steps, respectively. |
| 89 | + */ |
| 90 | +extensible predicate summaryModel( |
| 91 | + string crate, string path, string input, string output, string kind, string provenance, |
| 92 | + QlBuiltins::ExtensionId madId |
| 93 | +); |
| 94 | + |
| 95 | +/** |
| 96 | + * Holds if the given extension tuple `madId` should pretty-print as `model`. |
| 97 | + * |
| 98 | + * This predicate should only be used in tests. |
| 99 | + */ |
| 100 | +predicate interpretModelForTest(QlBuiltins::ExtensionId madId, string model) { |
| 101 | + exists(string crate, string path, string output, string kind | |
| 102 | + sourceModel(crate, path, kind, output, _, madId) and |
| 103 | + model = "Source: " + crate + "; " + path + "; " + output + "; " + kind |
| 104 | + ) |
| 105 | + or |
| 106 | + exists(string crate, string path, string input, string kind | |
| 107 | + sinkModel(crate, path, kind, input, _, madId) and |
| 108 | + model = "Sink: " + crate + "; " + path + "; " + input + "; " + kind |
| 109 | + ) |
| 110 | + or |
| 111 | + exists(string type, string path, string input, string output, string kind | |
| 112 | + summaryModel(type, path, input, output, kind, _, madId) and |
| 113 | + model = "Summary: " + type + "; " + path + "; " + input + "; " + output + "; " + kind |
| 114 | + ) |
| 115 | +} |
| 116 | + |
| 117 | +private class SummarizedCallableFromModel extends SummarizedCallable::Range { |
| 118 | + private string crate; |
| 119 | + private string path; |
| 120 | + |
| 121 | + SummarizedCallableFromModel() { |
| 122 | + summaryModel(crate, path, _, _, _, _, _) and |
| 123 | + this = crate + "::_::" + path |
| 124 | + } |
| 125 | + |
| 126 | + override predicate propagatesFlow( |
| 127 | + string input, string output, boolean preservesValue, string model |
| 128 | + ) { |
| 129 | + exists(string kind, QlBuiltins::ExtensionId madId | |
| 130 | + summaryModel(crate, path, input, output, kind, _, madId) and |
| 131 | + model = "MaD:" + madId.toString() |
| 132 | + | |
| 133 | + kind = "value" and |
| 134 | + preservesValue = true |
| 135 | + or |
| 136 | + kind = "taint" and |
| 137 | + preservesValue = false |
| 138 | + ) |
| 139 | + } |
| 140 | +} |
0 commit comments