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
|
|
|
|
'''
|
|
|
|
import subprocess
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
|
|
|
|
def call_security_check(cc, source, executable, options):
|
|
|
|
subprocess.check_call([cc,source,'-o',executable] + options)
|
2018-04-16 22:03:08 +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-30 16:01:58 +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-30 16:01:58 +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-30 16:01:58 +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-07-30 16:01:58 +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-07-30 16:01:58 +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, ''))
|
|
|
|
|
2018-05-07 15:14:33 +02:00
|
|
|
def test_64bit_PE(self):
|
|
|
|
source = 'test1.c'
|
|
|
|
executable = 'test1.exe'
|
|
|
|
cc = 'x86_64-w64-mingw32-gcc'
|
|
|
|
write_testcode(source)
|
|
|
|
|
2018-07-30 16:01:58 +02: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'))
|
2018-05-07 15:14:33 +02:00
|
|
|
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
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|