|
| 1 | +/** |
| 2 | + * @name Uncontrolled thread resource consumption from local input source |
| 3 | + * @description Using user input directly to control a thread's sleep time could lead to |
| 4 | + * performance problems or even resource exhaustion. |
| 5 | + * @kind path-problem |
| 6 | + * @id java/thread-resource-abuse |
| 7 | + * @problem.severity recommendation |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-400 |
| 10 | + */ |
| 11 | + |
| 12 | +import java |
| 13 | +import ThreadResourceAbuse |
| 14 | +import semmle.code.java.dataflow.FlowSources |
| 15 | +import DataFlow::PathGraph |
| 16 | + |
| 17 | +/** The `getInitParameter` method of servlet or JSF. */ |
| 18 | +class GetInitParameter extends Method { |
| 19 | + GetInitParameter() { |
| 20 | + ( |
| 21 | + this.getDeclaringType() |
| 22 | + .getASupertype*() |
| 23 | + .hasQualifiedName(["javax.servlet", "jakarta.servlet"], |
| 24 | + ["FilterConfig", "Registration", "ServletConfig", "ServletContext"]) or |
| 25 | + this.getDeclaringType() |
| 26 | + .getASupertype*() |
| 27 | + .hasQualifiedName(["javax.faces.context", "jakarta.faces.context"], "ExternalContext") |
| 28 | + ) and |
| 29 | + this.getName() = "getInitParameter" |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** An access to the `getInitParameter` method. */ |
| 34 | +class GetInitParameterAccess extends MethodAccess { |
| 35 | + GetInitParameterAccess() { this.getMethod() instanceof GetInitParameter } |
| 36 | +} |
| 37 | + |
| 38 | +/* Init parameter input of a Java EE web application. */ |
| 39 | +class InitParameterInput extends LocalUserInput { |
| 40 | + InitParameterInput() { this.asExpr() instanceof GetInitParameterAccess } |
| 41 | +} |
| 42 | + |
| 43 | +/** Taint configuration of uncontrolled thread resource consumption from local user input. */ |
| 44 | +class ThreadResourceAbuse extends TaintTracking::Configuration { |
| 45 | + ThreadResourceAbuse() { this = "ThreadResourceAbuse" } |
| 46 | + |
| 47 | + override predicate isSource(DataFlow::Node source) { source instanceof LocalUserInput } |
| 48 | + |
| 49 | + override predicate isSink(DataFlow::Node sink) { sink instanceof PauseThreadSink } |
| 50 | + |
| 51 | + override predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 52 | + any(AdditionalValueStep r).step(pred, succ) |
| 53 | + } |
| 54 | + |
| 55 | + override predicate isSanitizer(DataFlow::Node node) { |
| 56 | + exists( |
| 57 | + MethodAccess ma // Math.min(sleepTime, MAX_INTERVAL) |
| 58 | + | |
| 59 | + ma.getMethod().hasQualifiedName("java.lang", "Math", "min") and |
| 60 | + node.asExpr() = ma.getAnArgument() |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { |
| 65 | + guard instanceof LessThanSanitizer // if (sleepTime > 0 && sleepTime < 5000) { ... } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +from DataFlow::PathNode source, DataFlow::PathNode sink, ThreadResourceAbuse conf |
| 70 | +where conf.hasFlowPath(source, sink) |
| 71 | +select sink.getNode(), source, sink, "Possible uncontrolled resource consumption due to $@.", |
| 72 | + source.getNode(), "local user-provided value" |
0 commit comments