2018-04-16 22:03:08 +02:00
|
|
|
#!/usr/bin/env python3
|
2023-08-16 19:27:31 +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
|
|
|
'''
|
|
|
|
Test script for security-check.py
|
|
|
|
'''
|
2021-12-22 04:18:18 +01:00
|
|
|
import lief #type:ignore
|
2021-03-13 17:27:11 +01:00
|
|
|
import os
|
2015-10-19 14:53:56 +02:00
|
|
|
import subprocess
|
2023-05-13 17:23:21 +02:00
|
|
|
from typing import List
|
2015-10-19 14:53:56 +02:00
|
|
|
import unittest
|
|
|
|
|
2023-06-04 19:34:54 +02:00
|
|
|
from utils import determine_wellknown_cmd
|
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
def write_testcode(filename):
|
2018-06-16 15:21:01 +02:00
|
|
|
with open(filename, 'w', encoding="utf8") as f:
|
2015-10-19 14:53:56 +02:00
|
|
|
f.write('''
|
|
|
|
#include <stdio.h>
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
printf("the quick brown fox jumps over the lazy god\\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
|
2021-03-13 17:27:11 +01:00
|
|
|
def clean_files(source, executable):
|
|
|
|
os.remove(source)
|
|
|
|
os.remove(executable)
|
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
def call_security_check(cc, source, executable, options):
|
2023-05-13 17:23:21 +02:00
|
|
|
# This should behave the same as AC_TRY_LINK, so arrange well-known flags
|
|
|
|
# in the same order as autoconf would.
|
|
|
|
#
|
|
|
|
# See the definitions for ac_link in autoconf's lib/autoconf/c.m4 file for
|
|
|
|
# reference.
|
|
|
|
env_flags: List[str] = []
|
|
|
|
for var in ['CFLAGS', 'CPPFLAGS', 'LDFLAGS']:
|
|
|
|
env_flags += filter(None, os.environ.get(var, '').split(' '))
|
|
|
|
|
|
|
|
subprocess.run([*cc,source,'-o',executable] + env_flags + options, check=True)
|
2023-06-04 19:52:35 +02:00
|
|
|
p = subprocess.run(['./contrib/devtools/security-check.py',executable], stdout=subprocess.PIPE, universal_newlines=True)
|
|
|
|
return (p.returncode, p.stdout.rstrip())
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2021-12-22 04:18:18 +01:00
|
|
|
def get_arch(cc, source, executable):
|
|
|
|
subprocess.run([*cc, source, '-o', executable], check=True)
|
|
|
|
binary = lief.parse(executable)
|
|
|
|
arch = binary.abstract.header.architecture
|
|
|
|
os.remove(executable)
|
|
|
|
return arch
|
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
class TestSecurityChecks(unittest.TestCase):
|
|
|
|
def test_ELF(self):
|
|
|
|
source = 'test1.c'
|
|
|
|
executable = 'test1'
|
2023-06-04 19:34:54 +02:00
|
|
|
cc = determine_wellknown_cmd('CC', 'gcc')
|
2015-10-19 14:53:56 +02:00
|
|
|
write_testcode(source)
|
2021-12-22 04:18:18 +01:00
|
|
|
arch = get_arch(cc, source, executable)
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2021-12-22 04:18:18 +01:00
|
|
|
if arch == lief.ARCHITECTURES.X86:
|
2024-11-03 21:44:22 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-zexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
|
|
|
(1, executable+': failed PIE NX RELRO CONTROL_FLOW'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
2021-12-22 04:18:18 +01:00
|
|
|
(1, executable+': failed PIE RELRO CONTROL_FLOW'))
|
2024-11-03 21:44:22 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
|
|
|
(1, executable+': failed PIE RELRO CONTROL_FLOW'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-pie','-fPIE', '-Wl,-z,separate-code']),
|
2021-12-22 04:18:18 +01:00
|
|
|
(1, executable+': failed RELRO CONTROL_FLOW'))
|
2024-11-03 21:44:22 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,noseparate-code']),
|
2021-12-22 04:18:18 +01:00
|
|
|
(1, executable+': failed separate_code CONTROL_FLOW'))
|
2024-11-03 21:44:22 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code']),
|
2021-12-22 04:18:18 +01:00
|
|
|
(1, executable+': failed CONTROL_FLOW'))
|
2024-11-03 21:44:22 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code', '-fcf-protection=full']),
|
2021-12-22 04:18:18 +01:00
|
|
|
(0, ''))
|
|
|
|
else:
|
2024-11-03 21:44:22 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-zexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
|
|
|
(1, executable+': failed PIE NX RELRO'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
|
|
|
(1, executable+': failed PIE RELRO'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
2021-12-22 04:18:18 +01:00
|
|
|
(1, executable+': failed PIE RELRO'))
|
2024-11-03 21:44:22 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-pie','-fPIE', '-Wl,-z,separate-code']),
|
2021-12-22 04:18:18 +01:00
|
|
|
(1, executable+': failed RELRO'))
|
2024-11-03 21:44:22 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,noseparate-code']),
|
2021-12-22 04:18:18 +01:00
|
|
|
(1, executable+': failed separate_code'))
|
2024-11-03 21:44:22 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code']),
|
2021-12-22 04:18:18 +01:00
|
|
|
(0, ''))
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2021-03-13 17:27:11 +01:00
|
|
|
clean_files(source, executable)
|
|
|
|
|
2020-03-28 11:48:19 +01:00
|
|
|
def test_PE(self):
|
2018-05-07 15:14:33 +02:00
|
|
|
source = 'test1.c'
|
|
|
|
executable = 'test1.exe'
|
2023-06-04 19:34:54 +02:00
|
|
|
cc = determine_wellknown_cmd('CC', 'x86_64-w64-mingw32-gcc')
|
2018-05-07 15:14:33 +02:00
|
|
|
write_testcode(source)
|
|
|
|
|
2023-01-14 13:18:23 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--disable-nxcompat','-Wl,--disable-reloc-section','-Wl,--disable-dynamicbase','-Wl,--disable-high-entropy-va','-no-pie','-fno-PIE','-fno-stack-protector']),
|
|
|
|
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA NX RELOC_SECTION CONTROL_FLOW Canary'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--disable-reloc-section','-Wl,--disable-dynamicbase','-Wl,--disable-high-entropy-va','-no-pie','-fno-PIE','-fstack-protector-all', '-lssp']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA RELOC_SECTION CONTROL_FLOW'))
|
2023-01-14 13:18:23 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--disable-dynamicbase','-Wl,--disable-high-entropy-va','-no-pie','-fno-PIE','-fstack-protector-all', '-lssp']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA CONTROL_FLOW'))
|
2023-01-14 13:18:23 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--disable-dynamicbase','-Wl,--disable-high-entropy-va','-pie','-fPIE','-fstack-protector-all', '-lssp']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA CONTROL_FLOW')) # -pie -fPIE does nothing unless --dynamicbase is also supplied
|
2023-01-14 13:18:23 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--dynamicbase','-Wl,--disable-high-entropy-va','-pie','-fPIE','-fstack-protector-all', '-lssp']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed HIGH_ENTROPY_VA CONTROL_FLOW'))
|
2023-01-14 13:18:23 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--dynamicbase','-Wl,--high-entropy-va','-pie','-fPIE','-fstack-protector-all', '-lssp']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed CONTROL_FLOW'))
|
2023-01-14 13:18:23 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--dynamicbase','-Wl,--high-entropy-va','-pie','-fPIE', '-fcf-protection=full','-fstack-protector-all', '-lssp']),
|
2020-03-28 11:48:19 +01:00
|
|
|
(0, ''))
|
|
|
|
|
2021-03-13 17:27:11 +01:00
|
|
|
clean_files(source, executable)
|
|
|
|
|
2020-03-28 11:48:19 +01:00
|
|
|
def test_MACHO(self):
|
|
|
|
source = 'test1.c'
|
|
|
|
executable = 'test1'
|
2023-06-04 19:34:54 +02:00
|
|
|
cc = determine_wellknown_cmd('CC', 'clang')
|
2020-03-28 11:48:19 +01:00
|
|
|
write_testcode(source)
|
2021-05-03 08:45:08 +02:00
|
|
|
arch = get_arch(cc, source, executable)
|
|
|
|
|
|
|
|
if arch == lief.ARCHITECTURES.X86:
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-Wl,-allow_stack_execute','-fno-stack-protector']),
|
|
|
|
(1, executable+': failed NOUNDEFS LAZY_BINDINGS Canary PIE NX CONTROL_FLOW'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-Wl,-allow_stack_execute','-fstack-protector-all']),
|
|
|
|
(1, executable+': failed NOUNDEFS LAZY_BINDINGS PIE NX CONTROL_FLOW'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-fstack-protector-all']),
|
|
|
|
(1, executable+': failed NOUNDEFS LAZY_BINDINGS PIE CONTROL_FLOW'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-fstack-protector-all']),
|
|
|
|
(1, executable+': failed LAZY_BINDINGS PIE CONTROL_FLOW'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-bind_at_load','-fstack-protector-all']),
|
|
|
|
(1, executable+': failed PIE CONTROL_FLOW'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-bind_at_load','-fstack-protector-all', '-fcf-protection=full']),
|
|
|
|
(1, executable+': failed PIE'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-pie','-Wl,-bind_at_load','-fstack-protector-all', '-fcf-protection=full']),
|
|
|
|
(0, ''))
|
|
|
|
else:
|
|
|
|
# arm64 darwin doesn't support non-PIE binaries, control flow or executable stacks
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-flat_namespace','-fno-stack-protector']),
|
|
|
|
(1, executable+': failed NOUNDEFS LAZY_BINDINGS Canary'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-flat_namespace','-fstack-protector-all']),
|
|
|
|
(1, executable+': failed NOUNDEFS LAZY_BINDINGS'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-fstack-protector-all']),
|
|
|
|
(1, executable+': failed LAZY_BINDINGS'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-bind_at_load','-fstack-protector-all']),
|
|
|
|
(0, ''))
|
2020-03-28 11:48:19 +01:00
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2021-03-13 17:27:11 +01:00
|
|
|
clean_files(source, executable)
|
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|