Skip to content

Commit f14a90f

Browse files
authored
Merge pull request #9200 from tausbn/python-modernise-weak-file-permissions-query
Python: Modernise weak file permissions query
2 parents b24b275 + 5b9c668 commit f14a90f

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
import python
15+
import semmle.python.ApiGraphs
1516

1617
bindingset[p]
1718
int world_permission(int p) { result = p % 8 }
@@ -33,20 +34,20 @@ string permissive_permission(int p) {
3334
world_permission(p) = 0 and result = "group " + access(group_permission(p))
3435
}
3536

36-
predicate chmod_call(CallNode call, FunctionValue chmod, NumericValue num) {
37-
Value::named("os.chmod") = chmod and
38-
chmod.getACall() = call and
39-
call.getArg(1).pointsTo(num)
37+
predicate chmod_call(API::CallNode call, string name, int mode) {
38+
call = API::moduleImport("os").getMember("chmod").getACall() and
39+
mode = call.getParameter(1, "mode").getAValueReachingRhs().asExpr().(IntegerLiteral).getValue() and
40+
name = "chmod"
4041
}
4142

42-
predicate open_call(CallNode call, FunctionValue open, NumericValue num) {
43-
Value::named("os.open") = open and
44-
open.getACall() = call and
45-
call.getArg(2).pointsTo(num)
43+
predicate open_call(API::CallNode call, string name, int mode) {
44+
call = API::moduleImport("os").getMember("open").getACall() and
45+
mode = call.getParameter(2, "mode").getAValueReachingRhs().asExpr().(IntegerLiteral).getValue() and
46+
name = "open"
4647
}
4748

48-
from CallNode call, FunctionValue func, NumericValue num, string permission
49+
from API::CallNode call, string name, int mode, string permission
4950
where
50-
(chmod_call(call, func, num) or open_call(call, func, num)) and
51-
permission = permissive_permission(num.getIntValue())
52-
select call, "Overly permissive mask in " + func.getName() + " sets file to " + permission + "."
51+
(chmod_call(call, name, mode) or open_call(call, name, mode)) and
52+
permission = permissive_permission(mode)
53+
select call, "Overly permissive mask in " + name + " sets file to " + permission + "."

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
| test.py:8:1:8:20 | ControlFlowNode for Attribute() | Overly permissive mask in chmod sets file to world writable. |
33
| test.py:9:1:9:21 | ControlFlowNode for Attribute() | Overly permissive mask in chmod sets file to world writable. |
44
| test.py:11:1:11:21 | ControlFlowNode for Attribute() | Overly permissive mask in chmod sets file to group readable. |
5-
| test.py:13:1:13:28 | ControlFlowNode for Attribute() | Overly permissive mask in chmod sets file to group writable. |
65
| test.py:14:1:14:19 | ControlFlowNode for Attribute() | Overly permissive mask in chmod sets file to group writable. |
76
| test.py:16:1:16:25 | ControlFlowNode for Attribute() | Overly permissive mask in open sets file to world readable. |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
semmle-extractor-options: --max-import-depth=2 -p ../lib
1+
semmle-extractor-options: --max-import-depth=2 -p ../lib --lang=3

0 commit comments

Comments
 (0)