ssp and entropy hints

Robert Connolly robert at linuxfromscratch.org
Thu Nov 4 12:17:40 PST 2004


Please update these two attached hints.

A new define was added to the frandom patches so SYSCTL_ERANDOM is easy for 
code to detect. New package libarc4random-2.0 was added to entropy hint. 
Added note for patching existing /usr/include/linux/sysctl.h
-------------- next part --------------
AUTHOR:		Robert Connolly <robert at linuxfromscratch dot org> (ashes)

DATE:		2004-11-04

LICENSE:	Public Domain

SYNOPSIS:	Random number generation

PRIMARY URL:	http://www.linuxfromscratch.org/hints/

DESCRIPTION:
Many system components including smashing stack protector, mktemp,
cryptography, depend on a supply of random bits to ensure data integrity.
In the Linux kernel a combination of input devices are used to gather
randomness from. This includes the keyboard, mouse, and hard disc.
On an idle system none of these devices are receiving input, and the entropy
(randomness) of the system is easy to deplete, especially with cryptography.

Hardware random:
http://linuxcertified.com/hw_random.html
Some systems have hardware devices for random numbers. The kernel supports
many of them. For more information check the above web site. Also see:
http://sourceforge.net/projects/gkernel/

audio/video entropy daemon:
http://www.vanheusden.com/aed/
http://www.vanheusden.com/ved/
This describes two daemons which use either the static noise from the
system audio, or the video frames from a video4linux device. These devices
have a never ending supply of randomness created by thermal fluctuation and
electric fields on the devices. These entropy gathering daemons depend on the
kernel driver for your hardware to work properly, be it your sound or video
card. These programs will re-seed the kernel entropy pool. The programs can
be used together in combination with the kernel's internal values to create
a very random pool from several different sources.

Fast and Economical Random number suite:
http://frandom.sourceforge.net/
Frandom uses an arcfour stream cipher of seed data from the kernel's internal
pool. The advantage to frandom is that 256 bytes of kernel entropy can be
expanded into gigabytes of random output. Ideal for wiping discs, and maybe
even for online (casino) gaming. A new addition to the frandom package is
erandom. Economical random uses the state of frandom as a seed, and its use
does not drain any kernel entropy. This is done very efficiently, completely in
the kernel. Erandom is ideal for Smashing Stack Protector. Frandom now also
supports sysctl so SSP can use it regardless if /dev/erandom exists or not.
This is slightly faster and works through chroot. 

Note: The name "frandom" is used because thats the name of the package. The
patches for mktemp and ssp used the erandom interface even though the patch
is named frandom. Also, if you expect to have very long uptimes, the frandom
device should be dumped into /dev/null once in a while to reseed erandom.
Something like 'dd if=/dev/frandom of=/dev/null count=1' should be added to
your random script on boot, and done daily or weekly in your system scripts;
to prevent the output of erandom from ever repeating itself over several years
of use.

Libarc4random is an OpenBSD library function ported to Linux. So far only
Mktemp has been ported to use it. Libarc4random will use the Erandom sysctl
interface if SYSCTL_ERANDOM is defined from /usr/include/linux/sysctl.h
(if it is installed). This libaray will always return random data. If you
built Libarc4random with Erandom and later boot a vanilla kernel, this
library will try to use /dev/urandom next, if /dev/urandom does not exist
then gettimeofday will be used as a seed. The seed is used to stir an
Arcfour hash function. Therefore this library is more reliable then using
any one or two random sources alone.

LavaRnd Random Number Generator:
http://www.lavarnd.org/
This uses hardware as a source of entropy much like Video Entropy Daemon.

PREREQUISITES: None

HINT: 

Audio entropy daemon:
http://www.vanheusden.com/aed/
http://www.vanheusden.com/aed/audio-entropyd-0.0.6.tgz
http://www.linuxfromscratch.org/~robert/hlfs/hints/attachments/entropy/\
	audio-entropyd-0.0.6.tgz

