aboutsummaryrefslogtreecommitdiff
blob: f6d6043398998fa03b13d486306bd9b6e86f9eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link title="new" rel="stylesheet" href="http://www.gentoo.org/css/main.css" type="text/css">
<link REL="shortcut icon" HREF="http://www.gentoo.org/favicon.ico" TYPE="image/x-icon">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/www-gentoo-org.xml" title="Gentoo Website">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/forums-gentoo-org.xml" title="Gentoo Forums">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/bugs-gentoo-org.xml" title="Gentoo Bugzilla">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/packages-gentoo-org.xml" title="Gentoo Packages">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/archives-gentoo-org.xml" title="Gentoo List Archives">
<title>Gentoo Linux Documentation
--
  The Gentoo Hardened Toolchain</title>
</head>
<body style="margin:0px;" bgcolor="#ffffff"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td valign="top" height="125" bgcolor="#45347b"><a href="http://www.gentoo.org/"><img border="0" src="http://www.gentoo.org/images/gtop-www.jpg" alt="Gentoo Logo"></a></td></tr>
<tr><td valign="top" align="right" colspan="1" bgcolor="#ffffff"><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>
<td width="99%" class="content" valign="top" align="left">
<table class="ncontent" align="center" width="90%" border="2px" cellspacing="0" cellpadding="4px"><tr><td bgcolor="#ddddff"><p class="note"><b>Disclaimer : </b>
    This document is a work in progress and should not be considered official yet.
  </p></td></tr></table>
<br><h1>The Gentoo Hardened Toolchain</h1>
<form name="contents" action="http://www.gentoo.org">
<b>Content</b>:
        <select name="url" size="1" OnChange="location.href=form.url.options[form.url.selectedIndex].value" style="font-family:sans-serif,Arial,Helvetica"><option value="#doc_chap1">1. Introduction to the Gentoo Hardened toolchain</option>
<option value="#doc_chap2">2. The Stack Smashing Protector (SSP)</option>
<option value="#doc_chap3">3. Position Independent Executables (PIEs)</option>
<option value="#doc_chap4">4. Mark Read-Only Appropriate Sections</option>
<option value="#doc_chap5">5. Binding policy NOW</option>
<option value="#doc_chap6">6. References</option></select>
</form>
<p class="chaphead"><a name="introduction"></a><a name="doc_chap1"></a><span class="chapnum">1.
            </span>Introduction to the Gentoo Hardened toolchain</p>
