Skip to content

Commit 400f45e

Browse files
committed
Merge #19525: build: add -Wl,-z,separate-code to hardening flags
65d0f1a devtools: Add security check for separate_code (Wladimir J. van der Laan) 2e9e637 build: add -Wl,-z,separate-code to hardening flags (fanquake) Pull request description: TLDR: We are generally explicit about the hardening related flags we use, rather than letting the distro / toolchain decide via their defaults. This PR adds `-z,separate-code` which has been enabled by default for Linux targets since binutils 2.31. Ubuntu Bionic (currently used for gitian) ships with binutils 2.30, so this will enable the option for those builds. This flag was added to binutils/ld in the 2.30 release, see commit c11c786f0b45617bb8807ab6a57220d5ff50e414: > The new "-z separate-code" option will generate separate code LOAD segment which must be in wholly disjoint pages from any other data. It was made the default for Linux/x86 targets in the 2.31 release, see commit f6aec96dce1ddbd8961a3aa8a2925db2021719bb: > This patch adds --enable-separate-code to ld configure to turn on -z separate-code by default and enables it by default for Linux/x86. This avoids mixing code pages with data to improve cache performance as well as security. > To reduce x86-64 executable and shared object sizes, the maximum page size is reduced from 2MB to 4KB when -z separate-code is turned on by default. Note: -z max-page-size= can be used to set the maximum page size. > We compared SPEC CPU 2017 performance before and after this change on Skylake server. There are no any significant performance changes. Everything is mostly below +/-1%. Support was also added to LLVMs lld: https://reviews.llvm.org/D64903, however there it remains off by default. There were concerns about an increase in binary size, however in our case, the difference would seem negligible, given we are shipping a multi-megabyte binary, which then downloads 100's of GBs of data. Also note that most recent versions of distros are shipping a new enough version of binutils that this is available and/or already on by default (assuming the distro has not turned it off, I haven't checked everywhere): CentOS 8: 2.30 Debian Buster 2.31.1 Fedora 29: 2.31.1 FreeBSD: 2.33 GNU Guix: 2.33 / 2.34 Ubuntu 18.04: 2.30 Related threads / discussion: https://bugzilla.redhat.com/show_bug.cgi?id=1623218 The ELF header when building on Debian Buster (where it's already enabled by default in binutils): ```bash Program Header: PHDR off 0x0000000000000040 vaddr 0x0000000000000040 paddr 0x0000000000000040 align 2**3 filesz 0x00000000000002a0 memsz 0x00000000000002a0 flags r-- INTERP off 0x00000000000002e0 vaddr 0x00000000000002e0 paddr 0x00000000000002e0 align 2**0 filesz 0x000000000000001c memsz 0x000000000000001c flags r-- LOAD off 0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**12 filesz 0x0000000000038f10 memsz 0x0000000000038f10 flags r-- LOAD off 0x0000000000039000 vaddr 0x0000000000039000 paddr 0x0000000000039000 align 2**12 filesz 0x00000000006b9389 memsz 0x00000000006b9389 flags r-x LOAD off 0x00000000006f3000 vaddr 0x00000000006f3000 paddr 0x00000000006f3000 align 2**12 filesz 0x0000000000204847 memsz 0x0000000000204847 flags r-- LOAD off 0x00000000008f7920 vaddr 0x00000000008f8920 paddr 0x00000000008f8920 align 2**12 filesz 0x00000000000183e0 memsz 0x0000000000022fd0 flags rw- DYNAMIC off 0x000000000090adb0 vaddr 0x000000000090bdb0 paddr 0x000000000090bdb0 align 2**3 filesz 0x0000000000000240 memsz 0x0000000000000240 flags rw- ``` vs when opting out using `-Wl,-z,noseparate-code`: ```bash Program Header: PHDR off 0x0000000000000040 vaddr 0x0000000000000040 paddr 0x0000000000000040 align 2**3 filesz 0x0000000000000230 memsz 0x0000000000000230 flags r-- INTERP off 0x0000000000000270 vaddr 0x0000000000000270 paddr 0x0000000000000270 align 2**0 filesz 0x000000000000001c memsz 0x000000000000001c flags r-- LOAD off 0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**12 filesz 0x00000000008f6a87 memsz 0x00000000008f6a87 flags r-x LOAD off 0x00000000008f7920 vaddr 0x00000000008f8920 paddr 0x00000000008f8920 align 2**12 filesz 0x00000000000183e0 memsz 0x0000000000022fd0 flags rw- DYNAMIC off 0x000000000090adb0 vaddr 0x000000000090bdb0 paddr 0x000000000090bdb0 align 2**3 filesz 0x0000000000000240 memsz 0x0000000000000240 flags rw- ``` ACKs for top commit: laanwj: ACK 65d0f1a Tree-SHA512: 6e40e434efea8a8e39f6cb244dfd16aaa5a9db5a2ea762a05d1727357b20e33b7e47c1a652ee88490c9d7952a4caa2f992396fb30346239300d37ae123e36d49
2 parents a76ccb0 + 65d0f1a commit 400f45e