make &&
install -g 0 -o 0 -m 755 audio-entropyd /usr/sbin/audio-entropyd

Edit your /etc/rc.d/init.d/random and start audio-entropyd just after seeding
urandom, and stop it just after saving random-seed. The PID file will be in
/var/run. You don't need to reboot to use it, but you do need your sound card
driver loaded, and be root.

Video entropy daemon:
http://www.vanheusden.com/ved/
http://www.vanheusden.com/ved/video_entropyd-0.7.tgz
http://www.linuxfromscratch.org/~robert/hlfs/hints/attachments/entropy/\
        video_entropyd-0.7.tgz

make &&
install -g 0 -o 0 -m 755 video_entropyd /usr/sbin/video_entropyd

Add this to root's crontab every minute or so. It can not run as a daemon
because it will lock the video device. Depends on video4linux. Using one or
both of these daemons should be adequate for sustained moderate-to-heavy use.

Nothing else needs to be done, applications can continue to use /dev/random
and /dev/urandom normally. You should notice crypto keys get made faster.

Frandom/erandom:
http://frandom.sourceforge.net/
http://www.linuxfromscratch.org/~robert/hlfs/hints/attachments/frandom/\
	frandom-0.8.tar.gz

http://www.linuxfromscratch.org/patches/downloads/linux/\
	linux-2.4.27-frandom-2.patch
http://www.linuxfromscratch.org/patches/downloads/linux/\
	linux-2.6.7-frandom-2.patch

Libarc4random:
http://www.linuxfromscratch.org/~robert/libarc4random/
	libarc4random-2.0.tar.bz2

And for LFS-6.0

http://www.linuxfromscratch.org/patches/downloads/\
	linux-libc-headers/linux-libc-headers-2.6-frandom-1.patch
ftp://twocents.mooo.com/pub/hlfs-patches/linux-libc-headers-2.6-frandom-1.patch

Or get frandom and other patches in:
http://www.linuxfromscratch.org/~robert/hlfs/downloads/cvs/\
	hlfs-patches-{date}.tar.bz2
This filename will change, for example: hlfs-patches-20041028.tar.bz2

You don't need the frandom-0.8 source, its presented so you can read more
about it if you want. The Linux kernel patch is all we need.
Frandom is built in by default with this patch. It can be found in the
character devices menu. Build and install the new kernel.

The frandom device is enabled by default. Make sure sysctl is also built in
and not a module or else erandom will only work as a character device.
cd linux-2.4.27
patch -Np1 -i ../linux-2.4.27-frandom-1.patch
...
reboot

mknod /dev/frandom c 235 11
mknod /dev/erandom c 235 12

Add something like this to root's crontab:

0 0 * * 1 /bin/dd if=/dev/frandom of=/dev/null count=1 >/dev/null 2>&1

This will reseed frandom every Monday.

Finally patch the header. On an existing system the header can be patched like
this (this works for both kernel 2.4 and 2.6):

cd /usr/include &&
patch -Np2 -i /path/to/linux-libc-headers-2.6-frandom-1.patch

To use it for SSP use the glibc-ssp-frandom patch.
http://www.linuxfromscratch.org/hints/downloads/files/ssp.txt

Libarc4random:
This is easy to install. libarc4random.so and .a are installed to /lib,
two example programs are installed to /usr/bin, and a man page to /usr.
`man 3 arc4random` for more info.

make &&
make install

In the patches/ directory is a patch for Mktemp. To install Mktemp with
libarc4random support simply do:

patch -Np1 -i ../mktemp-1.5-arc4random-1.patch

Then install Mktemp normally but _without_ the --with-libc option, which
will use libc's mkstemp function.

 - Testing entropy
You should try to test this on an idle machine. Nothing compiling in
background, no updatedb running, etc. Moving/clicking the mouse, keyboard, and
even network traffic will create entropy in the pool, and affect results.
Todo: Have tests for entropy quality, not just quantity.

