|
| 1 | +/** |
| 2 | + * @name Arbitrary file write during tarfile extraction |
| 3 | + * @description Extracting files from a malicious tar archive without validating that the |
| 4 | + * destination file path is within the destination directory can cause files outside |
| 5 | + * the destination directory to be overwritten. |
| 6 | + * @kind path-problem |
| 7 | + * @id py/tarslip |
| 8 | + * @problem.severity error |
| 9 | + * @precision medium |
| 10 | + * @tags security |
| 11 | + * external/cwe/cwe-022 |
| 12 | + */ |
| 13 | + |
| 14 | +import python |
| 15 | +import semmle.python.security.Paths |
| 16 | +import semmle.python.dataflow.TaintTracking |
| 17 | +import semmle.python.security.strings.Basic |
| 18 | + |
| 19 | +/** A TaintKind to represent open tarfile objects. That is, the result of calling `tarfile.open(...)` */ |
| 20 | +class OpenTarFile extends TaintKind { |
| 21 | + OpenTarFile() { this = "tarfile.open" } |
| 22 | + |
| 23 | + override TaintKind getTaintOfMethodResult(string name) { |
| 24 | + name = "getmember" and result instanceof TarFileInfo |
| 25 | + or |
| 26 | + name = "getmembers" and result.(SequenceKind).getItem() instanceof TarFileInfo |
| 27 | + } |
| 28 | + |
| 29 | + override ClassValue getType() { result = Value::named("tarfile.TarFile") } |
| 30 | + |
| 31 | + override TaintKind getTaintForIteration() { result instanceof TarFileInfo } |
| 32 | +} |
| 33 | + |
| 34 | +/** The source of open tarfile objects. That is, any call to `tarfile.open(...)` */ |
| 35 | +class TarfileOpen extends TaintSource { |
| 36 | + TarfileOpen() { |
| 37 | + Value::named("tarfile.open").getACall() = this and |
| 38 | + /* |
| 39 | + * If argument refers to a string object, then it's a hardcoded path and |
| 40 | + * this tarfile is safe. |
| 41 | + */ |
| 42 | + |
| 43 | + not this.(CallNode).getAnArg().pointsTo(any(StringValue str)) and |
| 44 | + /* Ignore opens within the tarfile module itself */ |
| 45 | + not this.(ControlFlowNode).getLocation().getFile().getBaseName() = "tarfile.py" |
| 46 | + } |
| 47 | + |
| 48 | + override predicate isSourceOf(TaintKind kind) { kind instanceof OpenTarFile } |
| 49 | +} |
| 50 | + |
| 51 | +class TarFileInfo extends TaintKind { |
| 52 | + TarFileInfo() { this = "tarfile.entry" } |
| 53 | + |
| 54 | + override TaintKind getTaintOfMethodResult(string name) { name = "next" and result = this } |
| 55 | + |
| 56 | + override TaintKind getTaintOfAttribute(string name) { |
| 57 | + name = "name" and result instanceof TarFileInfo |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/* |
| 62 | + * For efficiency we don't want to track the flow of taint |
| 63 | + * around the tarfile module. |
| 64 | + */ |
| 65 | + |
| 66 | +class ExcludeTarFilePy extends Sanitizer { |
| 67 | + ExcludeTarFilePy() { this = "Tar sanitizer" } |
| 68 | + |
| 69 | + override predicate sanitizingNode(TaintKind taint, ControlFlowNode node) { |
| 70 | + node.getLocation().getFile().getBaseName() = "tarfile.py" and |
| 71 | + ( |
| 72 | + taint instanceof OpenTarFile |
| 73 | + or |
| 74 | + taint instanceof TarFileInfo |
| 75 | + or |
| 76 | + taint.(SequenceKind).getItem() instanceof TarFileInfo |
| 77 | + ) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/* Any call to an extractall method */ |
| 82 | +class ExtractAllSink extends TaintSink { |
| 83 | + CallNode call; |
| 84 | + |
| 85 | + ExtractAllSink() { |
| 86 | + this = call.getFunction().(AttrNode).getObject("extractall") and |
| 87 | + count(call.getAnArg()) = 0 |
| 88 | + } |
| 89 | + |
| 90 | + override predicate sinks(TaintKind kind) { kind instanceof OpenTarFile } |
| 91 | +} |
| 92 | + |
| 93 | +/* Argument to extract method */ |
| 94 | +class ExtractSink extends TaintSink { |
| 95 | + CallNode call; |
| 96 | + |
| 97 | + ExtractSink() { |
| 98 | + call.getFunction().(AttrNode).getName() = "extract" and |
| 99 | + this = call.getArg(0) |
| 100 | + } |
| 101 | + |
| 102 | + override predicate sinks(TaintKind kind) { kind instanceof TarFileInfo } |
| 103 | +} |
| 104 | + |
| 105 | +/* Members argument to extract method */ |
| 106 | +class ExtractMembersSink extends TaintSink { |
| 107 | + CallNode call; |
| 108 | + |
| 109 | + ExtractMembersSink() { |
| 110 | + call.getFunction().(AttrNode).getName() = "extractall" and |
| 111 | + (this = call.getArg(0) or this = call.getArgByName("members")) |
| 112 | + } |
| 113 | + |
| 114 | + override predicate sinks(TaintKind kind) { |
| 115 | + kind.(SequenceKind).getItem() instanceof TarFileInfo |
| 116 | + or |
| 117 | + kind instanceof OpenTarFile |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +class TarFileInfoSanitizer extends Sanitizer { |
| 122 | + TarFileInfoSanitizer() { this = "TarInfo sanitizer" } |
| 123 | + |
| 124 | + /** The test `if <path_sanitizing_test>:` clears taint on its `false` edge. */ |
| 125 | + override predicate sanitizingEdge(TaintKind taint, PyEdgeRefinement test) { |
| 126 | + taint instanceof TarFileInfo and |
| 127 | + clears_taint_on_false_edge(test.getTest(), test.getSense()) |
| 128 | + } |
| 129 | + |
| 130 | + private predicate clears_taint_on_false_edge(ControlFlowNode test, boolean sense) { |
| 131 | + path_sanitizing_test(test) and |
| 132 | + sense = false |
| 133 | + or |
| 134 | + // handle `not` (also nested) |
| 135 | + test.(UnaryExprNode).getNode().getOp() instanceof Not and |
| 136 | + clears_taint_on_false_edge(test.(UnaryExprNode).getOperand(), sense.booleanNot()) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +private predicate path_sanitizing_test(ControlFlowNode test) { |
| 141 | + /* Assume that any test with "path" in it is a sanitizer */ |
| 142 | + test.getAChild+().(AttrNode).getName().matches("%path") |
| 143 | + or |
| 144 | + test.getAChild+().(NameNode).getId().matches("%path") |
| 145 | +} |
| 146 | + |
| 147 | +class TarSlipConfiguration extends TaintTracking::Configuration { |
| 148 | + TarSlipConfiguration() { this = "TarSlip configuration" } |
| 149 | + |
| 150 | + override predicate isSource(TaintTracking::Source source) { source instanceof TarfileOpen } |
| 151 | + |
| 152 | + override predicate isSink(TaintTracking::Sink sink) { |
| 153 | + sink instanceof ExtractSink or |
| 154 | + sink instanceof ExtractAllSink or |
| 155 | + sink instanceof ExtractMembersSink |
| 156 | + } |
| 157 | + |
| 158 | + override predicate isSanitizer(Sanitizer sanitizer) { |
| 159 | + sanitizer instanceof TarFileInfoSanitizer |
| 160 | + or |
| 161 | + sanitizer instanceof ExcludeTarFilePy |
| 162 | + } |
| 163 | + |
| 164 | + override predicate isBarrier(DataFlow::Node node) { |
| 165 | + // Avoid flow into the tarfile module |
| 166 | + exists(ParameterDefinition def | |
| 167 | + node.asVariable().getDefinition() = def |
| 168 | + or |
| 169 | + node.asCfgNode() = def.getDefiningNode() |
| 170 | + | |
| 171 | + def.getScope() = Value::named("tarfile.open").(CallableValue).getScope() |
| 172 | + or |
| 173 | + def.isSelf() and def.getScope().getEnclosingModule().getName() = "tarfile" |
| 174 | + ) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +from TarSlipConfiguration config, TaintedPathSource src, TaintedPathSink sink |
| 179 | +where config.hasFlowPath(src, sink) |
| 180 | +select sink.getSink(), src, sink, "Extraction of tarfile from $@", src.getSource(), |
| 181 | + "a potentially untrusted source" |
0 commit comments