Skip to content

Commit 83d063e

Browse files
committed
scripts: add run_command to security-check.py
Deduplicate all the subprocess code as mentioned in 18713.
1 parent 13f606b commit 83d063e

File tree

1 file changed

+21
-37
lines changed

1 file changed

+21
-37
lines changed

contrib/devtools/security-check.py

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
OBJDUMP_CMD = os.getenv('OBJDUMP', '/usr/bin/objdump')
1717
OTOOL_CMD = os.getenv('OTOOL', '/usr/bin/otool')
1818

19+
def run_command(command):
20+
p = subprocess.run(command, stdout=subprocess.PIPE, check=True, universal_newlines=True)
21+
return p.stdout
22+
1923
def check_ELF_PIE(executable):
2024
'''
2125
Check for position independent executable (PIE), allowing for address space randomization.
2226
'''
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])
2728

2829
ok = False
2930
for line in stdout.splitlines():
@@ -34,10 +35,8 @@ def check_ELF_PIE(executable):
3435

3536
def get_ELF_program_headers(executable):
3637
'''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+
4140
in_headers = False
4241
count = 0
4342
headers = []
@@ -83,18 +82,17 @@ def check_ELF_RELRO(executable):
8382
have_gnu_relro = False
8483
for (typ, flags) in get_ELF_program_headers(executable):
8584
# 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.
8787
# However, the dynamic linker need to write to this area so these are RW.
8888
# Glibc itself takes care of mprotecting this area R after relocations are finished.
8989
# See also https://marc.info/?l=binutils&m=1498883354122353
9090
if typ == 'GNU_RELRO':
9191
have_gnu_relro = True
9292

9393
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+
9896
for line in stdout.splitlines():
9997
tokens = line.split()
10098
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):
105103
'''
106104
Check for use of stack canary
107105
'''
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+
112108
ok = False
113109
for line in stdout.splitlines():
114110
if '__stack_chk_fail' in line:
@@ -117,10 +113,8 @@ def check_ELF_Canary(executable):
117113

118114
def get_PE_dll_characteristics(executable) -> int:
119115
'''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+
124118
bits = 0
125119
for line in stdout.splitlines():
126120
tokens = line.split()
@@ -146,10 +140,8 @@ def check_PE_HIGH_ENTROPY_VA(executable):
146140

147141
def check_PE_RELOC_SECTION(executable) -> bool:
148142
'''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+
153145
for line in stdout.splitlines():
154146
if '.reloc' in line:
155147
return True
@@ -161,10 +153,7 @@ def check_PE_NX(executable):
161153
return (bits & IMAGE_DLL_CHARACTERISTICS_NX_COMPAT) == IMAGE_DLL_CHARACTERISTICS_NX_COMPAT
162154

163155
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])
168157

169158
flags = []
170159
for line in stdout.splitlines():
@@ -208,10 +197,7 @@ def check_MACHO_LAZY_BINDINGS(executable) -> bool:
208197
Check for no lazy bindings.
209198
We don't use or check for MH_BINDATLOAD. See #18295.
210199
'''
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])
215201

216202
for line in stdout.splitlines():
217203
tokens = line.split()
@@ -224,10 +210,8 @@ def check_MACHO_Canary(executable) -> bool:
224210
'''
225211
Check for use of stack canary
226212
'''
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+
231215
ok = False
232216
for line in stdout.splitlines():
233217
if '___stack_chk_fail' in line:

0 commit comments

Comments
 (0)