Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d553ae

Browse files
committed
Python: Check os.open as well as os.chmod for weak file permissions.
1 parent a3b5769 commit 6d553ae

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

python/ql/src/Security/CWE-732/WeakFilePermissions.ql

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@ string permissive_permission(int p) {
3434
world_permission(p) = 0 and result = "group " + access(group_permission(p))
3535
}
3636

37-
from FunctionObject chmod, CallNode call, NumericObject num, string permission
38-
where
37+
predicate chmod_call(CallNode call, FunctionObject chmod, NumericObject num) {
3938
any(ModuleObject os | os.getName() = "os").getAttribute("chmod") = chmod and
40-
chmod.getACall() = call and call.getArg(1).refersTo(num) and
39+
chmod.getACall() = call and call.getArg(1).refersTo(num)
40+
}
41+
42+
predicate open_call(CallNode call, FunctionObject open, NumericObject num) {
43+
any(ModuleObject os | os.getName() = "os").getAttribute("open") = open and
44+
open.getACall() = call and call.getArg(2).refersTo(num)
45+
}
46+
47+
48+
from CallNode call, FunctionObject func, NumericObject num, string permission
49+
where
50+
(chmod_call(call, func, num) or open_call(call, func, num))
51+
and
4152
permission = permissive_permission(num.intValue())
42-
select call, "Overly permissive mask in chmod sets file to " + permission + "."
53+
select call, "Overly permissive mask in " + func.getName() + " sets file to " + permission + "."

python/ql/test/query-tests/Security/CWE-732/WeakFilePermissions.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
| test.py:11:1:11:21 | ControlFlowNode for Attribute() | Overly permissive mask in chmod sets file to group readable. |
55
| test.py:13:1:13:28 | ControlFlowNode for Attribute() | Overly permissive mask in chmod sets file to group writable. |
66
| test.py:14:1:14:19 | ControlFlowNode for Attribute() | Overly permissive mask in chmod sets file to group writable. |
7+
| test.py:16:1:16:25 | ControlFlowNode for Attribute() | Overly permissive mask in open sets file to world readable. |

python/ql/test/query-tests/Security/CWE-732/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212
os.chmod(file, stat.S_IRWXU) # GOOD
1313
os.chmod(file, stat.S_IWGRP) # BAD
1414
os.chmod(file, 400) # BAD -- Decimal format.
15+
16+
os.open(file, 'w', 0o704) # BAD

python/ql/test/query-tests/Security/lib/os/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ def popen(cmd, *args, **kwargs):
66

77
def chmod(path, mode):
88
pass
9+
10+
def open(path, flags, mode):
11+
pass

0 commit comments

Comments
 (0)