Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The `py/tarslip` query now detects unsafe `TarFile.extractall` calls where the `members` argument may be `None`, including calls through wrappers with a `None` default.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module TarSlip {
* A sink capturing method calls to `extractall`.
*
* For a call to `file.extractall`, `file` is considered a sink if
* there is no `members` argument and the extraction filter is unsafe.
* the `members` argument is omitted or may be `None`, and the extraction filter is unsafe.
*/
class ExtractAllSink extends Sink {
ExtractAllSink() {
Expand All @@ -96,7 +96,11 @@ module TarSlip {
.getMember("extractall")
.getACall() and
hasUnsafeFilter(call) and
not exists(call.getParameter(1, "members")) and
(
not exists(call.getParameter(1, "members"))
or
call.getParameter(1, "members").getAValueReachingSink().asExpr() instanceof None
Comment on lines +99 to +102
) and
this = call.(DataFlow::MethodCallNode).getObject()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
| tarslip.py:113:24:113:26 | ControlFlowNode for tar | tarslip.py:112:7:112:39 | ControlFlowNode for Attribute() | tarslip.py:113:24:113:26 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:112:7:112:39 | ControlFlowNode for Attribute() | potentially untrusted source |
| tarslip.py:121:24:121:28 | ControlFlowNode for entry | tarslip.py:119:7:119:39 | ControlFlowNode for Attribute() | tarslip.py:121:24:121:28 | ControlFlowNode for entry | This file extraction depends on a $@. | tarslip.py:119:7:119:39 | ControlFlowNode for Attribute() | potentially untrusted source |
| tarslip.py:125:32:125:34 | ControlFlowNode for tar | tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | tarslip.py:125:32:125:34 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | potentially untrusted source |
| tarslip.py:132:1:132:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:132:1:132:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source |
| tarslip.py:133:1:133:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:133:1:133:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source |
| tarslip.py:136:1:136:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:136:1:136:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source |
| tarslip.py:138:1:138:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:138:1:138:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source |
| tarslip.py:139:1:139:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:139:1:139:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source |
| tarslip.py:146:5:146:7 | ControlFlowNode for tar | tarslip.py:153:7:153:39 | ControlFlowNode for Attribute() | tarslip.py:146:5:146:7 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:153:7:153:39 | ControlFlowNode for Attribute() | potentially untrusted source |
| tarslip.py:150:5:150:7 | ControlFlowNode for tar | tarslip.py:156:7:156:39 | ControlFlowNode for Attribute() | tarslip.py:150:5:150:7 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:156:7:156:39 | ControlFlowNode for Attribute() | potentially untrusted source |
edges
| tarslip.py:14:1:14:3 | ControlFlowNode for tar | tarslip.py:15:1:15:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:14:7:14:39 | ControlFlowNode for Attribute() | tarslip.py:14:1:14:3 | ControlFlowNode for tar | provenance | |
Expand Down Expand Up @@ -38,6 +45,20 @@ edges
| tarslip.py:120:5:120:9 | ControlFlowNode for entry | tarslip.py:121:24:121:28 | ControlFlowNode for entry | provenance | |
| tarslip.py:124:1:124:3 | ControlFlowNode for tar | tarslip.py:125:32:125:34 | ControlFlowNode for tar | provenance | |
| tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | tarslip.py:124:1:124:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:132:1:132:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:133:1:133:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:136:1:136:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:138:1:138:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:139:1:139:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:131:1:131:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:145:34:145:36 | ControlFlowNode for tar | tarslip.py:146:5:146:7 | ControlFlowNode for tar | provenance | |
| tarslip.py:149:37:149:39 | ControlFlowNode for tar | tarslip.py:150:5:150:7 | ControlFlowNode for tar | provenance | |
| tarslip.py:153:1:153:3 | ControlFlowNode for tar | tarslip.py:154:30:154:32 | ControlFlowNode for tar | provenance | |
| tarslip.py:153:7:153:39 | ControlFlowNode for Attribute() | tarslip.py:153:1:153:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:154:30:154:32 | ControlFlowNode for tar | tarslip.py:145:34:145:36 | ControlFlowNode for tar | provenance | |
| tarslip.py:156:1:156:3 | ControlFlowNode for tar | tarslip.py:157:33:157:35 | ControlFlowNode for tar | provenance | |
| tarslip.py:156:7:156:39 | ControlFlowNode for Attribute() | tarslip.py:156:1:156:3 | ControlFlowNode for tar | provenance | |
| tarslip.py:157:33:157:35 | ControlFlowNode for tar | tarslip.py:149:37:149:39 | ControlFlowNode for tar | provenance | |
nodes
| tarslip.py:14:1:14:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:14:7:14:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
Expand Down Expand Up @@ -77,4 +98,21 @@ nodes
| tarslip.py:124:1:124:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
| tarslip.py:125:32:125:34 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:131:1:131:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
| tarslip.py:132:1:132:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:133:1:133:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:136:1:136:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:138:1:138:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:139:1:139:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:145:34:145:36 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:146:5:146:7 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:149:37:149:39 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:150:5:150:7 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:153:1:153:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:153:7:153:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
| tarslip.py:154:30:154:32 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:156:1:156:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
| tarslip.py:156:7:156:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
| tarslip.py:157:33:157:35 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
subpaths
29 changes: 29 additions & 0 deletions python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,32 @@ def safemembers(members):

tar = tarfile.open(unsafe_filename_tar)
tar.extractall("/tmp/unpack/", safemembers(tar)) # safe

# `None` selects all archive members, just like an omitted `members` argument.
tar = tarfile.open(unsafe_filename_tar) # $ Source
tar.extractall("/tmp/unpack/", None) # $ Alert
tar.extractall("/tmp/unpack/", members=None) # $ Alert

members = None
tar.extractall("/tmp/unpack/", members) # $ Alert

tar.extractall("/tmp/unpack/", None, filter=None) # $ Alert
tar.extractall("/tmp/unpack/", members=None, filter="fully_trusted") # $ Alert
tar.extractall("/tmp/unpack/", members=None, filter="data") # safe
tar.extractall("/tmp/unpack/", []) # safe
tar.extractall("/tmp/unpack/", members=[]) # safe


def extract_with_default_members(tar, path=".", members=None, *, numeric_owner=False):
tar.extractall(path, members, numeric_owner=numeric_owner) # $ Alert


def extract_with_default_members_kw(tar, path=".", *, members=None, numeric_owner=False):
tar.extractall(path, members=members, numeric_owner=numeric_owner) # $ Alert


tar = tarfile.open(unsafe_filename_tar) # $ Source
extract_with_default_members(tar, "/tmp/unpack/")

tar = tarfile.open(unsafe_filename_tar) # $ Source
extract_with_default_members_kw(tar, "/tmp/unpack/")
Loading