12
12
import sys
13
13
import os
14
14
15
+ from typing import List , Optional
16
+
15
17
READELF_CMD = os .getenv ('READELF' , '/usr/bin/readelf' )
16
18
OBJDUMP_CMD = os .getenv ('OBJDUMP' , '/usr/bin/objdump' )
17
19
OTOOL_CMD = os .getenv ('OTOOL' , '/usr/bin/otool' )
18
- NONFATAL = {} # checks which are non-fatal for now but only generate a warning
19
20
20
- def check_ELF_PIE (executable ):
21
+ def run_command (command ) -> str :
22
+ p = subprocess .run (command , stdout = subprocess .PIPE , check = True , universal_newlines = True )
23
+ return p .stdout
24
+
25
+ def check_ELF_PIE (executable ) -> bool :
21
26
'''
22
27
Check for position independent executable (PIE), allowing for address space randomization.
23
28
'''
24
- p = subprocess .Popen ([READELF_CMD , '-h' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
25
- (stdout , stderr ) = p .communicate ()
26
- if p .returncode :
27
- raise IOError ('Error opening file' )
29
+ stdout = run_command ([READELF_CMD , '-h' , '-W' , executable ])
28
30
29
31
ok = False
30
32
for line in stdout .splitlines ():
31
- line = line .split ()
32
- if len (line )>= 2 and line [0 ] == 'Type:' and line [1 ] == 'DYN' :
33
+ tokens = line .split ()
34
+ if len (line )>= 2 and tokens [0 ] == 'Type:' and tokens [1 ] == 'DYN' :
33
35
ok = True
34
36
return ok
35
37
36
38
def get_ELF_program_headers (executable ):
37
39
'''Return type and flags for ELF program headers'''
38
- p = subprocess .Popen ([READELF_CMD , '-l' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
39
- (stdout , stderr ) = p .communicate ()
40
- if p .returncode :
41
- raise IOError ('Error opening file' )
40
+ stdout = run_command ([READELF_CMD , '-l' , '-W' , executable ])
41
+
42
42
in_headers = False
43
43
count = 0
44
44
headers = []
@@ -62,7 +62,7 @@ def get_ELF_program_headers(executable):
62
62
count += 1
63
63
return headers
64
64
65
- def check_ELF_NX (executable ):
65
+ def check_ELF_NX (executable ) -> bool :
66
66
'''
67
67
Check that no sections are writable and executable (including the stack)
68
68
'''
@@ -75,7 +75,7 @@ def check_ELF_NX(executable):
75
75
have_wx = True
76
76
return have_gnu_stack and not have_wx
77
77
78
- def check_ELF_RELRO (executable ):
78
+ def check_ELF_RELRO (executable ) -> bool :
79
79
'''
80
80
Check for read-only relocations.
81
81
GNU_RELRO program header must exist
@@ -84,101 +84,78 @@ def check_ELF_RELRO(executable):
84
84
have_gnu_relro = False
85
85
for (typ , flags ) in get_ELF_program_headers (executable ):
86
86
# Note: not checking flags == 'R': here as linkers set the permission differently
87
- # This does not affect security: the permission flags of the GNU_RELRO program header are ignored, the PT_LOAD header determines the effective permissions.
87
+ # This does not affect security: the permission flags of the GNU_RELRO program
88
+ # header are ignored, the PT_LOAD header determines the effective permissions.
88
89
# However, the dynamic linker need to write to this area so these are RW.
89
90
# Glibc itself takes care of mprotecting this area R after relocations are finished.
90
91
# See also https://marc.info/?l=binutils&m=1498883354122353
91
92
if typ == 'GNU_RELRO' :
92
93
have_gnu_relro = True
93
94
94
95
have_bindnow = False
95
- p = subprocess .Popen ([READELF_CMD , '-d' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
96
- (stdout , stderr ) = p .communicate ()
97
- if p .returncode :
98
- raise IOError ('Error opening file' )
96
+ stdout = run_command ([READELF_CMD , '-d' , '-W' , executable ])
97
+
99
98
for line in stdout .splitlines ():
100
99
tokens = line .split ()
101
100
if len (tokens )> 1 and tokens [1 ] == '(BIND_NOW)' or (len (tokens )> 2 and tokens [1 ] == '(FLAGS)' and 'BIND_NOW' in tokens [2 :]):
102
101
have_bindnow = True
103
102
return have_gnu_relro and have_bindnow
104
103
105
- def check_ELF_Canary (executable ):
104
+ def check_ELF_Canary (executable ) -> bool :
106
105
'''
107
106
Check for use of stack canary
108
107
'''
109
- p = subprocess .Popen ([READELF_CMD , '--dyn-syms' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
110
- (stdout , stderr ) = p .communicate ()
111
- if p .returncode :
112
- raise IOError ('Error opening file' )
108
+ stdout = run_command ([READELF_CMD , '--dyn-syms' , '-W' , executable ])
109
+
113
110
ok = False
114
111
for line in stdout .splitlines ():
115
112
if '__stack_chk_fail' in line :
116
113
ok = True
117
114
return ok
118
115
119
- def get_PE_dll_characteristics (executable ):
120
- '''
121
- Get PE DllCharacteristics bits.
122
- Returns a tuple (arch,bits) where arch is 'i386:x86-64' or 'i386'
123
- and bits is the DllCharacteristics value.
124
- '''
125
- p = subprocess .Popen ([OBJDUMP_CMD , '-x' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
126
- (stdout , stderr ) = p .communicate ()
127
- if p .returncode :
128
- raise IOError ('Error opening file' )
129
- arch = ''
116
+ def get_PE_dll_characteristics (executable ) -> int :
117
+ '''Get PE DllCharacteristics bits'''
118
+ stdout = run_command ([OBJDUMP_CMD , '-x' , executable ])
119
+
130
120
bits = 0
131
121
for line in stdout .splitlines ():
132
122
tokens = line .split ()
133
- if len (tokens )>= 2 and tokens [0 ] == 'architecture:' :
134
- arch = tokens [1 ].rstrip (',' )
135
123
if len (tokens )>= 2 and tokens [0 ] == 'DllCharacteristics' :
136
124
bits = int (tokens [1 ],16 )
137
- return ( arch , bits )
125
+ return bits
138
126
139
127
IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA = 0x0020
140
128
IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE = 0x0040
141
129
IMAGE_DLL_CHARACTERISTICS_NX_COMPAT = 0x0100
142
130
143
- def check_PE_DYNAMIC_BASE (executable ):
131
+ def check_PE_DYNAMIC_BASE (executable ) -> bool :
144
132
'''PIE: DllCharacteristics bit 0x40 signifies dynamicbase (ASLR)'''
145
- (arch ,bits ) = get_PE_dll_characteristics (executable )
146
- reqbits = IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE
147
- return (bits & reqbits ) == reqbits
133
+ bits = get_PE_dll_characteristics (executable )
134
+ return (bits & IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ) == IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE
148
135
149
- # On 64 bit, must support high-entropy 64-bit address space layout randomization in addition to DYNAMIC_BASE
150
- # to have secure ASLR.
151
- def check_PE_HIGH_ENTROPY_VA (executable ):
136
+ # Must support high-entropy 64-bit address space layout randomization
137
+ # in addition to DYNAMIC_BASE to have secure ASLR.
138
+ def check_PE_HIGH_ENTROPY_VA (executable ) -> bool :
152
139
'''PIE: DllCharacteristics bit 0x20 signifies high-entropy ASLR'''
153
- (arch ,bits ) = get_PE_dll_characteristics (executable )
154
- if arch == 'i386:x86-64' :
155
- reqbits = IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA
156
- else : # Unnecessary on 32-bit
157
- assert (arch == 'i386' )
158
- reqbits = 0
159
- return (bits & reqbits ) == reqbits
140
+ bits = get_PE_dll_characteristics (executable )
141
+ return (bits & IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ) == IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA
160
142
161
143
def check_PE_RELOC_SECTION (executable ) -> bool :
162
144
'''Check for a reloc section. This is required for functional ASLR.'''
163
- p = subprocess .Popen ([OBJDUMP_CMD , '-h' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
164
- (stdout , stderr ) = p .communicate ()
165
- if p .returncode :
166
- raise IOError ('Error opening file' )
145
+ stdout = run_command ([OBJDUMP_CMD , '-h' , executable ])
146
+
167
147
for line in stdout .splitlines ():
168
148
if '.reloc' in line :
169
149
return True
170
150
return False
171
151
172
- def check_PE_NX (executable ):
152
+ def check_PE_NX (executable ) -> bool :
173
153
'''NX: DllCharacteristics bit 0x100 signifies nxcompat (DEP)'''
174
- ( arch , bits ) = get_PE_dll_characteristics (executable )
154
+ bits = get_PE_dll_characteristics (executable )
175
155
return (bits & IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ) == IMAGE_DLL_CHARACTERISTICS_NX_COMPAT
176
156
177
- def get_MACHO_executable_flags (executable ):
178
- p = subprocess .Popen ([OTOOL_CMD , '-vh' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
179
- (stdout , stderr ) = p .communicate ()
180
- if p .returncode :
181
- raise IOError ('Error opening file' )
157
+ def get_MACHO_executable_flags (executable ) -> List [str ]:
158
+ stdout = run_command ([OTOOL_CMD , '-vh' , executable ])
182
159
183
160
flags = []
184
161
for line in stdout .splitlines ():
@@ -222,10 +199,7 @@ def check_MACHO_LAZY_BINDINGS(executable) -> bool:
222
199
Check for no lazy bindings.
223
200
We don't use or check for MH_BINDATLOAD. See #18295.
224
201
'''
225
- p = subprocess .Popen ([OTOOL_CMD , '-l' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
226
- (stdout , stderr ) = p .communicate ()
227
- if p .returncode :
228
- raise IOError ('Error opening file' )
202
+ stdout = run_command ([OTOOL_CMD , '-l' , executable ])
229
203
230
204
for line in stdout .splitlines ():
231
205
tokens = line .split ()
@@ -238,10 +212,8 @@ def check_MACHO_Canary(executable) -> bool:
238
212
'''
239
213
Check for use of stack canary
240
214
'''
241
- p = subprocess .Popen ([OTOOL_CMD , '-Iv' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
242
- (stdout , stderr ) = p .communicate ()
243
- if p .returncode :
244
- raise IOError ('Error opening file' )
215
+ stdout = run_command ([OTOOL_CMD , '-Iv' , executable ])
216
+
245
217
ok = False
246
218
for line in stdout .splitlines ():
247
219
if '___stack_chk_fail' in line :
@@ -270,7 +242,7 @@ def check_MACHO_Canary(executable) -> bool:
270
242
]
271
243
}
272
244
273
- def identify_executable (executable ):
245
+ def identify_executable (executable ) -> Optional [ str ] :
274
246
with open (filename , 'rb' ) as f :
275
247
magic = f .read (4 )
276
248
if magic .startswith (b'MZ' ):
@@ -292,18 +264,12 @@ def identify_executable(executable):
292
264
continue
293
265
294
266
failed = []
295
- warning = []
296
267
for (name , func ) in CHECKS [etype ]:
297
268
if not func (filename ):
298
- if name in NONFATAL :
299
- warning .append (name )
300
- else :
301
- failed .append (name )
269
+ failed .append (name )
302
270
if failed :
303
271
print ('%s: failed %s' % (filename , ' ' .join (failed )))
304
272
retval = 1
305
- if warning :
306
- print ('%s: warning %s' % (filename , ' ' .join (warning )))
307
273
except IOError :
308
274
print ('%s: cannot open' % filename )
309
275
retval = 1
0 commit comments