|
| 1 | +/** |
| 2 | + * @name User-controlled filename in archive library |
| 3 | + * @description User-controlled data that flows into File I/O of archive libraries could be dangerous |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity error |
| 6 | + * @security-severity 7.0 |
| 7 | + * @precision medium |
| 8 | + * @id rb/user-controlled-path-traversal-archive-library |
| 9 | + * @tags security external/cwe/cwe-22 |
| 10 | + */ |
| 11 | + |
| 12 | +import ruby |
| 13 | +import codeql.ruby.ApiGraphs |
| 14 | +import codeql.ruby.DataFlow |
| 15 | +import codeql.ruby.dataflow.RemoteFlowSources |
| 16 | +import codeql.ruby.dataflow.BarrierGuards |
| 17 | +import codeql.ruby.TaintTracking |
| 18 | +import DataFlow::PathGraph |
| 19 | + |
| 20 | +class ArchiveApiFileOpen extends DataFlow::Node { |
| 21 | + |
| 22 | + // this should find the first argument of Zlib::Inflate.inflate or Zip::File.extract |
| 23 | + ArchiveApiFileOpen() { |
| 24 | + this instanceof RubyZipFileOpen or |
| 25 | + this instanceof TarReaderFileOpen |
| 26 | + } |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +class RubyZipFileOpen extends DataFlow::Node { |
| 31 | + // find any use of Zip::File.open() |
| 32 | + RubyZipFileOpen() { |
| 33 | + this = API::getTopLevelMember("Zip").getMember("File").getAMethodCall("open").getArgument(0) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class TarReaderFileOpen extends DataFlow::Node { |
| 38 | + // this should find a use of File.open() in context of a block opened in context of Gem::Package::TarReader.new() |
| 39 | + TarReaderFileOpen() { |
| 40 | + this = API::getTopLevelMember("File").getAMethodCall("open").getArgument(0) and |
| 41 | + this.asExpr().getExpr().getParent+() = API::getTopLevelMember("Gem").getMember("Package").getMember("TarReader").getAnInstantiation().asExpr().getExpr() |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class Configuration extends TaintTracking::Configuration { |
| 46 | + Configuration() { this = "ArchiveApiFileOpen" } |
| 47 | + |
| 48 | + // this predicate will be used to contstrain our query to find instances where only remote user-controlled data flows to the sink |
| 49 | + override predicate isSource(DataFlow::Node source) { |
| 50 | + source instanceof RemoteFlowSource |
| 51 | + } |
| 52 | + |
| 53 | + // our Decompression APIs defined above will the the sinks we use for this query |
| 54 | + override predicate isSink(DataFlow::Node sink) { |
| 55 | + sink instanceof ArchiveApiFileOpen |
| 56 | + } |
| 57 | + |
| 58 | + // I think it would also be helpful to reduce false positives by adding a simple sanitizer config in the event |
| 59 | + // that the code first checks the file name against a string literal or array of string literals before calling |
| 60 | + // the decompression API |
| 61 | + override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { |
| 62 | + guard instanceof StringConstCompare or |
| 63 | + guard instanceof StringConstArrayInclusionCall |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | +from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink |
| 69 | +where |
| 70 | + config.hasFlowPath(source, sink) |
| 71 | +select sink.getNode(), source, sink, "This call to $@ appears to extract an archive using user-controlled data $@ to set the filename. If the filename is not properly handled, they could end up writing to unintended places in the filesystem.", sink.getNode().asExpr().getExpr().getParent().toString(), sink.getNode().asExpr().getExpr().getParent().toString(), source.toString(), source.toString() |
0 commit comments