|
|
|
@ -5,6 +5,7 @@
|
|
|
|
|
'''
|
|
|
|
|
Test script for security-check.py
|
|
|
|
|
'''
|
|
|
|
|
import lief #type:ignore
|
|
|
|
|
import os
|
|
|
|
|
import subprocess
|
|
|
|
|
from typing import List
|
|
|
|
@ -41,13 +42,37 @@ def call_security_check(cc, source, executable, options):
|
|
|
|
|
p = subprocess.run(['./contrib/devtools/security-check.py',executable], stdout=subprocess.PIPE, universal_newlines=True)
|
|
|
|
|
return (p.returncode, p.stdout.rstrip())
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
class TestSecurityChecks(unittest.TestCase):
|
|
|
|
|
def test_ELF(self):
|
|
|
|
|
source = 'test1.c'
|
|
|
|
|
executable = 'test1'
|
|
|
|
|
cc = determine_wellknown_cmd('CC', 'gcc')
|
|
|
|
|
write_testcode(source)
|
|
|
|
|
arch = get_arch(cc, source, executable)
|
|
|
|
|
|
|
|
|
|
if arch == lief.ARCHITECTURES.X86:
|
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-zexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
|
|
|
|
(1, executable+': failed PIE NX RELRO Canary CONTROL_FLOW'))
|
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
|
|
|
|
(1, executable+': failed PIE RELRO Canary CONTROL_FLOW'))
|
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-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','-fstack-protector-all','-Wl,-znorelro','-pie','-fPIE', '-Wl,-z,separate-code']),
|
|
|
|
|
(1, executable+': failed RELRO CONTROL_FLOW'))
|
|
|
|
|
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 CONTROL_FLOW'))
|
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code']),
|
|
|
|
|
(1, executable+': failed CONTROL_FLOW'))
|
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code', '-fcf-protection=full']),
|
|
|
|
|
(0, ''))
|
|
|
|
|
else:
|
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-zexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
|
|
|
|
(1, executable+': failed PIE NX RELRO Canary'))
|
|
|
|
|
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
|
|
|
|
|