|
| 1 | +# Taint analysis |
| 2 | + |
| 3 | +[//]: # (TODO) |
| 4 | + |
| 5 | +## Configuration |
| 6 | + |
| 7 | +There are four _rule types_: sources, passes, cleaners and sinks. |
| 8 | + |
| 9 | +The rule of each type contains |
| 10 | +* a method name, |
| 11 | +* a method description |
| 12 | + |
| 13 | +### Method name |
| 14 | + |
| 15 | +Fully qualified _method names_ can be written in one line. |
| 16 | + |
| 17 | +One can also define a full _method name_ using nested structure: the package name is specified first, |
| 18 | +then the class name appears, and finally there is the method name itself. |
| 19 | + |
| 20 | +Note that regular expressions in names are not supported. |
| 21 | + |
| 22 | +### Method description |
| 23 | + |
| 24 | +Method description constitutes the essence of a rule. |
| 25 | +Here are method descriptions for the rules of each type: sources, passes, cleaners and sinks. |
| 26 | + |
| 27 | +#### Sources |
| 28 | + |
| 29 | +The `add-to` field specifies the objects to be marked. |
| 30 | +You can specify only one value here or a whole list. |
| 31 | + |
| 32 | +Possible values are: |
| 33 | +* `this` |
| 34 | +* `arg1` |
| 35 | +* `arg2` |
| 36 | +* ... |
| 37 | +* `return` |
| 38 | + |
| 39 | +The `marks` field specifies the `marks` that should be added to the objects from the `add-to` list. You can also specify only one mark here or a whole list. |
| 40 | + |
| 41 | +Example: |
| 42 | + |
| 43 | +```yaml |
| 44 | +sources: |
| 45 | + - com: |
| 46 | + - abc: |
| 47 | + - method1: |
| 48 | + signature: [ _, _, <java.lang.Object> ] |
| 49 | + add-to: return |
| 50 | + marks: xss |
| 51 | + - method1: |
| 52 | + signature: [ <int>, _, <java.lang.String> ] |
| 53 | + add-to: [ this, return ] |
| 54 | + marks: sql-injection |
| 55 | + - bca.method2: |
| 56 | + add-to: return |
| 57 | + marks: [ sensitive-data, xss ] |
| 58 | + - org.example.method3: |
| 59 | + add-to: [ this, arg1, arg3 ] |
| 60 | + marks: sensitive-data |
| 61 | +``` |
| 62 | +
|
| 63 | +#### Passes |
| 64 | +
|
| 65 | +The `get-from` field specifies the sources of `marks`. |
| 66 | + |
| 67 | +The `add-to` field specifies the objects that will be marked if any element from `get-from` has any mark. |
| 68 | + |
| 69 | +The `marks` field specifies the actual marks that can be passed from one object to another. |
| 70 | + |
| 71 | +For all the keys listed above, there can be only one value or a whole list of values. |
| 72 | +This is also true for similar keys in the sections below. |
| 73 | + |
| 74 | +Example: |
| 75 | + |
| 76 | +```yaml |
| 77 | +passes: |
| 78 | + - com.abc.method2: |
| 79 | + get-from: [ this, arg1, arg3 ] |
| 80 | + add-to: [ arg2, return ] |
| 81 | + marks: sensitive-data |
| 82 | +``` |
| 83 | + |
| 84 | +#### Cleaners |
| 85 | + |
| 86 | +The `remove-from` field specifies the objects the `marks` should be removed from. |
| 87 | + |
| 88 | +The `marks` field specifies the marks to be removed. |
| 89 | + |
| 90 | +Example: |
| 91 | + |
| 92 | +```yaml |
| 93 | +cleaners: |
| 94 | + - com.example.pack.method7: |
| 95 | + remove-from: this |
| 96 | + marks: [ sensitive-data, sql-injection ] |
| 97 | +``` |
| 98 | + |
| 99 | +#### Sinks |
| 100 | + |
| 101 | +The `check` field specifies the objects that will be checked for `marks`. |
| 102 | + |
| 103 | +When one of the `marks` is found in one of the objects from the `check`, the analysis will report the problem found. |
| 104 | + |
| 105 | +Example: |
| 106 | + |
| 107 | +```yaml |
| 108 | +sinks: |
| 109 | + - org.example: |
| 110 | + - log: |
| 111 | + check: arg1 |
| 112 | + marks: sensitive-data |
| 113 | + - method17: |
| 114 | + check: [ arg1, arg3 ] |
| 115 | + marks: [ sql-injection, xss ] |
| 116 | +``` |
| 117 | +*** |
| 118 | + |
| 119 | +For all the rule types, method descriptions can optionally contain |
| 120 | +* a method signature, |
| 121 | +* runtime conditions. |
| 122 | + |
| 123 | +**Signature** |
| 124 | + |
| 125 | +Method `signature` (optional) is a list of _argument types_ (at compile time): |
| 126 | + |
| 127 | +`signature: [ <int>, _, <java.lang.String> ]` |
| 128 | + |
| 129 | +Please note that |
| 130 | +- the type name is written in `<>`, |
| 131 | +- `_` means any type. |
| 132 | + |
| 133 | +**Conditions** |
| 134 | + |
| 135 | +Runtime `conditions` (optional) are conditions that must be met to trigger this rule. |
| 136 | + |
| 137 | +To check the conformity to conditions, you can set: |
| 138 | +* the specific values of method arguments |
| 139 | +* or their runtime types. |
| 140 | + |
| 141 | +Values can be set for the following types: `boolean`, `int`, `float` or `string`. |
| 142 | + |
| 143 | +The full type name must be specified in the angle brackets `<>`. |
| 144 | + |
| 145 | +If you do not specify `conditions`, the rule will be triggered by any call of the method. |
| 146 | + |
| 147 | +If several rules are suitable for one method call, they will all be applied in some kind of order. |
| 148 | + |
| 149 | +The `conditions` field specifies runtime conditions for arguments (`arg1`, `arg2`, ...). Conditions can also be specified for `this` and `return` (if it makes sense). |
| 150 | + |
| 151 | +The rule is triggered if all the specified conditions are met. |
| 152 | + |
| 153 | +Example: |
| 154 | + |
| 155 | +```yaml |
| 156 | +conditions: |
| 157 | + this: <java.lang.String> # this should be java.lang.String |
| 158 | + arg1: "test" # the first argument should be equal to "test" |
| 159 | + arg2: 227 # int |
| 160 | + arg3: 227.001 # float |
| 161 | + arg4: null # null |
| 162 | + return: true # return value should be equal to `true` |
| 163 | +``` |
| 164 | + |
| 165 | +Values and types can be negated using the `not` key, as well as combined using lists (`or` semantics). |
| 166 | + |
| 167 | +Nesting is allowed. |
| 168 | + |
| 169 | +Example: |
| 170 | + |
| 171 | +```yaml |
| 172 | +conditions: |
| 173 | + this: [ "in", "out" ] # this should be equal to either "in" or "out" |
| 174 | + arg1: [ <int>, <float> ] # arg1 should be int or float |
| 175 | + arg2: { not: 0 } # arg2 should not be equal to 0 |
| 176 | + arg3: |
| 177 | + not: [ 1, 2, 3, 5, 8 ] # should not be equal to any of the listed numbers |
| 178 | + arg4: [ "", { not: <java.lang.String> } ] # should be an empty string or not a string at all |
| 179 | +``` |
| 180 | +
|
| 181 | +### Overall example |
| 182 | +
|
| 183 | +```yaml |
| 184 | +sources: |
| 185 | + - com: |
| 186 | + - abc: |
| 187 | + - method1: |
| 188 | + signature: [ _, _, <java.lang.Object> ] |
| 189 | + add-to: return |
| 190 | + marks: xss |
| 191 | + - method1: |
| 192 | + signature: [ <int>, _, <java.lang.String> ] |
| 193 | + add-to: [ this, return ] |
| 194 | + marks: sql-injection |
| 195 | + - bca.method2: |
| 196 | + conditions: |
| 197 | + this: |
| 198 | + not: "in" |
| 199 | + add-to: return |
| 200 | + marks: [ sensitive-data, xss ] |
| 201 | + |
| 202 | +passes: |
| 203 | + - com.abc.method2: |
| 204 | + get-from: [ this, arg1, arg3 ] |
| 205 | + add-to: return |
| 206 | + marks: sensitive-data |
| 207 | + - org.example.method3: |
| 208 | + conditions: |
| 209 | + arg1: { not: "" } |
| 210 | + get-from: arg1 |
| 211 | + add-to: [ this, return ] |
| 212 | + marks: sql-injection |
| 213 | + |
| 214 | +cleaners: |
| 215 | + - com.example.pack.method7: |
| 216 | + conditions: |
| 217 | + return: true |
| 218 | + remove-from: this |
| 219 | + marks: [ sensitive-data, sql-injection ] |
| 220 | + |
| 221 | +sinks: |
| 222 | + - org.example: |
| 223 | + - log: |
| 224 | + check: arg1 |
| 225 | + marks: sensitive-data |
| 226 | + - method17: |
| 227 | + check: [ arg1, arg3 ] |
| 228 | + marks: [ sql-injection, xss ] |
| 229 | +``` |
| 230 | +
|
| 231 | +## Usage examples |
| 232 | +
|
| 233 | +`java.lang.System.getenv` is a source of the "environment" mark. There are two overloads of this method: with one string parameter and no parameters at all. We want to describe only the first overload: |
| 234 | + |
| 235 | + ```yaml |
| 236 | + sources: |
| 237 | + - java.lang.System.getenv: |
| 238 | + signature: [ <java.lang.String> ] |
| 239 | + add-to: return |
| 240 | + marks: environment |
| 241 | + ``` |
| 242 | + |
| 243 | +`java.lang.String.concat` is a pass-through only if `this` is marked and not equal to `""`, or if `arg1` is marked and not equal to `""`: |
| 244 | + |
| 245 | + ```yaml |
| 246 | + passes: |
| 247 | + - java.lang.String: |
| 248 | + - concat: |
| 249 | + conditions: |
| 250 | + this: { not: "" } |
| 251 | + get-from: this |
| 252 | + add-to: return |
| 253 | + marks: sensitive-data |
| 254 | + - concat: |
| 255 | + conditions: |
| 256 | + arg1: { not: "" } |
| 257 | + get-from: arg1 |
| 258 | + add-to: return |
| 259 | + marks: sensitive-data |
| 260 | + ``` |
| 261 | + |
| 262 | +`java.lang.String.isEmpty` is a cleaner only if it returns `true`: |
| 263 | + |
| 264 | + ```yaml |
| 265 | + cleaners: |
| 266 | + - java.lang.String.isEmpty: |
| 267 | + conditions: |
| 268 | + return: true |
| 269 | + remove-from: this |
| 270 | + marks: [ sql-injection, xss ] |
| 271 | + ``` |
| 272 | + |
| 273 | +Suppose that the `org.example.util.unsafe` method is a sink for the "environment" mark if the second argument is an `Integer` and equal to zero: |
| 274 | + |
| 275 | + ```yaml |
| 276 | + sinks: |
| 277 | + - org.example.util.unsafe: |
| 278 | + signature: [ _, <java.lang.Integer> ] |
| 279 | + conditions: |
| 280 | + arg2: 0 |
| 281 | + check: arg2 |
| 282 | + marks: environment |
| 283 | + ``` |
| 284 | + |
| 285 | +The configuration above checks the type at compile-time, but sometimes we want to check the type at runtime: |
| 286 | + |
| 287 | + ```yaml |
| 288 | + sinks: |
| 289 | + - org.example.util.unsafe: |
| 290 | + conditions: |
| 291 | + arg2: |
| 292 | + not: [ { not: 0 }, { not: <java.lang.Integer> } ] |
| 293 | + check: arg2 |
| 294 | + marks: environment |
| 295 | + ``` |
| 296 | + |
| 297 | +Perhaps explicit `and` for `conditions` will be added in the future. |
0 commit comments