Python: Model file threat-model

This commit is contained in:
Rasmus Wriedt Larsen
2024-08-12 15:18:18 +02:00
parent 66f389a4b6
commit d245db54a1
4 changed files with 31 additions and 12 deletions

View File

@@ -17,6 +17,8 @@ extensions:
# if no argument is given, the default is to use sys.argv[1:]
- ['argparse.ArgumentParser', 'Member[parse_args,parse_known_args].WithArity[0].ReturnValue', 'commandargs']
- ['os', 'Member[read].ReturnValue', 'file']
- addsTo:
pack: codeql/python-all
extensible: summaryModel

View File

@@ -1499,13 +1499,17 @@ module StdlibPrivate {
* See https://docs.python.org/3/library/functions.html#open
*/
private class OpenCall extends FileSystemAccess::Range, Stdlib::FileLikeObject::InstanceSource,
DataFlow::CallCfgNode
ThreatModelSource::Range, DataFlow::CallCfgNode
{
OpenCall() { this = getOpenFunctionRef().getACall() }
override DataFlow::Node getAPathArgument() {
result in [this.getArg(0), this.getArgByName("file")]
}
override string getThreatModel() { result = "file" }
override string getSourceType() { result = "open()" }
}
/**

View File

@@ -5,25 +5,25 @@ import stat
import tempfile
import shutil
open("file") # $ getAPathArgument="file"
open(file="file") # $ getAPathArgument="file"
open("file") # $ getAPathArgument="file" threatModelSource[file]=open(..)
open(file="file") # $ getAPathArgument="file" threatModelSource[file]=open(..)
o = open
o("file") # $ getAPathArgument="file"
o(file="file") # $ getAPathArgument="file"
o("file") # $ getAPathArgument="file" threatModelSource[file]=o(..)
o(file="file") # $ getAPathArgument="file" threatModelSource[file]=o(..)
builtins.open("file") # $ getAPathArgument="file"
builtins.open(file="file") # $ getAPathArgument="file"
builtins.open("file") # $ getAPathArgument="file" threatModelSource[file]=builtins.open(..)
builtins.open(file="file") # $ getAPathArgument="file" threatModelSource[file]=builtins.open(..)
io.open("file") # $ getAPathArgument="file"
io.open(file="file") # $ getAPathArgument="file"
io.open("file") # $ getAPathArgument="file" threatModelSource[file]=io.open(..)
io.open(file="file") # $ getAPathArgument="file" threatModelSource[file]=io.open(..)
io.open_code("file") # $ getAPathArgument="file"
io.FileIO("file") # $ getAPathArgument="file"
f = open("path") # $ getAPathArgument="path"
f = open("path") # $ getAPathArgument="path" threatModelSource[file]=open(..)
f.write("foo") # $ getAPathArgument="path" fileWriteData="foo"
lines = ["foo"]
f.writelines(lines) # $ getAPathArgument="path" fileWriteData=lines
@@ -87,8 +87,8 @@ def test_fspath():
os.fspath(path=TAINTED_STRING), # $ tainted
)
os.open("path", os.O_RDONLY) # $ getAPathArgument="path"
os.open(path="path", flags=os.O_RDONLY) # $ getAPathArgument="path"
os.open("path", os.O_RDONLY) # $ getAPathArgument="path" SPURIOUS: threatModelSource[file]=os.open(..)
os.open(path="path", flags=os.O_RDONLY) # $ getAPathArgument="path" SPURIOUS: threatModelSource[file]=os.open(..)
os.access("path", os.R_OK) # $ getAPathArgument="path"
os.access(path="path", mode=os.R_OK) # $ getAPathArgument="path"

View File

@@ -48,6 +48,19 @@ ensure_tainted(
input(), # $ tainted threatModelSource[stdin]=input()
)
########################################
# reading data from files
########################################
ensure_tainted(
open("foo"), # $ tainted threatModelSource[file]=open(..) getAPathArgument="foo"
open("foo").read(), # $ tainted threatModelSource[file]=open(..) getAPathArgument="foo"
open("foo").readline(), # $ tainted threatModelSource[file]=open(..) getAPathArgument="foo"
open("foo").readlines(), # $ tainted threatModelSource[file]=open(..) getAPathArgument="foo"
os.read(os.open("foo"), 1024), # $ tainted threatModelSource[file]=os.read(..) SPURIOUS: threatModelSource[file]=os.open(..) getAPathArgument="foo"
)
########################################
# socket
########################################