21
21
22
22
.. code-block:: none
23
23
24
- >> Issue: Probable insecure usage of temp file/directory .
25
- Severity: Medium Confidence: Medium
24
+ >> Issue: Chmod setting a permissive mask 0o664 on file (/etc/passwd) .
25
+ Severity: Medium Confidence: High
26
26
CWE: CWE-732 (https://cwe.mitre.org/data/definitions/732.html)
27
- Location: ./examples/os-chmod.py:15
28
- 14 os.chmod('/etc/hosts ', 0o777 )
29
- 15 os.chmod('/tmp/oh_hai ', 0x1ff )
30
- 16 os.chmod('/etc/passwd', stat.S_IRWXU )
27
+ Location: ./examples/os-chmod.py:8
28
+ 7 os.chmod('/etc/passwd ', 0o7 )
29
+ 8 os.chmod('/etc/passwd ', 0o664 )
30
+ 9 os.chmod('/etc/passwd', 0o777 )
31
31
32
- >> Issue: Chmod setting a permissive mask 0777 on file (key_file ).
32
+ >> Issue: Chmod setting a permissive mask 0777 on file (keyfile ).
33
33
Severity: High Confidence: High
34
34
CWE: CWE-732 (https://cwe.mitre.org/data/definitions/732.html)
35
35
Location: ./examples/os-chmod.py:17
36
36
16 os.chmod('/etc/passwd', stat.S_IRWXU)
37
- 17 os.chmod(key_file, 0o777)
38
- 18
37
+ 17 os.chmod(keyfile, 0o777)
38
+ 18 os.chmod('~/hidden_exec', stat.S_IXGRP)
39
+
40
+ >> Issue: Chmod setting a permissive mask 0o666 on file (NOT PARSED).
41
+ Severity: High Confidence: High
42
+ CWE: CWE-732 (https://cwe.mitre.org/data/definitions/732.html)
43
+ Location: ./examples/pathlib-chmod.py:5
44
+ 4 p1 = pathlib.Path(filename)
45
+ 5 p1.chmod(0o666)
39
46
40
47
.. seealso::
41
48
52
59
.. versionchanged:: 1.7.5
53
60
Added checks for S_IWGRP and S_IXOTH
54
61
62
+ .. versionchanged:: 1.7.6
63
+ Added check for pathlib chmod
64
+
55
65
""" # noqa: E501
56
66
import stat
57
67
@@ -73,27 +83,35 @@ def _stat_is_dangerous(mode):
73
83
@test .test_id ("B103" )
74
84
def set_bad_file_permissions (context ):
75
85
if "chmod" in context .call_function_name :
76
- if context .call_args_count == 2 :
86
+ if (
87
+ context .call_function_name_qual .startswith ("os." )
88
+ and context .call_args_count == 2
89
+ ): # os chmod
90
+ filename = context .get_call_arg_at_position (0 )
77
91
mode = context .get_call_arg_at_position (1 )
92
+ elif context .call_args_count == 1 : # pathlib chmod
93
+ filename = None
94
+ mode = context .get_call_arg_at_position (0 )
95
+ else :
96
+ return
78
97
79
- if (
98
+ if (
80
99
mode is not None
81
100
and isinstance (mode , int )
82
101
and _stat_is_dangerous (mode )
83
- ):
84
- # world writable is an HIGH, group executable is a MEDIUM
85
- if mode & stat .S_IWOTH :
86
- sev_level = bandit .HIGH
87
- else :
88
- sev_level = bandit .MEDIUM
89
-
90
- filename = context .get_call_arg_at_position (0 )
91
- if filename is None :
92
- filename = "NOT PARSED"
93
- return bandit .Issue (
94
- severity = sev_level ,
95
- confidence = bandit .HIGH ,
96
- cwe = issue .Cwe .INCORRECT_PERMISSION_ASSIGNMENT ,
97
- text = "Chmod setting a permissive mask %s on file (%s)."
98
- % (oct (mode ), filename ),
99
- )
102
+ ):
103
+ # world writable is an HIGH, group executable is a MEDIUM
104
+ if mode & stat .S_IWOTH :
105
+ sev_level = bandit .HIGH
106
+ else :
107
+ sev_level = bandit .MEDIUM
108
+
109
+ if filename is None :
110
+ filename = "NOT PARSED"
111
+ return bandit .Issue (
112
+ severity = sev_level ,
113
+ confidence = bandit .HIGH ,
114
+ cwe = issue .Cwe .INCORRECT_PERMISSION_ASSIGNMENT ,
115
+ text = "Chmod setting a permissive mask %s on file (%s)."
116
+ % (oct (mode ), filename ),
117
+ )
0 commit comments