-
Notifications
You must be signed in to change notification settings - Fork 2k
py/tarslip: detect shutil.unpack_archive and subprocess tar extraction #21720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
c1ecca3
88b36c4
8efaa5d
6d03548
5084883
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,6 +132,79 @@ module TarSlip { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * A call to `shutil.unpack_archive`, considered as a flow source. | ||
| * | ||
| * The archive filename is not hardcoded, so it may come from user input. | ||
| */ | ||
| class ShutilUnpackArchiveSource extends Source { | ||
| ShutilUnpackArchiveSource() { | ||
| this = API::moduleImport("shutil").getMember("unpack_archive").getACall() and | ||
| not this.(DataFlow::CallCfgNode).getArg(0).getALocalSource().asExpr() instanceof StringLiteral | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A call to `shutil.unpack_archive`, considered as a flow sink. | ||
| * | ||
| * The archive filename is not hardcoded, so it may come from user input. | ||
| */ | ||
| class ShutilUnpackArchiveSink extends Sink { | ||
| ShutilUnpackArchiveSink() { | ||
| this = API::moduleImport("shutil").getMember("unpack_archive").getACall() and | ||
| not this.(DataFlow::CallCfgNode).getArg(0).getALocalSource().asExpr() instanceof StringLiteral | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this line, not really needed.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 5084883 — changed |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `call` is a subprocess call that invokes `tar` for archive extraction | ||
| * with at least one non-literal argument (the archive filename). | ||
| * | ||
| * Detects patterns like `subprocess.run(["tar", "-xf", untrusted_filename])`. | ||
| */ | ||
| private predicate isSubprocessTarExtraction(DataFlow::CallCfgNode call) { | ||
| exists(SequenceNode cmdList | | ||
| call = | ||
| API::moduleImport("subprocess") | ||
| .getMember(["run", "call", "check_call", "check_output", "Popen"]) | ||
| .getACall() and | ||
| cmdList = call.getArg(0).asCfgNode() and | ||
| // Command must be "tar" exactly or a path ending in "/tar" (e.g. "/usr/bin/tar") | ||
| exists(string cmd | | ||
| cmd = cmdList.getElement(0).getNode().(StringLiteral).getText() and | ||
| (cmd = "tar" or cmd.matches("%/tar")) | ||
| ) and | ||
| // At least one extraction-related flag must be present: | ||
| // single-dash flags containing 'x' (like -x, -xf, -xvf) or the long option --extract | ||
| exists(string flag | | ||
| flag = cmdList.getElement(_).getNode().(StringLiteral).getText() and | ||
| (flag.regexpMatch("-[a-zA-Z]*x[a-zA-Z]*") or flag = "--extract") | ||
| ) and | ||
| // At least one non-literal argument (the archive filename) | ||
| exists(int i | | ||
| i > 0 and | ||
| exists(cmdList.getElement(i)) and | ||
| not cmdList.getElement(i).getNode() instanceof StringLiteral | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * A call to `subprocess` functions that invokes `tar` for archive extraction, | ||
| * considered as a flow source. | ||
| */ | ||
| class SubprocessTarExtractionSource extends Source { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove (should not be a source).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 5084883 — |
||
| SubprocessTarExtractionSource() { isSubprocessTarExtraction(this) } | ||
| } | ||
|
|
||
| /** | ||
| * A call to `subprocess` functions that invokes `tar` for archive extraction, | ||
| * considered as a flow sink. | ||
| */ | ||
| class SubprocessTarExtractionSink extends Sink { | ||
| SubprocessTarExtractionSink() { isSubprocessTarExtraction(this) } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should not be the call that is the sink, but instead the element in the command list that corresponds to the file to extract.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 5084883 — the sink is now |
||
| } | ||
|
|
||
| /** | ||
| * Holds if `g` clears taint for `tarInfo`. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove (should not be a source).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 5084883 —
ShutilUnpackArchiveSourceremoved.