1 AUTHOR: Robert Connolly <robert at linuxfromscratch dot org> (ashes)
7 SYNOPSIS: Stack Smashing Protector, and _FORTIFY_SOURCE
9 PRIMARY URL: http://www.linuxfromscratch.org/hints/
12 Stack Smashing Protector (SSP) is a C, C++, Obj, and Obj++ debugging/security
13 extension for GCC. SSP was originally developed by IBM for protecting
14 applications from the single largest class of program attacks, and it has
15 since been adopted by many security oriented operating systems. More recently
16 SSP was officially added to GCC, Glibc, and uClibc. This recent addition
17 modified the original SSP implementation to add SSP to Tread Local Storage,
18 so that each thread can be guarded separately. The IBM homepage for SSP is
19 here: http://www.trl.ibm.com/projects/security/ssp/
20 Another nice description is here:
21 http://www.usenix.org/events/sec01/full_papers/frantzen/frantzen_html/
23 "Hiroaki Etoh's ProPolice is a modification to the GNU C compiler that places a
24 random canary between any stack allocated character buffers and the return
25 pointer [5]. It then validates that the canary has not been dirtied by an
26 overflowed buffer before the function returns. ProPolice can also reorder local
27 variables to protect local pointers from being overwritten in a buffer overflow.
29 _FORTIFY_SOURCE is a Glibc feature which adds memory and string function
30 protection. There is no home site for this feature, but it is described well
31 on this page: http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
34 GCC-4.1 (or newer) for SSP and _FORTIFY_SOURCE.
35 Glibc-2.4 (or newer) for SSP and _FORTIFY_SOURCE.
37 The standard version of SSP uses /dev/urandom directly. When a whole system is
38 built with SSP this tends to consume all the kernel entropy. /dev/erandom is
39 reccomended for SSP to conserve kernel entropy. See the entropy.txt hint for
40 this at: http://www.linuxfromscratch.org/hints/downloads/files/entropy.txt
45 Stack Smashing Protector
48 - Stack Smashing Protector
50 The GCC options for SSP are -fstack-protector, -fstack-protector-all, and
51 -Wstack-protector. The -fstack-protector option only protects functions with
52 character arrays, and is generally not recomended. The -fstack-protector-all
53 option protects all functions. The -Wstack-protector option will produce a
54 warning about any functions which are not protected. This warning can occure
55 in functions with buffers smaller than 8 bytes.
57 The '--param=ssp-buffer-size=' GCC option controls the minimum buffer size
60 There have been reports of problems with SSP and 'gcc -O3' with Python. It
61 may or may not cause problems in other packages with -O3.
63 The GCC manual page says to avoid using '-Wp' whenever possible, so use
64 -D_FORTIFY_SOURCE=2 when using _FORTIFY_SOURCE.
70 # In chapter 5 of the LFS book, you don't need to do anything different.
78 # Make SSP use /dev/erandom:
80 sed -i 's@/dev/urandom@/dev/erandom@' sysdeps/unix/sysv/linux/dl-osinfo.h
82 # The following does not work with Glibc-2.6.1... the build will go into an
83 # infinite loop. This does work with Glibc-2.5.
85 # Glibc's libraries can not be built with SSP or _FORTIFY_SOURCE, but the
86 # applications can. This is optional.
88 # The 'nscd' program is built with -fstack-protector by default. The following
89 # command will make -fstack-protector-all be used instead, for better
92 sed -i 's/fstack-protector/&-all/' nscd/Makefile
94 # After running ./configure, the follwing command will tell Glibc to build
95 # the libraries but not the application programs:
97 echo 'build-programs=no' > configparms
99 # Then run 'make' normally. Now the programs can be built with SSP and
100 # _FORTIFY_SOURCE. You can build the applications with SSP and/or
101 # _FORTIFY_SOURCE... both are optional and independent of eachother. To build
102 # Glibc's applications with both SSP and _FORTIFY_SOURCE use the following
103 # command after building the libraries:
105 echo 'CC = gcc -fstack-protector-all -D_FORTIFY_SOURCE=2
106 CXX = g++ -fstack-protector-all -D_FORTIFY_SOURCE=2
109 # Then run 'make' again.
111 # The CC and CXX variables are used instead of CFLAGS and CXXFLAGS because
112 # CFLAGS and CXXFLAGS are sometimes ignored by the Glibc build system.
114 # The Glibc test suite should pass as if -fstack-protector-all and
115 # -D_FORTIFY_SOURCE=2 were not used. Continue to test and install Glibc
120 # To make GCC use SSP by default get:
121 # http://www.linuxfromscratch.org/patches/downloads/gcc/
122 # gcc-4.1.2-fstack_protector-1.patch
124 # http://www.linuxfromscratch.org/~robert/new/patches/
125 # gcc-4.2.1-fstack_protector.patch
127 patch -Np1 -i gcc-4.1.2-fstack_protector-1.patch
129 # This SSP patch adds -fstack-protector-all as the default for C, C++, OBJC,
132 # To make GCC use -D_FORTIFY_SOURCE=2 by default get (this patch works for
134 # http://www.linuxfromscratch.org/patches/downloads/gcc/
135 # gcc-4.1.2-fortify_source-1.patch
137 # If you want to build GCC itself with SSP and _FORTIFY_SOURCE, then use
138 # 'make bootstrap'. If you want to build Binutils with SSP and _FORTIFY_SOURCE
139 # then rebuild and reinstall it. Add --disable-werror to work around warnings
140 # caused by _FORTIFY_SOURCE.
143 env CC="gcc -fno-stack-protector -U_FORTIFY_SOURCE" ./configure...
150 # The recent 2.6 kernels will detect SSP and disable it. _FORTIFY_SOURCE can
151 # be built into the kernel, or you can disable it with:
152 # make CC="gcc -U_FORTIFY_SOURCE"
158 # There have been problems reported with Python built with SSP and -O3. Use
159 # -O2 to build Python with SSP.
165 # The Glibc test suite includes tests for SSP and _FORTIFY_SOURCE.
167 # Additional regression tests can be found in NetBSD's regress/lib/libc/ssp/.
169 # There are a couple tests in the 'paxtest' package which may also be usefull.
170 # http://pax.grsecurity.net/paxtest-0.9.5.tar.gz
174 * Thanks to Hiroaki Etoh for providing the SSP patch to IBM
175 * Thanks to IBM for providing the SSP patch at
176 http://www.research.ibm.com/trl/projects/security/ssp/
177 * Thanks to OpenBSD for their XFree86 code. http://www.openbsd.org/
178 * Thanks to netsys.com for this
179 http://www.netsys.com/cgi-bin/display_article.cgi?1266
180 * Thanks to securityfocus.com and immunix.com for this
181 http://www.securityfocus.com/archive/1/333986/2003-08-17/2003-08-23/2
182 * Thanks to adamantix.org for kernel patches. http://www.adamantix.org/
183 * Thanks to Avaya Labs for Libsafe
184 http://www.research.avayalabs.com/project/libsafe/
185 * Thanks to Teemu Tervo for nptl hint
186 http://www.linuxfromscratch.org/hints/downloads/files/nptl.txt
187 * Thanks to cross compiling hint
188 http://www.linuxfromscratch.org/hints/downloads/files/ \
189 crosscompiling-x86.txt
190 * Thanks to http://www.isecurelabs.com/news/64 for proof of concept tests.
191 * Thanks to Eli Billauer for the Frandom suite
192 http://frandom.sourceforge.net/
193 http://www.billauer.co.il/
200 * Reformatted the patches so they're much easier to apply.
201 * Edit/rewrite hint & synopsis.
209 * GCC 2.95.3 patches made.
211 * XFree86-4.3.0 patch made.
212 * Hint is now Beta - Need more feedback.
215 * Reformatted patches.
219 * Add new example tests.
222 * Add homepage/mirror url.
225 * Added Glibc and kernel patches.
226 * Rewrote install procedure.
228 * Try to be more informative.
229 * Removed Gentoo property.
232 * Added new versions of binutils and glibc.
234 * Rename filename to winter.txt.
236 * Do not use "Enforce non-executable pages"
240 * Added LOPTS to Net-tools.
241 * Added LDFLAGS to Perl.
246 * Renamed hint back to propolice.txt.
247 * Added back Gentoo property as optional.
254 * Convert propolice to ssp
256 * Update gcc-3.3.3 and linux-2.6.2 ssp patches
258 * Update linux-2.6.3 patch and hgcc url
260 * Add sspspecs patch. Update.
262 * Added entropy.txt link for erandom.
264 * Fix more/again for erandom.
265 * Update some patches.
272 * Do not use -O3 or -O4
273 * Use CFLAGS="-O2" for Perl chapter 6.
275 * Remove frandom mktemp patch.
276 * Add note about arc4random.
277 * Update patches, new define for SYSCTL_ERANDOM.
281 * Add new Glibc patches with stderr overflow messages.
282 * Fixed sspspecs patches so they actually work with g++.
285 * Added note about using 2.6.7 frandom patch for older kernels.
287 * XFree86 patch works with Xorg too.
288 * -O3 optimizations are fine.
289 * Added -O2 to Grub's CFLAGS.
291 * Updated for LFS-6.0.
292 * Removed sspspecs patches, replaced with Perl command/script.
293 * Removed obsolete kernel patch.
294 * Added sed command for version.c.
295 * Added fstack_protector patch to Glibc in chapter 6.
296 * Add note for -O3 and Python.
298 * Fixed misspellings.
300 * Added --no-backup-if-mismatch to patch command.
302 * Fix commands for LFS-6.0.
305 * Added note for "ProPoliceSupport YES" in Xorg.
306 * Added sed for Arts.
308 * Finally updated for Glibc-2.4+ and GCC-4.1.
309 * Added _FORTIFY_SOURCE
310 * Removed Libsafe. It's own docs explain how to install it well.
312 * Arts can compile with -fstack-protector-all now.