|
| 1 | +/** |
| 2 | + * @name Injection in Java Script Engine |
| 3 | + * @description Evaluation of user-controlled data using the Java Script Engine may |
| 4 | + * lead to remote code execution. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity error |
| 7 | + * @precision high |
| 8 | + * @id java/unsafe-eval |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-094 |
| 11 | + */ |
| 12 | + |
| 13 | +import java |
| 14 | +import semmle.code.java.dataflow.FlowSources |
| 15 | +import DataFlow::PathGraph |
| 16 | + |
| 17 | +/** A method of ScriptEngine that allows code injection. */ |
| 18 | +class ScriptEngineMethod extends Method { |
| 19 | + ScriptEngineMethod() { |
| 20 | + this.getDeclaringType().getASupertype*().hasQualifiedName("javax.script", "ScriptEngine") and |
| 21 | + this.hasName("eval") |
| 22 | + or |
| 23 | + this.getDeclaringType().getASupertype*().hasQualifiedName("javax.script", "Compilable") and |
| 24 | + this.hasName("compile") |
| 25 | + or |
| 26 | + this.getDeclaringType().getASupertype*().hasQualifiedName("javax.script", "ScriptEngineFactory") and |
| 27 | + this.hasName(["getProgram", "getMethodCallSyntax"]) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** The context class `org.mozilla.javascript.Context` of Rhino Java Script Engine. */ |
| 32 | +class RhinoContext extends RefType { |
| 33 | + RhinoContext() { this.hasQualifiedName("org.mozilla.javascript", "Context") } |
| 34 | +} |
| 35 | + |
| 36 | +/** A method that evaluates a Rhino expression with `org.mozilla.javascript.Context`. */ |
| 37 | +class RhinoEvaluateExpressionMethod extends Method { |
| 38 | + RhinoEvaluateExpressionMethod() { |
| 39 | + this.getDeclaringType().getAnAncestor*() instanceof RhinoContext and |
| 40 | + this.hasName([ |
| 41 | + "evaluateString", "evaluateReader", "compileFunction", "compileReader", "compileString" |
| 42 | + ]) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * A method that compiles a Rhino expression with |
| 48 | + * `org.mozilla.javascript.optimizer.ClassCompiler`. |
| 49 | + */ |
| 50 | +class RhinoCompileClassMethod extends Method { |
| 51 | + RhinoCompileClassMethod() { |
| 52 | + this.getDeclaringType() |
| 53 | + .getASupertype*() |
| 54 | + .hasQualifiedName("org.mozilla.javascript.optimizer", "ClassCompiler") and |
| 55 | + this.hasName("compileToClassFiles") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * A method that defines a Java class from a Rhino expression with |
| 61 | + * `org.mozilla.javascript.GeneratedClassLoader`. |
| 62 | + */ |
| 63 | +class RhinoDefineClassMethod extends Method { |
| 64 | + RhinoDefineClassMethod() { |
| 65 | + this.getDeclaringType() |
| 66 | + .getASupertype*() |
| 67 | + .hasQualifiedName("org.mozilla.javascript", "GeneratedClassLoader") and |
| 68 | + this.hasName("defineClass") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Holds if `ma` is a call to a `ScriptEngineMethod` and `sink` is an argument that |
| 74 | + * will be executed. |
| 75 | + */ |
| 76 | +predicate isScriptArgument(MethodAccess ma, Expr sink) { |
| 77 | + exists(ScriptEngineMethod m | |
| 78 | + m = ma.getMethod() and |
| 79 | + if m.getDeclaringType().getASupertype*().hasQualifiedName("javax.script", "ScriptEngineFactory") |
| 80 | + then sink = ma.getArgument(_) // all arguments allow script injection |
| 81 | + else sink = ma.getArgument(0) |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Holds if a Rhino expression evaluation method is vulnerable to code injection. |
| 87 | + */ |
| 88 | +predicate evaluatesRhinoExpression(MethodAccess ma, Expr sink) { |
| 89 | + exists(RhinoEvaluateExpressionMethod m | m = ma.getMethod() | |
| 90 | + ( |
| 91 | + if ma.getMethod().getName() = "compileReader" |
| 92 | + then sink = ma.getArgument(0) // The first argument is the input reader |
| 93 | + else sink = ma.getArgument(1) // The second argument is the JavaScript or Java input |
| 94 | + ) and |
| 95 | + not exists(MethodAccess ca | |
| 96 | + ca.getMethod().hasName(["initSafeStandardObjects", "setClassShutter"]) and // safe mode or `ClassShutter` constraint is enforced |
| 97 | + ma.getQualifier() = ca.getQualifier().(VarAccess).getVariable().getAnAccess() |
| 98 | + ) |
| 99 | + ) |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Holds if a Rhino expression compilation method is vulnerable to code injection. |
| 104 | + */ |
| 105 | +predicate compilesScript(MethodAccess ma, Expr sink) { |
| 106 | + exists(RhinoCompileClassMethod m | m = ma.getMethod() | sink = ma.getArgument(0)) |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Holds if a Rhino class loading method is vulnerable to code injection. |
| 111 | + */ |
| 112 | +predicate definesRhinoClass(MethodAccess ma, Expr sink) { |
| 113 | + exists(RhinoDefineClassMethod m | m = ma.getMethod() | sink = ma.getArgument(1)) |
| 114 | +} |
| 115 | + |
| 116 | +/** A script injection sink. */ |
| 117 | +class ScriptInjectionSink extends DataFlow::ExprNode { |
| 118 | + MethodAccess methodAccess; |
| 119 | + |
| 120 | + ScriptInjectionSink() { |
| 121 | + isScriptArgument(methodAccess, this.getExpr()) or |
| 122 | + evaluatesRhinoExpression(methodAccess, this.getExpr()) or |
| 123 | + compilesScript(methodAccess, this.getExpr()) or |
| 124 | + definesRhinoClass(methodAccess, this.getExpr()) |
| 125 | + } |
| 126 | + |
| 127 | + /** An access to the method associated with this sink. */ |
| 128 | + MethodAccess getMethodAccess() { result = methodAccess } |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * A taint tracking configuration that tracks flow from `RemoteFlowSource` to an argument |
| 133 | + * of a method call that executes injected script. |
| 134 | + */ |
| 135 | +class ScriptInjectionConfiguration extends TaintTracking::Configuration { |
| 136 | + ScriptInjectionConfiguration() { this = "ScriptInjectionConfiguration" } |
| 137 | + |
| 138 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 139 | + |
| 140 | + override predicate isSink(DataFlow::Node sink) { sink instanceof ScriptInjectionSink } |
| 141 | +} |
| 142 | + |
| 143 | +from DataFlow::PathNode source, DataFlow::PathNode sink, ScriptInjectionConfiguration conf |
| 144 | +where conf.hasFlowPath(source, sink) |
| 145 | +select sink.getNode().(ScriptInjectionSink).getMethodAccess(), source, sink, |
| 146 | + "Java Script Engine evaluate $@.", source.getNode(), "user input" |
0 commit comments