From b721056e04ddd38564898b323caf8c559bf88cb7 Mon Sep 17 00:00:00 2001 From: Dave Wysochanski Date: Sun, 7 Dec 2008 04:23:37 +0000 Subject: [PATCH] Add generic function to read /dev/urandom, used in uuid calculation. --- WHATS_NEW | 1 + lib/misc/lvm-wrappers.c | 26 ++++++++++++++++++++++++++ lib/misc/lvm-wrappers.h | 5 +++++ lib/uuid/uuid.c | 14 ++------------ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/WHATS_NEW b/WHATS_NEW index e8dfbced8..9a2bdfa4c 100644 --- a/WHATS_NEW +++ b/WHATS_NEW @@ -1,5 +1,6 @@ Version 2.02.44 - ==================================== + Add generic function to read /dev/urandom, used in uuid calculation. Use displayable_lvs_in_vg and lv_is_displayable for consistency throughout. Fix race in vgcreate that would result in second caller overwriting first. Fix uninitialised lv_count in vgdisplay -c. diff --git a/lib/misc/lvm-wrappers.c b/lib/misc/lvm-wrappers.c index 1d8ca08f2..3ab8a7135 100644 --- a/lib/misc/lvm-wrappers.c +++ b/lib/misc/lvm-wrappers.c @@ -15,8 +15,34 @@ #include "lib.h" #include +#include int lvm_getpagesize(void) { return getpagesize(); } + +int read_urandom(void *buf, size_t len) +{ + int fd; + + /* FIXME: we should stat here, and handle other cases */ + /* FIXME: use common _io() routine's open/read/close */ + if ((fd = open("/dev/urandom", O_RDONLY)) < 0) { + log_sys_error("open", "read_urandom: /dev/urandom"); + return 0; + } + + if (read(fd, buf, len) != (ssize_t) len) { + log_sys_error("read", "read_urandom: /dev/urandom"); + if (close(fd)) + stack; + return 0; + } + + if (close(fd)) + stack; + + return 1; +} + diff --git a/lib/misc/lvm-wrappers.h b/lib/misc/lvm-wrappers.h index 851e73fde..717f2f413 100644 --- a/lib/misc/lvm-wrappers.h +++ b/lib/misc/lvm-wrappers.h @@ -18,4 +18,9 @@ int lvm_getpagesize(void); +/* + * Read 'len' bytes of entropy from /dev/urandom and store in 'buf'. + */ +int read_urandom(void *buf, size_t len); + #endif diff --git a/lib/uuid/uuid.c b/lib/uuid/uuid.c index 39252c0e5..1ded223fa 100644 --- a/lib/uuid/uuid.c +++ b/lib/uuid/uuid.c @@ -15,6 +15,7 @@ #include "lib.h" #include "uuid.h" +#include "lvm-wrappers.h" #include #include @@ -94,25 +95,14 @@ int lvid_in_restricted_range(union lvid *lvid) int id_create(struct id *id) { - int randomfile; unsigned i; size_t len = sizeof(id->uuid); memset(id->uuid, 0, len); - if ((randomfile = open("/dev/urandom", O_RDONLY)) < 0) { - log_sys_error("open", "id_create: /dev/urandom"); + if (!read_urandom(&id->uuid, len)) { return 0; } - if (read(randomfile, id->uuid, len) != (ssize_t) len) { - log_sys_error("read", "id_create: /dev/urandom"); - if (close(randomfile)) - stack; - return 0; - } - if (close(randomfile)) - stack; - /* * Skip out the last 2 chars in randomized creation for LVM1 * backwards compatibility.