2018-04-16 22:03:08 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-09-19 17:02:23 +02:00
|
|
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
|
|
|
# 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-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
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-07-10 14:53:28 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-zexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(1, executable+': failed PIE NX RELRO Canary'))
|
2020-07-10 14:53:28 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(1, executable+': failed PIE RELRO Canary'))
|
2020-07-10 14:53:28 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(1, executable+': failed PIE RELRO'))
|
2020-07-10 14:53:28 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro','-pie','-fPIE', '-Wl,-z,separate-code']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(1, executable+': failed RELRO'))
|
2020-07-10 14:53:28 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,noseparate-code']),
|
|
|
|
(1, executable+': failed separate_code'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(0, ''))
|
|
|
|
|
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-06-04 19:34:54 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--no-nxcompat','-Wl,--disable-reloc-section','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-no-pie','-fno-PIE']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA NX RELOC_SECTION CONTROL_FLOW'))
|
2023-06-04 19:34:54 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--disable-reloc-section','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-no-pie','-fno-PIE']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA RELOC_SECTION CONTROL_FLOW'))
|
2023-06-04 19:34:54 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-no-pie','-fno-PIE']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA CONTROL_FLOW'))
|
2023-06-04 19:34:54 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-pie','-fPIE']),
|
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-06-04 19:34:54 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--dynamicbase','-Wl,--no-high-entropy-va','-pie','-fPIE']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed HIGH_ENTROPY_VA CONTROL_FLOW'))
|
2023-06-04 19:34:54 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--dynamicbase','-Wl,--high-entropy-va','-pie','-fPIE']),
|
2021-11-17 02:43:01 +01:00
|
|
|
(1, executable+': failed CONTROL_FLOW'))
|
|
|
|
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']),
|
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)
|
|
|
|
|
2020-04-22 10:19:51 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-Wl,-allow_stack_execute','-fno-stack-protector']),
|
2021-05-09 05:32:59 +02:00
|
|
|
(1, executable+': failed PIE NOUNDEFS NX LAZY_BINDINGS Canary CONTROL_FLOW'))
|
2020-04-22 10:19:51 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-Wl,-allow_stack_execute','-fstack-protector-all']),
|
2021-05-09 05:32:59 +02:00
|
|
|
(1, executable+': failed PIE NOUNDEFS NX LAZY_BINDINGS CONTROL_FLOW'))
|
2020-04-22 10:19:51 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-fstack-protector-all']),
|
2021-05-09 05:32:59 +02:00
|
|
|
(1, executable+': failed PIE NOUNDEFS LAZY_BINDINGS CONTROL_FLOW'))
|
2020-04-22 10:19:51 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-fstack-protector-all']),
|
2021-05-09 05:32:59 +02:00
|
|
|
(1, executable+': failed PIE LAZY_BINDINGS CONTROL_FLOW'))
|
2020-04-22 10:19:51 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-bind_at_load','-fstack-protector-all']),
|
2021-05-09 05:32:59 +02:00
|
|
|
(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']),
|
2020-03-28 11:48:19 +01:00
|
|
|
(1, executable+': failed PIE'))
|
2021-05-09 05:32:59 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-pie','-Wl,-bind_at_load','-fstack-protector-all', '-fcf-protection=full']),
|
2020-03-28 11:48:19 +01:00
|
|
|
(0, ''))
|
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()
|