Skip to content

Commit 5de2b18

Browse files
committed
[contrib] fixup security-check.py Python3 support
1 parent 624bee9 commit 5de2b18

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

contrib/devtools/security-check.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,38 @@ def check_ELF_PIE(executable):
2020
'''
2121
Check for position independent executable (PIE), allowing for address space randomization.
2222
'''
23-
p = subprocess.Popen([READELF_CMD, '-h', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
23+
p = subprocess.Popen([READELF_CMD, '-h', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
2424
(stdout, stderr) = p.communicate()
2525
if p.returncode:
2626
raise IOError('Error opening file')
2727

2828
ok = False
29-
for line in stdout.split(b'\n'):
29+
for line in stdout.splitlines():
3030
line = line.split()
31-
if len(line)>=2 and line[0] == b'Type:' and line[1] == b'DYN':
31+
if len(line)>=2 and line[0] == 'Type:' and line[1] == 'DYN':
3232
ok = True
3333
return ok
3434

3535
def get_ELF_program_headers(executable):
3636
'''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)
37+
p = subprocess.Popen([READELF_CMD, '-l', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
3838
(stdout, stderr) = p.communicate()
3939
if p.returncode:
4040
raise IOError('Error opening file')
4141
in_headers = False
4242
count = 0
4343
headers = []
44-
for line in stdout.split(b'\n'):
45-
if line.startswith(b'Program Headers:'):
44+
for line in stdout.splitlines():
45+
if line.startswith('Program Headers:'):
4646
in_headers = True
47-
if line == b'':
47+
if line == '':
4848
in_headers = False
4949
if in_headers:
5050
if count == 1: # header line
51-
ofs_typ = line.find(b'Type')
52-
ofs_offset = line.find(b'Offset')
53-
ofs_flags = line.find(b'Flg')
54-
ofs_align = line.find(b'Align')
51+
ofs_typ = line.find('Type')
52+
ofs_offset = line.find('Offset')
53+
ofs_flags = line.find('Flg')
54+
ofs_align = line.find('Align')
5555
if ofs_typ == -1 or ofs_offset == -1 or ofs_flags == -1 or ofs_align == -1:
5656
raise ValueError('Cannot parse elfread -lW output')
5757
elif count > 1:
@@ -68,9 +68,9 @@ def check_ELF_NX(executable):
6868
have_wx = False
6969
have_gnu_stack = False
7070
for (typ, flags) in get_ELF_program_headers(executable):
71-
if typ == b'GNU_STACK':
71+
if typ == 'GNU_STACK':
7272
have_gnu_stack = True
73-
if b'W' in flags and b'E' in flags: # section is both writable and executable
73+
if 'W' in flags and 'E' in flags: # section is both writable and executable
7474
have_wx = True
7575
return have_gnu_stack and not have_wx
7676

@@ -87,31 +87,31 @@ def check_ELF_RELRO(executable):
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 http://permalink.gmane.org/gmane.comp.gnu.binutils/71347
90-
if typ == b'GNU_RELRO':
90+
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)
94+
p = subprocess.Popen([READELF_CMD, '-d', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
9595
(stdout, stderr) = p.communicate()
9696
if p.returncode:
9797
raise IOError('Error opening file')
98-
for line in stdout.split(b'\n'):
98+
for line in stdout.splitlines():
9999
tokens = line.split()
100-
if len(tokens)>1 and tokens[1] == b'(BIND_NOW)' or (len(tokens)>2 and tokens[1] == b'(FLAGS)' and b'BIND_NOW' in tokens[2]):
100+
if len(tokens)>1 and tokens[1] == '(BIND_NOW)' or (len(tokens)>2 and tokens[1] == '(FLAGS)' and 'BIND_NOW' in tokens[2]):
101101
have_bindnow = True
102102
return have_gnu_relro and have_bindnow
103103

104104
def check_ELF_Canary(executable):
105105
'''
106106
Check for use of stack canary
107107
'''
108-
p = subprocess.Popen([READELF_CMD, '--dyn-syms', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
108+
p = subprocess.Popen([READELF_CMD, '--dyn-syms', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
109109
(stdout, stderr) = p.communicate()
110110
if p.returncode:
111111
raise IOError('Error opening file')
112112
ok = False
113-
for line in stdout.split(b'\n'):
114-
if b'__stack_chk_fail' in line:
113+
for line in stdout.splitlines():
114+
if '__stack_chk_fail' in line:
115115
ok = True
116116
return ok
117117

@@ -121,13 +121,13 @@ def get_PE_dll_characteristics(executable):
121121
Returns a tuple (arch,bits) where arch is 'i386:x86-64' or 'i386'
122122
and bits is the DllCharacteristics value.
123123
'''
124-
p = subprocess.Popen([OBJDUMP_CMD, '-x', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
124+
p = subprocess.Popen([OBJDUMP_CMD, '-x', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
125125
(stdout, stderr) = p.communicate()
126126
if p.returncode:
127127
raise IOError('Error opening file')
128128
arch = ''
129129
bits = 0
130-
for line in stdout.split('\n'):
130+
for line in stdout.splitlines():
131131
tokens = line.split()
132132
if len(tokens)>=2 and tokens[0] == 'architecture:':
133133
arch = tokens[1].rstrip(',')

0 commit comments

Comments
 (0)