From 6126f7a53c57485a9a29ddd772765695f23c92e6 Mon Sep 17 00:00:00 2001 From: Petr Uzel Date: Thu, 3 May 2012 21:02:01 +0200 Subject: libuuid: avoid double open and leaking descriptor We are opening /dev/urandom twice in uuid_generate(): first to check if the file is available and then later __uuid_generate_random() again to actually get the random data. Moreover, descriptor from the first open is leaking. Fix by passign the descriptor down the stack and reusing it there. References: http://marc.info/?l=util-linux-ng&m=133406051131131&w=2 Signed-off-by: Petr Uzel --- libuuid/src/gen_uuid.c | 17 ++++++++++------- libuuid/src/uuidd.h | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'libuuid') diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c index 8ff41291c..76f1bbdad 100644 --- a/libuuid/src/gen_uuid.c +++ b/libuuid/src/gen_uuid.c @@ -277,7 +277,7 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low, } if ((last.tv_sec == 0) && (last.tv_usec == 0)) { - random_get_bytes(&clock_seq, sizeof(clock_seq)); + random_get_bytes(&clock_seq, sizeof(clock_seq), -1); clock_seq &= 0x3FFF; gettimeofday(&last, 0); last.tv_sec--; @@ -434,7 +434,7 @@ int __uuid_generate_time(uuid_t out, int *num) if (!has_init) { if (get_node_id(node_id) <= 0) { - random_get_bytes(node_id, 6); + random_get_bytes(node_id, 6, -1); /* * Set multicast bit, to prevent conflicts * with IEEE 802 addresses obtained from @@ -520,7 +520,7 @@ int uuid_generate_time_safe(uuid_t out) } -void __uuid_generate_random(uuid_t out, int *num) +void __uuid_generate_random(uuid_t out, int *num, int fd) { uuid_t buf; struct uuid uu; @@ -532,7 +532,7 @@ void __uuid_generate_random(uuid_t out, int *num) n = *num; for (i = 0; i < n; i++) { - random_get_bytes(buf, sizeof(buf)); + random_get_bytes(buf, sizeof(buf), fd); uuid_unpack(buf, &uu); uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000; @@ -548,7 +548,7 @@ void uuid_generate_random(uuid_t out) int num = 1; /* No real reason to use the daemon for random uuid's -- yet */ - __uuid_generate_random(out, &num); + __uuid_generate_random(out, &num, -1); } @@ -560,8 +560,11 @@ void uuid_generate_random(uuid_t out) */ void uuid_generate(uuid_t out) { - if (random_get_fd() >= 0) - uuid_generate_random(out); + int fd; + int num = 1; + + if ((fd = random_get_fd()) >= 0) + __uuid_generate_random(out, &num, fd); else uuid_generate_time(out); } diff --git a/libuuid/src/uuidd.h b/libuuid/src/uuidd.h index 27b79c216..2e19522df 100644 --- a/libuuid/src/uuidd.h +++ b/libuuid/src/uuidd.h @@ -49,6 +49,6 @@ #define UUIDD_MAX_OP UUIDD_OP_BULK_RANDOM_UUID extern int __uuid_generate_time(uuid_t out, int *num); -extern void __uuid_generate_random(uuid_t out, int *num); +extern void __uuid_generate_random(uuid_t out, int *num, int fd); #endif /* _UUID_UUID_H */ -- cgit v1.2.3-55-g7522