<p class="secthead"><a name="todo"></a><a name="doc_chap1_sect1">TODO</a></p>
<ul>
<li>Binutils modifications for PaX</li>
<li>Comment on relationship to SELinux</li>
</ul>
<p class="secthead"><a name="overview"></a><a name="doc_chap1_sect2">Overview</a></p>
<p>
The Gentoo Hardened project introduces a number of changes to the default behaviour of the toolchain (gcc, binutils, glibc/uclibc) intended to improve security. It supports other initiatives taken by the hardened project; most directly PaX and Grsecurity, but can also be applied to SELinux and RSBAC. This document describes each of the modifications made to the toolchain, showing what they achieve and how they relate to the Gentoo hardened strategy.
</p>
<p class="secthead"><a name="SSPintro"></a><a name="doc_chap1_sect3">Default addition of the Stack Smashing Protector (SSP)</a></p>
<p>
First developed by Dr Hiroaki Etoh at IBM for the 3.x series of GCC (originally under the name ProPolice) and re-developed in a different way for the 4.x series by RedHat, the Stack Smashing Protector attempts to protect against stack buffer overflows. It causes the compiler to insert a check for stack buffer overflows before function returns.  If an attempt is made to exploit a previously unfixed (and probably undiscovered) error that exposes a buffer overflow vulnerability, the application will be killed immediately.  This reduces any potential exploit to a denial-of-service.
</p>
<p>
Normally the compiler must be explicitly directed to switch on the stack protection via compiler options. The Gentoo hardened GCC switches on the stack protector by default unless explicitly requested not to. The chapter "<a href="#SSP">The Stack Smashing Protector</a>" describes the toolchain modifications to make this happen, and issues that may arise.
</p>
<p class="secthead"><a name="PIEintro"></a><a name="doc_chap1_sect4">Automatic generation of Position Independent Executables (PIEs)</a></p>
<p>
Standard executables have a fixed base address, and they must be loaded to this address otherwise they will not execute correctly. Position Independent Executables can be loaded anywhere in memory much like shared libraries, allowing <a href="pax-quickstart.html">PaX</a>'s Address Space Layout Randomisation (ASLR) to take effect.  This is achieved by building the code to be position-independent, and linking them as ELF shared objects.
</p>
<p>
In 2003 Hardened Gentoo introduced an approach referred to as '-y etdyn' which consisted of building all code with -fPIC, and modifying the link stage to provide an ET_DYN executable using a modified PIC version of crt1.o, and setting the interp header to cause the executable to be loaded by the loader from glibc.  ET_DYN versions of the crt1.o object were created for x86, parisc, ppc and sparc.
</p>
<p>
Further work was undertaken by RedHat, who implemented a '-pie' switch for the linker, and '-fPIE' to be used when compiling objects for linking into a Position Independent Executable.  Building an object with -fPIE is slightly different from -fPIC; in particular not all symbols are vectored through the GOT which means pre-loaded shared libraries do not override these symbols in the executable, which is also the case for ET_EXEC executables.
</p>
<p>
As the support for PIEs in the upstream toolchain matured, Hardened Gentoo switched to PIE, dropping the earlier "-y et_dyn" support.  PIEs have several advantages, not least of which is that upstream have taken on the burden of support in gcc, glibc and binutils.
</p>
<p>
The Gentoo hardened GCC automatically builds PIEs when building application code, unless explicitly requested not to (with a few built-in exceptions for cases where it is undesirable). The chapter "<a href="#PIE">Position Independent Executables</a> describes the toolchain modifications to make this happen, and issues that may arise.
</p>
<p class="secthead"><a name="RELROintro"></a><a name="doc_chap1_sect5">Default to marking read-only, sections that can be so marked after the loader is finished (RELRO)</a></p>
<p>
There are several sections that need to be writable by the loader before the application starts, but do not need to be writable by the application itself later.  Setting relro instructs the linker to record which sections this applies to, and the loader will mark them read-only before passing or returning execution control to the application.  Typical sections affected include .ctors, .dtors, .jcr, .dynamic and .got, although the exact list varies according to arch.  If <a href="#NOWintro">BIND_NOW</a> is also set then on some arches all of the GOT (i.e. including the PLT) can be set read-only in this way, preventing various attack methods that involve overwriting it.
</p>
<p>
The Gentoo hardened GCC automatically sets the linker to set RELRO, unless explicitly requested not to.  The chapter "<a href="#RELRO">Read-Only Relocation tables</a>" describes the toolchain modifications to make this happen.
</p>
<p class="secthead"><a name="NOWintro"></a><a name="doc_chap1_sect6">Default full binding at load-time (BIND_NOW)</a></p>
<p>
To reduce the time between starting an application and actually being able to use it, most software is built with "lazy binding". This means that references to functions in shared libraries are resolved when they are actually used for the first time, rather than when the application is loaded. The hardened toolchain changes this behaviour so that by default it will set the "BIND_NOW" flag, which causes the loader to sort out all of these links before starting execution.  It improves the effectiveness of <a href="#RELROintro">RELRO</a>.
</p>
<p>
The Gentoo hardened GCC automatically sets the linker to set the BIND_NOW flag, unless explicitly requested not to.  The chapter "<a href="#NOW">Binding policy NOW</a>" describes the toolchain modifications to make this happen.
</p>
<p class="chaphead"><a name="SSP"></a><a name="doc_chap2"></a><span class="chapnum">2.
            </span>The Stack Smashing Protector (SSP)</p>
