Skip to content

Commit 5335e45

Browse files
committed
contrib: add macho branch protection check
1 parent 65c05db commit 5335e45

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

contrib/devtools/security-check.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ def check_MACHO_control_flow(binary) -> bool:
192192
return True
193193
return False
194194

195+
def check_MACHO_branch_protection(binary) -> bool:
196+
'''
197+
Check for branch protection instrumentation
198+
'''
199+
content = binary.get_content_from_virtual_address(binary.entrypoint, 4, lief.Binary.VA_TYPES.AUTO)
200+
201+
if content.tolist() == [95, 36, 3, 213]: # bti
202+
return True
203+
return False
204+
195205
BASE_ELF = [
196206
('PIE', check_PIE),
197207
('NX', check_NX),
@@ -231,7 +241,7 @@ def check_MACHO_control_flow(binary) -> bool:
231241
lief.ARCHITECTURES.X86: BASE_MACHO + [('PIE', check_PIE),
232242
('NX', check_NX),
233243
('CONTROL_FLOW', check_MACHO_control_flow)],
234-
lief.ARCHITECTURES.ARM64: BASE_MACHO,
244+
lief.ARCHITECTURES.ARM64: BASE_MACHO + [('BRANCH_PROTECTION', check_MACHO_branch_protection)],
235245
}
236246
}
237247

contrib/devtools/test-security-check.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ def test_MACHO(self):
137137
else:
138138
# arm64 darwin doesn't support non-PIE binaries, control flow or executable stacks
139139
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-flat_namespace','-fno-stack-protector', '-Wl,-no_fixup_chains']),
140-
(1, executable+': failed NOUNDEFS Canary FIXUP_CHAINS'))
141-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-flat_namespace','-fno-stack-protector', '-Wl,-fixup_chains']),
140+
(1, executable+': failed NOUNDEFS Canary FIXUP_CHAINS BRANCH_PROTECTION'))
141+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-flat_namespace','-fno-stack-protector', '-Wl,-fixup_chains', '-mbranch-protection=bti']),
142142
(1, executable+': failed NOUNDEFS Canary'))
143-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-flat_namespace','-fstack-protector-all', '-Wl,-fixup_chains']),
143+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-flat_namespace','-fstack-protector-all', '-Wl,-fixup_chains', '-mbranch-protection=bti']),
144144
(1, executable+': failed NOUNDEFS'))
145-
self.assertEqual(call_security_check(cc, source, executable, ['-fstack-protector-all', '-Wl,-fixup_chains']),
145+
self.assertEqual(call_security_check(cc, source, executable, ['-fstack-protector-all', '-Wl,-fixup_chains', '-mbranch-protection=bti']),
146146
(0, ''))
147147

148148

0 commit comments

Comments
 (0)