Skip to content

Commit 1277ca4

Browse files
committed
Merge #18415: scripts: add MACHO tests to test-security-check.py
7142d50 scripts: rename test_64bit_PE to test_PE (fanquake) edaca2d scripts: add MACHO NX check to security-check.py (fanquake) 1a4e9f3 scripts: add MACHO tests to test-security-check.py (fanquake) Pull request description: Adds tests for the MACHO checks in security-check.py: https://github.com/bitcoin/bitcoin/blob/ac579ada7e83a1b8100d611412f9ede885a4e522/contrib/devtools/security-check.py#L212-L214 I'm planning on following up with more checks in security-check.py, and corresponding tests in test-security-check.py. Note that you'll probably have to be on macOS to run them. You can run just this suite with `python3 test-security-check.py TestSecurityChecks.test_MACHO`. ACKs for top commit: laanwj: ACK 7142d50 Tree-SHA512: ace3ca9f6df5d4fedd5988938fb7dc7563ec7dc587aa275f780b5f51e9b8d7d6f7768e0a1e05ce438510a07b8640aba92c76847b30c2990f46c66b78a0acf960
2 parents f2c416b + 7142d50 commit 1277ca4

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

contrib/devtools/security-check.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ def check_MACHO_NOUNDEFS(executable) -> bool:
197197
return True
198198
return False
199199

200+
def check_MACHO_NX(executable) -> bool:
201+
'''
202+
Check for no stack execution
203+
'''
204+
flags = get_MACHO_executable_flags(executable)
205+
if 'ALLOW_STACK_EXECUTION' in flags:
206+
return False
207+
return True
208+
200209
CHECKS = {
201210
'ELF': [
202211
('PIE', check_ELF_PIE),
@@ -212,6 +221,7 @@ def check_MACHO_NOUNDEFS(executable) -> bool:
212221
'MACHO': [
213222
('PIE', check_MACHO_PIE),
214223
('NOUNDEFS', check_MACHO_NOUNDEFS),
224+
('NX', check_MACHO_NX)
215225
]
216226
}
217227

contrib/devtools/test-security-check.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,35 @@ def test_ELF(self):
4343
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE']),
4444
(0, ''))
4545

46-
def test_64bit_PE(self):
46+
def test_PE(self):
4747
source = 'test1.c'
4848
executable = 'test1.exe'
4949
cc = 'x86_64-w64-mingw32-gcc'
5050
write_testcode(source)
5151

52-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--no-nxcompat','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va']), (1, executable+': failed DYNAMIC_BASE HIGH_ENTROPY_VA NX'))
53-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va']), (1, executable+': failed DYNAMIC_BASE HIGH_ENTROPY_VA'))
54-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase','-Wl,--no-high-entropy-va']), (1, executable+': failed HIGH_ENTROPY_VA'))
55-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase','-Wl,--high-entropy-va']), (0, ''))
52+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--no-nxcompat','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va']),
53+
(1, executable+': failed DYNAMIC_BASE HIGH_ENTROPY_VA NX'))
54+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va']),
55+
(1, executable+': failed DYNAMIC_BASE HIGH_ENTROPY_VA'))
56+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase','-Wl,--no-high-entropy-va']),
57+
(1, executable+': failed HIGH_ENTROPY_VA'))
58+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase','-Wl,--high-entropy-va']),
59+
(0, ''))
60+
61+
def test_MACHO(self):
62+
source = 'test1.c'
63+
executable = 'test1'
64+
cc = 'clang'
65+
write_testcode(source)
66+
67+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace', '-Wl,-allow_stack_execute']),
68+
(1, executable+': failed PIE NOUNDEFS NX'))
69+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace']),
70+
(1, executable+': failed PIE NOUNDEFS'))
71+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie']),
72+
(1, executable+': failed PIE'))
73+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-pie']),
74+
(0, ''))
5675

5776
if __name__ == '__main__':
5877
unittest.main()

0 commit comments

Comments
 (0)