Skip to content

Commit 6c9746f

Browse files
committed
contrib: simplify MACHO test-security-check
1 parent efbf4e7 commit 6c9746f

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

contrib/devtools/security-check.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def check_MACHO_FIXUP_CHAINS(binary) -> bool:
163163
'''
164164
return binary.has_dyld_chained_fixups
165165

166-
def check_MACHO_Canary(binary) -> bool:
166+
def check_MACHO_CANARY(binary) -> bool:
167167
'''
168168
Check for use of stack canary
169169
'''
@@ -182,7 +182,7 @@ def check_NX(binary) -> bool:
182182
'''
183183
return binary.has_nx
184184

185-
def check_MACHO_control_flow(binary) -> bool:
185+
def check_MACHO_CONTROL_FLOW(binary) -> bool:
186186
'''
187187
Check for control flow instrumentation
188188
'''
@@ -192,7 +192,7 @@ def check_MACHO_control_flow(binary) -> bool:
192192
return True
193193
return False
194194

195-
def check_MACHO_branch_protection(binary) -> bool:
195+
def check_MACHO_BRANCH_PROTECTION(binary) -> bool:
196196
'''
197197
Check for branch protection instrumentation
198198
'''
@@ -222,7 +222,7 @@ def check_MACHO_branch_protection(binary) -> bool:
222222

223223
BASE_MACHO = [
224224
('NOUNDEFS', check_MACHO_NOUNDEFS),
225-
('Canary', check_MACHO_Canary),
225+
('CANARY', check_MACHO_CANARY),
226226
('FIXUP_CHAINS', check_MACHO_FIXUP_CHAINS),
227227
]
228228

@@ -240,8 +240,8 @@ def check_MACHO_branch_protection(binary) -> bool:
240240
lief.EXE_FORMATS.MACHO: {
241241
lief.ARCHITECTURES.X86: BASE_MACHO + [('PIE', check_PIE),
242242
('NX', check_NX),
243-
('CONTROL_FLOW', check_MACHO_control_flow)],
244-
lief.ARCHITECTURES.ARM64: BASE_MACHO + [('BRANCH_PROTECTION', check_MACHO_branch_protection)],
243+
('CONTROL_FLOW', check_MACHO_CONTROL_FLOW)],
244+
lief.ARCHITECTURES.ARM64: BASE_MACHO + [('BRANCH_PROTECTION', check_MACHO_BRANCH_PROTECTION)],
245245
}
246246
}
247247

contrib/devtools/test-security-check.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,21 @@ def test_MACHO(self):
120120
arch = get_arch(cxx, source, executable)
121121

122122
if arch == lief.ARCHITECTURES.X86:
123-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-fno-stack-protector', '-Wl,-no_fixup_chains']),
124-
(1, executable+': failed NOUNDEFS Canary FIXUP_CHAINS PIE CONTROL_FLOW'))
125-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-flat_namespace','-fno-stack-protector', '-Wl,-fixup_chains']),
126-
(1, executable+': failed NOUNDEFS Canary CONTROL_FLOW'))
127-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-flat_namespace','-fstack-protector-all', '-Wl,-fixup_chains']),
128-
(1, executable+': failed NOUNDEFS CONTROL_FLOW'))
129-
self.assertEqual(call_security_check(cxx, source, executable, ['-fstack-protector-all', '-Wl,-fixup_chains']),
130-
(1, executable+': failed CONTROL_FLOW'))
131-
self.assertEqual(call_security_check(cxx, source, executable, ['-fstack-protector-all', '-fcf-protection=full', '-Wl,-fixup_chains']),
132-
(0, ''))
123+
pass_flags = ['-Wl,-pie', '-fstack-protector-all', '-fcf-protection=full', '-Wl,-fixup_chains']
124+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-no_pie', '-Wl,-no_fixup_chains']), (1, executable+': failed FIXUP_CHAINS PIE')) # -fixup_chains is incompatible with -no_pie
125+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-no_fixup_chains']), (1, executable + ': failed FIXUP_CHAINS'))
126+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-fno-stack-protector']), (1, executable + ': failed CANARY'))
127+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-flat_namespace']), (1, executable + ': failed NOUNDEFS'))
128+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-fcf-protection=none']), (1, executable + ': failed CONTROL_FLOW'))
129+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags), (0, ''))
133130
else:
134-
# arm64 darwin doesn't support non-PIE binaries, control flow or executable stacks
135-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-flat_namespace','-fno-stack-protector', '-Wl,-no_fixup_chains']),
136-
(1, executable+': failed NOUNDEFS Canary FIXUP_CHAINS BRANCH_PROTECTION'))
137-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-flat_namespace','-fno-stack-protector', '-Wl,-fixup_chains', '-mbranch-protection=bti']),
138-
(1, executable+': failed NOUNDEFS Canary'))
139-
self.assertEqual(call_security_check(cxx, source, executable, ['-Wl,-flat_namespace','-fstack-protector-all', '-Wl,-fixup_chains', '-mbranch-protection=bti']),
140-
(1, executable+': failed NOUNDEFS'))
141-
self.assertEqual(call_security_check(cxx, source, executable, ['-fstack-protector-all', '-Wl,-fixup_chains', '-mbranch-protection=bti']),
142-
(0, ''))
143-
131+
# arm64 darwin doesn't support non-PIE binaries or executable stacks
132+
pass_flags = ['-fstack-protector-all', '-Wl,-fixup_chains', '-mbranch-protection=bti']
133+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-mbranch-protection=none']), (1, executable + ': failed BRANCH_PROTECTION'))
134+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-no_fixup_chains']), (1, executable + ': failed FIXUP_CHAINS'))
135+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-fno-stack-protector']), (1, executable + ': failed CANARY'))
136+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags + ['-Wl,-flat_namespace']), (1, executable + ': failed NOUNDEFS'))
137+
self.assertEqual(call_security_check(cxx, source, executable, pass_flags), (0, ''))
144138

145139
clean_files(source, executable)
146140

0 commit comments

Comments
 (0)