Skip to content

Commit 3e38023

Browse files
committed
scripts: add PE .reloc section check to security-check.py
1 parent 47b94a3 commit 3e38023

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

contrib/devtools/security-check.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ def check_PE_HIGH_ENTROPY_VA(executable):
158158
reqbits = 0
159159
return (bits & reqbits) == reqbits
160160

161+
def check_PE_RELOC_SECTION(executable) -> bool:
162+
'''Check for a reloc section. This is required for functional ASLR.'''
163+
p = subprocess.Popen([OBJDUMP_CMD, '-h', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)
164+
(stdout, stderr) = p.communicate()
165+
if p.returncode:
166+
raise IOError('Error opening file')
167+
for line in stdout.splitlines():
168+
if '.reloc' in line:
169+
return True
170+
return False
171+
161172
def check_PE_NX(executable):
162173
'''NX: DllCharacteristics bit 0x100 signifies nxcompat (DEP)'''
163174
(arch,bits) = get_PE_dll_characteristics(executable)
@@ -247,7 +258,8 @@ def check_MACHO_Canary(executable) -> bool:
247258
'PE': [
248259
('DYNAMIC_BASE', check_PE_DYNAMIC_BASE),
249260
('HIGH_ENTROPY_VA', check_PE_HIGH_ENTROPY_VA),
250-
('NX', check_PE_NX)
261+
('NX', check_PE_NX),
262+
('RELOC_SECTION', check_PE_RELOC_SECTION)
251263
],
252264
'MACHO': [
253265
('PIE', check_MACHO_PIE),

contrib/devtools/test-security-check.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ def test_PE(self):
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']),
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']),
52+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--no-nxcompat','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-no-pie','-fno-PIE']),
53+
(1, executable+': failed DYNAMIC_BASE HIGH_ENTROPY_VA NX RELOC_SECTION'))
54+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-no-pie','-fno-PIE']),
55+
(1, executable+': failed DYNAMIC_BASE HIGH_ENTROPY_VA RELOC_SECTION'))
56+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase','-Wl,--no-high-entropy-va','-no-pie','-fno-PIE']),
57+
(1, executable+': failed HIGH_ENTROPY_VA RELOC_SECTION'))
58+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase','-Wl,--high-entropy-va','-no-pie','-fno-PIE']),
59+
(1, executable+': failed RELOC_SECTION'))
60+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase','-Wl,--high-entropy-va','-pie','-fPIE']),
5961
(0, ''))
6062

6163
def test_MACHO(self):

0 commit comments

Comments
 (0)