16
16
OBJDUMP_CMD = os .getenv ('OBJDUMP' , '/usr/bin/objdump' )
17
17
OTOOL_CMD = os .getenv ('OTOOL' , '/usr/bin/otool' )
18
18
19
+ def run_command (command ):
20
+ p = subprocess .run (command , stdout = subprocess .PIPE , check = True , universal_newlines = True )
21
+ return p .stdout
22
+
19
23
def check_ELF_PIE (executable ):
20
24
'''
21
25
Check for position independent executable (PIE), allowing for address space randomization.
22
26
'''
23
- p = subprocess .Popen ([READELF_CMD , '-h' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
24
- (stdout , stderr ) = p .communicate ()
25
- if p .returncode :
26
- raise IOError ('Error opening file' )
27
+ stdout = run_command ([READELF_CMD , '-h' , '-W' , executable ])
27
28
28
29
ok = False
29
30
for line in stdout .splitlines ():
@@ -34,10 +35,8 @@ def check_ELF_PIE(executable):
34
35
35
36
def get_ELF_program_headers (executable ):
36
37
'''Return type and flags for ELF program headers'''
37
- p = subprocess .Popen ([READELF_CMD , '-l' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
38
- (stdout , stderr ) = p .communicate ()
39
- if p .returncode :
40
- raise IOError ('Error opening file' )
38
+ stdout = run_command ([READELF_CMD , '-l' , '-W' , executable ])
39
+
41
40
in_headers = False
42
41
count = 0
43
42
headers = []
@@ -83,18 +82,17 @@ def check_ELF_RELRO(executable):
83
82
have_gnu_relro = False
84
83
for (typ , flags ) in get_ELF_program_headers (executable ):
85
84
# Note: not checking flags == 'R': here as linkers set the permission differently
86
- # This does not affect security: the permission flags of the GNU_RELRO program header are ignored, the PT_LOAD header determines the effective permissions.
85
+ # This does not affect security: the permission flags of the GNU_RELRO program
86
+ # header are ignored, the PT_LOAD header determines the effective permissions.
87
87
# However, the dynamic linker need to write to this area so these are RW.
88
88
# Glibc itself takes care of mprotecting this area R after relocations are finished.
89
89
# See also https://marc.info/?l=binutils&m=1498883354122353
90
90
if typ == 'GNU_RELRO' :
91
91
have_gnu_relro = True
92
92
93
93
have_bindnow = False
94
- p = subprocess .Popen ([READELF_CMD , '-d' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
95
- (stdout , stderr ) = p .communicate ()
96
- if p .returncode :
97
- raise IOError ('Error opening file' )
94
+ stdout = run_command ([READELF_CMD , '-d' , '-W' , executable ])
95
+
98
96
for line in stdout .splitlines ():
99
97
tokens = line .split ()
100
98
if len (tokens )> 1 and tokens [1 ] == '(BIND_NOW)' or (len (tokens )> 2 and tokens [1 ] == '(FLAGS)' and 'BIND_NOW' in tokens [2 :]):
@@ -105,10 +103,8 @@ def check_ELF_Canary(executable):
105
103
'''
106
104
Check for use of stack canary
107
105
'''
108
- p = subprocess .Popen ([READELF_CMD , '--dyn-syms' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
109
- (stdout , stderr ) = p .communicate ()
110
- if p .returncode :
111
- raise IOError ('Error opening file' )
106
+ stdout = run_command ([READELF_CMD , '--dyn-syms' , '-W' , executable ])
107
+
112
108
ok = False
113
109
for line in stdout .splitlines ():
114
110
if '__stack_chk_fail' in line :
@@ -117,10 +113,8 @@ def check_ELF_Canary(executable):
117
113
118
114
def get_PE_dll_characteristics (executable ) -> int :
119
115
'''Get PE DllCharacteristics bits'''
120
- p = subprocess .Popen ([OBJDUMP_CMD , '-x' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
121
- (stdout , stderr ) = p .communicate ()
122
- if p .returncode :
123
- raise IOError ('Error opening file' )
116
+ stdout = run_command ([OBJDUMP_CMD , '-x' , executable ])
117
+
124
118
bits = 0
125
119
for line in stdout .splitlines ():
126
120
tokens = line .split ()
@@ -146,10 +140,8 @@ def check_PE_HIGH_ENTROPY_VA(executable):
146
140
147
141
def check_PE_RELOC_SECTION (executable ) -> bool :
148
142
'''Check for a reloc section. This is required for functional ASLR.'''
149
- p = subprocess .Popen ([OBJDUMP_CMD , '-h' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
150
- (stdout , stderr ) = p .communicate ()
151
- if p .returncode :
152
- raise IOError ('Error opening file' )
143
+ stdout = run_command ([OBJDUMP_CMD , '-h' , executable ])
144
+
153
145
for line in stdout .splitlines ():
154
146
if '.reloc' in line :
155
147
return True
@@ -161,10 +153,7 @@ def check_PE_NX(executable):
161
153
return (bits & IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ) == IMAGE_DLL_CHARACTERISTICS_NX_COMPAT
162
154
163
155
def get_MACHO_executable_flags (executable ):
164
- p = subprocess .Popen ([OTOOL_CMD , '-vh' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
165
- (stdout , stderr ) = p .communicate ()
166
- if p .returncode :
167
- raise IOError ('Error opening file' )
156
+ stdout = run_command ([OTOOL_CMD , '-vh' , executable ])
168
157
169
158
flags = []
170
159
for line in stdout .splitlines ():
@@ -208,10 +197,7 @@ def check_MACHO_LAZY_BINDINGS(executable) -> bool:
208
197
Check for no lazy bindings.
209
198
We don't use or check for MH_BINDATLOAD. See #18295.
210
199
'''
211
- p = subprocess .Popen ([OTOOL_CMD , '-l' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
212
- (stdout , stderr ) = p .communicate ()
213
- if p .returncode :
214
- raise IOError ('Error opening file' )
200
+ stdout = run_command ([OTOOL_CMD , '-l' , executable ])
215
201
216
202
for line in stdout .splitlines ():
217
203
tokens = line .split ()
@@ -224,10 +210,8 @@ def check_MACHO_Canary(executable) -> bool:
224
210
'''
225
211
Check for use of stack canary
226
212
'''
227
- p = subprocess .Popen ([OTOOL_CMD , '-Iv' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
228
- (stdout , stderr ) = p .communicate ()
229
- if p .returncode :
230
- raise IOError ('Error opening file' )
213
+ stdout = run_command ([OTOOL_CMD , '-Iv' , executable ])
214
+
231
215
ok = False
232
216
for line in stdout .splitlines ():
233
217
if '___stack_chk_fail' in line :
0 commit comments