2018-04-13 17:37:53 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-04-16 19:14:08 +02: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
|
|
|
'''
|
|
|
|
Test script for security-check.py
|
|
|
|
'''
|
|
|
|
import subprocess
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
def write_testcode(filename):
|
2018-06-12 17:49:20 +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;
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
|
|
|
|
def call_security_check(cc, source, executable, options):
|
|
|
|
subprocess.check_call([cc,source,'-o',executable] + options)
|
2018-04-13 17:37:53 +02:00
|
|
|
p = subprocess.Popen(['./security-check.py',executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
|
2015-10-19 14:53:56 +02:00
|
|
|
(stdout, stderr) = p.communicate()
|
|
|
|
return (p.returncode, stdout.rstrip())
|
|
|
|
|
|
|
|
class TestSecurityChecks(unittest.TestCase):
|
|
|
|
def test_ELF(self):
|
|
|
|
source = 'test1.c'
|
|
|
|
executable = 'test1'
|
|
|
|
cc = 'gcc'
|
|
|
|
write_testcode(source)
|
|
|
|
|
2018-07-26 11:09:49 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-zexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(1, executable+': failed PIE NX RELRO Canary'))
|
2018-07-26 11:09:49 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(1, executable+': failed PIE RELRO Canary'))
|
2018-07-26 11:09:49 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro','-no-pie','-fno-PIE']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(1, executable+': failed PIE RELRO'))
|
2018-05-10 17:28:27 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro','-pie','-fPIE']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(1, executable+': failed RELRO'))
|
2018-05-10 17:28:27 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE']),
|
2015-10-19 14:53:56 +02:00
|
|
|
(0, ''))
|
|
|
|
|
2020-03-26 04:37:32 +01:00
|
|
|
def test_PE(self):
|
2018-04-26 18:43:01 +02:00
|
|
|
source = 'test1.c'
|
|
|
|
executable = 'test1.exe'
|
|
|
|
cc = 'x86_64-w64-mingw32-gcc'
|
|
|
|
write_testcode(source)
|
|
|
|
|
2020-03-26 04:37:32 +01:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--no-nxcompat','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va']),
|
|
|
|
(1, executable+': failed DYNAMIC_BASE HIGH_ENTROPY_VA NX'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va']),
|
|
|
|
(1, executable+': failed DYNAMIC_BASE HIGH_ENTROPY_VA'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase','-Wl,--no-high-entropy-va']),
|
|
|
|
(1, executable+': failed HIGH_ENTROPY_VA'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase','-Wl,--high-entropy-va']),
|
|
|
|
(0, ''))
|
2015-10-19 14:53:56 +02:00
|
|
|
|
2020-03-24 05:37:57 +01:00
|
|
|
def test_MACHO(self):
|
|
|
|
source = 'test1.c'
|
|
|
|
executable = 'test1'
|
|
|
|
cc = 'clang'
|
|
|
|
write_testcode(source)
|
|
|
|
|
2020-04-21 03:41:17 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-Wl,-allow_stack_execute','-fno-stack-protector']),
|
2020-04-21 03:55:12 +02:00
|
|
|
(1, executable+': failed PIE NOUNDEFS NX LAZY_BINDINGS Canary'))
|
2020-04-21 03:41:17 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-Wl,-allow_stack_execute','-fstack-protector-all']),
|
2020-04-21 03:55:12 +02:00
|
|
|
(1, executable+': failed PIE NOUNDEFS NX LAZY_BINDINGS'))
|
2020-04-21 03:41:17 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-fstack-protector-all']),
|
2020-04-21 03:55:12 +02:00
|
|
|
(1, executable+': failed PIE NOUNDEFS LAZY_BINDINGS'))
|
2020-04-21 03:41:17 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-fstack-protector-all']),
|
2020-04-21 03:55:12 +02:00
|
|
|
(1, executable+': failed PIE LAZY_BINDINGS'))
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-bind_at_load','-fstack-protector-all']),
|
2020-03-24 05:37:57 +01:00
|
|
|
(1, executable+': failed PIE'))
|
2020-04-21 03:55:12 +02:00
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-pie','-Wl,-bind_at_load','-fstack-protector-all']),
|
2020-03-24 05:37:57 +01:00
|
|
|
(0, ''))
|
|
|
|
|
2015-10-19 14:53:56 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|