Skip to content

Commit 51d8f43

Browse files
committed
contrib: simplify ELF test-security-check
1 parent 1810e20 commit 51d8f43

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

contrib/devtools/security-check.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ def check_ELF_RELRO(binary) -> bool:
3838

3939
return have_gnu_relro and have_bindnow
4040

41-
def check_ELF_Canary(binary) -> bool:
41+
def check_ELF_CANARY(binary) -> bool:
4242
'''
4343
Check for use of stack canary
4444
'''
4545
return binary.has_symbol('__stack_chk_fail')
4646

47-
def check_ELF_separate_code(binary):
47+
def check_ELF_SEPARATE_CODE(binary):
4848
'''
4949
Check that sections are appropriately separated in virtual memory,
5050
based on their permissions. This checks for missing -Wl,-z,separate-code
@@ -105,7 +105,7 @@ def check_ELF_separate_code(binary):
105105
return False
106106
return True
107107

108-
def check_ELF_control_flow(binary) -> bool:
108+
def check_ELF_CONTROL_FLOW(binary) -> bool:
109109
'''
110110
Check for control flow instrumentation
111111
'''
@@ -206,8 +206,8 @@ def check_MACHO_BRANCH_PROTECTION(binary) -> bool:
206206
('PIE', check_PIE),
207207
('NX', check_NX),
208208
('RELRO', check_ELF_RELRO),
209-
('Canary', check_ELF_Canary),
210-
('separate_code', check_ELF_separate_code),
209+
('CANARY', check_ELF_CANARY),
210+
('SEPARATE_CODE', check_ELF_SEPARATE_CODE),
211211
]
212212

213213
BASE_PE = [
@@ -228,7 +228,7 @@ def check_MACHO_BRANCH_PROTECTION(binary) -> bool:
228228

229229
CHECKS = {
230230
lief.EXE_FORMATS.ELF: {
231-
lief.ARCHITECTURES.X86: BASE_ELF + [('CONTROL_FLOW', check_ELF_control_flow)],
231+
lief.ARCHITECTURES.X86: BASE_ELF + [('CONTROL_FLOW', check_ELF_CONTROL_FLOW)],
232232
lief.ARCHITECTURES.ARM: BASE_ELF,
233233
lief.ARCHITECTURES.ARM64: BASE_ELF,
234234
lief.ARCHITECTURES.PPC: BASE_ELF,

contrib/devtools/test-security-check.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,20 @@ def test_ELF(self):
5959
arch = get_arch(cxx, source, executable)
6060

6161
if arch == lief.ARCHITECTURES.X86:
62-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-zexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
63-
(1, executable+': failed PIE NX RELRO CONTROL_FLOW'))
64-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
65-
(1, executable+': failed PIE RELRO CONTROL_FLOW'))
66-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
67-
(1, executable+': failed PIE RELRO CONTROL_FLOW'))
68-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-pie','-fPIE', '-Wl,-z,separate-code']),
69-
(1, executable+': failed RELRO CONTROL_FLOW'))
70-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,noseparate-code']),
71-
(1, executable+': failed separate_code CONTROL_FLOW'))
72-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code']),
73-
(1, executable+': failed CONTROL_FLOW'))
74-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code', '-fcf-protection=full']),
75-
(0, ''))
62+
pass_flags = ['-Wl,-znoexecstack', '-Wl,-zrelro', '-Wl,-z,now', '-pie', '-fPIE', '-Wl,-z,separate-code', '-fcf-protection=full']
63+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-zexecstack']), (1, executable + ': failed NX'))
64+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-no-pie','-fno-PIE']), (1, executable + ': failed PIE'))
65+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-znorelro']), (1, executable + ': failed RELRO'))
66+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-z,noseparate-code']), (1, executable + ': failed SEPARATE_CODE'))
67+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-fcf-protection=none']), (1, executable + ': failed CONTROL_FLOW'))
68+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags), (0, ''))
7669
else:
77-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-zexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
78-
(1, executable+': failed PIE NX RELRO'))
79-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
80-
(1, executable+': failed PIE RELRO'))
81-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
82-
(1, executable+': failed PIE RELRO'))
83-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-znorelro','-pie','-fPIE', '-Wl,-z,separate-code']),
84-
(1, executable+': failed RELRO'))
85-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,noseparate-code']),
86-
(1, executable+': failed separate_code'))
87-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-znoexecstack','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code']),
88-
(0, ''))
70+
pass_flags = ['-Wl,-znoexecstack', '-Wl,-zrelro', '-Wl,-z,now', '-pie', '-fPIE', '-Wl,-z,separate-code']
71+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-zexecstack']), (1, executable + ': failed NX'))
72+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-no-pie','-fno-PIE']), (1, executable + ': failed PIE'))
73+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-znorelro']), (1, executable + ': failed RELRO'))
74+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-z,noseparate-code']), (1, executable + ': failed SEPARATE_CODE'))
75+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags), (0, ''))
8976

9077
clean_files(source, executable)
9178

0 commit comments

Comments
 (0)