<p class="secthead"><a name="SSPrationale"></a><a name="doc_chap2_sect1">Rationale for enabling the stack smashing protector globally</a></p>
<p>
The stack smashing protector arranges the code so that a stack overflow is very likely to be detected by the application, which then aborts. This means that an attacker who tries to exploit such a stack overflow error can cause the application to die, but cannot exploit the error to execute code. So the threat is reduced from a privilege elevation to at most a denial of service. Obviously, any such errors in the code should be fixed - SSP is not an excuse to avoid fixing them - however it is extremely difficult to be confident no such errors remain, even after thorough code review and testing.  So SSP remains as a safety-net for unfixed stack overflow errors.
</p>
<p>
This is an important part of the overall Hardened Toolchain/PaX/GRsecurity strategy.  PaX prevents stack overflows from being executable by making the stack non-executable so an attacker cannot just put his shellcode into a buffer that overflows - however it does not prevent attacks that alter data that affect the program flow; in particular attacks that modify return addresses on the stack.
</p>
<p class="secthead"><a name="SSPtoolchain"></a><a name="doc_chap2_sect2">Toolchain modifications for default SSP</a></p>
<p>
At Gentoo we add stack smashing protection patches to the GCC-3.x series compilers and the C library (glibc-2.3.x and uclibc).  As such this is the most invasive action taken to harden the toolchain.  From gcc-4.1 and glibc-2.4 onwards a different implementation of SSP is provided by the upstream GNU toolchain; glibc-2.4 is also patched by Gentoo to support the GCC-3.x implementation of SSP so binaries built with either SSP implementation will work.
</p>
<p>
The patches to GCC-3.x are not trivial, so a detailed explanation here is not suitable, even if we could write one.  The modifications to the C library are simpler; they provide the canary value which is randomised separately for every process (and separately for every thread in the gcc-4.x/glibc-2.4 implementation), and the handler function called when the canary is checked and found to be corrupt.  The handler function reports an error and shuts down the process; this has to be done very carefully to ensure the handler itself cannot be exploited.
</p>
<p class="secthead"><a name="SSPissues"></a><a name="doc_chap2_sect3">Issues arising from default SSP</a></p>
<p>
The SSP implementation in gcc-3.x is not perfect, and can cause problems.  In particular C++ code can be built incorrectly when SSP is enabled, although the exact details are not clear at the moment.
</p>
<p>
The SSP implementation in gcc-4.x is completely different, even so far as changing the semantics of the compiler switches.  At the time of writing, we have little experience in the SSP implementation in gcc-4.x.
</p>
<p class="chaphead"><a name="PIE"></a><a name="doc_chap3"></a><span class="chapnum">3.
            </span>Position Independent Executables (PIEs)</p>
