Skip to content

Commit 7b99c74

Browse files
committed
scripts: add MACHO Canary check to security-check.py
1 parent c4c3f11 commit 7b99c74

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

contrib/devtools/security-check.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ def check_MACHO_LAZY_BINDINGS(executable) -> bool:
223223
return False
224224
return True
225225

226+
def check_MACHO_Canary(executable) -> bool:
227+
'''
228+
Check for use of stack canary
229+
'''
230+
p = subprocess.Popen([OTOOL_CMD, '-Iv', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
231+
(stdout, stderr) = p.communicate()
232+
if p.returncode:
233+
raise IOError('Error opening file')
234+
ok = False
235+
for line in stdout.splitlines():
236+
if '___stack_chk_fail' in line:
237+
ok = True
238+
return ok
239+
226240
CHECKS = {
227241
'ELF': [
228242
('PIE', check_ELF_PIE),
@@ -239,7 +253,8 @@ def check_MACHO_LAZY_BINDINGS(executable) -> bool:
239253
('PIE', check_MACHO_PIE),
240254
('NOUNDEFS', check_MACHO_NOUNDEFS),
241255
('NX', check_MACHO_NX),
242-
('LAZY_BINDINGS', check_MACHO_LAZY_BINDINGS)
256+
('LAZY_BINDINGS', check_MACHO_LAZY_BINDINGS),
257+
('Canary', check_MACHO_Canary)
243258
]
244259
}
245260

contrib/devtools/test-security-check.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ def test_MACHO(self):
6464
cc = 'clang'
6565
write_testcode(source)
6666

67-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace', '-Wl,-allow_stack_execute']),
67+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-Wl,-allow_stack_execute','-fno-stack-protector']),
68+
(1, executable+': failed PIE NOUNDEFS NX Canary'))
69+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-Wl,-allow_stack_execute','-fstack-protector-all']),
6870
(1, executable+': failed PIE NOUNDEFS NX'))
69-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace']),
71+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-Wl,-flat_namespace','-fstack-protector-all']),
7072
(1, executable+': failed PIE NOUNDEFS'))
71-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie']),
73+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-no_pie','-fstack-protector-all']),
7274
(1, executable+': failed PIE'))
73-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-pie']),
75+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-pie','-fstack-protector-all']),
7476
(0, ''))
7577

7678
if __name__ == '__main__':

0 commit comments

Comments
 (0)