| 1 |
#! /bin/sh |
| 2 |
### BEGIN INIT INFO |
| 3 |
# Provides: urandom |
| 4 |
# Required-Start: $local_fs $time |
| 5 |
# Required-Stop: $local_fs |
| 6 |
# Default-Start: S |
| 7 |
# Default-Stop: 0 6 |
| 8 |
# Short-Description: Save and restore random seed between restarts. |
| 9 |
# Description: This script saves the random seed between restarts. |
| 10 |
# It is called from the boot, halt and reboot scripts. |
| 11 |
### END INIT INFO |
| 12 |
|
| 13 |
## Assumption 1: We assume $SAVEDFILE is a file (or a symlink |
| 14 |
## to a file) that resides on a non-volatile medium that persists |
| 15 |
## across reboots. |
| 16 |
## Case 1a: Ideally, it is readable and writeable. Its is unshared, |
| 17 |
## i.e. its contents are unique to this machine. It is protected so |
| 18 |
## that its contents are not known to attackers. |
| 19 |
## Case 1b: Less than ideally, it is read-only. Its contents are |
| 20 |
## unique to this machine and not known to attackers. |
| 21 |
SAVEDFILE=/var/lib/urandom/random-seed |
| 22 |
|
| 23 |
[ -c /dev/urandom ] || exit 0 |
| 24 |
|
| 25 |
PATH=/sbin:/bin |
| 26 |
if ! POOLBYTES=$(((\ |
| 27 |
$(cat /proc/sys/kernel/random/poolsize 2>/dev/null) + 7) / 8)); then |
| 28 |
POOLBYTES=512 |
| 29 |
fi |
| 30 |
. /lib/init/vars.sh |
| 31 |
|
| 32 |
. /lib/lsb/init-functions |
| 33 |
|
| 34 |
do_status() { |
| 35 |
if [ -f $SAVEDFILE ]; then |
| 36 |
return 0 |
| 37 |
else |
| 38 |
return 4 |
| 39 |
fi |
| 40 |
} |
| 41 |
|
| 42 |
case "$1" in |
| 43 |
start | "") |
| 44 |
[ "$VERBOSE" = no ] || log_action_begin_msg "Initializing random number generator" |
| 45 |
# Seed the RNG with date and time. |
| 46 |
# This is helpful in the less-than-ideal case where $SAVEDFILE |
| 47 |
# is read-only. |
| 48 |
# The value of this is greatly reduced if $SAVEDFILE is missing, |
| 49 |
# or its contents are shared machine-to-machine or known to |
| 50 |
# attackers (since they might well know at what time this |
| 51 |
# machine booted up). |
| 52 |
( |
| 53 |
date +%s.%N |
| 54 |
|
| 55 |
# Load and then save $POOLBYTES bytes, |
| 56 |
# which is the size of the entropy pool |
| 57 |
if [ -f "$SAVEDFILE" ]; then |
| 58 |
cat "$SAVEDFILE" |
| 59 |
fi |
| 60 |
# Redirect output of subshell (not individual commands) |
| 61 |
# to cope with a misfeature in the FreeBSD (not Linux) |
| 62 |
# /dev/random, where every superuser write/close causes |
| 63 |
# an explicit reseed of the yarrow. |
| 64 |
) >/dev/urandom |
| 65 |
|
| 66 |
# Write a new seed into $SAVEDFILE because re-using a seed |
| 67 |
# compromises security. Each time we re-seed, we want the |
| 68 |
# seed to be as different as possible. |
| 69 |
# Write it now, in case the machine crashes without doing |
| 70 |
# an orderly shutdown. |
| 71 |
# The write will fail if $SAVEDFILE is read-only, but it |
| 72 |
# doesn't hurt to try. |
| 73 |
umask 077 |
| 74 |
dd if=/dev/urandom of=$SAVEDFILE bs=$POOLBYTES count=1 >/dev/null 2>&1 |
| 75 |
ES=$? |
| 76 |
umask 022 |
| 77 |
[ "$VERBOSE" = no ] || log_action_end_msg $ES |
| 78 |
;; |
| 79 |
stop) |
| 80 |
# Carry a random seed from shut-down to start-up; |
| 81 |
# Write it on shutdown, in case the one written at startup |
| 82 |
# has been lost, snooped, or otherwise compromised. |
| 83 |
# see documentation in linux/drivers/char/random.c |
| 84 |
[ "$VERBOSE" = no ] || log_action_begin_msg "Saving random seed" |
| 85 |
umask 077 |
| 86 |
dd if=/dev/urandom of=$SAVEDFILE bs=$POOLBYTES count=1 >/dev/null 2>&1 |
| 87 |
ES=$? |
| 88 |
[ "$VERBOSE" = no ] || log_action_end_msg $ES |
| 89 |
;; |
| 90 |
status) |
| 91 |
do_status |
| 92 |
exit $? |
| 93 |
;; |
| 94 |
restart | reload | force-reload) |
| 95 |
echo "Error: argument '$1' not supported" >&2 |
| 96 |
exit 3 |
| 97 |
;; |
| 98 |
*) |
| 99 |
echo "Usage: urandom start|stop" >&2 |
| 100 |
exit 3 |
| 101 |
;; |
| 102 |
esac |
| 103 |
|
| 104 |
: |