Skip to content

Commit f1fcc96

Browse files
committed
Merge bitcoin/bitcoin#29170: contrib: add macho branch protection check
5335e45 contrib: add macho branch protection check (fanquake) Pull request description: Followup to bitcoin/bitcoin#28459. Add a sanity check that `bti` instructions are present in the arm macho binary, similar to our x86_64 check for control flow. Could do something similar for aarch64 linux in future, and maybe could use lief-project/LIEF#975. ACKs for top commit: TheCharlatan: ACK 5335e45 Tree-SHA512: 6cc8721209fe07fe07f0524ef6f114004e2b98844f73d31ff16547f7055c7cb4a5609480058c45ede21b457b2dea5357f1475eaa5063ea1f9772aa260f49039b
2 parents 9fa8eda + 5335e45 commit f1fcc96

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)