|
| 1 | +/** |
| 2 | + * Provides classes and predicates for reasoning about insecure cookie |
| 3 | + * vulnerabilities. |
| 4 | + */ |
| 5 | + |
| 6 | +import rust |
| 7 | +private import codeql.rust.dataflow.DataFlow |
| 8 | +private import codeql.rust.dataflow.FlowSource |
| 9 | +private import codeql.rust.dataflow.FlowSink |
| 10 | +private import codeql.rust.Concepts |
| 11 | +private import codeql.rust.dataflow.internal.DataFlowImpl as DataFlowImpl |
| 12 | +private import codeql.rust.dataflow.internal.Node |
| 13 | +private import codeql.rust.controlflow.BasicBlocks |
| 14 | + |
| 15 | +/** |
| 16 | + * Provides default sources, sinks and barriers for detecting insecure |
| 17 | + * cookie vulnerabilities, as well as extension points for adding your own. |
| 18 | + */ |
| 19 | +module InsecureCookie { |
| 20 | + /** |
| 21 | + * A data flow source for insecure cookie vulnerabilities. |
| 22 | + */ |
| 23 | + abstract class Source extends DataFlow::Node { } |
| 24 | + |
| 25 | + /** |
| 26 | + * A data flow sink for insecure cookie vulnerabilities. |
| 27 | + */ |
| 28 | + abstract class Sink extends QuerySink::Range { |
| 29 | + override string getSinkType() { result = "InsecureCookie" } |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * A barrier for insecure cookie vulnerabilities. |
| 34 | + */ |
| 35 | + abstract class Barrier extends DataFlow::Node { } |
| 36 | + |
| 37 | + /** |
| 38 | + * A source for insecure cookie vulnerabilities from model data. |
| 39 | + */ |
| 40 | + private class ModelsAsDataSource extends Source { |
| 41 | + ModelsAsDataSource() { sourceNode(this, "cookie-create") } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * A sink for insecure cookie vulnerabilities from model data. |
| 46 | + */ |
| 47 | + private class ModelsAsDataSink extends Sink { |
| 48 | + ModelsAsDataSink() { sinkNode(this, "cookie-use") } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Holds if a models-as-data optional barrier for cookies is specified for `summaryNode`, |
| 53 | + * with arguments `attrib` (`secure` or `partitioned`) and `arg` (argument index). For example, |
| 54 | + * if a summary has input: |
| 55 | + * ``` |
| 56 | + * [..., Argument[self].OptionalBarrier[cookie-secure-arg0], ...] |
| 57 | + * ``` |
| 58 | + * then `attrib` is `secure` and `arg` is `0`. |
| 59 | + * |
| 60 | + * The meaning here is that a call to the function summarized by `summaryNode` would set |
| 61 | + * the cookie attribute `attrib` to the value of argument `arg`. This may in practice be |
| 62 | + * interpretted as a barrier to flow (when the cookie is made secure) or as a source (when |
| 63 | + * the cookie is made insecure). |
| 64 | + */ |
| 65 | + private predicate cookieOptionalBarrier(FlowSummaryNode summaryNode, string attrib, int arg) { |
| 66 | + exists(string barrierName | |
| 67 | + DataFlowImpl::optionalBarrier(summaryNode, barrierName) and |
| 68 | + attrib = barrierName.regexpCapture("cookie-(secure|partitioned)-arg([0-9]+)", 1) and |
| 69 | + arg = barrierName.regexpCapture("cookie-(secure|partitioned)-arg([0-9]+)", 2).toInt() |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Holds if cookie attribute `attrib` (`secure` or `partitioned`) is set to `value` |
| 75 | + * (`true` or `false`) at `node`. For example, the call: |
| 76 | + * ``` |
| 77 | + * cookie.secure(true) |
| 78 | + * ``` |
| 79 | + * sets the `"secure"` attribute to `true`. A value that cannot be determined is treated |
| 80 | + * as `false`. |
| 81 | + */ |
| 82 | + predicate cookieSetNode(DataFlow::Node node, string attrib, boolean value) { |
| 83 | + exists(FlowSummaryNode summaryNode, CallExprBase ce, int arg, DataFlow::Node argNode | |
| 84 | + // decode the models-as-data `OptionalBarrier` |
| 85 | + cookieOptionalBarrier(summaryNode, attrib, arg) and |
| 86 | + // find a call and arg referenced by this optional barrier |
| 87 | + ce.getStaticTarget() = summaryNode.getSummarizedCallable() and |
| 88 | + ce.getArg(arg) = argNode.asExpr().getExpr() and |
| 89 | + // check if the argument is always `true` |
| 90 | + ( |
| 91 | + if |
| 92 | + forex(DataFlow::Node argSourceNode, BooleanLiteralExpr argSourceValue | |
| 93 | + DataFlow::localFlow(argSourceNode, argNode) and |
| 94 | + argSourceValue = argSourceNode.asExpr().getExpr() |
| 95 | + | |
| 96 | + argSourceValue.getTextValue() = "true" |
| 97 | + ) |
| 98 | + then value = true // `true` flows to here |
| 99 | + else value = false // `false`, unknown, or multiple values |
| 100 | + ) and |
| 101 | + // and find the node where this happens (we can't just use the flow summary node, since its |
| 102 | + // shared across all calls to the modeled function, we need a node specific to this call) |
| 103 | + ( |
| 104 | + node.asExpr().getExpr() = ce.(MethodCallExpr).getReceiver() // e.g. `a` in `a.set_secure(true)` |
| 105 | + or |
| 106 | + exists(BasicBlock bb, int i | |
| 107 | + // associated SSA node |
| 108 | + node.(SsaNode).asDefinition().definesAt(_, bb, i) and |
| 109 | + ce.(MethodCallExpr).getReceiver() = bb.getNode(i).getAstNode() |
| 110 | + ) |
| 111 | + ) |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
0 commit comments