|
| 1 | +/** |
| 2 | + * Provides classes and predicates for reasoning about polynomial-time |
| 3 | + * regular-expression denial-of-service (ReDoS) vulnerabilities in C++. |
| 4 | + * |
| 5 | + * The library mirrors the Java `PolynomialReDoSQuery` library: it plugs the |
| 6 | + * C++ regex parse-tree view (`semmle.code.cpp.regex.RegexTreeView`) into the |
| 7 | + * shared `SuperlinearBackTracking` analysis, and defines a data-flow |
| 8 | + * configuration that tracks user-controlled data to a subject expression |
| 9 | + * that is matched against a `std::regex` whose parse tree contains a |
| 10 | + * polynomial-backtracking term. |
| 11 | + */ |
| 12 | + |
| 13 | +private import cpp |
| 14 | +private import semmle.code.cpp.regex.RegexTreeView::RegexTreeView as TreeView |
| 15 | +import codeql.regex.nfa.SuperlinearBackTracking::Make<TreeView> as SuperlinearBackTracking |
| 16 | +private import semmle.code.cpp.ir.dataflow.DataFlow |
| 17 | +private import semmle.code.cpp.ir.dataflow.TaintTracking |
| 18 | +private import semmle.code.cpp.regex.RegexFlowConfigs |
| 19 | +private import semmle.code.cpp.security.FlowSources |
| 20 | + |
| 21 | +/** |
| 22 | + * A sink for the polynomial ReDoS query: a subject expression that is |
| 23 | + * matched against a `std::regex` whose pattern is a `TreeView::RegExpLiteral`. |
| 24 | + */ |
| 25 | +class PolynomialRedosSink extends DataFlow::Node { |
| 26 | + TreeView::RegExpLiteral reg; |
| 27 | + |
| 28 | + PolynomialRedosSink() { |
| 29 | + exists(Expr e | |
| 30 | + regexMatchedAgainst(reg.getRegex(), e) and |
| 31 | + (this.asExpr() = e or this.asIndirectExpr() = e) |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + /** Gets a regex term (a child of the matched literal) associated with this sink. */ |
| 36 | + TreeView::RegExpTerm getRegExp() { result.getParent() = reg } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A function whose result typically has a limited length, such as HTTP |
| 41 | + * headers, cookies, request URIs, or their C++ analogues. Values derived |
| 42 | + * from calls to such functions are treated as length-restricted and act as |
| 43 | + * barriers for the polynomial ReDoS analysis. |
| 44 | + * |
| 45 | + * This is a conservative, name-based heuristic: it matches functions whose |
| 46 | + * unqualified name (or declaring class name for member functions) suggests |
| 47 | + * that the returned string is bounded in length in practice. |
| 48 | + */ |
| 49 | +private class LengthRestrictedFunction extends Function { |
| 50 | + LengthRestrictedFunction() { |
| 51 | + exists(string n | n = this.getName().toLowerCase() | |
| 52 | + n.matches(["%header%", "%cookie%", "%requesturi%", "%requesturl%", "%useragent%"]) |
| 53 | + ) |
| 54 | + or |
| 55 | + exists(MemberFunction mf, string cls, string n | |
| 56 | + mf = this and |
| 57 | + cls = mf.getDeclaringType().getName().toLowerCase() and |
| 58 | + n = mf.getName().toLowerCase() |
| 59 | + | |
| 60 | + cls.matches("%cookie%") and n.matches("get%") |
| 61 | + or |
| 62 | + cls.matches("%request%") and n.matches(["%get%path%", "get%user%", "%querystring%"]) |
| 63 | + ) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Holds if `node` is a value whose static type has a small, fixed size, so |
| 69 | + * that it is treated as length-restricted for the polynomial ReDoS |
| 70 | + * analysis. This includes integral and floating-point values, which cannot |
| 71 | + * usefully be matched against a regex. |
| 72 | + */ |
| 73 | +private predicate isSmallFixedSizeType(DataFlow::Node node) { |
| 74 | + node.asExpr().getUnspecifiedType() instanceof IntegralType |
| 75 | + or |
| 76 | + node.asExpr().getUnspecifiedType() instanceof FloatingPointType |
| 77 | +} |
| 78 | + |
| 79 | +/** A configuration for the polynomial ReDoS query. */ |
| 80 | +module PolynomialRedosConfig implements DataFlow::ConfigSig { |
| 81 | + predicate isSource(DataFlow::Node source) { source instanceof FlowSource } |
| 82 | + |
| 83 | + predicate isSink(DataFlow::Node sink) { |
| 84 | + exists(SuperlinearBackTracking::PolynomialBackTrackingTerm regexp | |
| 85 | + regexp.getRootTerm() = sink.(PolynomialRedosSink).getRegExp() |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + predicate isBarrier(DataFlow::Node node) { |
| 90 | + isSmallFixedSizeType(node) |
| 91 | + or |
| 92 | + node.asExpr().(Call).getTarget() instanceof LengthRestrictedFunction |
| 93 | + } |
| 94 | + |
| 95 | + predicate observeDiffInformedIncrementalMode() { any() } |
| 96 | + |
| 97 | + Location getASelectedSinkLocation(DataFlow::Node sink) { |
| 98 | + exists(SuperlinearBackTracking::PolynomialBackTrackingTerm regexp | |
| 99 | + regexp.getRootTerm() = sink.(PolynomialRedosSink).getRegExp() |
| 100 | + | |
| 101 | + result = sink.getLocation() |
| 102 | + or |
| 103 | + result = regexp.getLocation() |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** Taint-tracking flow from user input to a polynomial-backtracking regex match. */ |
| 109 | +module PolynomialRedosFlow = TaintTracking::Global<PolynomialRedosConfig>; |
0 commit comments