File tree

3 files changed

+100
-17
lines changed

3 files changed

+100
-17
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ if test x$use_hardening != xno; then
785785
AX_CHECK_LINK_FLAG([[-Wl,--high-entropy-va]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,--high-entropy-va"],, [[$LDFLAG_WERROR]])
786786
AX_CHECK_LINK_FLAG([[-Wl,-z,relro]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,relro"],, [[$LDFLAG_WERROR]])
787787
AX_CHECK_LINK_FLAG([[-Wl,-z,now]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,now"],, [[$LDFLAG_WERROR]])
788+
AX_CHECK_LINK_FLAG([[-Wl,-z,separate-code]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,separate-code"],, [[$LDFLAG_WERROR]])
788789
AX_CHECK_LINK_FLAG([[-fPIE -pie]], [PIE_FLAGS="-fPIE"; HARDENED_LDFLAGS="$HARDENED_LDFLAGS -pie"],, [[$CXXFLAG_WERROR]])
789790

790791
case $host in

contrib/devtools/security-check.py

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,48 @@ def get_ELF_program_headers(executable):
4040
stdout = run_command([READELF_CMD, '-l', '-W', executable])
4141

4242
in_headers = False
43-
count = 0
4443
headers = []
4544
for line in stdout.splitlines():
4645
if line.startswith('Program Headers:'):
4746
in_headers = True
47+
count = 0
4848
if line == '':
4949
in_headers = False
5050
if in_headers:
5151
if count == 1: # header line
52-
ofs_typ = line.find('Type')
53-
ofs_offset = line.find('Offset')
54-
ofs_flags = line.find('Flg')
55-
ofs_align = line.find('Align')
56-
if ofs_typ == -1 or ofs_offset == -1 or ofs_flags == -1 or ofs_align == -1:
52+
header = [x.strip() for x in line.split()]
53+
ofs_typ = header.index('Type')
54+
ofs_flags = header.index('Flg')
55+
# assert readelf output is what we expect
56+
if ofs_typ == -1 or ofs_flags == -1:
5757
raise ValueError('Cannot parse elfread -lW output')
5858
elif count > 1:
59-
typ = line[ofs_typ:ofs_offset].rstrip()
60-
flags = line[ofs_flags:ofs_align].rstrip()
61-
headers.append((typ, flags))
59+
splitline = [x.strip() for x in line.split()]
60+
typ = splitline[ofs_typ]
61+
if not typ.startswith('[R'): # skip [Requesting ...]
62+
splitline = [x.strip() for x in line.split()]
63+
flags = splitline[ofs_flags]
64+
# check for 'R', ' E'
65+
if splitline[ofs_flags + 1] is 'E':
66+
flags += ' E'
67+
headers.append((typ, flags, []))
68+
count += 1
69+
70+
if line.startswith(' Section to Segment mapping:'):
71+
in_mapping = True
72+
count = 0
73+
if line == '':
74+
in_mapping = False
75+
if in_mapping:
76+
if count == 1: # header line
77+
ofs_segment = line.find('Segment')
78+
ofs_sections = line.find('Sections...')
79+
if ofs_segment == -1 or ofs_sections == -1:
80+
raise ValueError('Cannot parse elfread -lW output')
81+
elif count > 1:
82+
segment = int(line[ofs_segment:ofs_sections].strip())
83+
sections = line[ofs_sections:].strip().split()
84+
headers[segment][2].extend(sections)
6285
count += 1
6386
return headers
6487

@@ -68,7 +91,7 @@ def check_ELF_NX(executable) -> bool:
6891
'''
6992
have_wx = False
7093
have_gnu_stack = False
71-
for (typ, flags) in get_ELF_program_headers(executable):
94+
for (typ, flags, _) in get_ELF_program_headers(executable):
7295
if typ == 'GNU_STACK':
7396
have_gnu_stack = True
7497
if 'W' in flags and 'E' in flags: # section is both writable and executable
@@ -82,7 +105,7 @@ def check_ELF_RELRO(executable) -> bool:
82105
Dynamic section must have BIND_NOW flag
83106
'''
84107
have_gnu_relro = False
85-
for (typ, flags) in get_ELF_program_headers(executable):
108+
for (typ, flags, _) in get_ELF_program_headers(executable):
86109
# Note: not checking flags == 'R': here as linkers set the permission differently
87110
# This does not affect security: the permission flags of the GNU_RELRO program
88111
# header are ignored, the PT_LOAD header determines the effective permissions.
@@ -113,6 +136,62 @@ def check_ELF_Canary(executable) -> bool:
113136
ok = True
114137
return ok
115138

139+
def check_ELF_separate_code(executable):
140+
'''
141+
Check that sections are appropriately separated in virtual memory,
142+
based on their permissions. This checks for missing -Wl,-z,separate-code
143+
and potentially other problems.
144+
'''
145+
EXPECTED_FLAGS = {
146+
# Read + execute
147+
'.init': 'R E',
148+
'.plt': 'R E',
149+
'.plt.got': 'R E',
150+
'.plt.sec': 'R E',
151+
'.text': 'R E',
152+
'.fini': 'R E',
153+
# Read-only data
154+
'.interp': 'R',
155+
'.note.gnu.property': 'R',
156+
'.note.gnu.build-id': 'R',
157+
'.note.ABI-tag': 'R',
158+
'.gnu.hash': 'R',
159+
'.dynsym': 'R',
160+
'.dynstr': 'R',
161+
'.gnu.version': 'R',
162+
'.gnu.version_r': 'R',
163+
'.rela.dyn': 'R',
164+
'.rela.plt': 'R',
165+
'.rodata': 'R',
166+
'.eh_frame_hdr': 'R',
167+
'.eh_frame': 'R',
168+
'.qtmetadata': 'R',
169+
'.gcc_except_table': 'R',
170+
'.stapsdt.base': 'R',
171+
# Writable data
172+
'.init_array': 'RW',
173+
'.fini_array': 'RW',
174+
'.dynamic': 'RW',
175+
'.got': 'RW',
176+
'.data': 'RW',
177+
'.bss': 'RW',
178+
}
179+
# For all LOAD program headers get mapping to the list of sections,
180+
# and for each section, remember the flags of the associated program header.
181+
flags_per_section = {}
182+
for (typ, flags, sections) in get_ELF_program_headers(executable):
183+
if typ == 'LOAD':
184+
for section in sections:
185+
assert(section not in flags_per_section)
186+
flags_per_section[section] = flags
187+
# Spot-check ELF LOAD program header flags per section
188+
# If these sections exist, check them against the expected R/W/E flags
189+
for (section, flags) in flags_per_section.items():
190+
if section in EXPECTED_FLAGS:
191+
if EXPECTED_FLAGS[section] != flags:
192+
return False
193+
return True
194+
116195
def get_PE_dll_characteristics(executable) -> int:
117196
'''Get PE DllCharacteristics bits'''
118197
stdout = run_command([OBJDUMP_CMD, '-x', executable])
@@ -225,7 +304,8 @@ def check_MACHO_Canary(executable) -> bool:
225304
('PIE', check_ELF_PIE),
226305
('NX', check_ELF_NX),
227306
('RELRO', check_ELF_RELRO),
228-
('Canary', check_ELF_Canary)
307+
('Canary', check_ELF_Canary),
308+
('separate_code', check_ELF_separate_code),
229309
],
230310
'PE': [
231311
('DYNAMIC_BASE', check_PE_DYNAMIC_BASE),

contrib/devtools/test-security-check.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ def test_ELF(self):
3131
cc = 'gcc'
3232
write_testcode(source)
3333

34-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-zexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE']),
34+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-zexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
3535
(1, executable+': failed PIE NX RELRO Canary'))
36-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE']),
36+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fno-stack-protector','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
3737
(1, executable+': failed PIE RELRO Canary'))
38-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro','-no-pie','-fno-PIE']),
38+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro','-no-pie','-fno-PIE', '-Wl,-z,separate-code']),
3939
(1, executable+': failed PIE RELRO'))
40-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro','-pie','-fPIE']),
40+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro','-pie','-fPIE', '-Wl,-z,separate-code']),
4141
(1, executable+': failed RELRO'))
42-
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE']),
42+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,noseparate-code']),
43+
(1, executable+': failed separate_code'))
44+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE', '-Wl,-z,separate-code']),
4345
(0, ''))
4446

4547
def test_PE(self):

0 commit comments

Comments
 (0)