|
| 1 | +/** |
| 2 | + * Provides models for C++ containers `std::map` and `std::unordered_map`. |
| 3 | + */ |
| 4 | + |
| 5 | +import semmle.code.cpp.models.interfaces.Taint |
| 6 | +import semmle.code.cpp.models.implementations.Iterator |
| 7 | + |
| 8 | +/** |
| 9 | + * The standard map `insert` and `insert_or_assign` functions. |
| 10 | + */ |
| 11 | +class StdMapInsert extends TaintFunction { |
| 12 | + StdMapInsert() { |
| 13 | + this.hasQualifiedName("std", ["map", "unordered_map"], ["insert", "insert_or_assign"]) |
| 14 | + } |
| 15 | + |
| 16 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 17 | + // flow from last parameter to qualifier and return value |
| 18 | + // (where the return value is a pair, this should really flow just to the first part of it) |
| 19 | + input.isParameterDeref(getNumberOfParameters() - 1) and |
| 20 | + ( |
| 21 | + output.isQualifierObject() or |
| 22 | + output.isReturnValue() |
| 23 | + ) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * The standard map `swap` functions. |
| 29 | + */ |
| 30 | +class StdMapSwap extends TaintFunction { |
| 31 | + StdMapSwap() { this.hasQualifiedName("std", ["map", "unordered_map"], "swap") } |
| 32 | + |
| 33 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 34 | + // container1.swap(container2) |
| 35 | + input.isQualifierObject() and |
| 36 | + output.isParameterDeref(0) |
| 37 | + or |
| 38 | + input.isParameterDeref(0) and |
| 39 | + output.isQualifierObject() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * The standard map functions `at` and `operator[]`. |
| 45 | + */ |
| 46 | +class StdMapAt extends TaintFunction { |
| 47 | + StdMapAt() { this.hasQualifiedName("std", ["map", "unordered_map"], ["at", "operator[]"]) } |
| 48 | + |
| 49 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 50 | + // flow from qualifier to referenced return value |
| 51 | + input.isQualifierObject() and |
| 52 | + output.isReturnValueDeref() |
| 53 | + or |
| 54 | + // reverse flow from returned reference to the qualifier |
| 55 | + input.isReturnValueDeref() and |
| 56 | + output.isQualifierObject() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * The standard map `find` function. |
| 62 | + */ |
| 63 | +class StdMapFind extends TaintFunction { |
| 64 | + StdMapFind() { this.hasQualifiedName("std", ["map", "unordered_map"], "find") } |
| 65 | + |
| 66 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 67 | + input.isQualifierObject() and |
| 68 | + output.isReturnValue() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * The standard map `erase` function. |
| 74 | + */ |
| 75 | +class StdMapErase extends TaintFunction { |
| 76 | + StdMapErase() { this.hasQualifiedName("std", ["map", "unordered_map"], "erase") } |
| 77 | + |
| 78 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 79 | + // flow from qualifier to iterator return value |
| 80 | + getType().getUnderlyingType() instanceof Iterator and |
| 81 | + input.isQualifierObject() and |
| 82 | + output.isReturnValue() |
| 83 | + } |
| 84 | +} |
0 commit comments