Fetch this:
http://www.linuxfromscratch.org/~robert/hlfs/hints/attachments/entropy/
	entropy_avail.sh

Open two windows with non-root login. This is easiest to do in X, else split
a console window in two. In one window do this:

sh ./entropy_avail.sh

In the next window do something like this:

dd if=/dev/{u,f,e}random of=/dev/null bs=1 count=1024

If one or both of the entropyd programs are running you should see the pool
being refilled. Kill the entropyd program(s) and you should see it does not
refill so quickly. Move the mouse and play with it if you like. If you use a
small count like count=512 the entropyd program(s) may not refill immediately
because the pool is still large enough. This is to improve preformance.

You might want to delete entropy_avail.log when you're done.

ACKNOWLEDGMENTS:
* Thanks to Eli Billauer for the Frandom suite. -
	http://frandom.sourceforge.net/
	http://www.billauer.co.il/
* Thanks to hlfs-dev at linuxfromscratch.org

CHANGELOG:
[2004-03-29]
* Initial post
[2004-03-30]
* Added test.
[2004-04-18]
* Added frandom/erandom.
[2004-04-25]
* Added hardware random url and notes.
* Switched the entropy_avail program to a more simple shell script.
[2004-05-07]
* Added patch for kernel 2.6 and for mktemp.
* Added LavaRnd.
[2004-10-01]
* Added libc-headers patch.
[2004-10-28]
* New patch tarball url added.
* Added second url for linux-libc-headers patch.
[2004-11-03]
* Added Libarc4random.
* Changed Mktemp patch from frandom to libarc4random.

-------------- next part --------------
AUTHOR:		Robert Connolly <robert at linuxfromscratch dot org> (ashes)

DATE:		2004-10-30

LICENSE:	Public Domain

SYNOPSIS:	Smashing Stack Protector and Libsafe

PRIMARY URL:	http://www.linuxfromscratch.org/hints/downloads/files/ssp.txt

DESCRIPTION:
Smashing Stack Protector is a C and C++ security extension for GCC.
Libsafe prevents format string attacks.

Based on StackGaurd, SSP was developed by IBM for protecting applications
from stack smashing attacks. This is the single largest class of attacks and
many security oriented vendors have added it to their default compiler. The
overhead lost to this type of guard is minimal. In practice if the entire
system is built with SSP users shouldn't notice any difference in preformance.

The official homepage for ProPolice Smashing Stack Srotector is at:
http://www.trl.ibm.com/projects/security/ssp/

http://www.usenix.org/events/sec01/full_papers/frantzen/frantzen_html/\
        node30.html
"Hiroaki Etoh's ProPolice is a modification to the GNU C compiler that places a
random canary between any stack allocated character buffers and the return
pointer [5]. It then validates that the canary has not been dirtied by an
overflowed buffer before the function returns. ProPolice can also reorder local
variables to protect local pointers from being overwritten in a buffer overflow.
"
Also see:
http://www.linuxfromscratch.org/hlfs/
http://www.linuxfromscratch.org/~robert/FreeBSD/freebsd-ssp.txt
http://www.linuxfromscratch.org/~robert/NetBSD/netbsd-ssp.txt

PREREQUISITES: None
The frandom kernel patch is now required for SSP. This provides the erandom
device and sysctl interface. Using erandom stops a serious entropy depletion
problem while still providing urandom quality random bytes. Idealy you should
reboot an frandom kernel before installing SSP, but SSP will build without it.
If the erandom sysctl interface is missing from the system (vanilla kernel)
then /dev/urandom will be used; if /dev/urandom is missing (chroot) then
gettimeofday() will be used. Read this to install frandom:
http://www.linuxfromscratch.org/hints/downloads/files/entropy.txt
You will need the header from the frandom patch installed to build glibc.

HINT:

=======
Context
=======

	Introduction
	Extras
		Extra security patches
		Libsafe
	Installation
	Testing
	Feedback
	Acknowledgments

============
Introduction
============

Smashing Stack Protector

