2017-12-12 20:47:24 +01:00
|
|
|
#!/usr/bin/env python3
|
2020-01-14 20:17:38 +01:00
|
|
|
# Copyright (c) 2015-2020 The Bitcoin Core developers
|
2016-09-11 23:32:22 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2015-10-19 14:53:56 +02:00
|
|
|
'''
|
2020-01-02 07:42:21 +01:00
|
|
|
Perform basic security checks on a series of executables.
|
2016-01-08 22:31:42 +01:00
|
|
|
Exit status will be 0 if successful, and the program will be silent.
|
2015-10-19 14:53:56 +02:00
|
|
|
Otherwise the exit status will be 1 and it will log which executables failed which checks.
|
2020-11-20 09:15:44 +01:00
|
|
|
Needs `objdump` (for PE) and `otool` (for MACHO).
|
2015-10-19 14:53:56 +02:00
|
|
|
'''
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import os
|
2020-04-28 04:05:45 +02:00
|
|
|
from typing import List, Optional
|
|
|
|
|
2020-11-20 09:15:44 +01:00
|
|
|
import pixie
|
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
OBJDUMP_CMD = os.getenv('OBJDUMP', '/usr/bin/objdump')
|
2020-01-02 07:42:21 +01:00
|
|
|
OTOOL_CMD = os.getenv('OTOOL', '/usr/bin/otool')
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2020-04-28 04:05:45 +02:00
|
|
|
def run_command(command) -> str:
|
2020-04-28 03:48:05 +02:00
|
|
|
p = subprocess.run(command, stdout=subprocess.PIPE, check=True, universal_newlines=True)
|
|
|
|
return p.stdout
|
|
|
|
|
2020-04-28 04:05:45 +02:00
|
|
|
def check_ELF_PIE(executable) -> bool:
|
2015-10-19 14:53:56 +02:00
|
|
|
'''
|
|
|
|
Check for position independent executable (PIE), allowing for address space randomization.
|
|
|
|
'''
|
2020-11-20 09:15:44 +01:00
|
|
|
elf = pixie.load(executable)
|
|
|
|
return elf.hdr.e_type == pixie.ET_DYN
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2020-04-28 04:05:45 +02:00
|
|
|
def check_ELF_NX(executable) -> bool:
|
2015-10-19 14:53:56 +02:00
|
|
|
'''
|
|
|
|
Check that no sections are writable and executable (including the stack)
|
|
|
|
'''
|
2020-11-20 09:15:44 +01:00
|
|
|
elf = pixie.load(executable)
|
2015-10-19 14:53:56 +02:00
|
|
|
have_wx = False
|
|
|
|
have_gnu_stack = False
|
2020-11-20 09:15:44 +01:00
|
|
|
for ph in elf.program_headers:
|
|
|
|
if ph.p_type == pixie.PT_GNU_STACK:
|
2015-10-19 14:53:56 +02:00
|
|
|
have_gnu_stack = True
|
2020-11-20 09:15:44 +01:00
|
|
|
if (ph.p_flags & pixie.PF_W) != 0 and (ph.p_flags & pixie.PF_X) != 0: # section is both writable and executable
|
2015-10-19 14:53:56 +02:00
|
|
|
have_wx = True
|
|
|
|
return have_gnu_stack and not have_wx
|
|
|
|
|
2020-04-28 04:05:45 +02:00
|
|
|
def check_ELF_RELRO(executable) -> bool:
|
2015-10-19 14:53:56 +02:00
|
|
|
'''
|
|
|
|
Check for read-only relocations.
|
|
|
|
GNU_RELRO program header must exist
|
|
|
|
Dynamic section must have BIND_NOW flag
|
|
|
|
'''
|
2020-11-20 09:15:44 +01:00
|
|
|
elf = pixie.load(executable)
|
2015-10-19 14:53:56 +02:00
|
|
|
have_gnu_relro = False
|
2020-11-20 09:15:44 +01:00
|
|
|
for ph in elf.program_headers:
|
|
|
|
# Note: not checking p_flags == PF_R: here as linkers set the permission differently
|
2020-04-28 03:48:05 +02:00
|
|
|
# This does not affect security: the permission flags of the GNU_RELRO program
|
|
|
|
# header are ignored, the PT_LOAD header determines the effective permissions.
|
2015-10-19 14:53:56 +02:00
|
|
|
# However, the dynamic linker need to write to this area so these are RW.
|
|
|
|
# Glibc itself takes care of mprotecting this area R after relocations are finished.
|
2018-11-30 14:51:55 +01:00
|
|
|
# See also https://marc.info/?l=binutils&m=1498883354122353
|
2020-11-20 09:15:44 +01:00
|
|
|
if ph.p_type == pixie.PT_GNU_RELRO:
|
2015-10-19 14:53:56 +02:00
|
|
|
have_gnu_relro = True
|
|
|
|
|
|
|
|
have_bindnow = False
|
2020-11-20 09:15:44 +01:00
|
|
|
for flags in elf.query_dyn_tags(pixie.DT_FLAGS):
|
|
|
|
assert isinstance(flags, int)
|
|
|
|
if flags & pixie.DF_BIND_NOW:
|
2015-10-19 14:53:56 +02:00
|
|
|
have_bindnow = True
|
2020-11-20 09:15:44 +01:00
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
return have_gnu_relro and have_bindnow
|
|
|
|
|
2020-04-28 04:05:45 +02:00
|
|
|
def check_ELF_Canary(executable) -> bool:
|
2015-10-19 14:53:56 +02:00
|
|
|
'''
|
|
|
|
Check for use of stack canary
|
|
|
|
'''
|
2020-11-20 09:15:44 +01:00
|
|
|
elf = pixie.load(executable)
|
2015-10-19 14:53:56 +02:00
|
|
|
ok = False
|
2020-11-20 09:15:44 +01:00
|
|
|
for symbol in elf.dyn_symbols:
|
|
|
|
if symbol.name == b'__stack_chk_fail':
|
2015-10-19 14:53:56 +02:00
|
|
|
ok = True
|
|
|
|
return ok
|
|
|
|
|
2020-07-22 16:00:07 +02:00
|
|
|
def check_ELF_separate_code(executable):
|
|
|
|
'''
|
|
|
|
Check that sections are appropriately separated in virtual memory,
|
|
|
|
based on their permissions. This checks for missing -Wl,-z,separate-code
|
|
|
|
and potentially other problems.
|
|
|
|
'''
|
2020-11-20 09:15:44 +01:00
|
|
|
elf = pixie.load(executable)
|
|
|
|
R = pixie.PF_R
|
|
|
|
W = pixie.PF_W
|
|
|
|
E = pixie.PF_X
|
2020-07-22 16:00:07 +02:00
|
|
|
EXPECTED_FLAGS = {
|
|
|
|
# Read + execute
|
2020-11-20 09:15:44 +01:00
|
|
|
b'.init': R | E,
|
|
|
|
b'.plt': R | E,
|
|
|
|
b'.plt.got': R | E,
|
|
|
|
b'.plt.sec': R | E,
|
|
|
|
b'.text': R | E,
|
|
|
|
b'.fini': R | E,
|
2020-07-22 16:00:07 +02:00
|
|
|
# Read-only data
|
2020-11-20 09:15:44 +01:00
|
|
|
b'.interp': R,
|
|
|
|
b'.note.gnu.property': R,
|
|
|
|
b'.note.gnu.build-id': R,
|
|
|
|
b'.note.ABI-tag': R,
|
|
|
|
b'.gnu.hash': R,
|
|
|
|
b'.dynsym': R,
|
|
|
|
b'.dynstr': R,
|
|
|
|
b'.gnu.version': R,
|
|
|
|
b'.gnu.version_r': R,
|
|
|
|
b'.rela.dyn': R,
|
|
|
|
b'.rela.plt': R,
|
|
|
|
b'.rodata': R,
|
|
|
|
b'.eh_frame_hdr': R,
|
|
|
|
b'.eh_frame': R,
|
|
|
|
b'.qtmetadata': R,
|
|
|
|
b'.gcc_except_table': R,
|
|
|
|
b'.stapsdt.base': R,
|
2020-07-22 16:00:07 +02:00
|
|
|
# Writable data
|
2020-11-20 09:15:44 +01:00
|
|
|
b'.init_array': R | W,
|
|
|
|
b'.fini_array': R | W,
|
|
|
|
b'.dynamic': R | W,
|
|
|
|
b'.got': R | W,
|
|
|
|
b'.data': R | W,
|
|
|
|
b'.bss': R | W,
|
2020-07-22 16:00:07 +02:00
|
|
|
}
|
2020-11-20 16:08:25 +01:00
|
|
|
if elf.hdr.e_machine == pixie.EM_PPC64:
|
|
|
|
# .plt is RW on ppc64 even with separate-code
|
|
|
|
EXPECTED_FLAGS[b'.plt'] = R | W
|
2020-07-22 16:00:07 +02:00
|
|
|
# For all LOAD program headers get mapping to the list of sections,
|
|
|
|
# and for each section, remember the flags of the associated program header.
|
|
|
|
flags_per_section = {}
|
2020-11-20 09:15:44 +01:00
|
|
|
for ph in elf.program_headers:
|
|
|
|
if ph.p_type == pixie.PT_LOAD:
|
|
|
|
for section in ph.sections:
|
|
|
|
assert(section.name not in flags_per_section)
|
|
|
|
flags_per_section[section.name] = ph.p_flags
|
2020-07-22 16:00:07 +02:00
|
|
|
# Spot-check ELF LOAD program header flags per section
|
|
|
|
# If these sections exist, check them against the expected R/W/E flags
|
|
|
|
for (section, flags) in flags_per_section.items():
|
|
|
|
if section in EXPECTED_FLAGS:
|
|
|
|
if EXPECTED_FLAGS[section] != flags:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-03-24 03:24:58 +01:00
|
|
|
def get_PE_dll_characteristics(executable) -> int:
|
|
|
|
'''Get PE DllCharacteristics bits'''
|
2020-04-28 03:48:05 +02:00
|
|
|
stdout = run_command([OBJDUMP_CMD, '-x', executable])
|
|
|
|
|
2016-06-23 16:52:12 +02:00
|
|
|
bits = 0
|
2018-03-28 19:37:45 +02:00
|
|
|
for line in stdout.splitlines():
|
2015-10-19 14:53:56 +02:00
|
|
|
tokens = line.split()
|
|
|
|
if len(tokens)>=2 and tokens[0] == 'DllCharacteristics':
|
2016-06-23 16:52:12 +02:00
|
|
|
bits = int(tokens[1],16)
|
2020-03-24 03:24:58 +01:00
|
|
|
return bits
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2016-06-23 16:52:12 +02:00
|
|
|
IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA = 0x0020
|
|
|
|
IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE = 0x0040
|
|
|
|
IMAGE_DLL_CHARACTERISTICS_NX_COMPAT = 0x0100
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2020-04-28 04:05:45 +02:00
|
|
|
def check_PE_DYNAMIC_BASE(executable) -> bool:
|
2015-10-19 14:53:56 +02:00
|
|
|
'''PIE: DllCharacteristics bit 0x40 signifies dynamicbase (ASLR)'''
|
2020-03-24 03:24:58 +01:00
|
|
|
bits = get_PE_dll_characteristics(executable)
|
|
|
|
return (bits & IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE) == IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE
|
2016-06-23 16:52:12 +02:00
|
|
|
|
2020-03-24 03:24:58 +01:00
|
|
|
# Must support high-entropy 64-bit address space layout randomization
|
|
|
|
# in addition to DYNAMIC_BASE to have secure ASLR.
|
2020-04-28 04:05:45 +02:00
|
|
|
def check_PE_HIGH_ENTROPY_VA(executable) -> bool:
|
2016-06-23 16:52:12 +02:00
|
|
|
'''PIE: DllCharacteristics bit 0x20 signifies high-entropy ASLR'''
|
2020-03-24 03:24:58 +01:00
|
|
|
bits = get_PE_dll_characteristics(executable)
|
|
|
|
return (bits & IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA) == IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2020-04-13 14:32:19 +02:00
|
|
|
def check_PE_RELOC_SECTION(executable) -> bool:
|
|
|
|
'''Check for a reloc section. This is required for functional ASLR.'''
|
2020-04-28 03:48:05 +02:00
|
|
|
stdout = run_command([OBJDUMP_CMD, '-h', executable])
|
|
|
|
|
2020-04-13 14:32:19 +02:00
|
|
|
for line in stdout.splitlines():
|
|
|
|
if '.reloc' in line:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-04-28 04:05:45 +02:00
|
|
|
def check_PE_NX(executable) -> bool:
|
2015-10-19 14:53:56 +02:00
|
|
|
'''NX: DllCharacteristics bit 0x100 signifies nxcompat (DEP)'''
|
2020-03-24 03:24:58 +01:00
|
|
|
bits = get_PE_dll_characteristics(executable)
|
2016-06-23 16:52:12 +02:00
|
|
|
return (bits & IMAGE_DLL_CHARACTERISTICS_NX_COMPAT) == IMAGE_DLL_CHARACTERISTICS_NX_COMPAT
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2020-04-28 04:05:45 +02:00
|
|
|
def get_MACHO_executable_flags(executable) -> List[str]:
|
2020-04-28 03:48:05 +02:00
|
|
|
stdout = run_command([OTOOL_CMD, '-vh', executable])
|
2020-01-02 07:42:21 +01:00
|
|
|
|
2020-11-20 09:15:44 +01:00
|
|
|
flags: List[str] = []
|
2020-01-02 07:42:21 +01:00
|
|
|
for line in stdout.splitlines():
|
|
|
|
tokens = line.split()
|
|
|
|
# filter first two header lines
|
|
|
|
if 'magic' in tokens or 'Mach' in tokens:
|
|
|
|
continue
|
|
|
|
# filter ncmds and sizeofcmds values
|
|
|
|
flags += [t for t in tokens if not t.isdigit()]
|
|
|
|
return flags
|
|
|
|
|
|
|
|
def check_MACHO_PIE(executable) -> bool:
|
|
|
|
'''
|
|
|
|
Check for position independent executable (PIE), allowing for address space randomization.
|
|
|
|
'''
|
|
|
|
flags = get_MACHO_executable_flags(executable)
|
|
|
|
if 'PIE' in flags:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-12-31 21:58:13 +01:00
|
|
|
def check_MACHO_NOUNDEFS(executable) -> bool:
|
|
|
|
'''
|
|
|
|
Check for no undefined references.
|
|
|
|
'''
|
|
|
|
flags = get_MACHO_executable_flags(executable)
|
|
|
|
if 'NOUNDEFS' in flags:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-03-25 01:11:20 +01:00
|
|
|
def check_MACHO_NX(executable) -> bool:
|
|
|
|
'''
|
|
|
|
Check for no stack execution
|
|
|
|
'''
|
|
|
|
flags = get_MACHO_executable_flags(executable)
|
|
|
|
if 'ALLOW_STACK_EXECUTION' in flags:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-03-08 06:57:49 +01:00
|
|
|
def check_MACHO_LAZY_BINDINGS(executable) -> bool:
|
|
|
|
'''
|
|
|
|
Check for no lazy bindings.
|
|
|
|
We don't use or check for MH_BINDATLOAD. See #18295.
|
|
|
|
'''
|
2020-04-28 03:48:05 +02:00
|
|
|
stdout = run_command([OTOOL_CMD, '-l', executable])
|
2020-03-08 06:57:49 +01:00
|
|
|
|
|
|
|
for line in stdout.splitlines():
|
|
|
|
tokens = line.split()
|
|
|
|
if 'lazy_bind_off' in tokens or 'lazy_bind_size' in tokens:
|
|
|
|
if tokens[1] != '0':
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-04-21 03:41:17 +02:00
|
|
|
def check_MACHO_Canary(executable) -> bool:
|
|
|
|
'''
|
|
|
|
Check for use of stack canary
|
|
|
|
'''
|
2020-04-28 03:48:05 +02:00
|
|
|
stdout = run_command([OTOOL_CMD, '-Iv', executable])
|
|
|
|
|
2020-04-21 03:41:17 +02:00
|
|
|
ok = False
|
|
|
|
for line in stdout.splitlines():
|
|
|
|
if '___stack_chk_fail' in line:
|
|
|
|
ok = True
|
|
|
|
return ok
|
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
CHECKS = {
|
|
|
|
'ELF': [
|
|
|
|
('PIE', check_ELF_PIE),
|
|
|
|
('NX', check_ELF_NX),
|
|
|
|
('RELRO', check_ELF_RELRO),
|
2020-07-22 16:00:07 +02:00
|
|
|
('Canary', check_ELF_Canary),
|
|
|
|
('separate_code', check_ELF_separate_code),
|
2015-10-19 14:53:56 +02:00
|
|
|
],
|
|
|
|
'PE': [
|
2016-06-23 16:52:12 +02:00
|
|
|
('DYNAMIC_BASE', check_PE_DYNAMIC_BASE),
|
|
|
|
('HIGH_ENTROPY_VA', check_PE_HIGH_ENTROPY_VA),
|
2020-04-13 14:32:19 +02:00
|
|
|
('NX', check_PE_NX),
|
|
|
|
('RELOC_SECTION', check_PE_RELOC_SECTION)
|
2020-01-02 07:42:21 +01:00
|
|
|
],
|
|
|
|
'MACHO': [
|
|
|
|
('PIE', check_MACHO_PIE),
|
2019-12-31 21:58:13 +01:00
|
|
|
('NOUNDEFS', check_MACHO_NOUNDEFS),
|
2020-03-08 06:57:49 +01:00
|
|
|
('NX', check_MACHO_NX),
|
2020-04-21 03:41:17 +02:00
|
|
|
('LAZY_BINDINGS', check_MACHO_LAZY_BINDINGS),
|
|
|
|
('Canary', check_MACHO_Canary)
|
2015-10-19 14:53:56 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-04-28 04:05:45 +02:00
|
|
|
def identify_executable(executable) -> Optional[str]:
|
2015-10-19 14:53:56 +02:00
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
magic = f.read(4)
|
|
|
|
if magic.startswith(b'MZ'):
|
|
|
|
return 'PE'
|
|
|
|
elif magic.startswith(b'\x7fELF'):
|
|
|
|
return 'ELF'
|
2020-01-02 07:42:21 +01:00
|
|
|
elif magic.startswith(b'\xcf\xfa'):
|
|
|
|
return 'MACHO'
|
2015-10-19 14:53:56 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
retval = 0
|
|
|
|
for filename in sys.argv[1:]:
|
|
|
|
try:
|
|
|
|
etype = identify_executable(filename)
|
|
|
|
if etype is None:
|
|
|
|
print('%s: unknown format' % filename)
|
|
|
|
retval = 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
failed = []
|
|
|
|
for (name, func) in CHECKS[etype]:
|
|
|
|
if not func(filename):
|
2020-04-28 03:30:35 +02:00
|
|
|
failed.append(name)
|
2015-10-19 14:53:56 +02:00
|
|
|
if failed:
|
|
|
|
print('%s: failed %s' % (filename, ' '.join(failed)))
|
|
|
|
retval = 1
|
|
|
|
except IOError:
|
|
|
|
print('%s: cannot open' % filename)
|
|
|
|
retval = 1
|
2017-07-09 22:57:02 +02:00
|
|
|
sys.exit(retval)
|
2015-10-19 14:53:56 +02:00
|
|
|
|