<p class="secthead"><a name="PIErationale"></a><a name="doc_chap3_sect1">Rationale for building Position Indepedent Executables (PIEs)</a></p>
<p>
The reason for building applications as position-independent is to allow the application to be loaded at a random address; normally the kernel loads all executables to the same fixed address.  Randomising this address makes it harder for an attacker to exploit the executable, since it is harder to know where the code (and heap) reside.
</p>
<p>
This is most effective when running a PaX kernel with Address Space Layout Randomisation (ASLR), which increases the randomness of the various parts of a process significantly.  It is also necessary to enable the GRsecurity option to hide the location information in the /proc filesystem - otherwise the attacker can just look there to find the addresses needed!
</p>
<p>
A note on prelink: prelinking sets hints for the address at which an ELF file will be loaded.  These hints, if followed, would make ASLR ineffective.  The PaX kernel causes these hints to be ignored, so prelinking does nothing useful for a Gentoo Hardened system.  Since there is no point using prelink, just don't use it.
</p>
<p>
Not using prelink also means you don't have to worry about knock-on effects of prelink.  Since prelink modifies the ELF files whenever it prelink is run, these changes need to be propogated to other systems that depend on them.  For example host-based Intrusion Detection Systems (IDS) watch for changes to executables and libraries and need to be informed of the changes prelink makes - if prelink is not used then maintenance of such systems is simplified.
</p>
<p>
There are some technologies on the way which reduce the need for prelink.  These include:
</p>
<ul>
<li>Symbol visibility support, which when used properly, reduces dramatically the number of symbols to resolve and hence the amount of time taken to resolve them</li>
<li>Hash tables, which will be generated by the linker and included as a extra section in the ELF file, which make looking up symbols to resolve them nearly free.</li>
<li>Direct binding, which simplifies the search that the loader by incorporating information in each library detailing exactly where the symbol to be resolved is located.</li>
</ul>
<p>
See <a href="#NOWissues">Issues arising from BIND_NOW</a> for more.
</p>
<p class="secthead"><a name="PIEtoolchain"></a><a name="doc_chap3_sect2">Toolchain modifications for automatic PIEs</a></p>
<p>
Support for position-independent executables is provided by the standard GNU toolchain.  For PIEs, GCC has different versions of some of the compiler support objects so that for example instead of using crtbegin.o, crt1.o and crtend.o it uses crtbeginS.o, Scrt1.o and crtendS.o (the exact files vary according to the compiler target).  It also builds code in a similar fashion to library PIC code, although in the case of executables some symbols are not referenced via the Global Offset Table (GOT).  The compiler obtains the list of compiler support objects via the "specs" file - see "info gcc" section 3 (Invoking GCC) subsection 15 (Spec Files).  Building code for PIEs is achieved by adding '-fPIE' when compiling and '-fPIE -pie' when linking.
</p>
<p>
To change the default for building executables from absolute to position-independent, it is necessary to change the selection of support objects and to set the -fPIE and -pie options appropriately.  The specs rules for this are startfile, endfile, cc1 and link_command.  Exact details of the modifications vary according to the target; to illustrate here are the changes for x86:
</p>
<a name="doc_chap3_pre1"></a><table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td bgcolor="#7a5ada"><p class="codetitle">Code Listing3.1: standard cc1 rule for x86</p></td></tr>
<tr><td bgcolor="#eeeeff" align="left" dir="ltr"><pre>
%(cc1_cpu) %{profile:-p}
</pre></td></tr>
</table>
<a name="doc_chap3_pre2"></a><table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td bgcolor="#7a5ada"><p class="codetitle">Code Listing3.2: addition to cc1 rule for x86</p></td></tr>
<tr><td bgcolor="#eeeeff" align="left" dir="ltr"><pre>
%{!D__KERNEL__: %{!static: %{!fno-PIC: %{!fno-pic: %{!shared:
%{!nostdlib: %{!nostartfiles: %{!fno-PIE: %{!fno-pie: %{!nopie:
%{!fPIC: %{!fpic:-fPIE} } } } } } } } } } } }
</pre></td></tr>
</table>
<p>
This looks a lot more scary than it really is. All it says is that unless one or more of the listed options are specified, add -fPIE to the compilation.  The kernel defines __KERNEL__ on all its compilations, so the -D__KERNEL__ check ensures that -fPIE is not added when building the kernel; the kernel is a static executable but as <a href="#PIEissues">explained below</a> this is not so easy to detect.  When building shared libraries, either -fPIC or -fpic should be specified, so this is used to prevent adding -fPIE when building shared libraries.  The -fno-* checks are to ensure that if a build explicitly requests no position-indepedent code, -fPIE is not added.
</p>
<a name="doc_chap3_pre3"></a><table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td bgcolor="#7a5ada"><p class="codetitle">Code Listing3.3: standard link_command rule for x86</p></td></tr>
<tr><td bgcolor="#eeeeff" align="left" dir="ltr"><pre>
%{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:    %(linker) %l
%{pie: -pie} %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r}
%{s} %{t} %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}
%{static:} %{L*} %(link_libgcc) %o %{fprofile-arcs|fprofile-generate:-lgcov}
%{!symbolic:%{!shared:%{fbounds-checking:libboundscheck.a%s}}}
%{!symbolic:%{!shared:%{fbc-strings-only:libboundscheck.a%s}}}
%{!nostdlib:%{!nodefaultlibs:%(link_gcc_c_sequence)}}
%{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}
</pre></td></tr>
</table>
<a name="doc_chap3_pre4"></a><table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td bgcolor="#7a5ada"><p class="codetitle">Code Listing3.4: replacement for %{pie: -pie} in link_command rule for x86</p></td></tr>
<tr><td bgcolor="#eeeeff" align="left" dir="ltr"><pre>
%{!nopie: %{!static: %{!A: %{!shared: %{!nostdlib: %{!nostartfiles:
%{!fno-PIE: %{!fno-pie: -pie} } } } } } } } %{pie: -pie}
</pre></td></tr>
</table>
<p>
As with the cc1 rule, the addition causes -pie to be set for link commands unless one of the listed options are specified.  Note that this replaces the '%{pie: -pie}' section in the original link_command rule.
</p>
<p>
In both rules, there is an additional condition '!nopie' - this provides a mechanism to stop the hardened compiler defaulting to PIE by adding '-nopie' to CFLAGS.  This is what filter-flags does when asked to filter -fPIE and the compiler is hardened.
</p>
<p class="secthead"><a name="PIEissues"></a><a name="doc_chap3_sect3">Issues with PIEs</a></p>
<p>
Ideally, when building a static executable, a shared library, or building without the standard gcc system files, PIE would not be automatically enabled.  Unfortunately, the options -static, -shared, -nostdlib and -nostartfiles are link options, so are usually only supplied to the link command of an executable and not to the compilation commands for individual objects.  In these cases, -fPIE will be added to the compilation commands when in fact it shouldn't be.  This is an unavoidable limitation, since it is impossible to know from a compilation command for an object exactly what the link command is going to be.  Indeed in some cases more than one link command can happen using the same objects.  Such cases have to be handled by the relevant ebuild on a case-by-case basis.  The -nostdlib and -nostartfiles options occur rarely.  The -shared option is usually used when the compilation commands have been performed with -fPIC, so the majority of such cases are not a problem.
</p>
<p>
Where an application builds libraries without -fPIC, it is necessary to modify the build process to avoid -fPIE being added by the compiler.  For packages that build only libraries, it is sufficient to do:
</p>
<a name="doc_chap3_pre5"></a><table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td bgcolor="#7a5ada"><p class="codetitle">Code Listing3.5: switch off automatic PIE for library ebuilds</p></td></tr>
<tr><td bgcolor="#eeeeff" align="left" dir="ltr"><pre>
inherit flag-o-matic
...
src_compile() {
...
filter-flags -fPIE
...
}
</pre></td></tr>
</table>
<p>
However if an ebuild creates both executables and libraries then more detailed modifications need to be made, to add the -fno-PIE to the compilation of objects destined for the libraries.  Where an object is used for both a shared library and an executable, it is necessary to modify the build process significantly in order to obtain two objects, one built -fPIC and one built -fPIE for linking to the library and the executable respectively. Most packages that provide both a shared library and a static archive do so by using libtool which does the right thing automatically.  Both of these approaches can be taken unconditionally; i.e. it is not necessary to make such changes conditional on the presence of the hardened compiler.
</p>
<p>
Occasionally application code will fail to compile with -fPIE.  If this happens it is usually down to non-position-independent assembler code, and is most prevelant on X86 which has a limited general purpose register set.  However this is rare in application code as normally application authors push most of their code into shared libraries, although it does happen.  Most position-independent build problems occur in shared libraries which are not built position-independent - this is a problem regardless of Hardened, and is nothing to do with PIE; it is just that the issue is highlighted by the hardened compiler due to the automatic enabling of -fPIE when -fPIC is not specified as described above.  See the <a href="pic-fix-guide.html">PIC fixing guide</a> for information on how to fix this sort of problem.
</p>
<p>
Some applications have been reported to segfault when built as PIEs.  Exactly why this occurs is unclear, but it is likely due to a compiler bug so later compiler versions may resolve such problems.
</p>
<p>
Debugging PIEs at the time of writing only works with sys-devel/gdb-6.3-r5, which includes patches by Elena Zannoni at RedHat.  These patches were not included in the main trunk of gdb, so have not been maintained in later versions.
</p>
<p class="chaphead"><a name="RELRO"></a><a name="doc_chap4"></a><span class="chapnum">4.
            </span>Mark Read-Only Appropriate Sections</p>
