|
| 1 | +/** |
| 2 | + * @name Denial of Service using unconstrained integer/float value |
| 3 | + * @description A remote user-controlled integer/float value can reach a condition that controls how many times a repeatable operation can be executed. A malicious user may misuse that value to cause an application-level denial of service. |
| 4 | + * @kind path-problem |
| 5 | + * @id githubsecuritylab/user-controlled-max-iterations |
| 6 | + * @precision high |
| 7 | + * @problem.severity error |
| 8 | + * @tags security |
| 9 | + * experimental |
| 10 | + * external/cwe/cwe-770 |
| 11 | + */ |
| 12 | + |
| 13 | +import ruby |
| 14 | +import codeql.ruby.ApiGraphs |
| 15 | +import codeql.ruby.Concepts |
| 16 | +import codeql.ruby.TaintTracking |
| 17 | +import codeql.ruby.dataflow.RemoteFlowSources |
| 18 | +import codeql.ruby.dataflow.BarrierGuards |
| 19 | +import codeql.ruby.AST |
| 20 | +import codeql.ruby.controlflow.CfgNodes as CfgNodes |
| 21 | +import codeql.ruby.CFG |
| 22 | +import codeql.ruby.dataflow.internal.DataFlowPublic |
| 23 | +import codeql.ruby.InclusionTests |
| 24 | + |
| 25 | +/** |
| 26 | + * Ensure that the user-provided value is limited to a certain amount |
| 27 | + * |
| 28 | + * @param node The node to check if limited by: |
| 29 | + * 1. A comparison operation node with a less than or less than or equal operator |
| 30 | + * 2. A comparison operation node with a greater than or greater than or equal operator |
| 31 | + * 3. A comparison operation node with a greater than or greater than or equal operator and the branch is false |
| 32 | + * 4. A comparison operation node with a less than or less than or equal operator and the branch is false |
| 33 | + */ |
| 34 | +predicate underAValue(CfgNodes::AstCfgNode g, CfgNode node, boolean branch) { |
| 35 | + exists(CfgNodes::ExprNodes::ComparisonOperationCfgNode cn | cn = g | |
| 36 | + exists(string op | |
| 37 | + ( |
| 38 | + // arg <= LIMIT OR arg < LIMIT |
| 39 | + (op = "<" or op = "<=") and |
| 40 | + branch = true and |
| 41 | + op = cn.getOperator() and |
| 42 | + node = cn.getLeftOperand() |
| 43 | + or |
| 44 | + // LIMIT >= arg OR LIMIT > arg |
| 45 | + (op = ">" or op = ">=") and |
| 46 | + branch = true and |
| 47 | + op = cn.getOperator() and |
| 48 | + node = cn.getRightOperand() |
| 49 | + or |
| 50 | + // not arg >= LIMIT OR not arg > LIMIT |
| 51 | + (op = ">" or op = ">=") and |
| 52 | + branch = false and |
| 53 | + op = cn.getOperator() and |
| 54 | + node = cn.getLeftOperand() |
| 55 | + or |
| 56 | + // not LIMIT <= arg OR not LIMIT < argo |
| 57 | + (op = "<" or op = "<=") and |
| 58 | + branch = false and |
| 59 | + op = cn.getOperator() and |
| 60 | + node = cn.getRightOperand() |
| 61 | + ) |
| 62 | + ) |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Sidekiq ensure using the `params` function that all keys in the resulting hash are strings and ingest `request.params`. So a call to `params` function is considered as a remote flow source. |
| 68 | + * |
| 69 | + * https://github.com/sidekiq/sidekiq/blob/79d254d9045bb5805beed6aaffec1886ef89f71b/lib/sidekiq/web/action.rb#L30-L37 |
| 70 | + */ |
| 71 | +class ParamsRFS extends RemoteFlowSource::Range { |
| 72 | + ParamsRFS() { |
| 73 | + exists(ElementReference er, MethodCall mc | |
| 74 | + er.getReceiver() = mc and |
| 75 | + mc.getMethodName() = "params" and |
| 76 | + this.asExpr() = er.getAControlFlowNode() |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + override string getSourceType() { result = "Request params data" } |
| 81 | +} |
| 82 | + |
| 83 | +private module DoSConfig implements DataFlow::ConfigSig { |
| 84 | + predicate isSource(DataFlow::Node source) { |
| 85 | + //source instanceof ParamsRFS or |
| 86 | + source instanceof RemoteFlowSource |
| 87 | + } |
| 88 | + |
| 89 | + predicate isBarrier(DataFlow::Node sanitizer) { |
| 90 | + // Sanitize the user-provided value if limited (underAValue check) |
| 91 | + sanitizer = DataFlow::BarrierGuard<underAValue/3>::getABarrierNode() |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Support additional flow step for a case like using a default value `(params["days"] | 100).to_i` |
| 96 | + */ |
| 97 | + predicate isAdditionalFlowStep(DataFlow::Node previousNode, DataFlow::Node nextNode) { |
| 98 | + // Additional flow step like `(RFS | INTEGER).to_i` or `(FLOAT | RFS).to_f` |
| 99 | + exists(ParenthesizedExpr loe, DataFlow::CallNode c, ExprNode en | |
| 100 | + en = c.getReceiver() and |
| 101 | + c.getMethodName() = ["to_i", "to_f"] and |
| 102 | + c = nextNode and |
| 103 | + loe = en.asExpr().getExpr() and |
| 104 | + loe.getStmt(_).(LogicalOrExpr).getAnOperand() = previousNode.asExpr().getExpr() and |
| 105 | + not previousNode.asExpr().getExpr() instanceof IntegerLiteral |
| 106 | + ) |
| 107 | + or |
| 108 | + // Additional flow step like `RFS.to_i` or `RFS.to_f` |
| 109 | + exists(MethodCall mc | |
| 110 | + mc.getReceiver() = previousNode.asExpr().getExpr() and |
| 111 | + mc.getMethodName() = ["to_i", "to_f"] and |
| 112 | + mc = nextNode.asExpr().getExpr() |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * - Check if the user-provided value is used in a repeatable operation using `downto`, `upto` or `times` |
| 118 | + * - Check if the user-provided value is used in a repeatable operation using `for` loop or conditional loop like `until` or `while`. |
| 119 | + */ |
| 120 | + predicate isSink(DataFlow::Node sink) { |
| 121 | + // sink = n in `100.downto(n)` or `1.upto(n)` |
| 122 | + exists(MethodCall mc | |
| 123 | + sink.asExpr().getExpr() = mc.getArgument(0) and |
| 124 | + mc.getMethodName() = ["downto", "upto"] |
| 125 | + ) |
| 126 | + or |
| 127 | + // sink = n in `n.times()` or `n.downto(1)` or `n.upto(100)` |
| 128 | + exists(MethodCall mc | |
| 129 | + sink.asExpr().getExpr() = mc.getReceiver() and |
| 130 | + mc.getMethodName() = ["times", "downto", "upto"] |
| 131 | + ) |
| 132 | + or |
| 133 | + // sink = 1..n in `for i in 0..n` |
| 134 | + exists(ForExpr forLoop | sink.asExpr().getExpr() = forLoop.getValue()) |
| 135 | + or |
| 136 | + // sink = n in `until n` |
| 137 | + exists(ConditionalLoop conditionalLoop | |
| 138 | + sink.asExpr().getExpr() = conditionalLoop.getCondition() |
| 139 | + ) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +module DoSFlow = TaintTracking::Global<DoSConfig>; |
| 144 | + |
| 145 | +import DoSFlow::PathGraph |
| 146 | + |
| 147 | +from DoSFlow::PathNode source, DoSFlow::PathNode sink |
| 148 | +where DoSFlow::flowPath(source, sink) |
| 149 | +select sink.getNode(), source, sink, "This $@ can control $@ a repeatable operation is executed.", |
| 150 | + source.getNode(), "user-provided value", sink.getNode(), "how many times" |
0 commit comments