The GCC patch will add -fstack-protector-all, -fstack-protector, and
-fno-stack-protector to GCC extensions for C and C++; and
__guard_setup and __stack_smash_handler are defined in libgcc2.c. This code is
supplied by IBM, I have changed one definition to enable libc functions, and
added "ssp" to the version string. The gcc2 patch is only needed if you plan to
use gcc2 to build the kernel, and want stack protection in the kernel.

If any of these links are broken look for a newer version.

*** All of these patches are in:
http://www.linuxfromscratch.org/~robert/hlfs/downloads/cvs/\
	hlfs-patches-{date}.tar.bz2
For example: hlfs-patches-20041104.tar.bz2

Note: gcc-3.3 patches apply to gcc-3.3.* too. Likewise with gcc-3.4 patches.

http://www.linuxfromscratch.org/patches/downloads/gcc/\
        gcc-3.3-ssp-4.patch
	gcc-3.4-ssp-2.patch
        gcc-2.95.3-ssp-4.patch

The Glibc patch will define __guard_setup and __stack_smash_handler in libc.so
so the kill function can be kept in a shared object. In the Glibc patch the
erandom device is used to gather a small amount of random bits for the gaurd
value. /dev/log will also need to be present in chroot for syslog to log stack
overflows. It is reccomended intrusion detection systems monitor the system
logs for these alerts.

http://www.linuxfromscratch.org/patches/downloads/glibc/\
        glibc-2.3.2-ssp-frandom-5.patch
	glibc-2.3.4-ssp_frandom-4.patch # This works for glibc-2.3.3 too.

This GCC Specs patch adds -fstack-protector-all to GCC's default compiler flags.
Filters prevent libraries and the kernel from being built with unnessesary
smash symbols. This patch will build all main executables with stack protection.
This patch makes using stack protector almost transparent. This gcc2 patch is
not nessesary for anyone using gcc3 as their main compiler, it is provided for
legacy.

http://www.linuxfromscratch.org/patches/downloads/gcc/\
	gcc-3.3-sspspecs-3.patch
	gcc-3.4-sspspecs-2.patch
	gcc-2.95.3-sspspecs-2.patch

The Linux kernel patch adds support to the Linux kernel for smash symbols. It
can only build with -fstack-protector, not -fstack-protector-all, and is
therefore excluded from the default specs in the sspspecs patch.

http://www.linuxfromscratch.org/patches/downloads/linux/\
        linux-2.4.27-ssp-1.patch # or
        linux-2.6.5-ssp-1.patch # This still works on newer 2.6 kernels.

Do not use older versions than these patches, a new define was added. These
patches should apply to old kernels if you are using an older version.

http://www.linuxfromscratch.org/patches/downloads/linux/\
        linux-2.4.27-frandom-2.patch # or
	linux-2.6.9-frandom-2.patch

In the entropy.txt hint there is a libarc3library using frandom, which
has a patch for Mktemp. See entropy.txt for more details.

The XFree86 patch disables stack protection for some modules.

http://www.linuxfromscratch.org/patches/downloads/XFree86/ \
        XFree86-4.3.0-ssp-1.patch

And for LFS-6

http://www.linuxfromscratch.org/patches/downloads/\
        linux-libc-headers/linux-libc-headers-2.6-frandom-2.patch
ftp://twocents.mooo.com/pub/hlfs-patches/linux-libc-headers-2.6-frandom-2.patch

======
Extras
======
----------------------
Extra security patches
----------------------
This patch fixes a bug in both glibc-2.3.2 and glibc-2.3.3. This bug can be
reproduced by bind9's testsuite.
http://www.linuxfromscratch.org/patches/downloads/glibc/\
	glibc-2.3.3-got-fix-1.diff