<p class="secthead"><a name="RELROrationale"></a><a name="doc_chap4_sect1">Rationale for enabling RELRO globally</a></p>
<p>
Various sections in an ELF file should be written only by the loader, and not by the application.  However in normal circumstances these sections remain read/write throughout the life of the process, and there are attack methods that exploit this to affect program execution flow.  Enabling RELRO causes the linker to include an extra header informing the loader which sections can be marked read-only after the loader has finished with them.  It also affects the layout of sections slightly, to avoid having RELRO sections and read-write sections on the same memory page.  Combining RELRO with BIND_NOW allows also the PLT to be managed this way on some arches.
</p>
<p class="secthead"><a name="RELROauto"></a><a name="doc_chap4_sect2">Toolchain modifications for default RELRO</a></p>
<p>
RELRO is a link option ("-z relro") provided by the standard GNU toolchain.  To switch it on by default, it is simply a matter of adding a small rule to the specs file for GCC as illustrated below:
</p>
<a name="doc_chap4_pre1"></a><table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td bgcolor="#7a5ada"><p class="codetitle">Code Listing4.1: standard link_command rule for x86</p></td></tr>
<tr><td bgcolor="#eeeeff" align="left" dir="ltr"><pre>
%{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:    %(linker) %l
%{pie: -pie} %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r}
%{s} %{t} %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}
%{static:} %{L*} %(link_libgcc) %o %{fprofile-arcs|fprofile-generate:-lgcov}
%{!symbolic:%{!shared:%{fbounds-checking:libboundscheck.a%s}}}
%{!symbolic:%{!shared:%{fbc-strings-only:libboundscheck.a%s}}}
%{!nostdlib:%{!nodefaultlibs:%(link_gcc_c_sequence)}}
%{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}
</pre></td></tr>
</table>
<a name="doc_chap4_pre2"></a><table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td bgcolor="#7a5ada"><p class="codetitle">Code Listing4.2: additionl segment to follow %{pie: -pie} in link_command rule for x86</p></td></tr>
<tr><td bgcolor="#eeeeff" align="left" dir="ltr"><pre>
%{!norelro: -z relro} %{relro: }
</pre></td></tr>
</table>
<p>
So a new option is introduced, "norelro", which can be used to prevent the hardened compiler from automatically switching on RELRO.  However this is likely to be phased out, as newer binutils provide a "-z norelro" option which can be appended to LDFLAGS as "-Wl,z,norelro".
</p>
<p class="secthead"><a name="RELROissues"></a><a name="doc_chap4_sect3">Issues arising from default RELRO</a></p>
<p>
So far, the hardened project has found no issues with switching on RELRO by default. It can make the executable image a little bit bigger (on average by half a page i.e. 2K bytes) which may be of interest for targets with extremely limited memory.
</p>
<p class="chaphead"><a name="NOW"></a><a name="doc_chap5"></a><span class="chapnum">5.
            </span>Binding policy NOW</p>
