2018-08-13 18:07:52 +02:00
|
|
|
#!/usr/bin/env python3
|
2023-04-25 13:51:26 +02:00
|
|
|
# Copyright (c) 2015-2020 The Bitcoin Core developers
|
2016-09-19 17:02:23 +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 12:44:32 +01:00
|
|
|
Perform basic security checks on a series of executables.
|
2016-01-18 10:45:19 +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.
|
|
|
|
'''
|
|
|
|
import sys
|
2023-05-13 17:23:21 +02:00
|
|
|
from typing import List
|
2020-05-14 19:59:54 +02:00
|
|
|
|
2023-05-13 19:44:39 +02:00
|
|
|
import lief
|
2020-11-20 09:15:44 +01:00
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_ELF_RELRO(binary) -> 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
|
|
|
|
'''
|
|
|
|
have_gnu_relro = False
|
2023-05-13 17:23:21 +02:00
|
|
|
for segment in binary.segments:
|
2020-11-20 09:15:44 +01:00
|
|
|
# Note: not checking p_flags == PF_R: here as linkers set the permission differently
|
2020-05-14 19:59:54 +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-12-04 11:46:21 +01:00
|
|
|
# See also https://marc.info/?l=binutils&m=1498883354122353
|
2023-05-13 17:23:21 +02:00
|
|
|
if segment.type == lief.ELF.SEGMENT_TYPES.GNU_RELRO:
|
2015-10-19 14:53:56 +02:00
|
|
|
have_gnu_relro = True
|
|
|
|
|
|
|
|
have_bindnow = False
|
2023-05-13 17:23:21 +02:00
|
|
|
try:
|
|
|
|
flags = binary.get(lief.ELF.DYNAMIC_TAGS.FLAGS)
|
|
|
|
if flags.value & lief.ELF.DYNAMIC_FLAGS.BIND_NOW:
|
2015-10-19 14:53:56 +02:00
|
|
|
have_bindnow = True
|
2023-05-13 17:23:21 +02:00
|
|
|
except:
|
|
|
|
have_bindnow = False
|
2020-11-20 09:15:44 +01:00
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
return have_gnu_relro and have_bindnow
|
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_ELF_Canary(binary) -> bool:
|
2015-10-19 14:53:56 +02:00
|
|
|
'''
|
|
|
|
Check for use of stack canary
|
|
|
|
'''
|
2023-05-13 17:23:21 +02:00
|
|
|
return binary.has_symbol('__stack_chk_fail')
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_ELF_separate_code(binary):
|
2020-07-10 14:53:28 +02:00
|
|
|
'''
|
|
|
|
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.
|
|
|
|
'''
|
2023-05-13 17:23:21 +02:00
|
|
|
R = lief.ELF.SEGMENT_FLAGS.R
|
|
|
|
W = lief.ELF.SEGMENT_FLAGS.W
|
|
|
|
E = lief.ELF.SEGMENT_FLAGS.X
|
2020-07-10 14:53:28 +02:00
|
|
|
EXPECTED_FLAGS = {
|
|
|
|
# Read + execute
|
2023-05-13 17:23:21 +02:00
|
|
|
'.init': R | E,
|
|
|
|
'.plt': R | E,
|
|
|
|
'.plt.got': R | E,
|
|
|
|
'.plt.sec': R | E,
|
|
|
|
'.text': R | E,
|
|
|
|
'.fini': R | E,
|
2020-07-10 14:53:28 +02:00
|
|
|
# Read-only data
|
2023-05-13 17:23:21 +02:00
|
|
|
'.interp': R,
|
|
|
|
'.note.gnu.property': R,
|
|
|
|
'.note.gnu.build-id': R,
|
|
|
|
'.note.ABI-tag': R,
|
|
|
|
'.gnu.hash': R,
|
|
|
|
'.dynsym': R,
|
|
|
|
'.dynstr': R,
|
|
|
|
'.gnu.version': R,
|
|
|
|
'.gnu.version_r': R,
|
|
|
|
'.rela.dyn': R,
|
|
|
|
'.rela.plt': R,
|
|
|
|
'.rodata': R,
|
|
|
|
'.eh_frame_hdr': R,
|
|
|
|
'.eh_frame': R,
|
|
|
|
'.qtmetadata': R,
|
|
|
|
'.gcc_except_table': R,
|
|
|
|
'.stapsdt.base': R,
|
2020-07-10 14:53:28 +02:00
|
|
|
# Writable data
|
2023-05-13 17:23:21 +02:00
|
|
|
'.init_array': R | W,
|
|
|
|
'.fini_array': R | W,
|
|
|
|
'.dynamic': R | W,
|
|
|
|
'.got': R | W,
|
|
|
|
'.data': R | W,
|
|
|
|
'.bss': R | W,
|
2020-07-10 14:53:28 +02:00
|
|
|
}
|
2023-05-13 17:23:21 +02:00
|
|
|
if binary.header.machine_type == lief.ELF.ARCH.PPC64:
|
2020-11-20 09:15:44 +01:00
|
|
|
# .plt is RW on ppc64 even with separate-code
|
2023-05-13 17:23:21 +02:00
|
|
|
EXPECTED_FLAGS['.plt'] = R | W
|
2020-07-10 14:53:28 +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 = {}
|
2023-05-13 17:23:21 +02:00
|
|
|
for segment in binary.segments:
|
|
|
|
if segment.type == lief.ELF.SEGMENT_TYPES.LOAD:
|
|
|
|
for section in segment.sections:
|
|
|
|
flags_per_section[section.name] = segment.flags
|
2020-07-10 14:53:28 +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:
|
2023-05-13 17:23:21 +02:00
|
|
|
if int(EXPECTED_FLAGS[section]) != int(flags):
|
2020-07-10 14:53:28 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-12-22 04:18:18 +01:00
|
|
|
def check_ELF_control_flow(binary) -> bool:
|
|
|
|
'''
|
|
|
|
Check for control flow instrumentation
|
|
|
|
'''
|
|
|
|
main = binary.get_function_address('main')
|
|
|
|
content = binary.get_content_from_virtual_address(main, 4, lief.Binary.VA_TYPES.AUTO)
|
|
|
|
|
2024-11-05 15:28:07 +01:00
|
|
|
if content.tolist() == [243, 15, 30, 250]: # endbr64
|
2021-12-22 04:18:18 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_PE_DYNAMIC_BASE(binary) -> bool:
|
2015-10-19 14:53:56 +02:00
|
|
|
'''PIE: DllCharacteristics bit 0x40 signifies dynamicbase (ASLR)'''
|
2023-05-13 19:44:39 +02:00
|
|
|
return lief.PE.DLL_CHARACTERISTICS.DYNAMIC_BASE in binary.optional_header.dll_characteristics_lists
|
2016-09-26 13:03:44 +02:00
|
|
|
|
2020-05-14 19:59:54 +02:00
|
|
|
# Must support high-entropy 64-bit address space layout randomization
|
|
|
|
# in addition to DYNAMIC_BASE to have secure ASLR.
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_PE_HIGH_ENTROPY_VA(binary) -> bool:
|
2016-09-26 13:03:44 +02:00
|
|
|
'''PIE: DllCharacteristics bit 0x20 signifies high-entropy ASLR'''
|
2023-05-13 19:44:39 +02:00
|
|
|
return lief.PE.DLL_CHARACTERISTICS.HIGH_ENTROPY_VA in binary.optional_header.dll_characteristics_lists
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_PE_RELOC_SECTION(binary) -> bool:
|
Merge #18629: scripts: add PE .reloc section check to security-check.py
3e38023af724a76972d39cbccfb0bba4c54a0323 scripts: add PE .reloc section check to security-check.py (fanquake)
Pull request description:
The `ld` in binutils has historically had a few issues with PE binaries, there's a good summary in this [thread](https://sourceware.org/bugzilla/show_bug.cgi?id=19011).
One issue in particular was `ld` stripping the `.reloc` section out of PE binaries, even though it's required for functioning ASLR. This was [reported by a Tor developer in 2014](https://sourceware.org/bugzilla/show_bug.cgi?id=17321) and they have been patching their [own binutils](https://gitweb.torproject.org/builders/tor-browser-build.git/tree/projects/binutils) ever since. However their patch only made it into binutils at the [start of this year](https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=dc9bd8c92af67947db44b3cb428c050259b15cd0). It adds an `--enable-reloc-section` flag, which is turned on by default if you are using `--dynamic-base`. In the mean time this issue has also been worked around by other projects, such as FFmpeg, see [this commit](https://github.com/TheRyuu/FFmpeg/commit/91b668acd6decec0a6f8d20bf56e2644f96adcb9).
I have checked our recent supported Windows release binaries, and they do contain a `.reloc` section. From what I understand, we are using all the right compile/linker flags, including `-pie` & `-fPIE`, and have never run into the crashing/entrypoint issues that other projects might have seen.
One other thing worth noting here, it how Debian/Ubuntu patch the binutils that they distribute, because that's what we end up using in our gitian builds.
In the binutils-mingw-w64 in Bionic (18.04), which we currently use in gitian, PE hardening options/security flags are enabled by default. See the [changelog](https://changelogs.ubuntu.com/changelogs/pool/universe/b/binutils-mingw-w64/binutils-mingw-w64_8ubuntu1/changelog) and the [relevant commit](https://salsa.debian.org/mingw-w64-team/binutils-mingw-w64/-/commit/452b3013b8280cbe35eaeb166a43621b88d5f8b7).
However in Focal (20.04), this has now been reversed. PE hardening options are no-longer the default. See the [changelog](https://changelogs.ubuntu.com/changelogs/pool/universe/b/binutils-mingw-w64/binutils-mingw-w64_8.8/changelog) and [relevant commit](https://salsa.debian.org/mingw-w64-team/binutils-mingw-w64/-/commit/7bd8b2fbc242a8c2fc2217f29fd61f94d3babf6f), which cites same .reloc issue mentioned here.
Given that we explicitly specify/opt-in to everything that we want to use, the defaults aren't necessarily an issue for us. However I think it highlights the importance of continuing to be explicit about what we want, and not falling-back or relying on upstream.
This was also prompted by the possibility of us doing link time garbage collection, see #18579 & #18605. It seemed some sanity checks would be worthwhile in-case the linker goes haywire while garbage collecting.
I think Guix is going to bring great benefits when dealing with these kinds of issues. Carl you might have something to say in that regard.
ACKs for top commit:
dongcarl:
ACK 3e38023af724a76972d39cbccfb0bba4c54a0323
Tree-SHA512: af14d63bdb334bde548dd7de3e0946556b7e2598d817b56eb4e75b3f56c705c26aa85dd9783134c4b6a7aeb7cb4de567eed996e94d533d31511f57ed332287da
2020-04-28 07:08:19 +02:00
|
|
|
'''Check for a reloc section. This is required for functional ASLR.'''
|
2023-05-13 19:44:39 +02:00
|
|
|
return binary.has_relocations
|
2020-01-02 12:44:32 +01:00
|
|
|
|
2021-11-17 02:43:01 +01:00
|
|
|
def check_PE_control_flow(binary) -> bool:
|
|
|
|
'''
|
|
|
|
Check for control flow instrumentation
|
|
|
|
'''
|
|
|
|
main = binary.get_symbol('main').value
|
|
|
|
|
|
|
|
section_addr = binary.section_from_rva(main).virtual_address
|
|
|
|
virtual_address = binary.optional_header.imagebase + section_addr + main
|
|
|
|
|
|
|
|
content = binary.get_content_from_virtual_address(virtual_address, 4, lief.Binary.VA_TYPES.VA)
|
|
|
|
|
2024-11-05 15:28:07 +01:00
|
|
|
if content.tolist() == [243, 15, 30, 250]: # endbr64
|
2021-11-17 02:43:01 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2023-01-14 13:18:23 +01:00
|
|
|
def check_PE_Canary(binary) -> bool:
|
|
|
|
'''
|
|
|
|
Check for use of stack canary
|
|
|
|
'''
|
|
|
|
return binary.has_symbol('__stack_chk_fail')
|
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_MACHO_NOUNDEFS(binary) -> bool:
|
2020-01-02 12:44:32 +01:00
|
|
|
'''
|
|
|
|
Check for no undefined references.
|
|
|
|
'''
|
2023-05-13 19:44:39 +02:00
|
|
|
return binary.header.has(lief.MachO.HEADER_FLAGS.NOUNDEFS)
|
2020-03-28 11:48:19 +01:00
|
|
|
|
2024-11-04 10:36:25 +01:00
|
|
|
def check_MACHO_FIXUP_CHAINS(binary) -> bool:
|
|
|
|
'''
|
|
|
|
Check for use of chained fixups.
|
|
|
|
'''
|
|
|
|
return binary.has_dyld_chained_fixups
|
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_MACHO_Canary(binary) -> bool:
|
2020-04-22 10:19:51 +02:00
|
|
|
'''
|
|
|
|
Check for use of stack canary
|
|
|
|
'''
|
2023-05-13 19:44:39 +02:00
|
|
|
return binary.has_symbol('___stack_chk_fail')
|
2020-05-14 19:59:54 +02:00
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_PIE(binary) -> bool:
|
2023-05-13 19:44:39 +02:00
|
|
|
'''
|
|
|
|
Check for position independent executable (PIE),
|
|
|
|
allowing for address space randomization.
|
|
|
|
'''
|
|
|
|
return binary.is_pie
|
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
def check_NX(binary) -> bool:
|
2023-05-13 19:44:39 +02:00
|
|
|
'''
|
|
|
|
Check for no stack execution
|
|
|
|
'''
|
|
|
|
return binary.has_nx
|
2020-04-22 10:19:51 +02:00
|
|
|
|
2021-12-22 04:18:18 +01:00
|
|
|
def check_MACHO_control_flow(binary) -> bool:
|
2021-05-09 05:32:59 +02:00
|
|
|
'''
|
|
|
|
Check for control flow instrumentation
|
|
|
|
'''
|
|
|
|
content = binary.get_content_from_virtual_address(binary.entrypoint, 4, lief.Binary.VA_TYPES.AUTO)
|
|
|
|
|
2024-11-05 15:28:07 +01:00
|
|
|
if content.tolist() == [243, 15, 30, 250]: # endbr64
|
2021-05-09 05:32:59 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-12-16 08:05:44 +01:00
|
|
|
BASE_ELF = [
|
2023-05-13 17:23:21 +02:00
|
|
|
('PIE', check_PIE),
|
|
|
|
('NX', check_NX),
|
2015-10-19 14:53:56 +02:00
|
|
|
('RELRO', check_ELF_RELRO),
|
2020-07-10 14:53:28 +02:00
|
|
|
('Canary', check_ELF_Canary),
|
|
|
|
('separate_code', check_ELF_separate_code),
|
2021-12-16 08:05:44 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
BASE_PE = [
|
2023-05-13 19:44:39 +02:00
|
|
|
('PIE', check_PIE),
|
2016-09-26 13:03:44 +02:00
|
|
|
('DYNAMIC_BASE', check_PE_DYNAMIC_BASE),
|
|
|
|
('HIGH_ENTROPY_VA', check_PE_HIGH_ENTROPY_VA),
|
2023-05-13 19:44:39 +02:00
|
|
|
('NX', check_NX),
|
2021-11-17 02:43:01 +01:00
|
|
|
('RELOC_SECTION', check_PE_RELOC_SECTION),
|
|
|
|
('CONTROL_FLOW', check_PE_control_flow),
|
2023-01-14 13:18:23 +01:00
|
|
|
('Canary', check_PE_Canary),
|
2021-12-16 08:05:44 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
BASE_MACHO = [
|
2020-01-02 12:44:32 +01:00
|
|
|
('NOUNDEFS', check_MACHO_NOUNDEFS),
|
2021-05-09 05:32:59 +02:00
|
|
|
('Canary', check_MACHO_Canary),
|
2024-11-04 10:36:25 +01:00
|
|
|
('FIXUP_CHAINS', check_MACHO_FIXUP_CHAINS),
|
2015-10-19 14:53:56 +02:00
|
|
|
]
|
2021-12-16 08:05:44 +01:00
|
|
|
|
|
|
|
CHECKS = {
|
|
|
|
lief.EXE_FORMATS.ELF: {
|
2021-12-22 04:18:18 +01:00
|
|
|
lief.ARCHITECTURES.X86: BASE_ELF + [('CONTROL_FLOW', check_ELF_control_flow)],
|
2021-12-16 08:05:44 +01:00
|
|
|
lief.ARCHITECTURES.ARM: BASE_ELF,
|
|
|
|
lief.ARCHITECTURES.ARM64: BASE_ELF,
|
|
|
|
lief.ARCHITECTURES.PPC: BASE_ELF,
|
2023-05-13 17:27:38 +02:00
|
|
|
lief.ARCHITECTURES.RISCV: BASE_ELF,
|
2021-12-16 08:05:44 +01:00
|
|
|
},
|
|
|
|
lief.EXE_FORMATS.PE: {
|
|
|
|
lief.ARCHITECTURES.X86: BASE_PE,
|
|
|
|
},
|
|
|
|
lief.EXE_FORMATS.MACHO: {
|
2021-05-03 08:45:08 +02:00
|
|
|
lief.ARCHITECTURES.X86: BASE_MACHO + [('PIE', check_PIE),
|
|
|
|
('NX', check_NX),
|
|
|
|
('CONTROL_FLOW', check_MACHO_control_flow)],
|
|
|
|
lief.ARCHITECTURES.ARM64: BASE_MACHO,
|
2021-12-16 08:05:44 +01:00
|
|
|
}
|
2015-10-19 14:53:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-05-13 19:44:39 +02:00
|
|
|
retval: int = 0
|
2015-10-19 14:53:56 +02:00
|
|
|
for filename in sys.argv[1:]:
|
|
|
|
try:
|
2023-05-13 17:23:21 +02:00
|
|
|
binary = lief.parse(filename)
|
2021-12-18 04:41:36 +01:00
|
|
|
etype = binary.format
|
2021-12-16 08:05:44 +01:00
|
|
|
arch = binary.abstract.header.architecture
|
|
|
|
binary.concrete
|
|
|
|
|
2023-05-13 17:23:21 +02:00
|
|
|
if etype == lief.EXE_FORMATS.UNKNOWN:
|
|
|
|
print(f'{filename}: unknown executable format')
|
2015-10-19 14:53:56 +02:00
|
|
|
retval = 1
|
|
|
|
continue
|
|
|
|
|
2021-12-16 08:05:44 +01:00
|
|
|
if arch == lief.ARCHITECTURES.NONE:
|
2023-05-13 17:27:38 +02:00
|
|
|
print(f'{filename}: unknown architecture')
|
|
|
|
retval = 1
|
|
|
|
continue
|
2021-12-16 08:05:44 +01:00
|
|
|
|
2023-05-13 19:44:39 +02:00
|
|
|
failed: List[str] = []
|
2021-12-16 08:05:44 +01:00
|
|
|
for (name, func) in CHECKS[etype][arch]:
|
2023-05-13 17:23:21 +02:00
|
|
|
if not func(binary):
|
2020-05-14 19:59:54 +02:00
|
|
|
failed.append(name)
|
2015-10-19 14:53:56 +02:00
|
|
|
if failed:
|
2023-05-13 19:44:39 +02:00
|
|
|
print(f'{filename}: failed {" ".join(failed)}')
|
2015-10-19 14:53:56 +02:00
|
|
|
retval = 1
|
|
|
|
except IOError:
|
2023-05-13 19:44:39 +02:00
|
|
|
print(f'{filename}: cannot open')
|
2015-10-19 14:53:56 +02:00
|
|
|
retval = 1
|
2017-08-28 22:53:34 +02:00
|
|
|
sys.exit(retval)
|
2015-10-19 14:53:56 +02:00
|
|
|
|