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