Improve urandom plugin for embedded systems w/o random seed.

For embedded systems with no initial random seed, usually stored in
/var/lib/urandom/urandom-seed on desktops, servers and laptops, we now
provide an initial seed file using a built-in PRNG.

Also, let the configure script default to *enable* the random seeding
at boot, granted the urandom plugin is enabled.  We should encourage
users to properly seed their random, for security reasons.

Moreover, the plugin did not properly regenerate the random seed on
reboot, or shutdown, this patch also rectifies that problem.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2015-10-26 03:31:19 +01:00
1 parent 369fcf2100
commit a33478d563
3 files changed
+48 -11

No files matched your search

Vendored
+4 -3
View File
@@ -22,6 +22,7 @@ defaults()
plugindir=${plugindir:="$libdir/$pdir"}
# The initctl FIFO, should probably be in /run, not /dev
fifo=${fifo:="/dev/initctl"}
random=${random:="/var/lib/urandom/random-seed"}
conf="finit.conf"
config=${config:="$sysconfdir/$conf"}
confdir=${confdir:="finit.d"}
@@ -149,9 +150,9 @@ usage()
echo " --with-fifo=FIFO FIFO for initctl plugin, default '$fifo'"
echo " --with-config=CONFIG Finit CONFIG file, default '\$sysconfdir/$conf'"
echo " --with-heading=GREET Display GREET instead of Finit version at boot"
echo " --with-randomseed=SEED Seed /dev/urandom from file SEED. On Debian/Ubuntu"
echo " systems, set this to /var/lib/urandom/random-seed"
echo " Default: none"
echo " --with-randomseed=SEED Seed /dev/urandom at boot from SEED. Usually found in"
echo " standard Linux systems at $random"
echo " which is also the default. Requires urandom plugin!"
echo " --with-plugins=\"a b c\" Build plugins 'a', 'b' and 'c', either as dynamic .so"
echo " files, or as built-in if --enable-static is given,"
echo " defaults to all plugins. Exclude extension!"
+2
View File
@@ -24,6 +24,7 @@
#include <string.h>
#include <utmp.h>
#include "../config.h"
#include "../finit.h"
#include "../helpers.h"
#include "../plugin.h"
@@ -45,6 +46,7 @@ static void setup(void *UNUSED(arg))
makedir("/var/lib", 0755);
makedir("/var/lib/misc", 0755);
makedir("/var/lib/alarm", 0755);
makedir("/var/lib/urandom",0755);
makedir("/var/lock", 0755);
makedir("/var/log", 0755);
makedir("/var/mail", 0755);
+42 -8
View File
@@ -22,29 +22,63 @@
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h> /* gettimeofday() */
#include <sys/types.h>
#include "../config.h"
#include "../finit.h"
#include "libite/lite.h"
#include "../helpers.h"
#include "../plugin.h"
#include "libite/lite.h"
static void setup(void *UNUSED(arg))
{
#ifdef RANDOMSEED
copyfile(RANDOMSEED, "/dev/urandom", 0, 0);
unlink(RANDOMSEED);
if (!fexist(RANDOMSEED)) {
int ret = 1;
FILE *fp;
print_desc("Bootstrapping random seed", NULL);
umask(077);
fp = fopen(RANDOMSEED, "w");
if (fp) {
int iter = 128;
struct timeval tv;
gettimeofday(&tv, NULL);
srandom(tv.tv_sec % 3600);
while (iter--) {
uint32_t i, prng = random();
for (i = 0; i < sizeof(prng); i++)
fputc((prng >> (i * CHAR_BIT)) & UCHAR_MAX, fp);
}
ret = fclose(fp);
}
print_result(ret);
umask(0);
}
print_desc("Initializing random number generator", NULL);
print_result(512 != copyfile(RANDOMSEED, "/dev/urandom", 0, 0));
#endif
}
static void save(void *UNUSED(arg))
{
#ifdef RANDOMSEED
umask(077);
copyfile("/dev/urandom", RANDOMSEED, 4096, 0);
print_desc("Saving random seed", NULL);
print_result(512 != copyfile("/dev/urandom", RANDOMSEED, 512, 0));
umask(0);
#endif
}
static plugin_t plugin = {
.hook[HOOK_BASEFS_UP] = {
.cb = setup
},
.name = "urandom",
.hook[HOOK_BASEFS_UP] = { .cb = setup },
.hook[HOOK_SHUTDOWN] = { .cb = save },
.depends = { "bootmisc", }
};