<p class="secthead"><a name="NOWrationale"></a><a name="doc_chap5_sect1">Rationale for enabling NOW binding globally</a></p>
<p>
As described in the <a href="#RELRO">RELRO</a> chapter, setting BIND_NOW increases the effectiveness of setting RELRO, making attacks that involve overwriting data in the Global Offset Table (GOT) fail.
</p>
<p class="secthead"><a name="NOWauto"></a><a name="doc_chap5_sect2">Toolchain modifications for default NOW</a></p>
<p>
NOW binding is a link option ("-z now") provided by the standard GNU toolchain.  To switch it on by default, it is simply a matter of adding a small rule to the specs file for GCC as illustrated below:
</p>
<a name="doc_chap5_pre1"></a><table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td bgcolor="#7a5ada"><p class="codetitle">Code Listing5.1: standard link_command rule for x86</p></td></tr>
<tr><td bgcolor="#eeeeff" align="left" dir="ltr"><pre>
%{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:    %(linker) %l
%{pie: -pie} %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r}
%{s} %{t} %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}
%{static:} %{L*} %(link_libgcc) %o %{fprofile-arcs|fprofile-generate:-lgcov}
%{!symbolic:%{!shared:%{fbounds-checking:libboundscheck.a%s}}}
%{!symbolic:%{!shared:%{fbc-strings-only:libboundscheck.a%s}}}
%{!nostdlib:%{!nodefaultlibs:%(link_gcc_c_sequence)}}
%{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}
</pre></td></tr>
</table>
<a name="doc_chap5_pre2"></a><table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td bgcolor="#7a5ada"><p class="codetitle">Code Listing5.2: additionl segment to follow %{pie: -pie} in link_command rule for x86</p></td></tr>
<tr><td bgcolor="#eeeeff" align="left" dir="ltr"><pre>
%{!nonow: -z now} %{now: }
</pre></td></tr>
</table>
<p>
So a new option is introduced, "nonow", which can be used to prevent the hardened compiler from automatically switching on NOW binding.  However this is likely to be phased out, as newer binutils provide a "-z lazy" option which can be appended to LDFLAGS as "-Wl,z,lazy".
</p>
<p class="secthead"><a name="NOWissues"></a><a name="doc_chap5_sect3">Issues arising from default NOW</a></p>
<p>
NOW binding has several noticeable effects.  The first is that initial loading time for applications increases, sometimes very noticeably, as the loader resolves all the references before passing execution to the loaded process.
</p>
<p>
One technology that could reduce this overhead significantly is the introduction of "Direct Binding", something that exists on Unix systems (e.g. Solaris) but does not exist in the GNU toolchain.  Direct binding adds information to libraries when they are built, to tell the linker which library contains the symbol it is looking for.  Normally the linker performs a search across all referenced libraries to find symbols, which adds significantly to the time taken to resolve them.  However the implications of direct-binding are significant, and cannot be taken lightly.  Michael Meeks at Novell is working on this; see our <a href="http://bugs.gentoo.org/114008">bug #114008</a> for our status on this.
</p>
<p>
Other technologies which should help are symbol visibility and hash tables in the ELF files.  Both are technologies supported upstream, so when they appear they will be supported directly.  With these two together, it is likely that there will not be much further benefit from direct binding and the complications that arise from direct binding may mean it won't be supported.
</p>
<p>
The second more serious effect is that applications that are not written to refer to shared libraries in the standard way can fail; the most obvious of these is X, which has modules with circular resolution dependencies amongst other unusual behaviour.  Another trick occasionally performed by applications is to decide between a number of shared libraries at run time, and use lazy binding to resolve references to the chosen library.  Normally this would be done with dlopen(3) and friends, including obtaining symbol addresses via dlsym(3), but it is possible to avoid using dlsym(3) and a plethora of pointers in the code by using lazy binding, although it's not pretty.
</p>
<p>
The following packages have issues with BIND_NOW at the time of writing, and it has to be relaxed somewhat for them:
</p>
<ul>
<li>X - some drivers consist of several libraries which are co-dependent, and the modules frequently have references to modules that they load.</li>
<li>transcode - relies on lazy binding to be able to load its modules; the issues are similar to the X issues.</li>
</ul>
<p class="chaphead"><a name="references"></a><a name="doc_chap6"></a><span class="chapnum">6.
            </span>References</p>