This patch adds a sanity check to malloc. Backported from the Owl project.
(http://www.openwall.com/Owl/)

Note: This patch was integrated in the latest glibc-2.3.4 (cvs) package.

http://www.linuxfromscratch.org/patches/downloads/glibc/\
	glibc-2.3.3-owl-malloc-unlink-sanity-check-1.patch

--------
Libsafe
--------
Official site:
http://www.research.avayalabs.com/project/libsafe/src/libsafe-2.0-16.tgz

Note: Libsafe is obsolete, you can still use it if you wish.

Libsafe was developed by Avaya Labs to protect against format string
vulnerabilities. Though not widely used it has been widely tested. This
protection can be installed on an already running system, using ld.so.preload
to watch applications at runtime for functions which are known to be vulnerable.
This of course only protects dynamically linked applications. There should not
be a noticeable performance decrease, and it also logs to syslog.

We get some errors if we install Libsafe early in the build.
GCC
FAIL: g++.dg/expr/anew1.C execution test
FAIL: g++.dg/expr/anew2.C execution test
FAIL: g++.dg/expr/anew3.C execution test
FAIL: g++.dg/expr/anew4.C execution test

Binutils
FAIL: S-records
FAIL: S-records with constructors

To avoid these errors install Libsafe after GCC in chapter 6. Libsafe is
somewhat obsolete. Most modern software either doesn't use these strings, or
uses them properly. All of the example exploits in exploits/ will fail because
of SSP.

=====================
Installation
=====================

Do not use -O3 (or -O4) optimizations with stack protector or things will
not work.

---------
Chapter 5
---------
Kernel headers
(See under PREREQUISITES above)
patch -Np1 -i ../linux-2.4.27-frandom-2.patch

 - GCC pass 1
If the host system has SSP in Glibc already, then you can patch gcc
here. Otherwise do not. If in doubt, wait until pass two.
 - Glibc
patch -Np1 -i ../glibc-2.3.4-ssp-frandom-4.patch # or 2.3.2's patch

 - GCC pass 2
If you use sspspecs patch then a `make bootstrap` is a good idea too.
patch -Np1 -i ../gcc-3.3-ssp-3.patch
patch -Np1 -i ../gcc-3.3-sspspecs-3.patch

 - Binutils pass 2
Just for the testsuite.
make CFLAGS="-fno-stack-protector" check

---------
Chapter 6
---------
Make sure the frandom header get installed again.

 - Glibc
patch -Np1 -i ../glibc-2.3.4-ssp-frandom-4.patch

 - Binutils
make CFLAGS="-fno-stack-protector" check

 - GCC
hgcc -fa
patch -Np1 -i ../gcc-3.3-ssp-3.patch
patch -Np1 -i ../gcc-3.3-sspspecs-3.patch

 - Grub
CFLAGS="-fno-stack-protector" ./configure...

 - GCC 2.95.3
patch -Np1 -i ../gcc-2.95.3-ssp-4.patch

 - Perl
# Perl uses -O3 by default which can cause problems with SSP, reset it
# to use -O2.
env CFLAGS="-O2" ./configure...

---------
Chapter 8
---------
Linux kernel

make mrproper &&
patch -Np1 -i ../linux-2.4.27-ssp-1.patch
patch -Np1 -i ../linux-2.4.27-frandom-2.patch

make menuconfig

make CC="/opt/gcc-2.95.3/bin/gcc -fstack-protector" dep
make CC="/opt/gcc-2.95.3/bin/gcc -fstack-protector" bzImage
...

========
Testing
========
There are a couple tests in this package which may also be usefull here.
http://pax.grsecurity.net/paxtest-0.9.5.tar.gz
There are also tests in the libsafe source.

This will test -fstack-protector-all

cat > fail.c << "EOF"
#include <stdio.h>
#include <unistd.h>

int foo(char *blah) {
  char buffer[7];
  sprintf(buffer, "12345678901234567890123456789012345678901234567890");
  return(1234);
}

int main(int argc, char **argv) {
  printf("before foo()\n");
  foo("blah");
  printf("after foo()\n");
}
EOF

gcc -fstack-protector-all -o fail fail.c &&
./fail

This will display the __guard value. It should change each runtime. This will
test erandom/urandom/gettimeofday is working. Test urandom by booting a
vanilla kernel, test gettimeofday by removing /dev/urandom with a vanilla
kernel, or compile this statically linked and `chroot . ./guard-test`.

cat > guard-test.c << "EOF"
extern unsigned long __guard[];
int main () {
        printf("__guard\t=\t0x%08x;\n", __guard[0]);
        return 0;
}
EOF

ACKNOWLEDGMENTS:

* Thanks to Hiroaki Etoh for providing the SSP patch to IBM
* Thanks to IBM for providing the SSP patch at
	http://www.research.ibm.com/trl/projects/security/ssp/
* Thanks to OpenBSD for their XFree86 code. http://www.openbsd.org/
* Thanks to netsys.com for this
	http://www.netsys.com/cgi-bin/display_article.cgi?1266
* Thanks to securityfocus.com and immunix.com for this
	http://www.securityfocus.com/archive/1/333986/2003-08-17/2003-08-23/2
* Thanks to adamantix.org for kernel patches. http://www.adamantix.org/
* Thanks to Avaya Labs for Libsafe
	http://www.research.avayalabs.com/project/libsafe/
* Thanks to Teemu Tervo for nptl hint
	http://www.linuxfromscratch.org/hints/downloads/files/nptl.txt
* Thanks to cross compiling hint
	http://www.linuxfromscratch.org/hints/downloads/files/ \
		crosscompiling-x86.txt
* Thanks to http://www.isecurelabs.com/news/64 for proof of concept tests.
* Thanks to Eli Billauer for the Frandom suite
        http://frandom.sourceforge.net/
	http://www.billauer.co.il/

CHANGELOG:
[2003-10-18]
* Debut
* Reformat hint
[2003-10-22]
* Reformatted the patches so they're much easier to apply.
* Edit/rewrite hint & synopsis.
[2003-10-24]
* Added caveat.
* Fixed URLS.
* Lite edit
[2003-10-25]
* New bugs found.
[2003-10-26]
* GCC 2.95.3 patches made.
[2003-10-27]
* XFree86-4.3.0 patch made.
* Hint is now Beta - Need more feedback.
[2003-11-03]
* Edit
* Reformatted patches.
[2003-11-12]
* Reformat patches.
* Update/edit hint.
* Add new example tests.
[2003-11-21]
* Reformat patches.
* Add homepage/mirror url.
* Small edit.
[2003-12-01]
* Added Glibc and kernel patches.
* Rewrote install procedure.
[2003-12-20]
* Try to be more informative.
* Removed Gentoo property.
* Added Libsafe.
* Added Pax.
* Added new versions of binutils and glibc.
* Added GCC PIE.
* Rename filename to winter.txt.
[2003-12-21]
* Do not use "Enforce non-executable pages"
* Spell check.
* Fixed URL.
[2003-12-22]
* Added LOPTS to Net-tools.
* Added LDFLAGS to Perl.
[2003-12-25]
* More cflags.
* New tests.
[2003-12-30]
* Renamed hint back to propolice.txt.
* Added back Gentoo property as optional.
[2004-01-01]
* Added HCC
[2004-01-17]
* Cleanup
[2004-02-08]
* Update urls
* Convert propolice to ssp
[2004-02-15]
* Update gcc-3.3.3 and linux-2.6.2 ssp patches
[2004-02-19]
* Update linux-2.6.3 patch and hgcc url
[2004-03-27]
* Add sspspecs patch. Update.
[2004-04-18]
* Added entropy.txt link for erandom.
[2004-04-25]
* Fix more/again for erandom.
* Update some patches.
[2004-10-01]
* New patches.
* Added guard-test.c
[2004-10-28]
* New patches
[2004-10-30]
* Do not use -O3 or -O4
* Use CFLAGS="-O2" for Perl chapter 6.
[2004-11-04]
* Remove frandom mktemp patch.
* Add note about arc4random.
* Update patches, new define for SYSCTL_ERANDOM.


More information about the hints mailing list