|
| 1 | +private import codeql.util.Unit |
| 2 | +private import rust |
| 3 | +private import rust as R |
| 4 | +private import codeql.rust.dataflow.DataFlow |
| 5 | +private import codeql.rust.dataflow.internal.DataFlowImpl |
| 6 | +private import codeql.rust.dataflow.internal.TaintTrackingImpl |
| 7 | +private import codeql.mad.modelgenerator.internal.ModelGeneratorImpl |
| 8 | +private import codeql.rust.dataflow.internal.FlowSummaryImpl as FlowSummary |
| 9 | + |
| 10 | +module ModelGeneratorInput implements ModelGeneratorInputSig<Location, RustDataFlow> { |
| 11 | + // NOTE: We are not using type information for now. |
| 12 | + class Type = Unit; |
| 13 | + |
| 14 | + class Parameter = R::ParamBase; |
| 15 | + |
| 16 | + class Callable = R::Callable; |
| 17 | + |
| 18 | + class NodeExtended extends DataFlow::Node { |
| 19 | + Callable getAsExprEnclosingCallable() { result = this.asExpr().getScope() } |
| 20 | + |
| 21 | + Type getType() { any() } |
| 22 | + |
| 23 | + Callable getEnclosingCallable() { |
| 24 | + result = this.(Node::Node).getEnclosingCallable().asCfgScope() |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private predicate relevant(Function api) { |
| 29 | + // This excludes closures (these are not exported API endpoints) and |
| 30 | + // functions without a `pub` visiblity. A function can be `pub` without |
| 31 | + // ultimately being exported by a crate, so this is an overapproximation. |
| 32 | + api.hasVisibility() |
| 33 | + or |
| 34 | + // If a method implements a public trait it is exposed through the trait. |
| 35 | + // We overapproximate this by including all trait method implementations. |
| 36 | + exists(Impl impl | impl.hasTrait() and impl.getAssocItemList().getAssocItem(_) = api) |
| 37 | + } |
| 38 | + |
| 39 | + predicate isUninterestingForDataFlowModels(Callable api) { none() } |
| 40 | + |
| 41 | + predicate isUninterestingForHeuristicDataFlowModels(Callable api) { none() } |
| 42 | + |
| 43 | + class SourceOrSinkTargetApi extends Callable { |
| 44 | + SourceOrSinkTargetApi() { relevant(this) } |
| 45 | + } |
| 46 | + |
| 47 | + class SinkTargetApi extends SourceOrSinkTargetApi { } |
| 48 | + |
| 49 | + class SourceTargetApi extends SourceOrSinkTargetApi { } |
| 50 | + |
| 51 | + class SummaryTargetApi extends Callable { |
| 52 | + private Callable lift; |
| 53 | + |
| 54 | + SummaryTargetApi() { |
| 55 | + lift = this and |
| 56 | + relevant(this) |
| 57 | + } |
| 58 | + |
| 59 | + Callable lift() { result = lift } |
| 60 | + |
| 61 | + predicate isRelevant() { relevant(this) } |
| 62 | + } |
| 63 | + |
| 64 | + predicate isRelevantType(Type t) { any() } |
| 65 | + |
| 66 | + /** |
| 67 | + * Gets the underlying type of the content `c`. |
| 68 | + */ |
| 69 | + Type getUnderlyingContentType(DataFlow::ContentSet c) { result = any(Type t) and exists(c) } |
| 70 | + |
| 71 | + string qualifierString() { result = "Argument[self]" } |
| 72 | + |
| 73 | + string parameterAccess(R::ParamBase p) { |
| 74 | + result = "Argument[" + any(ParameterPosition pos | p = pos.getParameterIn(_)).toString() + "]" |
| 75 | + } |
| 76 | + |
| 77 | + string parameterContentAccess(R::ParamBase p) { result = parameterAccess(p) } |
| 78 | + |
| 79 | + class InstanceParameterNode extends DataFlow::ParameterNode { |
| 80 | + InstanceParameterNode() { this.asParameter() instanceof SelfParam } |
| 81 | + } |
| 82 | + |
| 83 | + bindingset[c] |
| 84 | + string paramReturnNodeAsOutput(R::Callable c, ParameterPosition pos) { |
| 85 | + // TODO: Implement this to support returning through parameters. |
| 86 | + result = "paramReturnNodeAsOutput(" + c + ", " + pos + ")" |
| 87 | + } |
| 88 | + |
| 89 | + bindingset[c] |
| 90 | + string paramReturnNodeAsContentOutput(Callable c, ParameterPosition pos) { |
| 91 | + // TODO: Implement this to support returning through parameters. |
| 92 | + result = "paramReturnNodeAsContentOutput(" + c + ", " + pos + ")" |
| 93 | + } |
| 94 | + |
| 95 | + Callable returnNodeEnclosingCallable(DataFlow::Node ret) { |
| 96 | + result = ret.(Node::Node).getEnclosingCallable().asCfgScope() |
| 97 | + } |
| 98 | + |
| 99 | + predicate isOwnInstanceAccessNode(RustDataFlow::ReturnNode node) { |
| 100 | + // This is probably not relevant to implement for Rust, as we only use |
| 101 | + // `captureMixedFlow` which doesn't explicitly distinguish between |
| 102 | + // functions that return `self` and those that don't. |
| 103 | + none() |
| 104 | + } |
| 105 | + |
| 106 | + predicate sinkModelSanitizer(DataFlow::Node node) { none() } |
| 107 | + |
| 108 | + predicate apiSource(DataFlow::Node source) { none() } |
| 109 | + |
| 110 | + bindingset[sourceEnclosing, api] |
| 111 | + predicate irrelevantSourceSinkApi(Callable sourceEnclosing, SourceTargetApi api) { none() } |
| 112 | + |
| 113 | + string getInputArgument(DataFlow::Node source) { |
| 114 | + // TODO: Implement when we want to generate sources and sinks |
| 115 | + result = "getInputArgument(" + source + ")" |
| 116 | + } |
| 117 | + |
| 118 | + bindingset[kind] |
| 119 | + predicate isRelevantSinkKind(string kind) { any() } |
| 120 | + |
| 121 | + bindingset[kind] |
| 122 | + predicate isRelevantSourceKind(string kind) { any() } |
| 123 | + |
| 124 | + predicate containerContent(DataFlow::ContentSet c) { |
| 125 | + c.(SingletonContentSet).getContent() instanceof ElementContent |
| 126 | + } |
| 127 | + |
| 128 | + predicate isAdditionalContentFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { none() } |
| 129 | + |
| 130 | + predicate isField(DataFlow::ContentSet c) { |
| 131 | + c.(SingletonContentSet).getContent() instanceof StructFieldContent |
| 132 | + } |
| 133 | + |
| 134 | + predicate isCallback(DataFlow::ContentSet c) { none() } |
| 135 | + |
| 136 | + string getSyntheticName(DataFlow::ContentSet c) { none() } |
| 137 | + |
| 138 | + string printContent(DataFlow::ContentSet cs) { |
| 139 | + exists(string arg | result = FlowSummary::Input::encodeContent(cs, arg) + "[" + arg + "]") |
| 140 | + } |
| 141 | + |
| 142 | + string partialModelRow(Callable api, int i) { |
| 143 | + i = 0 and |
| 144 | + ( |
| 145 | + result = api.(Function).getCrateOrigin() |
| 146 | + or |
| 147 | + not api.(Function).hasCrateOrigin() and result = "" |
| 148 | + ) // crate |
| 149 | + or |
| 150 | + i = 1 and result = api.(Function).getExtendedCanonicalPath() // name |
| 151 | + } |
| 152 | + |
| 153 | + string partialNeutralModelRow(Callable api, int i) { result = partialModelRow(api, i) } |
| 154 | + |
| 155 | + // TODO: Implement this when we want to generate sources. |
| 156 | + predicate sourceNode(DataFlow::Node node, string kind) { none() } |
| 157 | + |
| 158 | + // TODO: Implement this when we want to generate sinks. |
| 159 | + predicate sinkNode(DataFlow::Node node, string kind) { none() } |
| 160 | +} |
| 161 | + |
| 162 | +import MakeModelGenerator<Location, RustDataFlow, RustTaintTracking, ModelGeneratorInput> |
0 commit comments