<p class="secthead"><a name="gentoorefs"></a><a name="doc_chap6_sect1">Other Gentoo Documentation</a></p>
<ul>
<li><a href="pax-quickstart.html">PaX QuickStart</a></li>
<li><a href="pic-guide.html">Introduction to Position-Independent Code (PIC)</a></li>
<li><a href="pic-fix-guide.html">Guide to fixing non-PIC shared libraries</a></li>
</ul>
<p class="secthead"><a name="externalrefs"></a><a name="doc_chap6_sect2">External Documentation</a></p>
<ul>
<li>
<a href="http://people.redhat.com/drepper/dsohowto.pdf">How to Write Shared Libraries</a> by Ulrich Drepper (PDF)</li>
</ul>
<br><p class="copyright">
	The contents of this document, unless otherwise expressly stated, are licensed under the <a href="http://creativecommons.org/licenses/by-sa/2.5">CC-BY-SA-2.5</a> license. The <a href="http://www.gentoo.org/main/en/name-logo.xml"> Gentoo Name and Logo Usage Guidelines </a> apply.
  </p>
<!--
  <rdf:RDF xmlns="http://web.resource.org/cc/"
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <License rdf:about="http://creativecommons.org/licenses/by-sa/2.5/">
     <permits rdf:resource="http://web.resource.org/cc/Reproduction" />
     <permits rdf:resource="http://web.resource.org/cc/Distribution" />
     <requires rdf:resource="http://web.resource.org/cc/Notice" />
     <requires rdf:resource="http://web.resource.org/cc/Attribution" />
     <permits rdf:resource="http://web.resource.org/cc/DerivativeWorks" />
     <requires rdf:resource="http://web.resource.org/cc/ShareAlike" />
  </License>
  </rdf:RDF>
