Skip to content

Commit 252c1b0

Browse files
committed
Merge #12829: Python3 fixup
f50975b [contrib] fixup symbol-check.py Python3 support (John Newbery) 5de2b18 [contrib] fixup security-check.py Python3 support (John Newbery) Pull request description: security-check.py and symbol-check.py were broken by #11881. Fix them. Tree-SHA512: 86de3d6dc3292b1ae4cc04c2d7d7dbbf39c9270551d7b224b8d8b19e3184c30c897dbf823200403706d06bb405c0decad5cfd690cb2c0312992a235a4ffcf6bf
2 parents f0f9732 + f50975b commit 252c1b0

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
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(',')

contrib/devtools/symbol-check.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,28 @@
4646

4747
# Ignore symbols that are exported as part of every executable
4848
IGNORE_EXPORTS = {
49-
b'_edata', b'_end', b'_init', b'__bss_start', b'_fini', b'_IO_stdin_used'
49+
'_edata', '_end', '_init', '__bss_start', '_fini', '_IO_stdin_used'
5050
}
5151
READELF_CMD = os.getenv('READELF', '/usr/bin/readelf')
5252
CPPFILT_CMD = os.getenv('CPPFILT', '/usr/bin/c++filt')
5353
# Allowed NEEDED libraries
5454
ALLOWED_LIBRARIES = {
5555
# bitcoind and bitcoin-qt
56-
b'libgcc_s.so.1', # GCC base support
57-
b'libc.so.6', # C library
58-
b'libpthread.so.0', # threading
59-
b'libanl.so.1', # DNS resolve
60-
b'libm.so.6', # math library
61-
b'librt.so.1', # real-time (clock)
62-
b'ld-linux-x86-64.so.2', # 64-bit dynamic linker
63-
b'ld-linux.so.2', # 32-bit dynamic linker
56+
'libgcc_s.so.1', # GCC base support
57+
'libc.so.6', # C library
58+
'libpthread.so.0', # threading
59+
'libanl.so.1', # DNS resolve
60+
'libm.so.6', # math library
61+
'librt.so.1', # real-time (clock)
62+
'ld-linux-x86-64.so.2', # 64-bit dynamic linker
63+
'ld-linux.so.2', # 32-bit dynamic linker
6464
# bitcoin-qt only
65-
b'libX11-xcb.so.1', # part of X11
66-
b'libX11.so.6', # part of X11
67-
b'libxcb.so.1', # part of X11
68-
b'libfontconfig.so.1', # font support
69-
b'libfreetype.so.6', # font parsing
70-
b'libdl.so.2' # programming interface to dynamic linker
65+
'libX11-xcb.so.1', # part of X11
66+
'libX11.so.6', # part of X11
67+
'libxcb.so.1', # part of X11
68+
'libfontconfig.so.1', # font support
69+
'libfreetype.so.6', # font parsing
70+
'libdl.so.2' # programming interface to dynamic linker
7171
}
7272

7373
class CPPFilt(object):
@@ -77,10 +77,10 @@ class CPPFilt(object):
7777
Use a pipe to the 'c++filt' command.
7878
'''
7979
def __init__(self):
80-
self.proc = subprocess.Popen(CPPFILT_CMD, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
80+
self.proc = subprocess.Popen(CPPFILT_CMD, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
8181

8282
def __call__(self, mangled):
83-
self.proc.stdin.write(mangled + b'\n')
83+
self.proc.stdin.write(mangled + '\n')
8484
self.proc.stdin.flush()
8585
return self.proc.stdout.readline().rstrip()
8686

@@ -94,43 +94,43 @@ def read_symbols(executable, imports=True):
9494
Parse an ELF executable and return a list of (symbol,version) tuples
9595
for dynamic, imported symbols.
9696
'''
97-
p = subprocess.Popen([READELF_CMD, '--dyn-syms', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
97+
p = subprocess.Popen([READELF_CMD, '--dyn-syms', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
9898
(stdout, stderr) = p.communicate()
9999
if p.returncode:
100100
raise IOError('Could not read symbols for %s: %s' % (executable, stderr.strip()))
101101
syms = []
102-
for line in stdout.split(b'\n'):
102+
for line in stdout.splitlines():
103103
line = line.split()
104-
if len(line)>7 and re.match(b'[0-9]+:$', line[0]):
105-
(sym, _, version) = line[7].partition(b'@')
106-
is_import = line[6] == b'UND'
107-
if version.startswith(b'@'):
104+
if len(line)>7 and re.match('[0-9]+:$', line[0]):
105+
(sym, _, version) = line[7].partition('@')
106+
is_import = line[6] == 'UND'
107+
if version.startswith('@'):
108108
version = version[1:]
109109
if is_import == imports:
110110
syms.append((sym, version))
111111
return syms
112112

113113
def check_version(max_versions, version):
114-
if b'_' in version:
115-
(lib, _, ver) = version.rpartition(b'_')
114+
if '_' in version:
115+
(lib, _, ver) = version.rpartition('_')
116116
else:
117117
lib = version
118118
ver = '0'
119-
ver = tuple([int(x) for x in ver.split(b'.')])
119+
ver = tuple([int(x) for x in ver.split('.')])
120120
if not lib in max_versions:
121121
return False
122122
return ver <= max_versions[lib]
123123

124124
def read_libraries(filename):
125-
p = subprocess.Popen([READELF_CMD, '-d', '-W', filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
125+
p = subprocess.Popen([READELF_CMD, '-d', '-W', filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
126126
(stdout, stderr) = p.communicate()
127127
if p.returncode:
128128
raise IOError('Error opening file')
129129
libraries = []
130-
for line in stdout.split(b'\n'):
130+
for line in stdout.splitlines():
131131
tokens = line.split()
132-
if len(tokens)>2 and tokens[1] == b'(NEEDED)':
133-
match = re.match(b'^Shared library: \[(.*)\]$', b' '.join(tokens[2:]))
132+
if len(tokens)>2 and tokens[1] == '(NEEDED)':
133+
match = re.match('^Shared library: \[(.*)\]$', ' '.join(tokens[2:]))
134134
if match:
135135
libraries.append(match.group(1))
136136
else:
@@ -144,18 +144,18 @@ def read_libraries(filename):
144144
# Check imported symbols
145145
for sym,version in read_symbols(filename, True):
146146
if version and not check_version(MAX_VERSIONS, version):
147-
print('%s: symbol %s from unsupported version %s' % (filename, cppfilt(sym).decode('utf-8'), version.decode('utf-8')))
147+
print('%s: symbol %s from unsupported version %s' % (filename, cppfilt(sym), version))
148148
retval = 1
149149
# Check exported symbols
150150
for sym,version in read_symbols(filename, False):
151151
if sym in IGNORE_EXPORTS:
152152
continue
153-
print('%s: export of symbol %s not allowed' % (filename, cppfilt(sym).decode('utf-8')))
153+
print('%s: export of symbol %s not allowed' % (filename, cppfilt(sym)))
154154
retval = 1
155155
# Check dependency libraries
156156
for library_name in read_libraries(filename):
157157
if library_name not in ALLOWED_LIBRARIES:
158-
print('%s: NEEDED library %s is not allowed' % (filename, library_name.decode('utf-8')))
158+
print('%s: NEEDED library %s is not allowed' % (filename, library_name))
159159
retval = 1
160160

161161
sys.exit(retval)

0 commit comments

Comments
 (0)