|
| 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 | + |
| 17 | +import semmle.python.security.TaintTracking |
| 18 | +import semmle.python.security.strings.Basic |
| 19 | + |
| 20 | +/** A TaintKind to represent open tarfile objects. That is, the result of calling `tarfile.open(...)` */ |
| 21 | +class OpenTarFile extends TaintKind { |
| 22 | + OpenTarFile() { |
| 23 | + this = "tarfile.open" |
| 24 | + } |
| 25 | + |
| 26 | + override TaintKind getTaintOfMethodResult(string name) { |
| 27 | + name = "getmember" and result instanceof TarFileInfo |
| 28 | + or |
| 29 | + name = "getmembers" and result.(SequenceKind).getItem() instanceof TarFileInfo |
| 30 | + } |
| 31 | + |
| 32 | + override ClassValue getType() { |
| 33 | + result = Module::named("tarfile").attr("TarFile") |
| 34 | + } |
| 35 | + |
| 36 | + override TaintKind getTaintForIteration() { |
| 37 | + result instanceof TarFileInfo |
| 38 | + } |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +/** The source of open tarfile objects. That is, any call to `tarfile.open(...)` */ |
| 43 | +class TarfileOpen extends TaintSource { |
| 44 | + |
| 45 | + TarfileOpen() { |
| 46 | + Module::named("tarfile").attr("open").getACall() = this |
| 47 | + and |
| 48 | + /* If argument refers to a string object, then it's a hardcoded path and |
| 49 | + * this tarfile is safe. |
| 50 | + */ |
| 51 | + not this.(CallNode).getAnArg().refersTo(any(StringObject str)) |
| 52 | + and |
| 53 | + /* Ignore opens within the tarfile module itself */ |
| 54 | + not this.(ControlFlowNode).getLocation().getFile().getBaseName() = "tarfile.py" |
| 55 | + } |
| 56 | + |
| 57 | + override predicate isSourceOf(TaintKind kind) { |
| 58 | + kind instanceof OpenTarFile |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +class TarFileInfo extends TaintKind { |
| 64 | + |
| 65 | + TarFileInfo() { |
| 66 | + this = "tarfile.entry" |
| 67 | + } |
| 68 | + |
| 69 | + override TaintKind getTaintOfMethodResult(string name) { |
| 70 | + name = "next" and result = this |
| 71 | + } |
| 72 | + |
| 73 | + override TaintKind getTaintOfAttribute(string name) { |
| 74 | + name = "name" and result instanceof TarFileInfo |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +/* For efficiency we don't want to track the flow of taint |
| 80 | + * around the tarfile module. */ |
| 81 | +class ExcludeTarFilePy extends Sanitizer { |
| 82 | + |
| 83 | + ExcludeTarFilePy() { |
| 84 | + this = "Tar sanitizer" |
| 85 | + } |
| 86 | + |
| 87 | + override predicate sanitizingNode(TaintKind taint, ControlFlowNode node) { |
| 88 | + node.getLocation().getFile().getBaseName() = "tarfile.py" and |
| 89 | + ( |
| 90 | + taint instanceof OpenTarFile |
| 91 | + or |
| 92 | + taint instanceof TarFileInfo |
| 93 | + or |
| 94 | + taint.(SequenceKind).getItem() instanceof TarFileInfo |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +/* Any call to an extractall method */ |
| 101 | +class ExtractAllSink extends TaintSink { |
| 102 | + |
| 103 | + CallNode call; |
| 104 | + |
| 105 | + ExtractAllSink() { |
| 106 | + this = call.getFunction().(AttrNode).getObject("extractall") and |
| 107 | + count(call.getAnArg()) = 0 |
| 108 | + } |
| 109 | + |
| 110 | + override predicate sinks(TaintKind kind) { |
| 111 | + kind instanceof OpenTarFile |
| 112 | + } |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +/* Argument to extract method */ |
| 117 | +class ExtractSink extends TaintSink { |
| 118 | + |
| 119 | + CallNode call; |
| 120 | + |
| 121 | + ExtractSink() { |
| 122 | + call.getFunction().(AttrNode).getName() = "extract" and |
| 123 | + this = call.getArg(0) |
| 124 | + } |
| 125 | + |
| 126 | + override predicate sinks(TaintKind kind) { |
| 127 | + kind instanceof TarFileInfo |
| 128 | + } |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +/* Members argument to extract method */ |
| 134 | +class ExtractMembersSink extends TaintSink { |
| 135 | + |
| 136 | + CallNode call; |
| 137 | + |
| 138 | + ExtractMembersSink() { |
| 139 | + call.getFunction().(AttrNode).getName() = "extractall" and |
| 140 | + (this = call.getArg(0) or this = call.getArgByName("members")) |
| 141 | + } |
| 142 | + |
| 143 | + override predicate sinks(TaintKind kind) { |
| 144 | + kind.(SequenceKind).getItem() instanceof TarFileInfo |
| 145 | + or |
| 146 | + kind instanceof OpenTarFile |
| 147 | + } |
| 148 | + |
| 149 | +} |
| 150 | + |
| 151 | +class TarFileInfoSanitizer extends Sanitizer { |
| 152 | + |
| 153 | + TarFileInfoSanitizer() { |
| 154 | + this = "TarInfo sanitizer" |
| 155 | + } |
| 156 | + |
| 157 | + override predicate sanitizingEdge(TaintKind taint, PyEdgeRefinement test) { |
| 158 | + path_sanitizing_test(test.getTest()) and |
| 159 | + taint instanceof TarFileInfo |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | +} |
| 164 | + |
| 165 | +private predicate path_sanitizing_test(ControlFlowNode test) { |
| 166 | + /* Assume that any test with "path" in it is a sanitizer */ |
| 167 | + test.getAChild+().(AttrNode).getName().matches("%path") |
| 168 | + or |
| 169 | + test.getAChild+().(NameNode).getId().matches("%path") |
| 170 | +} |
| 171 | + |
| 172 | +class TarSlipConfiguration extends TaintTracking::Configuration { |
| 173 | + |
| 174 | + TarSlipConfiguration() { this = "TarSlip configuration" } |
| 175 | + |
| 176 | + override predicate isSource(TaintTracking::Source source) { source instanceof TarfileOpen } |
| 177 | + |
| 178 | + override predicate isSink(TaintTracking::Sink sink) { |
| 179 | + sink instanceof ExtractSink or |
| 180 | + sink instanceof ExtractAllSink or |
| 181 | + sink instanceof ExtractMembersSink |
| 182 | + } |
| 183 | + |
| 184 | + override predicate isSanitizer(Sanitizer sanitizer) { |
| 185 | + sanitizer instanceof TarFileInfoSanitizer |
| 186 | + or |
| 187 | + sanitizer instanceof ExcludeTarFilePy |
| 188 | + } |
| 189 | + |
| 190 | +} |
| 191 | + |
| 192 | + |
| 193 | +from TarSlipConfiguration config, TaintedPathSource src, TaintedPathSink sink |
| 194 | +where config.hasFlowPath(src, sink) |
| 195 | +select sink.getSink(), src, sink, "Extraction of tarfile from $@", src.getSource(), "a potentially untrusted source" |
| 196 | + |
0 commit comments