--><br>
</td>
<td width="1%" bgcolor="#dddaec" valign="top"><table border="0" cellspacing="4px" cellpadding="4px">
<tr><td class="topsep" align="center"><p class="altmenu"><a title="View a printer-friendly version" class="altlink" href="hardened-toolchain.xml?style=printable">Print</a></p></td></tr>
<tr><td class="topsep" align="center"><p class="alttext">Updated August 31, 2006</p></td></tr>
<tr><td class="topsep" align="left"><p class="alttext"><b>Summary: </b>
Technical description of, and rationale for, the Gentoo Hardened Toolchain modifications.
</p></td></tr>
<tr><td align="left" class="topsep"><p class="alttext">
<a href="mailto:g2@kevquinn.com" class="altlink"><b>Kevin F. Quinn</b></a>
<br><i>Author</i><br><br>
<a href="mailto:solar@gentoo.org" class="altlink"><b>Ned Ludd</b></a>
<br><i>Contributor</i><br><br>
<a href="mailto:pageexec@freemail.hu" class="altlink"><b>The PaX Team</b></a>
<br><i>Contributor</i><br></p></td></tr>
<tr lang="en"><td align="center" class="topsep">
<p class="alttext"><b>Donate</b> to support our development efforts.
        </p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick"><input type="hidden" name="business" value="paypal@gentoo.org"><input type="hidden" name="item_name" value="Gentoo Linux Support"><input type="hidden" name="item_number" value="1000"><input type="hidden" name="image_url" value="http://www.gentoo.org/images/paypal.png"><input type="hidden" name="no_shipping" value="1"><input type="hidden" name="return" value="http://www.gentoo.org"><input type="hidden" name="cancel_return" value="http://www.gentoo.org"><input type="image" src="http://images.paypal.com/images/x-click-but21.gif" name="submit" alt="Donate to Gentoo">
</form>
</td></tr>
<tr lang="en"><td align="center"><iframe src="http://sidebar.gentoo.org" scrolling="no" width="125" height="850" frameborder="0" style="border:0px padding:0x" marginwidth="0" marginheight="0"><p>Your browser does not support iframes.</p></iframe></td></tr>
</table></td>
</tr></table></td></tr>
<tr><td colspan="2" align="right" class="infohead">
Copyright 2001-2011 Gentoo Foundation, Inc. Questions, Comments? <a class="highlight" href="http://www.gentoo.org/main/en/contact.xml">Contact us</a>.
</td></tr>
</table></body>
</html>