|
| 1 | +/** |
| 2 | + * @name Use of `Kernel.open` or `IO.read` |
| 3 | + * @description Using `Kernel.open` or `IO.read` may allow a malicious |
| 4 | + * user to execute arbitrary system commands. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity error |
| 7 | + * @security-severity 9.8 |
| 8 | + * @precision high |
| 9 | + * @id rb/kernel-open |
| 10 | + * @tags correctness |
| 11 | + * security |
| 12 | + * external/cwe/cwe-078 |
| 13 | + * external/cwe/cwe-088 |
| 14 | + */ |
| 15 | + |
| 16 | +import ruby |
| 17 | +import codeql.ruby.ApiGraphs |
| 18 | +import codeql.ruby.frameworks.StandardLibrary |
| 19 | +import codeql.ruby.TaintTracking |
| 20 | +import codeql.ruby.dataflow.BarrierGuards |
| 21 | +import codeql.ruby.dataflow.RemoteFlowSources |
| 22 | +import DataFlow::PathGraph |
| 23 | + |
| 24 | +/** |
| 25 | + * Method calls that have a suggested replacement. |
| 26 | + */ |
| 27 | +abstract class Replacement extends MethodCall { |
| 28 | + abstract string getFrom(); |
| 29 | + |
| 30 | + abstract string getTo(); |
| 31 | +} |
| 32 | + |
| 33 | +class KernelOpenCall extends KernelMethodCall, Replacement { |
| 34 | + KernelOpenCall() { this.getMethodName() = "open" } |
| 35 | + |
| 36 | + override string getFrom() { result = "Kernel.open" } |
| 37 | + |
| 38 | + override string getTo() { result = "File.open" } |
| 39 | +} |
| 40 | + |
| 41 | +class IOReadCall extends MethodCall, Replacement { |
| 42 | + IOReadCall() { this = API::getTopLevelMember("IO").getAMethodCall("read").asExpr().getExpr() } |
| 43 | + |
| 44 | + override string getFrom() { result = "IO.read" } |
| 45 | + |
| 46 | + override string getTo() { result = "File.read" } |
| 47 | +} |
| 48 | + |
| 49 | +class Configuration extends TaintTracking::Configuration { |
| 50 | + Configuration() { this = "KernelOpen" } |
| 51 | + |
| 52 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 53 | + |
| 54 | + override predicate isSink(DataFlow::Node sink) { |
| 55 | + exists(KernelOpenCall c | c.getArgument(0) = sink.asExpr().getExpr()) |
| 56 | + or |
| 57 | + exists(IOReadCall c | c.getArgument(0) = sink.asExpr().getExpr()) |
| 58 | + } |
| 59 | + |
| 60 | + override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { |
| 61 | + guard instanceof StringConstCompare or |
| 62 | + guard instanceof StringConstArrayInclusionCall |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +from |
| 67 | + Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink, |
| 68 | + DataFlow::Node sourceNode, MethodCall call |
| 69 | +where |
| 70 | + config.hasFlowPath(source, sink) and |
| 71 | + sourceNode = source.getNode() and |
| 72 | + call.getArgument(0) = sink.getNode().asExpr().getExpr() |
| 73 | +select sink.getNode(), source, sink, |
| 74 | + "This call to " + call.(Replacement).getFrom() + |
| 75 | + " depends on a user-provided value. Replace it with " + call.(Replacement).getTo() + "." |
0 commit comments